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

Spreading request headers object in middleware function doesn't work as per examples #907

Closed
jonesty opened this issue Jun 6, 2024 · 2 comments · Fixed by #1001
Closed

Comments

@jonesty
Copy link

jonesty commented Jun 6, 2024

Perceived Problem

The headers object on the request passed to a request middleware function is of type Headers, which yields an empty object when spread. The examples in the repo show usage as below.

const requestMiddleware: RequestMiddleware = async (request) => {
  return {
    ...request,
    headers: {
      ...request.headers,
      ^^^^^^^^^^^^^^^^^^ This always yields an empty object so any headers passed to the request call are lost.
      'x-auth-token': await getAccessToken(),
    },
  }
}

Ideas / Proposed Solution(s)

I guess there's a couple of options here - modify createFetcher to convert the headers to a plain object before passing in or update the examples to show something like below.

const requestMiddleware: RequestMiddleware = async (request) => {
  const headers = new Headers(request.headers);
  headers.set('x-auth-token', await getAccessToken());

  return {
    ...request,
    headers
  }
}
@aecorredor
Copy link

+1. This just threw off for a while. It must have changed in a recent version, since it did work before.

@jasonkuhrt
Copy link
Owner

jasonkuhrt commented Jun 9, 2024

This issue doesn't need more comments, it's a docs improvement for anyone motivated. Thanks.

Repository owner locked as resolved and limited conversation to collaborators Jun 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants