UNPKG

@revrag-ai/embed-react-native

Version:

A powerful React Native library for integrating AI-powered voice agents into mobile applications. Features real-time voice communication, intelligent speech processing, customizable UI components, and comprehensive event handling for building conversation

191 lines (181 loc) 5.42 kB
"use strict"; import { getAgentData, setAgentData } from "../store/store.key.js"; import { processApiError } from "./api.error.js"; /** * APIService class that ensures proper initialization before API calls */ export class APIService { static instance = null; apiBaseUrl = null; isInitialized = false; constructor() {} /** * Get singleton instance of APIService */ static getInstance() { if (!APIService.instance) { APIService.instance = new APIService(); } return APIService.instance; } /** * Initialize the API service with the base URL */ async initialize() { if (this.isInitialized && this.apiBaseUrl) { return; // Already initialized } const AgentData = await getAgentData(); if (AgentData?.embedUrl) { this.apiBaseUrl = AgentData.embedUrl; this.isInitialized = true; } else { throw new Error('API base URL not found in keychain'); } } /** * Ensure the service is initialized before making API calls */ async ensureInitialized() { if (!this.isInitialized || !this.apiBaseUrl) { await this.initialize(); } } /** * Get headers with stored API key */ async getHeaders() { const AgentData = await getAgentData(); if (!AgentData?.apiKey) { throw new Error('API key not found in keychain'); } return { 'Content-Type': 'application/json', 'Authorization': `Bearer ${AgentData.apiKey}`, 'X-Revrag-Embedded-Key': AgentData.apiKey }; } /** * Register a new user/device on initialization * @returns Promise with registration response */ async registerOnInitialize() { let response; try { await this.ensureInitialized(); const headers = await this.getHeaders(); response = await fetch(`${this.apiBaseUrl}/embedded-agent/initialize`, { method: 'GET', headers: headers }); const data = await response.json(); if (!response.ok) { return await processApiError(null, response, this.apiBaseUrl || undefined); } await setAgentData(data, '@config_data'); return { success: true, data: data }; } catch (error) { return await processApiError(error, response, this.apiBaseUrl || undefined); } } /** * Update user data * @param params Update parameters including userId and data to update * @returns Promise with update response */ async updateUserData(params) { let response; try { await this.ensureInitialized(); const headers = await this.getHeaders(); response = await fetch(`${this.apiBaseUrl}/embedded-agent/user-context/update?app_user_id=${params.data.app_user_id}`, { method: 'PUT', headers, body: JSON.stringify({ [params.eventKey]: params.data }) }); if (!response.ok) { return await processApiError(null, response, this.apiBaseUrl || undefined); } return { success: true }; } catch (error) { return await processApiError(error, response, this.apiBaseUrl || undefined); } } /** * Get token details for a user * @param params Parameters including app_user_id and call_type * @returns Promise with token details */ async getTokenDetails(params) { let response; try { await this.ensureInitialized(); const headers = await this.getHeaders(); response = await fetch(`${this.apiBaseUrl}/embedded-agent/token`, { method: 'POST', headers, body: JSON.stringify(params) }); const data = await response.json(); if (!response.ok) { return await processApiError(null, response, this.apiBaseUrl || undefined); } return { success: true, data: data }; } catch (error) { return await processApiError(error, response, this.apiBaseUrl || undefined); } } async getPopupDescription(params) { let response; try { await this.ensureInitialized(); const headers = await this.getHeaders(); response = await fetch(`${this.apiBaseUrl}/embedded-agent/dynamic-popup?app_user_id=${params.app_user_id}`, { method: 'GET', headers }); const data = await response.json(); if (!response.ok) { return await processApiError(null, response, this.apiBaseUrl || undefined); } return { success: true, data: data }; } catch (error) { return await processApiError(error, response, this.apiBaseUrl || undefined); } } } // Export convenience functions for backward compatibility export const initializeApi = async () => { const apiService = APIService.getInstance(); await apiService.initialize(); }; export const registerOnInitialize = async () => { const apiService = APIService.getInstance(); return await apiService.registerOnInitialize(); }; export const updateUserData = async params => { const apiService = APIService.getInstance(); return await apiService.updateUserData(params); }; export const getTokenDetails = async params => { const apiService = APIService.getInstance(); return await apiService.getTokenDetails(params); }; export const getPopupDescription = async params => { const apiService = APIService.getInstance(); return await apiService.getPopupDescription(params); }; //# sourceMappingURL=api.js.map