Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 611
JLavoie Avatar asked Aug 04 '19 03:08

JLavoie


1 Answers

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.

like image 198
JLavoie Avatar answered Oct 20 '22 17:10

JLavoie