UNPKG

@orga-ai/node

Version:

OrgaAI backend SDK for Node.js - simplifies API integration

106 lines 3.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OrgaAI = void 0; const errors_1 = require("./errors"); class OrgaAI { apiKey; userEmail; baseUrl; debug; timeout; constructor(config) { if (!config.apiKey) { throw new errors_1.OrgaAIError("API key is required"); } if (!config.userEmail) { throw new errors_1.OrgaAIError("User email is required"); } const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(config.userEmail)) { throw new errors_1.OrgaAIError("Invalid email format"); } this.apiKey = config.apiKey; this.userEmail = config.userEmail; this.baseUrl = config.baseUrl || "https://api.orga-ai.com"; this.debug = config.debug || false; this.timeout = config.timeout || 10000; } log(message, data) { if (this.debug) { console.log(`[OrgaAI] ${message}`, data); } } async fetchWithTimeout(url, options) { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), this.timeout); try { const response = await fetch(url, { ...options, signal: controller.signal, }); clearTimeout(timeoutId); return response; } catch (error) { clearTimeout(timeoutId); throw error; } } /** * @description Get session configuration for the user * @returns ephemeral token and ICE servers needed for WebRTC connection */ async getSessionConfig() { try { this.log("Fetching session config"); const ephemeralToken = await this.fetchEphemeralToken(); this.log("Fetched ephemeral token", ephemeralToken); const iceServers = await this.fetchIceServers(ephemeralToken); this.log("Fetched ICE servers", iceServers); return { ephemeralToken, iceServers, }; } catch (error) { if (error instanceof errors_1.OrgaAIError) { throw error; } throw new errors_1.OrgaAIServerError(`Failed to get session config: ${error instanceof Error ? error.message : "Unknown error"}`); } } async fetchEphemeralToken() { const apiUrl = `${this.baseUrl}/v1/realtime/client-secrets?email=${encodeURIComponent(this.userEmail)}`; const response = await this.fetchWithTimeout(apiUrl, { method: "POST", headers: { Authorization: `Bearer ${this.apiKey}`, "Content-Type": "application/json", }, }); if (!response.ok) { if (response.status === 401) { throw new errors_1.OrgaAIAuthenticationError("Invalid API key or user email"); } throw new errors_1.OrgaAIServerError(`Failed to fetch ephemeral token: ${response.statusText}`, response.status); } const data = await response.json(); return data.ephemeral_token; } async fetchIceServers(ephemeralToken) { const url = `${this.baseUrl}/v1/realtime/ice-config`; const response = await this.fetchWithTimeout(url, { method: "GET", headers: { Authorization: `Bearer ${ephemeralToken}`, }, }); if (!response.ok) { throw new errors_1.OrgaAIServerError(`Failed to fetch ICE servers: ${response.statusText}`, response.status); } const data = await response.json(); return data.iceServers; } } exports.OrgaAI = OrgaAI; //# sourceMappingURL=client.js.map