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

Parmeters set with in: header are not added to headers #1230

Closed
1 task
FinnIckler opened this issue Jul 19, 2023 · 5 comments · Fixed by #1239
Closed
1 task

Parmeters set with in: header are not added to headers #1230

FinnIckler opened this issue Jul 19, 2023 · 5 comments · Fixed by #1239
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library

Comments

@FinnIckler
Copy link

Description

When you have a parameter in your openAPI spec with in: header, it is not added to the headers in the request

Reproduction

openapi spec like this:

(...)
paths:
  "/api/v1/register":
    post:
      summary: Add an attendee registration
      parameters:
      - name: SomeHeader
        in: header
        schema:
          type: string
      responses:
        '202':
          description: only required fields included
(...)

Generates a schema file like this

(...)
  "/api/v1/register": {
    /** Add an attendee registration */
    post: {
      parameters: {
        header?: {
          SomeHeader?: string;
        };
      };
(...)

which I use like this:

const { data, error, response } = await post('/api/v1/register', {
    params: { header: { SomeHeader: value } },
    body,
  })

If I look into the request in Firefox, the header parameter is not send with it. I also took a quick look at the code and I don't think this is in there either.
Expected result
Header to be added to the headers.

Checklist

@FinnIckler FinnIckler added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Jul 19, 2023
@drwpow
Copy link
Contributor

drwpow commented Jul 20, 2023

ah good call out—I think this is indeed missing.

🤔 Just from an API perspective, we do have the params key to namespace it from other options like generic (untyped) headers and body. But would it be weird to have both headers and params.header?

get('/some/url', {
  headers: {
    foo: 'bar',
  },
  params: {
    header: {
      typedHeader: 'value',
    },
  },
});

My initial take is “yes,” because probably this is rare—headers will usually be defined on createClient() and not on the method calls. Plus, there would be a distinction between typed (in your OpenAPI schema) and untyped headers (not in your schema). But wanted to gauge others’ knee-jerk reaction (after all, a key goal of this project is a clean, easy-to-use API 🙂)

@FinnIckler
Copy link
Author

The way it works currently is params.header not params.headers, which I find fine to distinguish it as a "parameter in a header field" instead, but it could get confusing if there are multiple

@drwpow
Copy link
Contributor

drwpow commented Jul 20, 2023

Ah yes—my mistake. Edited the code snippet.

@angularsen
Copy link

angularsen commented Jul 20, 2023

+1, just stumbled on this on my initial experimentation with this library

Update:
I'm also fine with passing params.header.typedHeader in addition to the headers property, but it does feel a bit wonky.

Another option, maybe, would be to extend headers to be a union type of the OpenAPI declared headers and HeadersInit type.

So you could get strongly typed suggestions in addition to supporting any string keys for headers:

get('/some/url', {
  headers: {
    'x-custom-undocumented-header': 'foo',
    typedHeader: 'I am declared in OpenAPI schema',
  }
});

@drwpow
Copy link
Contributor

drwpow commented Jul 25, 2023

Fixed in 0.6.2!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants