fix: Pass RequestInit options to auth requests (#1066)
Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Paul Carleton <paulcarletonjr@gmail.com>
This commit is contained in:
parent
a7e525a100
commit
df56e35322
1 changed files with 165 additions and 23 deletions
|
|
@ -2,6 +2,49 @@ import { JSONRPCMessage, MessageExtraInfo, RequestId } from '../types.js';
|
|||
|
||||
export type FetchLike = (url: string | URL, init?: RequestInit) => Promise<Response>;
|
||||
|
||||
/**
|
||||
* Normalizes HeadersInit to a plain Record<string, string> for manipulation.
|
||||
* Handles Headers objects, arrays of tuples, and plain objects.
|
||||
*/
|
||||
export function normalizeHeaders(headers: HeadersInit | undefined): Record<string, string> {
|
||||
if (!headers) return {};
|
||||
|
||||
if (headers instanceof Headers) {
|
||||
return Object.fromEntries(headers.entries());
|
||||
}
|
||||
|
||||
if (Array.isArray(headers)) {
|
||||
return Object.fromEntries(headers);
|
||||
}
|
||||
|
||||
return { ...(headers as Record<string, string>) };
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a fetch function that includes base RequestInit options.
|
||||
* This ensures requests inherit settings like credentials, mode, headers, etc. from the base init.
|
||||
*
|
||||
* @param baseFetch - The base fetch function to wrap (defaults to global fetch)
|
||||
* @param baseInit - The base RequestInit to merge with each request
|
||||
* @returns A wrapped fetch function that merges base options with call-specific options
|
||||
*/
|
||||
export function createFetchWithInit(baseFetch: FetchLike = fetch, baseInit?: RequestInit): FetchLike {
|
||||
if (!baseInit) {
|
||||
return baseFetch;
|
||||
}
|
||||
|
||||
// Return a wrapped fetch that merges base RequestInit with call-specific init
|
||||
return async (url: string | URL, init?: RequestInit): Promise<Response> => {
|
||||
const mergedInit: RequestInit = {
|
||||
...baseInit,
|
||||
...init,
|
||||
// Headers need special handling - merge instead of replace
|
||||
headers: init?.headers ? { ...normalizeHeaders(baseInit.headers), ...normalizeHeaders(init.headers) } : baseInit.headers
|
||||
};
|
||||
return baseFetch(url, mergedInit);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Options for sending a JSON-RPC message.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in a new issue