How to generate an Express + TypeScript API from OpenAPI 3.0 specification?
Asked Answered
T

2

15

I wrote specifications for my REST API server using OpenAPI 3.0. Now, instead of manually writing a lot of repetitive code, with all the errors that could be introduced, I'd like to quickly generate an expressjs app. I know I can generate a server app from editor.swagger.io, but the generated code is javascript, so I can't use the typings from my models!

Is there a tool that can generate, from an OpenAPI 3.0 specification, an express app written in typescript? It would be awesome if it could create all folders, controllers and properly use the models (in a standard way!). That would definitely start my project quickly!

Tabes answered 4/8, 2019 at 3:52 Comment(0)
T
9

swagger-node-codegen allows to generate an expressjs server from an OpenAPI yaml or json file.

The following command:

snc schema.yml  -o ./my-api

will generate the skeleton of the REST API, with mocked data if you have examples in your specifications. Then, you can add your business logic.

It has the following features:

  • ES7
  • ESLint
  • YAML config file
  • Express

The only thing is, it doesn't generate code in TypeScript with models. But the models can be integrated easily in the code. Upgrading the code Typescript using "tsc" and renaming all .js files to .ts does the job.

Tabes answered 4/8, 2019 at 18:6 Comment(3)
Combine with github.com/horiuchi/dtsgenerator to also generate Typescript models.Screed
Thanks Aleksi. This was the missing piece!Tabes
swagger-node-codegen is no longer maintainedIngrain
W
10

Could have a look at the following 3 places:

  • express-openapi-validator: Recommended tool. Although the name indicates it's just a validator. It actually can handle the routing work. Just implement the plain handler functions and EOV will take care of request validation and request routing according to the OAS you give;
  • OpenAPI Generator: widely used openapi codegen tools with custom generators to support various languages and frameworks. For Node.js, there is a nodejs-exporess-server, but it's marked beta. Actually it's based on express-openapi-validator.
  • openapi.tools: a collection of numerous OpenAPI tools to try, including quite a few server codegen tools;

express-openapi-validator is widely used and actively maintained so I think the quality is solid. The quality of the tools in the latter 2 places remain unknown.

Waiwaif answered 1/7, 2022 at 8:51 Comment(1)
this should be the accepted answer at this point as these tools are still available and swagger-node-codegen isn'tIngrain
T
9

swagger-node-codegen allows to generate an expressjs server from an OpenAPI yaml or json file.

The following command:

snc schema.yml  -o ./my-api

will generate the skeleton of the REST API, with mocked data if you have examples in your specifications. Then, you can add your business logic.

It has the following features:

  • ES7
  • ESLint
  • YAML config file
  • Express

The only thing is, it doesn't generate code in TypeScript with models. But the models can be integrated easily in the code. Upgrading the code Typescript using "tsc" and renaming all .js files to .ts does the job.

Tabes answered 4/8, 2019 at 18:6 Comment(3)
Combine with github.com/horiuchi/dtsgenerator to also generate Typescript models.Screed
Thanks Aleksi. This was the missing piece!Tabes
swagger-node-codegen is no longer maintainedIngrain

© 2022 - 2024 — McMap. All rights reserved.