mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-10-01 19:20:31 +00:00
* Introduced `headers` property in `ClientConfig` interface. * Updated `HeaderSpecClient` and `ResolveSpecClient` to utilize custom headers. * Implemented `mergeHeaders` function to handle case-insensitive header merging. * Added tests for custom header functionality in clients.
31 lines
1.2 KiB
TypeScript
31 lines
1.2 KiB
TypeScript
import type { ClientConfig } from './types';
|
|
|
|
/** Merge HTTP headers case-insensitively, preserving the winning spelling. */
|
|
export function mergeHeaders(...sources: Record<string, string>[]): Record<string, string> {
|
|
const result: Record<string, string> = {};
|
|
for (const source of sources) {
|
|
for (const [name, value] of Object.entries(source)) {
|
|
for (const existing of Object.keys(result)) {
|
|
if (existing.toLowerCase() === name.toLowerCase()) delete result[existing];
|
|
}
|
|
Object.defineProperty(result, name, { value, enumerable: true, configurable: true, writable: true });
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function clientHeaders(config: ClientConfig): Record<string, string> {
|
|
return mergeHeaders(
|
|
{ 'Content-Type': 'application/json' },
|
|
config.headers ?? {},
|
|
config.token ? { Authorization: `Bearer ${config.token}` } : {},
|
|
);
|
|
}
|
|
|
|
export function clientCacheKey(config: ClientConfig): string {
|
|
const headers = Object.entries(clientHeaders(config))
|
|
.map(([name, value]) => [name.toLowerCase(), value])
|
|
.sort(([a], [b]) => a.localeCompare(b));
|
|
return JSON.stringify([config.baseUrl, headers]);
|
|
}
|