diff --git a/src/treaty2/index.ts b/src/treaty2/index.ts index fe54d80..d1e4ed0 100644 --- a/src/treaty2/index.ts +++ b/src/treaty2/index.ts @@ -292,31 +292,6 @@ const createProxy = ( if (isGetOrHead) delete fetchInit.body - if (onRequest) { - if (!Array.isArray(onRequest)) onRequest = [onRequest] - - for (const value of onRequest) { - const temp = await value(path, fetchInit) - - if (typeof temp === 'object') - fetchInit = { - ...fetchInit, - ...temp, - headers: { - ...fetchInit.headers, - ...processHeaders( - temp.headers, - path, - fetchInit - ) - } - } - } - } - - // ? Duplicate because end-user might add a body in onRequest - if (isGetOrHead) delete fetchInit.body - if (hasFile(body)) { const formData = new FormData() @@ -396,12 +371,19 @@ const createProxy = ( ...temp, headers: { ...fetchInit.headers, - ...temp.headers - } as Record + ...processHeaders( + temp.headers, + path, + fetchInit + ) + } } } } + // ? Duplicate because end-user might add a body in onRequest + if (isGetOrHead) delete fetchInit.body + const url = domain + path + q const response = await (elysia?.handle( new Request(url, fetchInit) diff --git a/test/treaty2.test.ts b/test/treaty2.test.ts index 2b96873..c7c26b1 100644 --- a/test/treaty2.test.ts +++ b/test/treaty2.test.ts @@ -3,6 +3,7 @@ import { treaty } from '../src' import { describe, expect, it, beforeAll, afterAll, mock } from 'bun:test' + const randomObject = { a: 'a', b: 2, @@ -20,6 +21,7 @@ const randomArray = [ new Date(0), { a: 'a', b: 2, c: true, d: false, e: null, f: new Date(0) } ] + const websocketPayloads = [ // strings 'str', @@ -110,6 +112,21 @@ const app = new Elysia() }) } ) + .get( + '/headers-uppercased', + ({ headers, headers: { username, alias } }) => ({ + username, + alias, + authorization: headers.authorization + }), + { + headers: t.Object({ + username: t.String(), + alias: t.Literal('Kristen'), + authorization: t.Optional(t.Literal('Bearer token')) + }) + } + ) .get( '/headers-custom', ({ headers, headers: { username, alias } }) => ({ @@ -341,6 +358,38 @@ describe('Treaty2', () => { }) }) + it('can handle upper case header values', async () => { + const client = treaty(app, { + onRequest(path) { + if (path === '/headers-uppercased') { + return { + headers: { + Authorization: 'Bearer token' + } + } + } + }, + async onResponse(response) { + return { intercepted: true, data: await response.json() } + } + }) + + const headers = { username: 'a', alias: 'Kristen' } as const + + const { data } = await client['headers-uppercased'].get({ + headers + }) + + expect(data).toEqual({ + // @ts-expect-error + intercepted: true, + data: { + ...headers, + authorization: 'Bearer token' + } + }) + }) + it('handle interception array', async () => { const client = treaty(app, { onRequest: [