Upgrade to Pro — share decks privately, control downloads, hide ads and more …

OpenAPI と型 | Mita.ts #1

OpenAPI と型 | Mita.ts #1

Kobayashi Kazuhiro

August 28, 2024
Tweet

More Decks by Kobayashi Kazuhiro

Other Decks in Programming

Transcript

  1. Example https://github.com/kzhrk/mita.ts-1/blob/main/spec.yml openapi: "3.0.0" info: version: 1.0.0 title: Sample paths:

    /items: get: summary: アイテム一蘭取得 operationId: getItems tags: - items parameters: - name: page in: query required: false schema: type: integer maximum: 100 format: int32 responses: '200':
  2. OpenAPI generator Docker npm cf. Generators List https://openapi-generator.tech/docs/installation/ docker run

    -t --rm \ -v ${CURDIR}:/local openapitools/openapi-generator-cli generate \ -i local/spec.yml \ -g typescript-axios \ -o local/sample/openapi-generator npx @openapitools/openapi-generator-cli generate \ -i ./spec.yml \ -g typescript-axios \ -o ./sample/openapi-generator
  3. 出力した型を読み込む OpenAPI のパスと operationId の巨大な interface が提供される。 import type {

    paths, operations } from './sample/openapi-typescript/schema'; type Item = paths['/items/{itemId}']['get']['responses']['200']['content']['application/json']; type Item2 = operations['getItem']['responses']['200']['content']['application/json']; const item: Item = { id: 1, name: '', }; const item2: Item2 = { id: 1, name: '', };
  4. まとめ OpenAPI generators generator から言語が選べる 抽象化した型を提供する OpenAPI TypeScript paths, operations

    の巨大な interface を提供する openapi-fetch でテストのモックなども提供する