MofuMofu2 / react-sandbox

React.js Sandbox
1 stars 0 forks source link

OpenAPIでAPI定義を生成して組んでみる #97

Closed MofuMofu2 closed 5 months ago

MofuMofu2 commented 8 months ago

凝ったことをするとすぐハマるので、自分でもやってみようと思った。

MofuMofu2 commented 5 months ago

トップページはプロジェクト紹介って感じだ。

MofuMofu2 commented 5 months ago

まずは出力まで漕ぎ着ける。GPT-4に出してもらったものを使ってみよう。

openapi

```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 ```

MofuMofu2 commented 5 months ago

ほんとにできるのかなあ。

# Dockerイメージのビルド
docker build -t api-client-generator .

# コンテナの実行とコードの生成
# 生成されたコードはコンテナ内の/app/outputに配置されますが、
# これをホストマシンの任意のディレクトリにマッピングすることで取り出すことができます。
docker run --rm -v $(pwd)/output:/app/output api-client-generator
MofuMofu2 commented 5 months ago

カスタムの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