首页 > 解决方案 > 如何使用 Typescript 和 Node 生成 REST API 文档?

问题描述

我可以使用https://github.com/TypeStrong/typedoc创建像https://apidocjs.com/这样的 REST API 文档吗?

欢迎任何关于如何重用 TypeScript 类型来生成 REST API 文档的建议(使用 Next.js)

标签: typescriptswaggernext.jsapi-doctypedoc

解决方案


如果您真正想要的是在 TypeScript 中描述您的 API 并从中得出 Swagger/OpenAPI 定义,请尝试https://github.com/airtasker/spot

IT 不仅会生成 REST API 文档,还会让您运行一个模拟服务器,其中包含符合 REST API 定义(用于测试客户端)的随机数据和一个数据模型验证器(用于测试服务器)。

项目自述文件中的示例:

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;
}

推荐阅读