mirror of
https://github.com/immich-app/immich.git
synced 2025-09-29 15:31:13 -04:00
feat: add support for custom headers to TS SDK (#21205)
* Add support for custom headers * fix: added assertNoApiKey function
This commit is contained in:
parent
59627e2b4c
commit
c2313f7a99
@ -6,11 +6,15 @@ export * from './fetch-errors.js';
|
||||
export interface InitOptions {
|
||||
baseUrl: string;
|
||||
apiKey: string;
|
||||
headers?: Record<string, string>;
|
||||
}
|
||||
|
||||
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,26 @@ export const setApiKey = (apiKey: string) => {
|
||||
defaults.headers['x-api-key'] = apiKey;
|
||||
};
|
||||
|
||||
export const setHeader = (key: string, value: string) => {
|
||||
assertNoApiKey(key);
|
||||
defaults.headers = defaults.headers || {};
|
||||
defaults.headers[key] = value;
|
||||
};
|
||||
|
||||
export const setHeaders = (headers: Record<string, string>) => {
|
||||
defaults.headers = defaults.headers || {};
|
||||
for (const [key, value] of Object.entries(headers)) {
|
||||
assertNoApiKey(key);
|
||||
defaults.headers[key] = value;
|
||||
}
|
||||
};
|
||||
|
||||
const assertNoApiKey = (headerKey: string) => {
|
||||
if (headerKey.toLowerCase() === 'x-api-key') {
|
||||
throw new Error('The API key header can only be set using setApiKey().');
|
||||
}
|
||||
};
|
||||
|
||||
export const getAssetOriginalPath = (id: string) => `/assets/${id}/original`;
|
||||
|
||||
export const getAssetThumbnailPath = (id: string) => `/assets/${id}/thumbnail`;
|
||||
|
Loading…
x
Reference in New Issue
Block a user