import { CookieJar } from "./cookie_jar.ts";
export type WrapFetchOptions = { fetch?: typeof fetch; cookieJar?: CookieJar;};
type FetchParameters = Parameters<typeof fetch>;
export function wrapFetch(options?: WrapFetchOptions): typeof fetch { const { cookieJar = new CookieJar(), fetch = globalThis.fetch } = options || {};
async function wrappedFetch( input: FetchParameters[0], init?: FetchParameters[1], ) { if (!input) { return await fetch(input); } const cookieString = cookieJar.getCookieString(input);
let interceptedInit: RequestInit; if (init) { interceptedInit = init; } else if (input instanceof Request) { interceptedInit = input; } else { interceptedInit = {}; }
if (!(interceptedInit.headers instanceof Headers)) { interceptedInit.headers = new Headers(interceptedInit.headers || {}); } interceptedInit.headers.set("cookie", cookieString);
const response = await fetch(input, interceptedInit); response.headers.forEach((value, key) => { if (key.toLowerCase() === "set-cookie") { cookieJar.setCookie(value, input); } }); return response; }
return wrappedFetch;}