fix: OpenAPI spec not generating properly

Summary:

Test Plan:
This commit is contained in:
2024-05-24 14:56:34 -04:00
parent 88b1a4ced5
commit 62e780e8bf
4 changed files with 28 additions and 19 deletions

View File

@@ -17,7 +17,10 @@ const route = createRoute({
method: "get", method: "get",
path: "/", path: "/",
request: { request: {
query: z.object({ query: z.string(), page: z.number().min(1).default(1) }), query: z.object({
query: z.string(),
page: z.number({ coerce: true }).int().min(1).default(1),
}),
}, },
responses: { responses: {
200: { 200: {

View File

@@ -2,7 +2,7 @@ import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources"; import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources";
import { import {
AniListIdSchema, AniListIdQuerySchema,
ErrorResponseSchema, ErrorResponseSchema,
SuccessResponseSchema, SuccessResponseSchema,
} from "~/types/schema"; } from "~/types/schema";
@@ -20,7 +20,7 @@ const route = createRoute({
method: "get", method: "get",
path: "/", path: "/",
request: { request: {
query: z.object({ id: AniListIdSchema }), query: z.object({ id: AniListIdQuerySchema }),
headers: z.object({ "x-anilist-token": z.string().nullish() }), headers: z.object({ "x-anilist-token": z.string().nullish() }),
}, },
responses: { responses: {

View File

@@ -2,11 +2,13 @@ import { type ZodSchema, z } from "zod";
export const SuccessResponse = { success: true } as const; export const SuccessResponse = { success: true } as const;
export const SuccessResponseSchema = <T extends ZodSchema>(schema?: T) => { export const SuccessResponseSchema = <T extends ZodSchema>(schema?: T) => {
const success = z.literal(true).openapi({ type: "boolean" });
if (!schema) { if (!schema) {
return z.object({ success: z.literal(true) }); return z.object({ success });
} }
return z.object({ success: z.literal(true), result: schema }); return z.object({ success, result: schema });
}; };
export const PaginatedResponseSchema = <T extends ZodSchema>(schema: T) => { export const PaginatedResponseSchema = <T extends ZodSchema>(schema: T) => {
@@ -18,9 +20,12 @@ export const PaginatedResponseSchema = <T extends ZodSchema>(schema: T) => {
}; };
export const ErrorResponseSchema = z.object({ export const ErrorResponseSchema = z.object({
success: z.literal(false), success: z.literal(false).openapi({ type: "boolean" }),
}); });
export const AniListIdSchema = z export const NullableNumberSchema = z.number().int().nullable();
.number({ coerce: true })
.openapi({ type: "integer" }); export const AniListIdSchema = z.number().int().openapi({ format: "int64" });
export const AniListIdQuerySchema = z
.string()
.openapi({ type: "integer", format: "int64" });

View File

@@ -1,14 +1,15 @@
import { z } from "zod"; import { z } from "zod";
import { NullableNumberSchema } from "../schema";
import { countryCodeSchema } from "./countryCodes"; import { countryCodeSchema } from "./countryCodes";
export type Title = z.infer<typeof Title>; export type Title = z.infer<typeof Title>;
export const Title = z.object({ export const Title = z.object({
nextAiringEpisode: z.nullable( nextAiringEpisode: z.nullable(
z.object({ z.object({
episode: z.number(), episode: z.number().int(),
airingAt: z.number(), airingAt: z.number().int().openapi({ format: "int64" }),
timeUntilAiring: z.number(), timeUntilAiring: z.number().int().openapi({ format: "int64" }),
}), }),
), ),
mediaListEntry: z.nullable( mediaListEntry: z.nullable(
@@ -23,11 +24,11 @@ export const Title = z.object({
"REPEATING", "REPEATING",
]), ]),
), ),
progress: z.number().nullable(), progress: NullableNumberSchema,
id: z.number(), id: z.number().int(),
}), }),
), ),
countryOfOrigin: z.optional(countryCodeSchema), countryOfOrigin: countryCodeSchema,
coverImage: z.nullable( coverImage: z.nullable(
z.object({ z.object({
medium: z.nullable(z.string()).optional(), medium: z.nullable(z.string()).optional(),
@@ -35,7 +36,7 @@ export const Title = z.object({
extraLarge: z.nullable(z.string()).optional(), extraLarge: z.nullable(z.string()).optional(),
}), }),
), ),
averageScore: z.number().nullable(), averageScore: NullableNumberSchema,
bannerImage: z.nullable(z.string()), bannerImage: z.nullable(z.string()),
status: z.nullable( status: z.nullable(
z.enum([ z.enum([
@@ -47,7 +48,7 @@ export const Title = z.object({
]), ]),
), ),
genres: z.nullable(z.array(z.nullable(z.string()))), genres: z.nullable(z.array(z.nullable(z.string()))),
episodes: z.number().nullable(), episodes: NullableNumberSchema,
description: z.nullable(z.string()), description: z.nullable(z.string()),
title: z.nullable( title: z.nullable(
z.object({ z.object({
@@ -55,6 +56,6 @@ export const Title = z.object({
english: z.nullable(z.string()), english: z.nullable(z.string()),
}), }),
), ),
idMal: z.number().nullable(), idMal: NullableNumberSchema,
id: z.number().openapi({ type: "integer", format: "int64" }), id: z.number().int().openapi({ format: "int64" }),
}); });