plazbot
Version:
Official Plazbot SDK for creating AI agents for WhatsApp, portals, and developers.
154 lines (153 loc) • 6.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Portal = void 0;
const http_1 = require("./http");
class Portal extends http_1.PlazbotHttp {
constructor(options) {
super(options);
}
async addPortal(params) {
if (params.zone !== "LA" && params.zone !== "EU") {
throw new Error("Invalid zone. Must be 'LA' or 'EU'.");
}
// Check if portal already exists
try {
const existing = await this.getExistsPortal();
if (existing?.success) {
throw new Error("Only one portal per workspace is allowed.");
}
}
catch (err) {
const error = err;
const isNotFound = error?.response?.status === 404 ||
error?.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",
theme: params.theme ?? "light",
disabled: typeof params.disabled === "boolean" ? params.disabled : false,
brandOff: typeof params.brandOff === "boolean" ? params.brandOff : false,
};
if (params.logo) {
this.validateLogoUrl(params.logo, "Logo");
body.logo = params.logo;
}
if (params.logodark) {
this.validateLogoUrl(params.logodark, "Logo Dark");
body.logodark = params.logodark;
}
const response = await this.http.post('/api/portal/add', body);
this.throwIfError(response, 'Error creating portal');
return { id: response.data.id, url: response.data.url };
}
async updatePortal(params) {
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) {
this.validateLogoUrl(params.logo, "Logo");
body.logo = params.logo;
}
if (params.logodark) {
this.validateLogoUrl(params.logodark, "Logo Dark");
body.logodark = params.logodark;
}
body.access = params.access ?? "direct";
body.theme = params.theme ?? "light";
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);
this.throwIfError(response, 'Error updating portal');
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 },
});
this.throwIfError(response, 'Error retrieving portal');
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 },
});
this.throwIfError(response, 'Error deleting portal');
return response.data;
}
async getExistsPortal() {
try {
const response = await this.http.get('/api/portal/get-exists-portal', {
params: { workspaceId: this.workspaceId },
});
return response.data;
}
catch (err) {
const error = err;
if (error?.response?.status === 404) {
throw new Error("Portal not found for this workspace.");
}
throw new Error(`Error retrieving portal: ${error?.message ?? "Unknown error"}`);
}
}
async addLinkToPortal(params) {
if (!params.portalId || !params.value) {
throw new Error("portalId and value are required to add a link.");
}
const response = await this.http.post('/api/portal/add-link-portal', { value: params.value, url: params.url ?? "#" }, { params: { portalId: params.portalId, workspaceId: this.workspaceId } });
this.throwIfError(response, 'Error adding link');
}
async addAgentToPortal(params) {
if (!params.portalId || !params.id) {
throw new Error("portalId and id are required to add an agent.");
}
const response = await this.http.post('/api/portal/add-agent-portal', { id: params.id }, { params: { portalId: params.portalId, workspaceId: this.workspaceId } });
this.throwIfError(response, 'Error adding agent to portal');
}
async removeAgentFromPortal(params) {
if (!params.portalId || !params.id) {
throw new Error("portalId and id are required to remove an agent.");
}
const response = await this.http.post('/api/portal/delete-agent-portal', { id: params.id }, { params: { portalId: params.portalId, workspaceId: this.workspaceId } });
this.throwIfError(response, 'Error removing agent from portal');
}
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 },
});
this.throwIfError(response, 'Error clearing links');
}
validateLogoUrl(url, label) {
const valid = /\.(png|jpg)$/i.test(url) && /^(https?:\/\/|\/)/.test(url);
if (!valid) {
throw new Error(`${label} must be a PNG or JPG and a valid path or URL.`);
}
}
}
exports.Portal = Portal;