From 0a266bf13dfb1186b12712587528bb49a520cf39 Mon Sep 17 00:00:00 2001 From: NanoSpicer Date: Tue, 4 Jun 2024 16:21:09 +0200 Subject: [PATCH 1/2] Adds test that doesn't pass --- test/treaty2.test.ts | 49 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/test/treaty2.test.ts b/test/treaty2.test.ts index 412e7df..8ed2fbf 100644 --- a/test/treaty2.test.ts +++ b/test/treaty2.test.ts @@ -5,7 +5,7 @@ import { describe, expect, it, beforeAll, afterAll, mock } from 'bun:test' const randomObject = { a: 'a', b: 2, c: true, d: false, e: null, f: new Date(0) } -const randomArray = ['a', 2, true, false, null, new Date(0), { a: 'a', b: 2, c: true, d: false, e: null, f: new Date(0)}] +const randomArray = ['a', 2, true, false, null, new Date(0), { a: 'a', b: 2, c: true, d: false, e: null, f: new Date(0) }] const websocketPayloads = [ // strings 'str', @@ -94,6 +94,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 } }) => ({ @@ -322,6 +337,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: [ From e06556e8e4428e70ca57b67afc3831c5db429123 Mon Sep 17 00:00:00 2001 From: NanoSpicer Date: Tue, 4 Jun 2024 16:21:17 +0200 Subject: [PATCH 2/2] Fixes test that doesn't pass --- src/treaty2/index.ts | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/src/treaty2/index.ts b/src/treaty2/index.ts index b5ec310..33c2fa4 100644 --- a/src/treaty2/index.ts +++ b/src/treaty2/index.ts @@ -229,31 +229,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() @@ -333,12 +308,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)