Skip to content

Commit

Permalink
fix: pass query options when using $get
Browse files Browse the repository at this point in the history
Close #14
  • Loading branch information
Julien-R44 committed Nov 2, 2024
1 parent 1ad92d4 commit 5412592
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ function createProxy(options: {
/**
* Otherwise, it's time to make the final request.
*/
const method = paths[paths.length - 1].slice(1) as (typeof methods)[number]
const isGetOrHead = ['get', 'head'].includes(method)
return new TuyauRequest({
body,
client,
queryOptions,
method,
path: paths.slice(0, -1).join('/'),
method: paths[paths.length - 1].slice(1) as (typeof methods)[number],
queryOptions: isGetOrHead ? body : queryOptions,
})
},
})
Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ export class TuyauRequest {
* Make the request
*/
const isGetOrHead = ['get', 'head'].includes(this.#options.method)
const query = isGetOrHead ? body?.query : this.#options.queryOptions?.query
const response = await this.#options.client[this.#options.method](
removeSlash(this.#options.path),
{
searchParams: buildSearchParams(query),
searchParams: buildSearchParams(this.#options.queryOptions?.query || {}),
[key]: !isGetOrHead ? body : undefined,
...this.#options.queryOptions,
},
Expand Down
92 changes: 92 additions & 0 deletions packages/client/tests/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,96 @@ test.group('Client | Runtime', () => {

await tuyau.users.$post(formData as any)
})

test('pass headers from queryOptions', async () => {
const tuyau = createTuyau<{
routes: []
definition: {
users: {
$get: {
request: { email: string }
response: { 200: Simplify<Serialize<{ token: string }>> }
}
}
}
}>({
baseUrl: 'http://localhost:3333',
headers: { 'default-header': 'default-value' },
})

nock('http://localhost:3333', {})
.get('/users')
.query({ email: '[email protected]' })
// .matchHeader('default-header', 'default-value')
.matchHeader('custom-header', 'custom-value')
.reply(200, { token: '123' })

await tuyau.users.$get({
query: { email: '[email protected]' },
headers: { 'custom-header': 'custom-value' },
})
})

test('pass headers to post request', async () => {
const tuyau = createTuyau<{
routes: []
definition: {
users: {
$post: {
request: { name: string; email: string }
response: { 200: Simplify<Serialize<{ token: string }>> }
}
}
}
}>({
baseUrl: 'http://localhost:3333',
headers: {
'default-header': 'default-value',
},
})

nock('http://localhost:3333')
.post('/users')
.reply(200, { token: '123' })
.matchHeader('content-type', /multipart\/form-data/)
.matchHeader('custom-header', 'custom-value')
.matchHeader('default-header', 'default-value')

const formData = new FormData()
formData.append('name', 'julien')
formData.append('email', '[email protected]')

await tuyau.users.$post(formData as any, {
headers: { 'custom-header': 'custom-value' },
})
})

test('pass query options to get request', async () => {
const tuyau = createTuyau<{
routes: []
definition: {
users: {
$get: {
request: { email: string }
response: { 200: Simplify<Serialize<{ token: string }>> }
}
}
}
}>({ baseUrl: 'http://localhost:3333' })

nock('http://localhost:3333')
.get('/users')
.query({ email: '[email protected]' })
.reply(200, { token: '123' })
.matchHeader('custom-header', 'custom-value')
.matchHeader('second-header', 'custom-value')

await tuyau.users.$get({
query: { email: '[email protected]' },
headers: { 'custom-header': 'custom-value' },
hooks: {
beforeRequest: [(request) => request.headers.set('second-header', 'custom-value')],
},
})
})
})

0 comments on commit 5412592

Please sign in to comment.