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]booleanTspecのデバッグ情報を出力false
--ignoreErrors [true / false]booleanTspecのエラーを無視するかどうかfalse
--nestjs [true / false]booleanNestJSコントローラーから生成(specPathGlobsをコントローラーファイルに使用)false

server

Swagger UIでOpenAPI仕様を表示するTspecサーバーを起動します。

使用法

bash
yarn tspec server [options]
bash
npx tspec server [options]
bash
pnpx tspec server [options]
オプション

generateコマンドのCLIオプションも使用できます。

オプション説明
--port [port]numberTspecサーバーのポート番号を指定7080
--proxyHost [host]stringTspecサーバーのプロキシホストを指定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);