orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
3.26k stars 340 forks source link

Support global headers from securitySchemes #885

Open Maxim-Mazurok opened 1 year ago

Maxim-Mazurok commented 1 year ago

This is related to #465

However even when using headers: true orval doesn't pick up this part of the swagger.json:

...
  "securitySchemes": {
      "Bearer": {
        "type": "apiKey",
        "description": "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 1safsfsdfdfd\"",
        "name": "Authorization",
        "in": "header"
      }
    }
  },
  "security": [
    {
      "Bearer": [ ]
    }
  ]

Ideally, I would like to have some sort of global way of setting it up which would be type-safe.

When using openapi-generator we used to pass it into Configuration:

import { Configuration } from "../api/configuration";

export const configuration: Configuration = new Configuration({ basePath: " " });

  configuration.baseOptions = {
    headers: {
      Authorization: "Bearer " + myToken,
    },
  };

Later that configuration was used like this:

const appApi = new AppApi(configuration)

dipsaus9 commented 1 year ago

We encounter the same issue. For us this doesn't have to be a global option as it is a security schema, so we should add it like a normal parameter to the generated options.

maapteh commented 6 months ago

we have the same, so:

  securitySchemes:
    fooAuth:
      type: apiKey
      name: X-Foo
      in: header
      description: 'jaja'
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

Then on the method:

   /foo/{bar}/snip:
    post:
      tags: ['Foo']
      summary: 'Foo'
      security:
        - bearerAuth: []
          fooAuth: []

Now the bearer is just normal jwt we set it on axios level, but the other option is not possible to be handled over via the hook. At least the option headers: true in the config is not helping me in any way. @melloware did i miss something?

melloware commented 6 months ago

No this is an open enhancement request so @Maxim-Mazurok is stating it currently does not work as expected.

maapteh commented 6 months ago

I hoped maybe some config with the operationId :) because fully from openapi spec would in our case be weird with the simple bearer one :)