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

monwx1rj  于 5个月前  发布在  TypeScript
关注(0)|答案(2)|浏览(87)

我可以使用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的示例:

import { api, endpoint, request, response, body } from "@airtasker/spot";

@api({
  name: "My API"
})
class Api {}

@endpoint({
  method: "POST",
  path: "/users"
})
class CreateUser {
  @request
  request(@body body: CreateUserRequest) {}

  @response({ status: 201 })
  response(@body body: CreateUserResponse) {}
}

interface CreateUserRequest {
  firstName: string;
  lastName: string;
}

interface CreateUserResponse {
  firstName: string;
  lastName: string;
  role: string;
}

字符串

yiytaume

yiytaume2#

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

/**
 * @api {get} /user/:id Request User information
 * @apiName GetUser
 * @apiGroup User
 *
 * @apiParam {Number} id User's unique ID.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */

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

相关问题