Closed MofuMofu2 closed 5 months ago
まずは出力まで漕ぎ着ける。GPT-4に出してもらったものを使ってみよう。
```yaml openapi: 3.0.0 info: title: Simple Pet Store API description: This is a simple Pet Store Server. version: 1.0.0 servers: - url: http://petstore.example.com/api paths: /pets: get: summary: List all pets operationId: listPets tags: - pets responses: '200': description: A paged array of pets content: application/json: schema: $ref: '#/components/schemas/Pets' 'default': description: unexpected error content: application/json: schema: $ref: '#/components/schemas/Error' post: summary: Create a pet operationId: createPets tags: - pets requestBody: description: Pet to add to the store required: true content: application/json: schema: $ref: '#/components/schemas/NewPet' responses: '201': description: Null response 'default': description: unexpected error content: application/json: schema: $ref: '#/components/schemas/Error' components: schemas: Pets: type: array items: $ref: '#/components/schemas/Pet' Pet: required: - id - name properties: id: type: integer format: int64 name: type: string tag: type: string nullable: true NewPet: required: - name properties: name: type: string tag: type: string nullable: true Error: required: - code - message properties: code: type: integer format: int32 message: type: string ```
ほんとにできるのかなあ。
# Dockerイメージのビルド
docker build -t api-client-generator .
# コンテナの実行とコードの生成
# 生成されたコードはコンテナ内の/app/outputに配置されますが、
# これをホストマシンの任意のディレクトリにマッピングすることで取り出すことができます。
docker run --rm -v $(pwd)/output:/app/output api-client-generator
カスタムのDockerfileは使わないらしい 🤔
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
-i /local/openapi.yaml \
-g typescript-fetch \
-o /local/generated
こうなる
➜ api git:(97-generate-openapi) ✗ pwd
/Users/mofumofu/git/react-sandbox/src/api
➜ api git:(97-generate-openapi) ✗ ls -al
total 8
drwxr-xr-x 4 mofumofu staff 128 Apr 8 17:26 .
drwxr-xr-x@ 19 mofumofu staff 608 Apr 8 16:51 ..
drwxr-xr-x 8 mofumofu staff 256 Apr 8 17:26 generated
-rw-r--r-- 1 mofumofu staff 1880 Apr 8 16:52 openapi.yaml
➜ api git:(97-generate-openapi) ✗ ls -al generated
total 48
drwxr-xr-x 8 mofumofu staff 256 Apr 8 17:26 .
drwxr-xr-x 4 mofumofu staff 128 Apr 8 17:26 ..
drwxr-xr-x 4 mofumofu staff 128 Apr 8 17:26 .openapi-generator
-rw-r--r-- 1 mofumofu staff 1040 Apr 8 17:26 .openapi-generator-ignore
drwxr-xr-x 4 mofumofu staff 128 Apr 8 17:26 apis
-rw-r--r-- 1 mofumofu staff 131 Apr 8 17:26 index.ts
drwxr-xr-x 6 mofumofu staff 192 Apr 8 17:26 models
-rw-r--r-- 1 mofumofu staff 14028 Apr 8 17:26 runtime.ts
凝ったことをするとすぐハマるので、自分でもやってみようと思った。