UNPKG

@elusion-sdk/briq

Version:

A modern TypeScript SDK for Briq SMS API integration

99 lines 3.88 kB
import { ENDPOINTS } from "../utils/constants"; import { validatePaginationParams, validateUUID, validateWorkspaceName, } from "../utils/validators"; import { NotFoundError, ValidationError } from "../utils/errors"; import { BaseService } from "./BaseService"; export class WorkspaceService extends BaseService { async create(request) { this.validateRequired(request, ["name"]); validateWorkspaceName(request.name); if (request.description && request.description.length > 500) { throw new ValidationError("Description too long. Maximum 500 characters allowed"); } const sanitizedRequest = this.sanitizeInput(request); try { return await this.client.post(ENDPOINTS.WORKSPACES.CREATE, sanitizedRequest); } catch (error) { throw new Error(`Failed to create workspace: ${this.formatError(error)}`); } } async list(params = {}) { if (Object.keys(params).length > 0) { validatePaginationParams(params); } if (params.search) { params.search = params.search.trim(); if (params.search.length > 100) { throw new ValidationError("Search term too long. Maximum 100 characters allowed"); } } try { const response = await this.client.get(ENDPOINTS.WORKSPACES.GET_ALL, { params }); if (response.data && Array.isArray(response.data)) { return { success: true, data: response.data, pagination: { page: params.page || 1, limit: params.limit || 20, total: response.data.length, totalPages: Math.ceil(response.data.length / (params.limit || 20)), hasNext: false, hasPrev: (params.page || 1) > 1, }, }; } return response; } catch (error) { throw new Error(`Failed to list workspaces: ${this.formatError(error)}`); } } async getById(workspaceId) { validateUUID(workspaceId, "Workspace ID"); try { return await this.client.get(ENDPOINTS.WORKSPACES.GET_BY_ID(workspaceId)); } catch (error) { if (error.statusCode === 404) { throw new NotFoundError("Workspace", workspaceId); } throw new Error(`Failed to get workspace: ${this.formatError(error)}`); } } async update(workspaceId, request) { validateUUID(workspaceId, "Workspace ID"); if (Object.keys(request).length === 0) { throw new ValidationError("Update request cannot be empty"); } if (request.name !== undefined) { validateWorkspaceName(request.name); } if (request.description && request.description.length > 500) { throw new ValidationError("Description too long. Maximum 500 characters allowed"); } const sanitizedRequest = this.sanitizeInput(request); try { return await this.client.patch(ENDPOINTS.WORKSPACES.UPDATE(workspaceId), sanitizedRequest); } catch (error) { if (error.statusCode === 404) { throw new NotFoundError("Workspace", workspaceId); } throw new Error(`Failed to update workspace: ${this.formatError(error)}`); } } async exists(workspaceId) { try { await this.getById(workspaceId); return true; } catch (error) { if (error instanceof NotFoundError) { return false; } throw error; } } } //# sourceMappingURL=WorkspaceService.js.map