UNPKG

@sociate/sociate-api-sdk

Version:

Javascript client for Sociate AI APIs

52 lines (51 loc) 1.37 kB
export class HttpClient { basePath; token; constructor(basePath, token) { this.basePath = basePath; this.token = token; } get(path) { return this.request('GET', path); } post(path, body) { return this.request('POST', path, body); } put(path, body) { return this.request('PUT', path, body); } delete(path, body) { return this.request('DELETE', path, body); } setToken(token) { this.token = token; } clearToken() { this.token = undefined; } async request(method, path, body) { const url = `${this.basePath}${path}`; const headers = { 'Content-Type': 'application/json', }; if (this.token) { headers['x-api-key'] = this.token; } const options = { method, headers, body: body ? JSON.stringify(body) : undefined, }; try { const response = await fetch(url, options); if (!response.ok) { const error = await response.json(); throw new Error(error.message || 'Request failed'); } return (await response.json()); } catch (error) { throw new Error(`HTTP ${method} request to ${url} failed: ${error}`); } } }