Skip to content

[OC-79] ✨ feat: APIからのレスポンスの型信用度を向上#30

Open
YutaK1026 wants to merge 4 commits into
mainfrom
OC-79
Open

[OC-79] ✨ feat: APIからのレスポンスの型信用度を向上#30
YutaK1026 wants to merge 4 commits into
mainfrom
OC-79

Conversation

@YutaK1026
Copy link
Copy Markdown
Contributor

@YutaK1026 YutaK1026 commented Oct 26, 2025

タイトル

概要

  • Zodを用いて型定義・型検証を行うようにしました.
  • 既存のFE側の型定義ファイルを削除し,Zodから型をimportするようにしました.
  • APIからのレスポンスの型をバリデーションし,検証できるようにしました.
  • エラーハンドリングを追加しました.

変更内容

スクリーンショット

備考

型をチェックするようになったら動かなくなります.
動きません.jsonを直そう...

@notion-workspace
Copy link
Copy Markdown

APIの型定義

@vercel
Copy link
Copy Markdown

vercel Bot commented Oct 26, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
ori-cube Ready Ready Preview Comment Oct 26, 2025 3:48am

Copy link
Copy Markdown
Contributor

@yutteee yutteee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commented

API呼び出し側のコードがどうなるかが気になりました!
api呼び出すたびにfe側でzod.parseする必要があるのかなぁというのが気になってます

Comment thread src/app/api/data/route.ts
Comment on lines +31 to +34
return new Response(JSON.stringify(viewModel), {
headers: corsHeaders,
status: 200,
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝
json型でいい
NextResponse.jsonが正しいかも

https://nextjs.org/docs/app/guides/backend-for-frontend

import { extendZodWithOpenApi } from "@asteasolutions/zod-to-openapi";

// ZodをOpenAPI対応に拡張
extendZodWithOpenApi(z);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q.
openAPIは結局生成する?

Comment thread src/server/db/guards.ts
Comment on lines +43 to +76
/**
* ZodErrorを適切なHTTPエラーレスポンスに変換する(詳細版)
* @param error - ZodError
* @returns エラーメッセージと詳細情報
*/
export function formatZodErrorDetailed(error: ZodError) {
return {
message: "データの形式が正しくありません",
details:
error.issues?.map((err) => ({
path: err.path.join("."),
message: err.message,
code: err.code,
received: err.input, // 実際に受信した値
})) || [],
};
}

/**
* ZodErrorを適切なHTTPエラーレスポンスに変換する
* @param error - ZodError
* @returns エラーメッセージと詳細情報
*/
export function formatZodError(error: ZodError) {
return {
message: "データの形式が正しくありません",
details:
error.issues?.map((err) => ({
path: err.path.join("."),
message: err.message,
code: err.code,
})) || [],
};
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q.
これらはどう使い分ける想定?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants