Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/treaty2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ const createNewFile = (v: File) =>
reader.readAsArrayBuffer(v)
})

const processHeaders = (
const processHeaders = async (
h: Treaty.Config['headers'],
path: string,
options: RequestInit = {},
headers: Record<string, string> = {}
): Record<string, string> => {
): Promise<Record<string, string>> => {
if (Array.isArray(h)) {
for (const value of h)
if (!Array.isArray(value))
headers = processHeaders(value, path, options, headers)
headers = await processHeaders(value, path, options, headers)
else {
const key = value[0]
if (typeof key === 'string')
Expand All @@ -93,7 +93,7 @@ const processHeaders = (
if (h instanceof Headers)
return processHeaders(h, path, options, headers)

const v = h(path, options)
const v = await h(path, options)
if (v) return processHeaders(v, path, options, headers)
return headers

Expand Down Expand Up @@ -204,8 +204,6 @@ const createProxy = (
method === 'head' ||
method === 'subscribe'

headers = processHeaders(headers, path, options)

const query = isGetOrHead
? (body as Record<string, string | string[] | undefined>)
?.query
Expand Down Expand Up @@ -260,6 +258,8 @@ const createProxy = (
}

return (async () => {
headers = await processHeaders(headers, path, options)

let fetchInit = {
method: method?.toUpperCase(),
body,
Expand All @@ -269,12 +269,12 @@ const createProxy = (

fetchInit.headers = {
...headers,
...processHeaders(
...(await processHeaders(
// For GET and HEAD, options is moved to body (1st param)
isGetOrHead ? body?.headers : options?.headers,
path,
fetchInit
)
))
}

const fetchOpts =
Expand All @@ -301,11 +301,11 @@ const createProxy = (
...temp,
headers: {
...fetchInit.headers,
...processHeaders(
...(await processHeaders(
temp.headers,
path,
fetchInit
)
))
}
}
}
Expand Down Expand Up @@ -396,11 +396,11 @@ const createProxy = (
...temp,
headers: {
...fetchInit.headers,
...processHeaders(
...(await processHeaders(
temp.headers,
path,
fetchInit
)
))
} as Record<string, string>
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/treaty2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export namespace Treaty {
| ((
path: string,
options: RequestInit
) => RequestInit['headers'] | void)
) => MaybePromise<RequestInit['headers'] | void>)
>
onRequest?: MaybeArray<
(
Expand Down
31 changes: 31 additions & 0 deletions test/treaty2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,37 @@ describe('Treaty2', () => {
})
})

it('accept async headers configuration', async () => {
const client = treaty(app, {
async headers(path) {
// Simulate async operation (e.g., fetching token)
await new Promise((r) => setTimeout(r, 10))
if (path === '/headers-custom')
return {
'x-custom': 'custom'
}
},
async onResponse(response) {
return { intercepted: true, data: await response.json() }
}
})

const headers = { username: 'a', alias: 'Kristen' } as const

const { data } = await client['headers-custom'].get({
headers
})

expect(data).toEqual({
// @ts-expect-error
intercepted: true,
data: {
...headers,
'x-custom': 'custom'
}
})
})

it('accept headers configuration array', async () => {
const client = treaty(app, {
headers: [
Expand Down