odyssey-spatial-comms
Version:
Drop-in replacement for Dolby/Voxeet SDK using Odyssey Spatial Audio Service
87 lines • 2.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiClient = void 0;
const socket_io_client_1 = require("socket.io-client");
class ApiClient {
constructor(config) {
this.socket = null;
this.accessToken = null;
this.session = null; // Reference to session for participant context
this.config = config;
}
setAccessToken(token) {
this.accessToken = token;
}
getAccessToken() {
return this.accessToken;
}
async post(endpoint, data) {
const response = await fetch(`${this.config.serviceUrl}${endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.accessToken}`
},
body: JSON.stringify(data)
});
if (!response.ok) {
let errorMessage = `API request failed: ${response.status}`;
try {
const errorBody = await response.text();
console.error(`SPATIAL-AUDIO: ${response.status} error response:`, errorBody);
errorMessage += ` - ${errorBody}`;
}
catch (e) {
console.error(`SPATIAL-AUDIO: Could not read error response body`);
}
throw new Error(errorMessage);
}
return response.json();
}
async get(endpoint) {
const response = await fetch(`${this.config.serviceUrl}${endpoint}`, {
method: 'GET',
headers: {
'Authorization': this.accessToken ? `Bearer ${this.accessToken}` : ''
}
});
if (!response.ok) {
throw new Error(`API request failed: ${response.status} ${response.statusText}`);
}
return response.json();
}
connectWebSocket() {
return new Promise((resolve, reject) => {
if (this.socket && this.socket.connected) {
resolve(this.socket);
return;
}
this.socket = (0, socket_io_client_1.io)(this.config.serviceUrl, {
auth: {
token: this.accessToken
},
transports: ['polling', 'websocket']
});
this.socket.on('connect', () => {
if (this.config.debug) {
console.log('WebSocket connected to odyssey-spatial-comms');
}
resolve(this.socket);
});
this.socket.on('connect_error', (error) => {
reject(new Error(`WebSocket connection failed: ${error.message}`));
});
});
}
getWebSocket() {
return this.socket;
}
disconnect() {
if (this.socket) {
this.socket.disconnect();
this.socket = null;
}
}
}
exports.ApiClient = ApiClient;
//# sourceMappingURL=api-client.js.map