Generating swagger docs from typescript interfaces

喜你入骨 提交于 2019-12-08 10:44:08

问题


I'm using swager-jsdoc to document all the DTOs of the app.

I was wondering is there any way to generate the swagger docs automatically from typescript interfaces.

I have a lot of them in the project and also a lot of mongoose schemas and models. It is getting tedious to keep them in sync. On the other hand I do not want to use the swagger generation tools. I prefer the bottom-up approach.

Cheers


回答1:


Yes, you can easily generate Swagger and OpenAPI documents from your TypeScript types by using tsoa. The readme contains all of the setup information that you would need to start using it. It's compatible with express, hapi, koa, and more:

https://github.com/lukeautry/tsoa


(Full Transparency: I am one of the maintainers of tsoa. But I was first a consumer of tsoa and I find it to be a great product... that's why I asked to help maintain it! :) )




回答2:


Another option to tsoa is routing-controllers + routing-controllers-openapi. The main difference between the two (AFAIK) is that tsoa relies on code generation whereas routing-controllers operates wholly on runtime. Both methods have their upsides: tsoa is able to e.g. utilize richer metadata (such as code comments) whereas with routing-controllers we can skip the generation step. My recommendation is to check both out!

One more option is https://github.com/YousefED/typescript-json-schema, which generates JSONS chema out of Typescript interfaces; after you've got your models defined in JSON Schema you're not terribly far off from an OpenAPI spec.



来源:https://stackoverflow.com/questions/53570605/generating-swagger-docs-from-typescript-interfaces

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