@speechmatics/auth
Version:
Library for fetching temporary keys for Speechmatics APIs
1 lines • 3.81 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["// See documentation at https://docs.speechmatics.com/introduction/authentication#temporary-key-configuration\nexport async function createSpeechmaticsJWT({\n type,\n apiKey,\n clientRef,\n ttl = 60,\n managementPlatformURL = 'https://mp.speechmatics.com/v1',\n region = 'eu',\n}: {\n type: 'batch' | 'rt' | 'flow';\n apiKey: string;\n clientRef?: string;\n ttl?: number;\n region?: 'eu' | 'usa' | 'au';\n managementPlatformURL?: string;\n}): Promise<string> {\n if (type === 'batch' && !clientRef) {\n throw new SpeechmaticsJWTError(\n 'ValidationFailed',\n 'Must set the `client_ref` parameter when using temporary keys for batch transcription. See documentation at https://docs.speechmatics.com/introduction/authentication#batch-transcription',\n );\n }\n\n const resp = await fetch(`${managementPlatformURL}/api_keys?type=${type}`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${apiKey}`,\n },\n body: JSON.stringify({\n ttl,\n region,\n client_ref: clientRef,\n }),\n });\n\n if (resp.ok) {\n return (await resp.json()).key_value;\n }\n\n // Handle errors\n if (resp.status === 403) {\n throw new SpeechmaticsJWTError('Unauthorized', 'unauthorized');\n }\n\n let json: { code: string; message: string };\n\n try {\n json = await resp.json();\n } catch (e) {\n throw new SpeechmaticsJWTError(\n 'UnknownError',\n 'Failed to parse JSON response',\n { cause: e },\n );\n }\n\n if (resp.status === 422) {\n throw new SpeechmaticsJWTError('ValidationFailed', json.message, {\n cause: json,\n });\n }\n\n throw new SpeechmaticsJWTError(\n 'UnknownError',\n `Got response with status ${resp.status}`,\n );\n}\n\nexport type SpeechmaticsJWTErrorType =\n | 'ValidationFailed'\n | 'Unauthorized'\n | 'UnknownError';\n\nexport class SpeechmaticsJWTError extends Error {\n constructor(\n readonly type: SpeechmaticsJWTErrorType,\n message: string,\n opts?: ErrorOptions,\n ) {\n super(message, opts);\n this.name = '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