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

走远了吗. 提交于 2019-12-24 07:17:11

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/57343654/how-to-generate-an-express-typescript-api-from-openapi-3-0-specification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!