From f7d069172cf8700c7130b00be19d0af18efc8927 Mon Sep 17 00:00:00 2001 From: Luke Hagar Date: Sat, 23 Aug 2025 23:31:13 -0500 Subject: [PATCH] Add support for custom headers --- open-api/typescript-sdk/src/index.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/open-api/typescript-sdk/src/index.ts b/open-api/typescript-sdk/src/index.ts index 77be18f0e..9a2a4b2ee 100644 --- a/open-api/typescript-sdk/src/index.ts +++ b/open-api/typescript-sdk/src/index.ts @@ -6,11 +6,15 @@ export * from './fetch-errors.js'; export interface InitOptions { baseUrl: string; apiKey: string; + headers?: Record; } -export const init = ({ baseUrl, apiKey }: InitOptions) => { +export const init = ({ baseUrl, apiKey, headers }: InitOptions) => { setBaseUrl(baseUrl); setApiKey(apiKey); + if (headers) { + setHeaders(headers); + } }; export const getBaseUrl = () => defaults.baseUrl; @@ -24,6 +28,24 @@ export const setApiKey = (apiKey: string) => { defaults.headers['x-api-key'] = apiKey; }; +export const setHeader = (key: string, value: string) => { + if (key.toLowerCase() === 'x-api-key') { + throw new Error('The API key header can only be set using setApiKey().'); + } + defaults.headers = defaults.headers || {}; + defaults.headers[key] = value; +}; + +export const setHeaders = (headers: Record) => { + defaults.headers = defaults.headers || {}; + for (const [key, value] of Object.entries(headers)) { + if (key.toLowerCase() === 'x-api-key') { + throw new Error('The API key header can only be set using setApiKey().'); + } + defaults.headers[key] = value; + } +}; + export const getAssetOriginalPath = (id: string) => `/assets/${id}/original`; export const getAssetThumbnailPath = (id: string) => `/assets/${id}/thumbnail`;