swagger 如何使用Typescript和Node生成REST API文档?

monwx1rj  于 2024-01-08  发布在  TypeScript
关注(0)|答案(2)|浏览(363)

我可以使用https://github.com/TypeStrong/typedoc创建类似https://apidocjs.com/的REST API文档吗?
欢迎提出任何关于如何重用TypeScript类型来生成REST API文档的建议(使用Next.js)

ff29svar

ff29svar1#

如果您实际上想要的是用TypeScript描述您的API,并从中产生Swagger/OpenAPI定义,请尝试https://github.com/airtasker/spot
IT不仅可以生成REST API文档,还可以让您运行一个模拟服务器,其中包含符合REST API定义的随机数据(用于测试客户端)和一个数据模型验证器(用于测试服务器)。
来自项目README的示例:

  1. import { api, endpoint, request, response, body } from "@airtasker/spot";
  2. @api({
  3. name: "My API"
  4. })
  5. class Api {}
  6. @endpoint({
  7. method: "POST",
  8. path: "/users"
  9. })
  10. class CreateUser {
  11. @request
  12. request(@body body: CreateUserRequest) {}
  13. @response({ status: 201 })
  14. response(@body body: CreateUserResponse) {}
  15. }
  16. interface CreateUserRequest {
  17. firstName: string;
  18. lastName: string;
  19. }
  20. interface CreateUserResponse {
  21. firstName: string;
  22. lastName: string;
  23. role: string;
  24. }

字符串

展开查看全部
yiytaume

yiytaume2#

有没有看过npm的apidoc
它根据代码注解生成API文档:

  1. /**
  2. * @api {get} /user/:id Request User information
  3. * @apiName GetUser
  4. * @apiGroup User
  5. *
  6. * @apiParam {Number} id User's unique ID.
  7. *
  8. * @apiSuccess {String} firstname Firstname of the User.
  9. * @apiSuccess {String} lastname Lastname of the User.
  10. */

字符串
还有Gulp、Grunt、Eclipse、Sublime Text、Docmaster、Markdown、Swagger等配套工具/转换器(*cf.*apidoc GitHub README.md)

相关问题