简体   繁体   English

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

[英]How to Generate REST API documentation with Typescript and Node?

Can I use https://github.com/TypeStrong/typedoc to create REST API docs like https://apidocjs.com/ ?我可以使用https://github.com/TypeStrong/typedoc来创建 REST API 文档,例如https://apidocjs.com/吗?

Any suggestions on how to reuse TypeScript types to generate REST API docs are welcome (Using Next.js)欢迎任何关于如何重用 TypeScript 类型生成 REST API 文档的建议(使用 Next.js)

Have you checked the npm package apidoc ?您检查过 npm package apidoc 了吗?

It generates API documentation based on code comments:它根据代码注释生成 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.
 */

And there are companion tools / converters for Gulp, Grunt, Eclipse, Sublime Text, Docmaster, Markdown, Swagger... ( cf. apidoc GitHub README.md )还有 Gulp、Grunt、Eclipse、Sublime Text、Docmaster、Markdown、Swagger 的配套工具/转换器...(参见apidoc GitHub README.md

If what you actually want is to describe your API in TypeScript and have a Swagger/OpenAPI definition come out of it, try https://github.com/airtasker/spot如果您真正想要的是在 TypeScript 中描述您的 API 并从中得出 Swagger/OpenAPI 定义,请尝试 httpsgithubtasker/ spoter://.com/

IT will not only generate REST API documentation, it will also let you run a mock server with random data that fits the REST API definition (for testing clients) and a data model validator (for testing servers). IT will not only generate REST API documentation, it will also let you run a mock server with random data that fits the REST API definition (for testing clients) and a data model validator (for testing servers).

Example from the project 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM