@gladiaio/sdk
Version:
Gladia JavaScript/TypeScript SDK
77 lines (75 loc) • 2.79 kB
JavaScript
const require_helpers = require('./helpers.cjs');
const require_client = require('./v2/live/client.cjs');
require('./v2/live/index.cjs');
const require_version = require('./version.cjs');
//#region src/client.ts
function normalizeGladiaHeaders(headers) {
const entries = Array.isArray(headers) ? headers : Object.entries(headers);
return Object.fromEntries(entries.map(([key, value]) => {
const lcKey = key.toLowerCase();
return [lcKey.startsWith("x-gladia-") ? lcKey : key, value];
}));
}
function assertValidOptions(options) {
let url;
try {
url = new URL(options.apiUrl);
} catch {
throw new Error(`Invalid url: "${options.apiUrl}".`);
}
if (!options?.apiKey && url.hostname.endsWith(".gladia.io")) throw new Error(`You have to set your "apiKey" or define a proxy "apiUrl".`);
if (![
"https:",
"http:",
"wss:",
"ws:"
].includes(url.protocol)) throw new Error(`Only HTTP and WebSocket protocols are supported for apiUrl (received: ${url.protocol}).`);
}
const defaultHttpDelay = (attemptCount) => Math.min(.3 * 2 ** (attemptCount - 1) * 1e3, 1e4);
const defaultWsDelay = (attemptCount) => Math.min(.3 * 2 ** (attemptCount - 1) * 1e3, 2e3);
const gladiaVersion = `SdkJavascript/${require_version.SDK_VERSION}`;
const defaultOptions = {
apiKey: require_helpers.getEnv("GLADIA_API_KEY"),
apiUrl: require_helpers.getEnv("GLADIA_API_URL", "https://api.gladia.io"),
region: require_helpers.getEnv("GLADIA_REGION"),
httpHeaders: { "x-gladia-version": gladiaVersion },
httpRetry: {
maxAttempts: 2,
statusCodes: [
408,
413,
429,
[500, 599]
],
delay: defaultHttpDelay
},
httpTimeout: 1e4,
wsRetry: {
maxAttemptsPerConnection: 5,
closeCodes: [[1002, 4399], [4500, 9999]],
delay: defaultWsDelay,
maxConnections: 0
},
wsTimeout: 1e4
};
/**
* Gladia Client
*/
var GladiaClient = class {
options;
constructor(options) {
if (options?.httpHeaders) options.httpHeaders = normalizeGladiaHeaders(options.httpHeaders);
this.options = require_helpers.deepMergeObjects(defaultOptions, options);
}
liveV2(options) {
if (options?.httpHeaders) options.httpHeaders = normalizeGladiaHeaders(options.httpHeaders);
const mergedOptions = require_helpers.deepMergeObjects(this.options, options);
if (mergedOptions.apiKey) mergedOptions.httpHeaders = require_helpers.deepMergeObjects(mergedOptions.httpHeaders, { "x-gladia-key": mergedOptions.apiKey });
if (mergedOptions.httpHeaders["x-gladia-version"] !== gladiaVersion) mergedOptions.httpHeaders["x-gladia-version"] = `${mergedOptions.httpHeaders["x-gladia-version"].trim()} ${gladiaVersion}`.trim();
assertValidOptions(mergedOptions);
return new require_client.LiveV2Client(mergedOptions);
}
};
//#endregion
exports.GladiaClient = GladiaClient;
//# sourceMappingURL=client.cjs.map