File
Blob: src/worker/routes/api/openapi.ts
| 1 | import { describeRoute, resolver } from "hono-openapi"; |
| 2 | import type { ZodTypeAny } from "zod"; |
| 3 | |
| 4 | import { ErrorResponseSchema } from "@/worker/openapi/schemas"; |
| 5 | |
| 6 | export const jsonErrorResponse = { |
| 7 | description: "JSON error response", |
| 8 | content: { |
| 9 | "application/json": { |
| 10 | schema: resolver(ErrorResponseSchema), |
| 11 | }, |
| 12 | }, |
| 13 | }; |
| 14 | |
| 15 | export function describeJson( |
| 16 | summary: string, |
| 17 | tag: string, |
| 18 | schema: ZodTypeAny, |
| 19 | status = 200, |
| 20 | errorStatuses: readonly (400 | 401 | 403 | 404 | 405 | 409 | 413 | 423 | 500)[] = [400, 401, 403, 500], |
| 21 | ) { |
| 22 | return describeRoute({ |
| 23 | summary, |
| 24 | tags: [tag], |
| 25 | responses: { |
| 26 | [status]: { |
| 27 | description: summary, |
| 28 | content: { |
| 29 | "application/json": { |
| 30 | schema: resolver(schema), |
| 31 | }, |
| 32 | }, |
| 33 | }, |
| 34 | ...Object.fromEntries(errorStatuses.map((errorStatus) => [errorStatus, jsonErrorResponse])), |
| 35 | }, |
| 36 | }); |
| 37 | } |