본문으로 바로가기

OpenAPI + Typescript

category software engineeringdevops 2년 전
728x90
 

OpenAPI (Swagger) Editor - Visual Studio Marketplace

Extension for Visual Studio Code - OpenAPI extension for Visual Studio Code

marketplace.visualstudio.com

# gen.sh
npx openapi-typescript spec.yml --output ./index.ts

 

GitHub - OpenAPITools/openapi-generator-cli: A node package wrapper for https://github.com/OpenAPITools/openapi-generator

A node package wrapper for https://github.com/OpenAPITools/openapi-generator - GitHub - OpenAPITools/openapi-generator-cli: A node package wrapper for https://github.com/OpenAPITools/openapi-generator

github.com

openapi: '3.0.2'
info:
  title: 
  version: '1.0'

servers:
  - url: http://localhost:3000/v1

tags:
  - name: users

paths:
  /users:
    $ref: "./resources/users.yaml"
  /users/{userId}:
    $ref: "./resources/user.yaml"
  /health:
    get:
      operationId: checkHealth
      responses:
        '200':
          description: OK
        '404':
          description: NOT FOUND

refs를 이용해서 분리하고 open-generator-cli를 통해 합친다.

# resources/user.yaml 
get:
  summary: Detail
  operationId: getUser
  description: Info for a specific user
  tags:
    - users
  parameters:
    - $ref: "../parameters/path/userId.yaml"
  responses:
    '200':
      description: Expected response to a valid request
      content:
        application/json:
          schema:
            $ref: '../schemas/User.yaml'
    default:
      $ref: '../responses/UnexpectedError.yaml'
# gen.sh
docker run --rm -v "${PWD}:/output" openapitools/openapi-generator-cli generate \
    -i output/swagger.yaml \
    -g openapi-yaml \
    -o /output/gen

npx openapi-typescript gen/openapi/openapi.yaml --output ./gen/index.ts
  • gen.sh에서 open-typescript 하기 전 openapi.yaml 만들어주는 command 추가
  • 우선 폴더구조를 어떻게 가져갈지 크게 고민하지 않고 대표 예제 따라서 생성

https://github.com/2hyeong/Empathy/tree/main/packages/idl

 

GitHub - 2hyeong/Empathy: A platform to emphathize with the personality of the people around you.

A platform to emphathize with the personality of the people around you. - GitHub - 2hyeong/Empathy: A platform to emphathize with the personality of the people around you.

github.com

software engineeringdevops카테고리의 다른글

pnpm link  (1) 2023.10.29
TRPC, GraphQL, Restful API  (0) 2023.01.03
Jenkins, GitHub Actions  (0) 2022.08.31
Lambda cold start  (0) 2022.08.29
AWS Amplify  (0) 2022.08.29