UNPKG

@speechmatics/auth

Version:

Library for fetching temporary keys for Speechmatics APIs

63 lines (61 loc) 1.63 kB
async function createSpeechmaticsJWT({ type, apiKey, clientRef, ttl = 60, managementPlatformURL = "https://mp.speechmatics.com/v1", region = "eu" }) { if (type === "batch" && !clientRef) { throw new SpeechmaticsJWTError( "ValidationFailed", "Must set the `client_ref` parameter when using temporary keys for batch transcription. See documentation at https://docs.speechmatics.com/introduction/authentication#batch-transcription" ); } const resp = await fetch(`${managementPlatformURL}/api_keys?type=${type}`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${apiKey}` }, body: JSON.stringify({ ttl, region, client_ref: clientRef }) }); if (resp.ok) { return (await resp.json()).key_value; } if (resp.status === 403) { throw new SpeechmaticsJWTError("Unauthorized", "unauthorized"); } let json; try { json = await resp.json(); } catch (e) { throw new SpeechmaticsJWTError( "UnknownError", "Failed to parse JSON response", { cause: e } ); } if (resp.status === 422) { throw new SpeechmaticsJWTError("ValidationFailed", json.message, { cause: json }); } throw new SpeechmaticsJWTError( "UnknownError", `Got response with status ${resp.status}` ); } class SpeechmaticsJWTError extends Error { constructor(type, message, opts) { super(message, opts); this.type = type; this.name = "SpeechmaticsJWTError"; } } export { SpeechmaticsJWTError, createSpeechmaticsJWT }; //# sourceMappingURL=index.mjs.map