@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
199 lines (190 loc) • 6.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.updateUserData = exports.registerOnInitialize = exports.initializeApi = exports.getTokenDetails = exports.getPopupDescription = exports.APIService = void 0;
var _storeKey = require("../store/store.key.js");
var _apiError = require("./api.error.js");
var _constant = require("../utils/constant.js");
/**
* APIService class that ensures proper initialization before API calls
*/
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 (0, _storeKey.getAgentData)();
if (!AgentData) {
throw new Error('API base URL not found in keychain');
}
const resolvedEmbedUrl = AgentData.embedUrl && typeof AgentData.embedUrl === 'string' ? AgentData.embedUrl : _constant.DEFAULT_EMBED_URL;
this.apiBaseUrl = resolvedEmbedUrl;
this.isInitialized = true;
}
/**
* 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 (0, _storeKey.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 (0, _apiError.processApiError)(null, response, this.apiBaseUrl || undefined);
}
await (0, _storeKey.setAgentData)(data, '@config_data');
return {
success: true,
data: data
};
} catch (error) {
return await (0, _apiError.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.app_user_id}`, {
method: 'PUT',
headers,
body: JSON.stringify(params)
});
if (!response.ok) {
return await (0, _apiError.processApiError)(null, response, this.apiBaseUrl || undefined);
}
return {
success: true
};
} catch (error) {
return await (0, _apiError.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 (0, _apiError.processApiError)(null, response, this.apiBaseUrl || undefined);
}
return {
success: true,
data: data
};
} catch (error) {
return await (0, _apiError.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 (0, _apiError.processApiError)(null, response, this.apiBaseUrl || undefined);
}
return {
success: true,
data: data
};
} catch (error) {
return await (0, _apiError.processApiError)(error, response, this.apiBaseUrl || undefined);
}
}
}
// Export convenience functions for backward compatibility
exports.APIService = APIService;
const initializeApi = async () => {
const apiService = APIService.getInstance();
await apiService.initialize();
};
exports.initializeApi = initializeApi;
const registerOnInitialize = async () => {
const apiService = APIService.getInstance();
return await apiService.registerOnInitialize();
};
exports.registerOnInitialize = registerOnInitialize;
const updateUserData = async params => {
const apiService = APIService.getInstance();
return await apiService.updateUserData(params);
};
exports.updateUserData = updateUserData;
const getTokenDetails = async params => {
const apiService = APIService.getInstance();
return await apiService.getTokenDetails(params);
};
exports.getTokenDetails = getTokenDetails;
const getPopupDescription = async params => {
const apiService = APIService.getInstance();
return await apiService.getPopupDescription(params);
};
exports.getPopupDescription = getPopupDescription;
//# sourceMappingURL=api.js.map