wobsoriano / trpc-nuxt

End-to-end typesafe APIs in Nuxt applications.
trpc-nuxt.vercel.app
MIT License
683 stars 38 forks source link

Response headers not copied over in customFetch #72

Closed imulab closed 1 year ago

imulab commented 1 year ago

It seems the customFetch used in the custom httpLink and httpBatchLink does not copy over the response headers. Consequently, response headers become undefined in links downstream.

This is a snippet of the customFetch function, which adds the resolved json to the response object. However, response.headers is not copied over when using the spread operator.

// catch part is redacted
function customFetch(input: any, init: any) {
  return globalThis.$fetch.raw(input.toString(), init)
    .then((response) => {
      return {
        ...response,
        json: () => Promise.resolve(response._data),
      }
    });
}

I believe this is because the spread operator only deals with string based properties and response.headers is keyed as a Symbol.

Currently, I rectify this behavior by explicitly copying over the headers property:

// catch part is redacted
function customFetch(input: any, init: any) {
  return globalThis.$fetch.raw(input.toString(), init)
    .then((response) => {
      return {
        ...response,
        // explicitly copy over the headers
        headers: response.headers,
        json: () => Promise.resolve(response._data),
      }
    });
}

I am not sure if omitting the headers is by design or not (since there's opinion from the tRPC repo saying that tRPC does not care about the transport, so the only thing matters is the response body).

wobsoriano commented 1 year ago

Added here, thanks! Forgot to add it :)

https://github.com/wobsoriano/trpc-nuxt/releases/tag/v0.10.12