问题
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