Prompt file imported from antai0926/BookingDoctor (
.github/prompts/add-api-endpoint.prompt.md). Copyright stays with the author.
新增 API endpoint
步驟
- 定義 schema(
packages/shared/src/schemas.ts)- 用 zod;命名
XxxInputSchema+export type XxxInput = z.infer<typeof ...>
- 用 zod;命名
- 新增 route handler(
packages/backend/src/routes/<resource>.ts)- 包
asyncHandler - 用
Schema.parse(req.body)驗證 - 取使用者:
req.user!.sub - 錯誤一律用
throw new HttpError(status, code, message)
- 包
- owner check:所有 by-id 操作要驗證 row 屬於
req.user.sub - 測試(
packages/backend/tests/<resource>.test.ts)- 至少:成功 + 401(無 token)+ 404(非 owner)+ validation 錯誤
- 前端整合(如需要):在
packages/frontend/lib/api.ts用api()呼叫
範例:閱讀現有 patients endpoint
packages/backend/src/routes/patients.tspackages/shared/src/schemas.ts中的PatientInputSchema
注意
- 不要在 controller 直接做加解密 / queue 操作 — 抽到
services/ - DB 變更請:
yarn workspace @doctor/backend prisma migrate dev --name <change>