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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joshAg
Copy link

@joshAg joshAg commented Jun 24, 2022

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.

  • I only have 1 commit.
  • 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.
  • I'm using the latest code from the master branch and there are no conflicts on my branch.
  • 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.

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;
```
@joshAg joshAg changed the title extend expressJS Request Handler Parameters through express-openapi express-openapi: extend expressJS Request Handler Parameters through express-openapi Jun 24, 2022
@jsdevel
Copy link
Contributor

jsdevel commented Jun 25, 2022

@joshAg please get the build to pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants