diff --git a/example/a.ts b/example/a.ts index 124266d..d600fb2 100644 --- a/example/a.ts +++ b/example/a.ts @@ -1,10 +1,50 @@ import { Elysia, t } from 'elysia' import { treaty } from '../src' -const app = new Elysia().get('/foo/:id', ({ params: { id } }) => { - return `ID is ${id}` +const app = new Elysia() + .get( + '/headers-custom', + ({ headers, headers: { username, alias } }) => ({ + username, + alias, + 'x-custom': headers['x-custom'] + }), + { + headers: t.Object({ + username: t.String(), + alias: t.Literal('Kristen'), + 'x-custom': t.Optional(t.Literal('custom')) + }) + } + ) + +const client = treaty(app, { + headers: [ + (path) => { + 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 }) -const api = treaty(app) +console.log(data) -await api.foo({ id: 999 }).get().then(console.log) +// expect(data).toEqual({ +// // @ts-expect-error +// intercepted: true, +// data: { +// ...headers, +// 'x-custom': 'custom' +// } +// }) \ No newline at end of file diff --git a/src/treaty2/index.ts b/src/treaty2/index.ts index b6e055d..c24c463 100644 --- a/src/treaty2/index.ts +++ b/src/treaty2/index.ts @@ -298,7 +298,7 @@ const createProxy = ( try { const temp = await value(response.clone()) - if (data !== undefined && data !== null) { + if (temp !== undefined && temp !== null) { data = temp break } @@ -353,15 +353,13 @@ const createProxy = ( } } - const result = { + return { data, error, response, status: response.status, headers: response.headers } - - return result })() }