@helping-desk/web-sdk
Version:
Web SDK for Helping Desk - Ticket creation and support widget integration
139 lines (138 loc) • 4.5 kB
JavaScript
/**
* Helping Desk Web SDK
*
* A JavaScript SDK for integrating Helping Desk ticket creation functionality
* into any website or web application.
*/
import { ChatBubbleWidget } from './widget';
export class HelpingDeskSDK {
constructor(config) {
this.widget = null;
if (!config.tenantId) {
throw new Error('Tenant ID is required. Please provide your tenantId from your .env file.');
}
// Get API URL from config, environment variable, or throw error
const apiUrl = config.apiUrl ||
(typeof process !== 'undefined' && process.env?.HELPING_DESK_API_URL) ||
(typeof window !== 'undefined' && window.HELPING_DESK_API_URL);
if (!apiUrl) {
throw new Error('API URL is required. Please set HELPING_DESK_API_URL in your environment variables or provide apiUrl in the config.');
}
this.config = {
showWidget: config.showWidget !== false, // Default to true
widgetPosition: config.widgetPosition || 'bottom-right',
widgetColor: config.widgetColor || '#667eea',
widgetIcon: config.widgetIcon || '💬',
...config,
apiUrl,
};
// Initialize widget if enabled
if (this.config.showWidget && typeof window !== 'undefined') {
this.initWidget();
}
}
/**
* Initialize the support widget
*/
initWidget() {
if (this.widget) {
return; // Already initialized
}
this.widget = new ChatBubbleWidget({
position: this.config.widgetPosition,
primaryColor: this.config.widgetColor,
bubbleIcon: this.config.widgetIcon,
}, async (data) => {
return await this.createTicket({
title: data.title,
description: data.description,
priority: data.priority,
category: data.category,
});
});
this.widget.init();
}
/**
* Create a ticket
*/
async createTicket(data) {
try {
// Internal endpoint - abstracted from users
const endpoint = '/api/tickets/sdk/create/';
const headerName = 'X-Tenant-ID';
const response = await fetch(`${this.config.apiUrl}${endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
[headerName]: this.config.tenantId,
},
body: JSON.stringify({
title: data.title,
description: data.description,
priority: data.priority || 'medium',
category: data.category || '',
tags: data.tags || [],
}),
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({ detail: 'Unknown error' }));
throw {
message: errorData.detail || errorData.message || 'Failed to create ticket',
status: response.status,
details: errorData,
};
}
return await response.json();
}
catch (error) {
if (error && typeof error === 'object' && 'message' in error) {
throw error;
}
throw {
message: error instanceof Error ? error.message : 'Failed to create ticket',
details: error,
};
}
}
/**
* Update configuration
*/
updateConfig(config) {
this.config = {
...this.config,
...config,
};
}
/**
* Get current configuration
*/
getConfig() {
return { ...this.config };
}
/**
* Show the support widget (if it was hidden)
*/
showWidget() {
if (!this.widget && typeof window !== 'undefined') {
this.initWidget();
}
}
/**
* Hide the support widget
*/
hideWidget() {
if (this.widget) {
this.widget.destroy();
this.widget = null;
}
}
}
// Export default instance creator
export function createSDK(config) {
return new HelpingDeskSDK(config);
}
// For browser global usage
if (typeof window !== 'undefined') {
window.HelpingDeskSDK = HelpingDeskSDK;
window.createHelpingDeskSDK = createSDK;
}