Julien-R44/tuyau

passing headers issue

Closed this issue ยท 3 comments

hey @Julien-R44 ๐Ÿ‘‹ , so I've noticed that passing headers to the tuyau client at request doesn't seem to pass those headers correctly, check the little test I wrote in the clinet.speck.ts file

  test('pass headers from queryOptions', async ({ assert }) => {
    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', {
      reqheaders: {
        'default-header': 'default-value',
        'custom-header': 'custom-value',
      },
    })
      .get('/users')
      .query({ email: 'foo@ok.com' })
      .reply(200, { token: '123' })

    await tuyau.users.$get({
      query: { email: 'foo@ok.com' },
      headers: { 'custom-header': 'custom-value' },
    })
  })

and this the result I got

image

it's sees the test above might had a mistake related to matching headers. I wrote the two tests below and it's seems the issue only affect get requests

test.group('Client | Headers Handling', () => {
  test('pass headers from queryOptions', async ({ assert }) => {
    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: 'foo@ok.com' })
      .matchHeader('default-header', 'default-value')
      .matchHeader('custom-header', 'custom-value')
      .reply(200, { token: '123' })

    await tuyau.users.$get({
      query: { email: 'foo@ok.com' },
      headers: { 'custom-header': 'custom-value' },
    })
  })

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

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

    const formData = new FormData()
    formData.append('name', 'julien')
    formData.append('email', 'foo@ok.com')

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

image

seems this fixes the issue but wouldn't' say it's a good solution, from request.ts at the client package

image

Thanks a lot for the reproduction !!

Fixed with latest commit. Released soon