open-api icon indicating copy to clipboard operation
open-api copied to clipboard

express-openapi: extend expressJS Request Handler Parameters through express-openapi

Open joshAg opened this issue 2 years ago • 1 comments

The added generic types for RequestHandler come from expressJS and allow the user to give types to various parts of the request and response object in a way that allows typescript to infer types. Currently if the user wants typescript to infer one of these parts has a certain type they have to explicitly case the object as such. This will remove the need to cast at all. This change is necessary because with the current code the user cannot explicitly type req or res because the compiler will complain about assigning a function where they have been more defined to the Operation type since Operation is missing those specific type definitions.

For example

// normally generated from the openAPI definition
interface myExampleDictionary extends core.ParamsDictionary {
  key: string
}
const GET: Operation =  async (req: express.Request<myExampleDictionary>, res: express.Response, next: express.NextFunction) => {

Instead we currently have to do something like

// normally generated from the openAPI definition
interface myExampleDictionary extends core.ParamsDictionary {
  key: string
}
const GET: Operation =  async (req, res, next) => {
  const params = req.params as myExampleDictionary;

Note: This checklist isn't meant to show up on the actual Pull Request (PR). It is added here to make you aware of items our maintainers will look for when reviewing your PR. If your PR is missing any of these items it will be rejected! Please delete this message and the following checklist and replace it with your own message as you see fit.

  • [x] I only have 1 commit.
  • [x] My commit is prefixed with the relevant package (e.g. express-openapi: fixing something) Note: You can use the bin/commit script to automate this.
  • [ ] I have added tests.
  • [x] I'm using the latest code from the master branch and there are no conflicts on my branch.
  • [x] I have added a suffix to my commit message that will close any existing issue my PR addresses (e.g. openapi-jsonschema-parameters: Adding examples to the validation keywords (fixes #455)).
  • [ ] My tests pass locally.
  • [ ] I have run linting against my code.

joshAg avatar Jun 24 '22 02:06 joshAg

@joshAg please get the build to pass

jsdevel avatar Jun 25 '22 02:06 jsdevel