Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes sure the headers are processed right before making a request #101

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
36 changes: 9 additions & 27 deletions src/treaty2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -396,12 +371,19 @@ const createProxy = (
...temp,
headers: {
...fetchInit.headers,
...temp.headers
} as Record<string, string>
...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)
Expand Down
49 changes: 49 additions & 0 deletions test/treaty2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { treaty } from '../src'

import { describe, expect, it, beforeAll, afterAll, mock } from 'bun:test'


const randomObject = {
a: 'a',
b: 2,
Expand All @@ -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',
Expand Down Expand Up @@ -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 } }) => ({
Expand Down Expand Up @@ -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: [
Expand Down