UNPKG

snowcell

Version:

Official Snowcell Node.js/TypeScript SDK

50 lines 2.04 kB
import { SnowcellError } from './errors.js'; import { fetchWithRetries } from './util.js'; export const DEFAULT_API_BASE_URL = process.env.SNOWCELL_API_BASE_URL ?? 'https://api.snowcell.io'; export const DEFAULT_INFERENCE_BASE_URL = process.env.SNOWCELL_INFERENCE_BASE_URL ?? 'https://inference.snowcell.io'; export class SnowcellClientBase { _apiToken; _apiBaseUrl; _inferenceBaseUrl; _timeoutMs; _headers; constructor(opts = {}) { this._apiToken = opts.apiToken ?? process.env.SNOWCELL_API_TOKEN ?? undefined; this._apiBaseUrl = (opts.apiBaseUrl ?? DEFAULT_API_BASE_URL).replace(/\/$/, ''); this._inferenceBaseUrl = (opts.inferenceBaseUrl ?? DEFAULT_INFERENCE_BASE_URL).replace(/\/$/, ''); this._timeoutMs = opts.timeoutMs ?? 300_000; this._headers = { 'User-Agent': `snowcell-node/${process.env.npm_package_version ?? '0'}`, ...(this._apiToken ? { Authorization: `Bearer ${this._apiToken}` } : {}), 'Content-Type': 'application/json', ...opts.headers, }; } async _request(base, method, path, init = {}) { const url = new URL(path, base); const controller = new AbortController(); const id = setTimeout(() => controller.abort(), this._timeoutMs); try { const res = await fetchWithRetries(url, { method, signal: controller.signal, headers: { ...this._headers, ...init.headers }, body: init.body, }); if (res.status >= 400) { throw await SnowcellError.fromResponse(res); } return res; } finally { clearTimeout(id); } } _api(method, path, init = {}) { return this._request(this._apiBaseUrl + '/', method, path, init); } _inference(method, path, init = {}) { return this._request(this._inferenceBaseUrl + '/', method, path, init); } } //# sourceMappingURL=client.js.map