Skip to content

Commit

Permalink
🎉 feat: release 1.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Mar 25, 2024
1 parent 961cf39 commit 992ee9d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
48 changes: 44 additions & 4 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -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'
// }
// })
6 changes: 2 additions & 4 deletions src/treaty2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -353,15 +353,13 @@ const createProxy = (
}
}

const result = {
return {
data,
error,
response,
status: response.status,
headers: response.headers
}

return result
})()
}

Expand Down

0 comments on commit 992ee9d

Please sign in to comment.