From 5122b26f230a4aef0656dd2e5e47c73ff08d6719 Mon Sep 17 00:00:00 2001 From: Max Holman Date: Sat, 29 Oct 2022 21:25:43 +0800 Subject: [PATCH] feat: add support for resolvable headers (headers that can also be functions) --- lib/fetcher.ts | 7 ++++++- src/isomorphic-fetch.ts | 31 +++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/fetcher.ts b/lib/fetcher.ts index 235c5a2..9cc3e3d 100644 --- a/lib/fetcher.ts +++ b/lib/fetcher.ts @@ -1,10 +1,15 @@ import type { HttpMethod } from './generated/models.js'; +export type ResolvableHeaders = Record< + string, + string | (() => string) | (() => Promise) +>; + export type FetcherParams = { url: URL; method: HttpMethod; body?: T; - headers?: Record; + headers?: ResolvableHeaders; credentials?: 'include' | 'omit' | 'same-origin'; signal?: AbortSignal; }; diff --git a/src/isomorphic-fetch.ts b/src/isomorphic-fetch.ts index 4efd9bc..95b7b52 100644 --- a/src/isomorphic-fetch.ts +++ b/src/isomorphic-fetch.ts @@ -1,21 +1,44 @@ import ky from 'ky-universal'; -import type { FetcherParams, FetcherResponse } from '../lib/fetcher.js'; +import type { + FetcherParams, + FetcherResponse, + ResolvableHeaders, +} from '../lib/fetcher.js'; + +async function resolveHeaders( + headers: ResolvableHeaders | undefined, +): Promise> { + if (!headers) { + return {}; + } + + return Object.fromEntries( + await Promise.all( + Object.entries(headers).map( + async ([key, value]): Promise<[string, string]> => [ + key, + value instanceof Function ? await value() : value, + ], + ), + ), + ); +} export async function isomorphicFetcher( params: FetcherParams, ): Promise> { const { url, method, body, headers, credentials, signal } = params; - // const { keepAlive, } = options; + const resolvedHeaders = await resolveHeaders(headers); const res = await ky(url, { method, throwHttpErrors: false, - ...(headers && { headers }), + ...(resolvedHeaders && { headers: resolvedHeaders }), ...(credentials && { credentials }), ...(!!body && { json: body }), ...(signal && { signal }), - // ...(keepAlive && { keepalive: keepAlive }), + timeout: 10000, }); return {