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