🔥

@hono/zod-openapiを使ったZodスキーマを起点とした開発フロー

に公開

@hono/zod-openapiを使ってスキーマ駆動開発手法Zodスキーマを起点とした以下のような開発フローを試してみました。
(最初スキーマ駆動開発手法としていましたが、実際はAPI実装後にOpenAPIスキーマを生成しており、適切ではなかったためタイトルを修正しました。)

プロジェクトの全体像

pnpm のワークスペースを利用したモノレポ構成となっています。

.
├── apps
│   ├── api
│   │   ├── drizzle
│   │   │   └── migrations
│   │   ├── drizzle.config.ts
│   │   ├── package.json
│   │   ├── scripts
│   │   │   └── generate-openapi.ts
│   │   ├── src
│   │   │   ├── db
│   │   │   │   └── schemas
│   │   │   │       └── pets.ts
│   │   │   └── index.ts
│   │   └── wrangler.jsonc
│   ├── front
│   │   ├── index.html
│   │   ├── orval.config.ts
│   │   ├── package.json
│   │   ├── src
│   │   │   ├── api
│   │   │   │   └── custom-instance.ts
│   │   │   ├── App.css
│   │   │   ├── App.tsx
│   │   │   ├── generated
│   │   │   │   ├── models
│   │   │   │   ├── pets
│   │   │   │   ├── swaggerPetstore.msw.ts
│   │   │   │   └── swaggerPetstore.ts
│   │   │   ├── index.css
│   │   │   └── main.tsx
│   │   └── vite.config.ts
│   └── openapi.yaml
├── package.json
├── pnpm-lock.yaml
└── pnpm-workspace.yaml

開発の流れ

  1. バックエンドで Zod スキーマを定義
  2. Hono で API 実装
  3. OpenAPI スキーマ生成
  4. フロントエンドで Orval による型や Tanstack Query のカスタムフック等を生成
  5. 4.で生成された型や Tanstack Query のカスタムフックを利用して実装

API 実装

Zod スキーマ定義

Orval の sample にある OpenAPI スキーマのpetstore.yamlを参考に Zod スキーマを定義。

https://github.com/orval-labs/orval/blob/master/samples/hono/hono-with-fetch-client/petstore.yaml

import { z } from '@hono/zod-openapi';

const PetSchema = z
  .object({
    id: z.number().int().openapi({ format: 'int64' }),
    name: z
      .string()
      .min(1, 'Name is required')
      .max(40)
      .openapi({ description: 'Name of the pet' }),
    tag: z
      .string()
      .min(1, 'Tag is required')
      .max(20)
      .openapi({ description: 'Type of the pet' }),
  })
  .openapi('Pet');

const PetsSchema = z.array(PetSchema).openapi('Pets');

const ErrorSchema = z
  .object({
    code: z.number().int().openapi({ example: 500, format: 'int32' }),
    message: z.string().openapi({ example: 'Internal Server Error' }),
  })
  .openapi('Error');

Zod のメソッドが OpenAPI スキーマの properties の型や制約に変換される。
例えば、min(), max() は OpenAPI スキーマ生成により minimum, maximumで出力される。
exclusiveMinimum,exclusiveMaximum で表現したい場合は gt(),lt() を使う。

Hono で API 実装

apps/api/src/index.ts
import type { D1Database } from '@cloudflare/workers-types';
import { z, createRoute, OpenAPIHono } from '@hono/zod-openapi';
import { swaggerUI } from '@hono/swagger-ui';
import { drizzle } from 'drizzle-orm/d1';
import { eq } from 'drizzle-orm';
import { pets } from './db/schemas/pets';
import { cors } from 'hono/cors';

type Bindings = {
  DB: D1Database;
};

// Zod schema定義は省略

const app = new OpenAPIHono<{ Bindings: Bindings }>().basePath('/api');

app.use('/api/*', cors())

// GET /pets - List all pets
app.openapi(
  createRoute({
    method: 'get',
    path: '/pets',
    summary: 'List all pets',
    operationId: 'listPets',
    tags: ['pets'],
    request: {
      query: z.object({
        limit: z.string().optional().openapi({
          description: 'How many items to return at one time (max 100)',
        }),
      }),
    },
    responses: {
      200: {
        description: 'A paged array of pets',
        content: {
          'application/json': {
            schema: PetsSchema,
          },
        },
      },
    },
  }),
  async (c) => {
    const db = drizzle(c.env.DB);
    const { limit } = c.req.valid('query');
    const limitNum = limit ? parseInt(limit) : undefined;

    const allPets = await db
      .select()
      .from(pets)
      .limit(limitNum || 100)
      .all();
    return c.json(allPets, 200);
  }
);

// POST /pets - Create a pet
app.openapi(
  createRoute({
    method: 'post',
    path: '/pets',
    summary: 'Create a pet',
    operationId: 'createPet',
    tags: ['pets'],
    request: {
      body: {
        required: true,
        content: {
          'application/json': {
            schema: PetSchema.omit({ id: true }),
          },
        },
      },
    },
    responses: {
      200: {
        description: 'Created Pet',
        content: {
          'application/json': {
            schema: PetSchema,
          },
        },
      },
      default: {
        description: 'unexpected error',
        content: {
          'application/json': {
            schema: ErrorSchema,
          },
        },
      },
    },
  }),
  async (c) => {
    const db = drizzle(c.env.DB);
    const body = c.req.valid('json');

    const result = await db
      .insert(pets)
      .values({
        name: body.name,
        tag: body.tag,
      })
      .returning()
      .get();

    return c.json(result, 200);
  }
);

// PUT /pets - Update a pet
app.openapi(
  createRoute({
    method: 'put',
    path: '/pets',
    summary: 'Update a pet',
    operationId: 'updatePets',
    tags: ['pets'],
    request: {
      body: {
        required: true,
        content: {
          'application/json': {
            schema: PetSchema,
          },
        },
      },
    },
    responses: {
      200: {
        description: 'Created Pet',
        content: {
          'application/json': {
            schema: PetSchema,
          },
        },
      },
      default: {
        description: 'unexpected error',
        content: {
          'application/json': {
            schema: ErrorSchema,
          },
        },
      },
    },
  }),
  async (c) => {
    const db = drizzle(c.env.DB);
    const updatedPet = c.req.valid('json');

    const result = await db
      .update(pets)
      .set({
        name: updatedPet.name,
        tag: updatedPet.tag,
      })
      .where(eq(pets.id, updatedPet.id))
      .returning()
      .get();

    if (!result) {
      return c.json({ code: 404, message: 'Pet not found' }, 404);
    }

    return c.json(result, 200);
  }
);

// GET /pets/{petId} - Info for a specific pet
app.openapi(
  createRoute({
    method: 'get',
    path: '/pets/{petId}',
    summary: 'Info for a specific pet',
    operationId: 'showPetById',
    tags: ['pets'],
    request: {
      params: z.object({
        petId: z.string().openapi({
          description: 'The id of the pet to retrieve',
        }),
      }),
    },
    responses: {
      200: {
        description: 'Expected response to a valid request',
        content: {
          'application/json': {
            schema: PetSchema,
          },
        },
      },
      default: {
        description: 'unexpected error',
        content: {
          'application/json': {
            schema: ErrorSchema,
          },
        },
      },
    },
  }),
  async (c) => {
    const db = drizzle(c.env.DB);
    const { petId } = c.req.valid('param');

    const pet = await db
      .select()
      .from(pets)
      .where(eq(pets.id, parseInt(petId)))
      .get();

    if (!pet) {
      return c.json({ code: 404, message: 'Pet not found' }, 404);
    }

    return c.json(pet, 200);
  }
);

export { app };
export default app;

Swagger UI で OpenAPI 仕様を Web 画面で確認できる。
Orval の v6.9.0 で OpenAPI v3.1 のサポートが追加されているようですが、ドキュメントから読み取れず、一旦 v3 系を使用しています。
v3.1を使用したい場合は、doc31やgetOpenAPI31Documentで定義します。

https://github.com/orval-labs/orval/releases/tag/v6.9.0

apps/api/src/index.ts
app.doc('/doc', {
  openapi: '3.0.0',
  info: {
    version: '1.0.0',
    title: 'Swagger Petstore',
    license: {
      name: 'MIT',
    },
  },
});

app.get('/doc/ui', swaggerUI({ url: '/api/doc' }));

OpenAPI スキーマ生成

スキーマファイル生成用スクリプト

apps/api/scripts/generate-openapi.ts
import { writeFileSync } from 'node:fs';
import { app } from '../src/index.js';
import yaml from 'yaml';

const doc = app.getOpenAPIDocument(
  {
    openapi: '3.0.0',
    info: {
      version: '1.0.0',
      title: 'Swagger Petstore',
      license: { name: 'MIT' },
    },
  },
  {
    unionPreferredType: 'oneOf',
  }
);

// YAML出力
writeFileSync('../openapi.yaml', yaml.stringify(doc));
console.log('✅ openapi.yaml generated');

// JSON出力
// writeFileSync('../openapi.json', JSON.stringify(doc, null, 2));
// console.log('✅ openapi.json generated');
package.json
{
  "scripts": {
    "openapi:generate": "tsx scripts/generate-openapi.ts",
  }
}
pnpm api openapi:generate

> schema-driven-development-otameshi@1.0.0 api /Users/kmkkiii/ghq/github.com/kmkkiii/schema-driven-development-otameshi
> pnpm -F api openapi:generate


> api@ openapi:generate /Users/kmkkiii/ghq/github.com/kmkkiii/schema-driven-development-otameshi/apps/api
> tsx scripts/generate-openapi.ts

✅ openapi.yaml generated

Orval で OpenAPI スキーマから型や Tanstack Query のカスタムフック等を生成

生成する API クライアントの種類や生成ファイルの格納先等を設定。

apps/front/orval.config.ts
import { defineConfig } from 'orval';

export default defineConfig({
  petstoreClient: {
    input: {
      target: '../openapi.yaml',
    },
    output: {
      mode: 'tags-split',
      client: 'react-query',
      target: 'src/generated/',
      schemas: 'src/generated/models',
      mock: true,
      httpClient: 'axios',
      override: {
        mutator: {
          path: './src/api/custom-instance.ts',
          name: 'customInstance',
        },
      },
    },
  },
});
pnpm front orval

> schema-driven-development-otameshi@1.0.0 front /Users/kmkkiii/ghq/github.com/kmkkiii/schema-driven-development-otameshi
> pnpm -F front orval


> front@0.0.0 orval /Users/kmkkiii/ghq/github.com/kmkkiii/schema-driven-development-otameshi/apps/front
> orval

🍻 orval v7.17.0 - A swagger client generator for typescript
🎉 petstoreClient - Your OpenAPI spec has been converted into ready to use orval!

以下のようなファイルが生成される。

.
├── apps
│   ├── front
│   │   ├── src
│   │   │   ├── generated
│   │   │   │   ├── models
│   │   │   │   │   ├── createPetBody.ts
│   │   │   │   │   ├── createPetsBodyItem.ts
│   │   │   │   │   ├── error.ts
│   │   │   │   │   ├── index.ts
│   │   │   │   │   ├── listPetsParams.ts
│   │   │   │   │   ├── pet.ts
│   │   │   │   │   └── pets.ts
│   │   │   │   ├── pets
│   │   │   │   │   ├── pets.msw.ts
│   │   │   │   │   └── pets.ts
│   │   │   │   ├── swaggerPetstore.msw.ts
│   │   │   │   └── swaggerPetstore.ts

生成された Tanstack Query のカスタムフックを使ってフロントエンド実装

Tanstack Query を使う準備。

apps/front/src/main.tsx
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import './index.css';
import App from './App.tsx';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

const queryClient = new QueryClient();

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <QueryClientProvider client={queryClient}>
      <App />
    </QueryClientProvider>
  </StrictMode>
);

カスタムフックを使って実装。

apps/front/src/App.tsx
import './App.css';
import { useListPets } from './generated/pets/pets';

function App() {
  const { data: pets, isLoading, error } = useListPets();

  if (isLoading) {
    return <div>Loading...</div>;
  }

  if (error) {
    return <div>Error: {error.message}</div>;
  }

  return (
    <>
      <h2>Pets</h2>
      <ul>
        {pets?.map((pet) => (
          <li key={pet.id}>{pet.name}</li>
        ))}
      </ul>
    </>
  );
}

export default App;

おわり

Zod を使うことでスキーマと一緒にバリデーションも定義できること、Orval による 型や Tanstack Query のカスタムフック等の生成で実装の共通化を行えることで快適な開発を体験できました。

最近は TypeSpecやoRPC も気になっているのでそちらも触ってみたいと思っています。

ここまで読んでいただきありがとうございました!

GitHubで編集を提案

Discussion