Skip to content

生成文档

在本页中,我们将学习如何使用Tspec生成OpenAPI规范并通过Swagger UI提供服务。

CLI使用方法

generate

从TypeScript类型生成OpenAPI规范。

使用方法

bash
yarn tspec generate [options]
bash
npx tspec generate [options]
bash
pnpx tspec generate [options]
选项
选项类型描述示例
--specPathGlobs [path]string[]要生成OpenAPI模式的TypeScript文件路径src/**/*.ts
--tsconfigPath [path]stringtsconfig路径./tsconfig.json
--configPath [path]stringTspec配置文件路径./tspec.config.json
--outputPath [path]string输出OpenAPI模式路径./generate/openapi.yaml
--specVersion [version]numberOpenAPI模式版本(目前仅支持3)3
--openapiTitle [title]stringOpenAPI模式的title属性Tspec
--openapiVersion [version]stringOpenAPI模式的version属性1.0.0
--openapiDescription [description]stringOpenAPI模式的description属性This is Tspec API
--debug [true / false]boolean打印Tspec调试信息false
--ignoreErrors [true / false]boolean是否忽略Tspec错误false
--nestjs [true / false]boolean从NestJS控制器生成(使用specPathGlobs作为控制器文件)false

server

启动Tspec服务器,使用Swagger UI显示OpenAPI规范。

使用方法

bash
yarn tspec server [options]
bash
npx tspec server [options]
bash
pnpx tspec server [options]
选项

您也可以使用generate命令的CLI选项。

选项类型描述示例
--port [port]number指定Tspec服务器端口号7080
--proxyHost [host]string指定Tspec服务器代理主机https://tspec.org

配置文件

您也可以为generateserver命令使用配置文件。

在项目根目录创建tspec.config.json

json
{
  "specPathGlobs": ["src/**/*.ts"],
  "tsconfigPath": "./tsconfig.json",
  "outputPath": "./generate/openapi.json",
  "specVersion": 3,
  "openapi": {
    "title": "Tspec API",
    "version": "0.0.1",
    "description": "This is Tspec API",
    "securityDefinitions": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "JWT"
      }
    },
    "servers": [
      {
        "url": "https://api.example.com",
        "description": "Production server"
      }
    ]
  },
  "debug": false,
  "ignoreErrors": true,
  "nestjs": false
}

配置文件的类型是Tspec.GenerateParams

编程式使用

generate

ts
import { generateTspec, Tspec } from 'tspec';

const options: Tspec.GenerateParams = {
  specPathGlobs: ['src/**/*.ts'],
  tsconfigPath: './tsconfig.json',
  configPath: './tspec.config.json',
  outputPath: './generate/openapi.json',
  specVersion: 3,
  openapi: {
    title: 'Tspec API',
    version: '0.0.1',
    description: 'This is Tspec API',
    securityDefinitions: {
      bearerAuth: {
        type: 'http',
        scheme: 'bearer',
        bearerFormat: 'JWT',
      },
    },
    servers: [
      {
        url: 'https://api.example.com',
        description: 'Production server',
      },
    ],
  },
  debug: false,
  ignoreErrors: true,
  nestjs: false, // NestJS模式设置为true(使用specPathGlobs作为控制器文件)
};

const openApiSpec = await generateTspec(options);

server

ts
import { initTspecServer, Tspec } from 'tspec';

const options: Tspec.InitTspecServerOptions = {
  specPathGlobs: ['src/**/*.ts'],
  tsconfigPath: './tsconfig.json',
  configPath: './tspec.config.json',
  outputPath: './generate/openapi.json',
  specVersion: 3,
  openapi: {
    title: 'Tspec API',
    version: '0.0.1',
    description: 'This is Tspec API',
    securityDefinitions: {
      bearerAuth: {
        type: 'http',
        scheme: 'bearer',
        bearerFormat: 'JWT',
      },
    },
    servers: [
      {
        url: 'https://api.example.com',
        description: 'Production server',
      },
    ],
  },
  debug: false,
  ignoreErrors: true,
  port: 3000,
  proxyHost: 'https://tspec.org',
};

initTspecServer(options);