UNPKG

plazbot

Version:

Official Plazbot SDK for creating AI agents for WhatsApp, portals, and developers.

228 lines (227 loc) 8.34 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Portal = void 0; const axios_1 = __importDefault(require("axios")); class Portal { constructor(options) { this.workspaceId = options.workspaceId; this.apiKey = options.apiKey; if (options.customUrl) { this.mcpUrl = options.customUrl; } else { const zone = options.zone ?? "LA"; if (zone !== "EU" && zone !== "LA") { throw new Error("Invalid zone. Must be 'EU' or 'LA'."); } this.mcpUrl = zone === "EU" ? "https://apieu.plazbot.com" : "https://api.plazbot.com"; } this.http = axios_1.default.create({ baseURL: this.mcpUrl, headers: { ...(this.apiKey && { 'Authorization': `Bearer ${this.apiKey}` }), 'x-workspace-id': this.workspaceId } }); } async addPortal(params) { if (params.zone !== "LA" && params.zone !== "EU") { throw new Error("Invalid zone. Must be 'LA' or 'EU'."); } // Verificar si ya existe un portal para este workspace try { const existing = await this.getExistsPortal(); if (existing?.success) { throw new Error("Only one portal per workspace is allowed."); } } catch (err) { const isNotFound = err?.response?.status === 404 || err?.message?.toLowerCase()?.includes("not found"); if (!isNotFound) { throw err; } } const body = { workspaceId: this.workspaceId, name: params.name, zone: params.zone, url: params.url, title: params.title, subtitle: params.subtitle, access: params.access ?? "direct", disabled: typeof params.disabled === "boolean" ? params.disabled : false, brandOff: typeof params.brandOff === "boolean" ? params.brandOff : false }; if (params.logo) { const valid = /\.(png|jpg)$/i.test(params.logo) && /^(https?:\/\/|\/)/.test(params.logo); if (!valid) throw new Error("Logo must be a PNG or JPG and a valid path or URL."); body.logo = params.logo; } const response = await this.http.post('/api/portal/add', body); if (response.status < 200 || response.status >= 300) { throw new Error(`Error creating portal: ${response.statusText}`); } const data = response.data; return { id: data.id, url: data.url }; } async updatePortal(params) { if (params.zone) { throw new Error("Zone cannot be updated. To change zones, delete and recreate the portal."); } const body = { id: params.id, workspaceId: this.workspaceId }; if (params.name) body.name = params.name; if (params.url) body.url = params.url; if (params.title) body.title = params.title; if (params.subtitle) body.subtitle = params.subtitle; if (params.logo) { const valid = /\.(png|jpg)$/i.test(params.logo) && /^(https?:\/\/|\/)/.test(params.logo); if (!valid) throw new Error("Logo must be a PNG or JPG and a valid path or URL."); body.logo = params.logo; } if (params.logodark) { const valid = /\.(png|jpg)$/i.test(params.logodark) && /^(https?:\/\/|\/)/.test(params.logodark); if (!valid) throw new Error("Logo Dark must be a PNG or JPG and a valid path or URL."); body.logodark = params.logodark; } if (params.access) { if (params.access !== "direct" && params.access !== "form") { throw new Error("Access must be either 'direct' or 'form'."); } body.access = params.access; } else { body.access = "direct"; } body.disabled = typeof params.disabled === "boolean" ? params.disabled : false; body.brandOff = typeof params.brandOff === "boolean" ? params.brandOff : false; const response = await this.http.post('/api/portal/update', body); if (response.status < 200 || response.status >= 300) { throw new Error(`Error updating portal: ${response.statusText}`); } return { success: true, message: "Portal updated successfully." }; } async getPortal(id) { if (!id) { throw new Error("Portal ID is required."); } const response = await this.http.get('/api/portal/get', { params: { id, workspaceId: this.workspaceId } }); if (response.status < 200 || response.status >= 300) { throw new Error(`Error retrieving portal: ${response.statusText}`); } return response.data; } async deletePortal(id) { if (!id) { throw new Error("Portal ID is required."); } const response = await this.http.delete('/api/portal/delete', { params: { id, workspaceId: this.workspaceId } }); if (response.status < 200 || response.status >= 300) { throw new Error(`Error deleting portal: ${response.statusText}`); } return response.data; } async getExistsPortal() { if (!this.workspaceId) { throw new Error("WorkspaceId is required to check for existing portal."); } try { const response = await this.http.get('/api/portal/get-exists-portal', { params: { workspaceId: this.workspaceId } }); return response.data; } catch (err) { if (err?.response?.status === 404) { throw new Error("Portal not found for this workspace."); } throw new Error(`Error retrieving portal: ${err?.message ?? "Unknown error"}`); } } async addLinkToPortal(params) { if (!params.portalId || !params.value) { throw new Error("portalId and value are required to add a link."); } const body = { value: params.value, url: params.url ?? "#" }; const response = await this.http.post('/api/portal/add-link-portal', body, { params: { portalId: params.portalId, workspaceId: this.workspaceId } }); if (response.status < 200 || response.status >= 300) { throw new Error(`Error adding link: ${response.statusText}`); } return response.data; } async addAgentToPortal(params) { if (!params.portalId || !params.id) { throw new Error("portalId and id are required to add an agent."); } const body = { id: params.id }; const response = await this.http.post('/api/portal/add-agent-portal', body, { params: { portalId: params.portalId, workspaceId: this.workspaceId } }); if (response.status < 200 || response.status >= 300) { throw new Error(`Error adding agent: ${response.statusText}`); } return response.data; } async clearLinks(portalId) { if (!portalId) { throw new Error("Portal ID is required."); } const response = await this.http.post('/api/portal/clear-links', null, { params: { id: portalId, workspaceId: this.workspaceId } }); if (response.status < 200 || response.status >= 300) { throw new Error(`Error clearing links: ${response.statusText}`); } return response.data; } } exports.Portal = Portal;