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

Add express-openapi types #64

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

Conversation

erikjuhani
Copy link

Hey @wesleytodd! 👋

I noticed that you had an open issue #25 for adding typescript types to this library. Fortunately there was already a base to start with, which I used here.

I've tested the types so hopefully they are 1 to 1 match with the runtime data. However there could be some things I missed.

Let me know if you want any changes or clarification!


This commit includes typing all the public facing signatures. The types also include API documentation in JSDoc format that is available in the root README of the repository. Most of the types were deduced by hand from the source code and tested with actual runtime data to make sure that the written types match with the runtime data.

The component methods use function overloading to provide better code completion of the expected parameters and return values. For example giving a type and a name to component("examples", "FooExample") returns the expected type of ReferenceObject conforming to the API documentation.

Similarly setting the type for the component function affects what it will return or take as an input. The input parameter and return type will conform to the expected type. For example:

const schemas = openapi.component("schemas");
//    ^? { [key: string]: SchemaObject | ReferenceObject }

const examples = openapi.component("examples");
//    ^? { [key: string]: ExampleObject | ReferenceObject }

The types utilize existing types defined in "openapi-types" and most of the public facing API functions rely on these types.

The initial typings file was derived from the work presented here: #25 (comment)

Copy link

socket-security bot commented Sep 6, 2024

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@types/[email protected] None +11 2.4 MB types
npm/@types/[email protected] None 0 16.1 kB types

View full report↗︎

@wesleytodd
Copy link
Owner

Hey, really sorry I left this hanging for so long. We were busy doing the big express release and then I needed to take a bit of time to try and recover. I will get to this soon, but wanted to drop a note in advance to thank you very much for this.

@erikjuhani
Copy link
Author

Hey, really sorry I left this hanging for so long. We were busy doing the big express release and then I needed to take a bit of time to try and recover. I will get to this soon, but wanted to drop a note in advance to thank you very much for this.

No worries at all! Take your time, recover and don't take too much pressure from this PR! 🙇 Thanks for letting me know! (BTW, we already upgraded one node project using express 4.x to 5.x — works like a charm!)

Also I realised that there's a slight mishap in this PR. I think the new dependencies should be defined under dependencies and not under devDependencies as these are consumer facing types. I'll make a quick change to that.

This commit includes typing all the public facing signatures. The types
also include API documentation in JSDoc format that is available in the
root README of the repository. Most of the types were deduced by hand
from the source code and tested with actual runtime data to make sure
that the written types match with the runtime data.

The component methods use function overloading to provide better code
completion of the expected parameters and return values. For example
giving a type and a name to `component("examples", "FooExample")`
returns the expected type of `ReferenceObject` conforming to the API
documentation.

Similarly setting the type for the component function affects what it
will return or take as an input. The input parameter and return type
will conform to the expected type. For example:

```ts
const schemas = openapi.component("schemas");
//    ^? { [key: string]: SchemaObject | ReferenceObject }

const examples = openapi.component("examples");
//    ^? { [key: string]: ExampleObject | ReferenceObject }
```

The types utilize existing types defined in "openapi-types" and most of
the public facing API functions rely on these types.

The initial typings file was derived from the work presented here: wesleytodd#25 (comment)
@erikjuhani
Copy link
Author

Also I realised that there's a slight mishap in this PR. I think the new dependencies should be defined under dependencies and not under devDependencies as these are consumer facing types. I'll make a quick change to that.

Changed the type dependencies to be under dependencies.

@erikjuhani
Copy link
Author

Tests are breaking, but it's unrelated to my package.json change and I believe it's caused by the new minor version releases in express.

@henry232323
Copy link

I'm getting the following error when attempting to run this on Express v5. This seems to be a regression from v4.

TypeError: Cannot read properties of undefined (reading 'fast_slash')
    at split (/path/to/project/node_modules/@wesleytodd/openapi/lib/generate-doc.js:127:20)
    at /path/to/project/node_modules/@wesleytodd/openapi/lib/generate-doc.js:75:27
    at Array.forEach (<anonymous>)
    at iterateStack (/path/to/project/node_modules/@wesleytodd/openapi/lib/generate-doc.js:73:24)
    at /path/to/project/node_modules/@wesleytodd/openapi/lib/generate-doc.js:17:5
    at Array.forEach (<anonymous>)
    at generateDocument (/path/to/project/node_modules/@wesleytodd/openapi/lib/generate-doc.js:16:26)
    at OpenApiMiddleware (/path/to/project/node_modules/@wesleytodd/openapi/index.js:42:29)
    at Layer.handleRequest (/path/to/project/node_modules/express/node_modules/router/lib/layer.js:145:17)
    at trimPrefix (/path/to/project/node_modules/express/node_modules/router/index.js:337:13)

I can share more code if needed.

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.

3 participants