diff --git a/packages/hono/src/index.ts b/packages/hono/src/index.ts index 12bffd1f0..78ad27a97 100644 --- a/packages/hono/src/index.ts +++ b/packages/hono/src/index.ts @@ -39,15 +39,6 @@ const HONO_DEPENDENCIES: GeneratorDependency[] = [ ], dependency: 'hono', }, - { - exports: [ - { - name: 'zValidator', - values: true, - }, - ], - dependency: '@hono/zod-validator', - }, ]; export const getHonoDependencies = () => HONO_DEPENDENCIES; @@ -58,28 +49,29 @@ export const getHonoHeader: ClientHeaderBuilder = ({ tag, clientImplementation, }) => { - const targetFileName = getFileInfo(output.target)?.filename; + const targetInfo = getFileInfo(output.target); let handlers = ''; if (output.override.hono?.handlers) { + const handlerFileInfo = getFileInfo(output.override.hono.handlers); handlers = Object.keys(verbOptions) .filter((operationName) => clientImplementation.includes(`${operationName}Handlers`), ) .map((operationName) => { - const handlersPath = upath.getSpecName( - upath.join(output.override.hono.handlers ?? '', `./${operationName}`), - output.target ?? '', + const handlersPath = upath.relativeSafe( + targetInfo.dirname ?? '', + upath.join(handlerFileInfo.dirname ?? '', `./${operationName}`), ); - return `import { ${operationName}Handlers } from '.${handlersPath}';`; + return `import { ${operationName}Handlers } from '${handlersPath}';`; }) .join('\n'); } else { handlers = `import {\n${Object.keys(verbOptions) .map((operationName) => ` ${operationName}Handlers`) - .join(`, \n`)}\n} from './${tag ?? targetFileName}.handlers';`; + .join(`, \n`)}\n} from './${tag ?? targetInfo.filename}.handlers';`; } return `${handlers}\n\n @@ -89,7 +81,7 @@ const app = new Hono()\n\n`; export const getHonoFooter: ClientFooterBuilder = () => 'export default app'; const generateHonoRoute = ( - { operationName, verb, queryParams, params, body }: GeneratorVerbOptions, + { operationName, verb }: GeneratorVerbOptions, { pathRoute }: GeneratorOptions, ) => { const path = getRoute(pathRoute); @@ -144,6 +136,11 @@ ${ verbOption.body.definition ? `zValidator('json', ${verbOption.operationName}Body),\n` : '' + }${ + verbOption.response.contentTypes.length === 1 && + verbOption.response.contentTypes[0] === 'application/json' + ? `zValidator('response', ${verbOption.operationName}Response),\n` + : '' }(c: ${contextTypeName}) => { }, @@ -169,17 +166,22 @@ const getZvalidatorImports = (verbOption: GeneratorVerbOptions) => { imports.push(`${verbOption.operationName}Body`); } + if ( + verbOption.response.contentTypes.length === 1 && + verbOption.response.contentTypes[0] === 'application/json' + ) { + imports.push(`${verbOption.operationName}Response`); + } + return imports.join(',\n'); }; const getHandlerFix = ({ rawFile, content, - hasZValidator, }: { rawFile: string; content: string; - hasZValidator: boolean; }) => { let newContent = content; @@ -187,10 +189,6 @@ const getHandlerFix = ({ newContent = `import { createFactory } from 'hono/factory';\n${newContent}`; } - if (!rawFile.includes('@hono/zod-validator') && hasZValidator) { - newContent = `import { zValidator } from '@hono/zod-validator';\n${newContent}`; - } - if (!rawFile.includes('const factory = createFactory();')) { newContent += '\nconst factory = createFactory();'; } @@ -201,17 +199,14 @@ const getHandlerFix = ({ const getVerbOptionGroupByTag = ( verbOptions: Record, ) => { - return Object.values(verbOptions).reduce( - (acc, value) => { - const tag = value.tags[0]; - if (!acc[tag]) { - acc[tag] = []; - } - acc[tag].push(value); - return acc; - }, - {} as Record, - ); + return Object.values(verbOptions).reduce((acc, value) => { + const tag = value.tags[0]; + if (!acc[tag]) { + acc[tag] = []; + } + acc[tag].push(value); + return acc; + }, {} as Record); }; const generateHandlers = async ( @@ -254,7 +249,6 @@ const generateHandlers = async ( let content = getHandlerFix({ rawFile, content: rawFile, - hasZValidator, }); if (!rawFile.includes(handlerName)) { @@ -273,7 +267,7 @@ const generateHandlers = async ( const content = `import { createFactory } from 'hono/factory';${ hasZValidator - ? `\nimport { zValidator } from '@hono/zod-validator';` + ? `\nimport { zValidator } from '${outputPath}.validator';` : '' } import { ${contextTypeName} } from '${outputPath}.context'; @@ -321,7 +315,6 @@ ${getHonoHandlers({ let content = getHandlerFix({ rawFile, content: rawFile, - hasZValidator, }); content += Object.values(verbs).reduce((acc, verbOption) => { @@ -351,7 +344,7 @@ ${getHonoHandlers({ let content = `import { createFactory } from 'hono/factory';${ hasZValidator - ? `\nimport { zValidator } from '@hono/zod-validator';` + ? `\nimport { zValidator } from '${outputRelativePath}.validator';` : '' } import { ${Object.values(verbs) @@ -389,7 +382,9 @@ const factory = createFactory();`; !!verb.headers || !!verb.params.length || !!verb.queryParams || - !!verb.body, + !!verb.body || + (verb.response.contentTypes.length === 1 && + verb.response.contentTypes[0] === 'application/json'), ); const handlerPath = upath.join(dirname, `${filename}.handlers${extension}`); @@ -401,7 +396,6 @@ const factory = createFactory();`; let content = getHandlerFix({ rawFile, content: rawFile, - hasZValidator, }); content += Object.values(verbOptions).reduce((acc, verbOption) => { @@ -430,7 +424,9 @@ const factory = createFactory();`; const outputRelativePath = `./${filename}`; let content = `import { createFactory } from 'hono/factory';${ - hasZValidator ? `\nimport { zValidator } from '@hono/zod-validator';` : '' + hasZValidator + ? `\nimport { zValidator } from '${outputRelativePath}.validator';` + : '' } import { ${Object.values(verbOptions) .map((verb) => `${pascal(verb.operationName)}Context`) @@ -479,9 +475,9 @@ const getContext = (verbOption: GeneratorVerbOptions) => { return `export type ${pascal( verbOption.operationName, - )}Context = Context`; + )}Context = Context`; }; const getHeader = ( @@ -704,18 +700,171 @@ const generateZodFiles = async ( ]; }; +const generateZvalidator = ( + output: NormalizedOutputOptions, + context: ContextSpecs, +) => { + const header = getHeader( + output.override.header, + context.specs[context.specKey].info, + ); + + const { extension, dirname, filename } = getFileInfo(output.target); + const content = ` +// based on https://github.com/honojs/middleware/blob/main/packages/zod-validator/src/index.ts +import type { z, ZodSchema, ZodError } from 'zod'; +import { + Context, + Env, + Input, + MiddlewareHandler, + TypedResponse, + ValidationTargets, +} from 'hono'; + +type HasUndefined = undefined extends T ? true : false; + +type Hook = ( + result: + | { success: true; data: T } + | { success: false; error: ZodError; data: T }, + c: Context, +) => + | Response + | Promise + | void + | Promise + | TypedResponse; +import { zValidator as zValidatorBase } from '@hono/zod-validator'; + +type ValidationTargetsWithResponse = ValidationTargets & { response: any }; + +export const zValidator = + < + T extends ZodSchema, + Target extends keyof ValidationTargetsWithResponse, + E extends Env, + P extends string, + In = z.input, + Out = z.output, + I extends Input = { + in: HasUndefined extends true + ? { + [K in Target]?: K extends 'json' + ? In + : HasUndefined< + keyof ValidationTargetsWithResponse[K] + > extends true + ? { [K2 in keyof In]?: ValidationTargetsWithResponse[K][K2] } + : { [K2 in keyof In]: ValidationTargetsWithResponse[K][K2] }; + } + : { + [K in Target]: K extends 'json' + ? In + : HasUndefined< + keyof ValidationTargetsWithResponse[K] + > extends true + ? { [K2 in keyof In]?: ValidationTargetsWithResponse[K][K2] } + : { [K2 in keyof In]: ValidationTargetsWithResponse[K][K2] }; + }; + out: { [K in Target]: Out }; + }, + V extends I = I, + >( + target: Target, + schema: T, + hook?: Hook, E, P>, + ): MiddlewareHandler => + async (c, next) => { + if (target !== 'response') { + const value = await zValidatorBase< + T, + keyof ValidationTargets, + E, + P, + In, + Out, + I, + V + >( + target, + schema, + hook, + )(c, next); + + if (value instanceof Response) { + return value; + } + } else { + await next(); + + const clonedResponse = c.res.clone(); + + let value: unknown; + try { + value = await clonedResponse.json(); + } catch { + const message = 'Malformed JSON in response'; + c.res = new Response(message, { status: 400 }); + } + + const result = await schema.safeParseAsync(value); + + if (hook) { + const hookResult = hook({ data: value, ...result }, c); + if (hookResult) { + if (hookResult instanceof Response || hookResult instanceof Promise) { + const hookResponse = await hookResult; + + if (hookResponse instanceof Response) { + c.res = new Response(hookResponse.body, hookResponse); + } + } + if ( + 'response' in hookResult && + hookResult.response instanceof Response + ) { + c.res = new Response(hookResult.response.body, hookResult.response); + } + } + } + + if (!result.success) { + c.res = new Response(JSON.stringify(result), { + status: 400, + headers: { + 'Content-Type': 'application/json', + }, + }); + } + } + }; +`; + + const validatorPath = upath.join( + dirname, + `${filename}.validator${extension}`, + ); + + return { + content: `${header}${content}`, + path: validatorPath, + }; +}; + export const generateExtraFiles: ClientExtraFilesBuilder = async ( verbOptions, output, context, ) => { - const [handlers, contexts, zods] = await Promise.all([ + const [handlers, contexts, zods, validator] = await Promise.all([ generateHandlers(verbOptions, output), generateContext(verbOptions, output, context), generateZodFiles(verbOptions, output, context), + generateZvalidator(output, context), ]); - return [...handlers, ...contexts, ...zods]; + return [...handlers, ...contexts, ...zods, validator]; }; const honoClientBuilder: ClientGeneratorsBuilder = { diff --git a/samples/hono/src/handlers/createPets.ts b/samples/hono/src/handlers/createPets.ts index 97f7e1710..8bd45e2c9 100644 --- a/samples/hono/src/handlers/createPets.ts +++ b/samples/hono/src/handlers/createPets.ts @@ -1,11 +1,16 @@ import { createFactory } from 'hono/factory'; -import { zValidator } from '@hono/zod-validator'; +import { zValidator } from '../petstore.validator'; import { CreatePetsContext } from '../petstore.context'; -import { createPetsBody } from '../petstore.zod'; +import { createPetsBody, +createPetsResponse } from '../petstore.zod'; const factory = createFactory(); + export const createPetsHandlers = factory.createHandlers( - zValidator('json', createPetsBody), - (c: CreatePetsContext) => {}, +zValidator('json', createPetsBody), +zValidator('response', createPetsResponse), +(c: CreatePetsContext) => { + + }, ); diff --git a/samples/hono/src/handlers/listPets.ts b/samples/hono/src/handlers/listPets.ts index 90da571f8..f4888e732 100644 --- a/samples/hono/src/handlers/listPets.ts +++ b/samples/hono/src/handlers/listPets.ts @@ -1,11 +1,16 @@ import { createFactory } from 'hono/factory'; -import { zValidator } from '@hono/zod-validator'; +import { zValidator } from '../petstore.validator'; import { ListPetsContext } from '../petstore.context'; -import { listPetsQueryParams } from '../petstore.zod'; +import { listPetsQueryParams, +listPetsResponse } from '../petstore.zod'; const factory = createFactory(); + export const listPetsHandlers = factory.createHandlers( - zValidator('query', listPetsQueryParams), - (c: ListPetsContext) => {}, +zValidator('query', listPetsQueryParams), +zValidator('response', listPetsResponse), +(c: ListPetsContext) => { + + }, ); diff --git a/samples/hono/src/handlers/showPetById.ts b/samples/hono/src/handlers/showPetById.ts index 48608941d..d88ede533 100644 --- a/samples/hono/src/handlers/showPetById.ts +++ b/samples/hono/src/handlers/showPetById.ts @@ -1,11 +1,16 @@ import { createFactory } from 'hono/factory'; -import { zValidator } from '@hono/zod-validator'; +import { zValidator } from '../petstore.validator'; import { ShowPetByIdContext } from '../petstore.context'; -import { showPetByIdParams } from '../petstore.zod'; +import { showPetByIdParams, +showPetByIdResponse } from '../petstore.zod'; const factory = createFactory(); + export const showPetByIdHandlers = factory.createHandlers( - zValidator('param', showPetByIdParams), - (c: ShowPetByIdContext) => {}, +zValidator('param', showPetByIdParams), +zValidator('response', showPetByIdResponse), +(c: ShowPetByIdContext) => { + + }, ); diff --git a/samples/hono/src/handlers/updatePets.ts b/samples/hono/src/handlers/updatePets.ts index b7ce3bd2b..3d0a59d22 100644 --- a/samples/hono/src/handlers/updatePets.ts +++ b/samples/hono/src/handlers/updatePets.ts @@ -1,11 +1,16 @@ import { createFactory } from 'hono/factory'; -import { zValidator } from '@hono/zod-validator'; +import { zValidator } from '../petstore.validator'; import { UpdatePetsContext } from '../petstore.context'; -import { updatePetsBody } from '../petstore.zod'; +import { updatePetsBody, +updatePetsResponse } from '../petstore.zod'; const factory = createFactory(); + export const updatePetsHandlers = factory.createHandlers( - zValidator('json', updatePetsBody), - (c: UpdatePetsContext) => {}, +zValidator('json', updatePetsBody), +zValidator('response', updatePetsResponse), +(c: UpdatePetsContext) => { + + }, ); diff --git a/samples/hono/src/petstore.context.ts b/samples/hono/src/petstore.context.ts index d271bbb7d..6ff83282f 100644 --- a/samples/hono/src/petstore.context.ts +++ b/samples/hono/src/petstore.context.ts @@ -1,64 +1,46 @@ /** - * Generated by orval v6.25.0 🍺 + * Generated by orval v6.26.0 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 */ import type { Context, Env } from 'hono'; + type IsAny = 0 extends 1 & T ? true : false; -type IsUnknown = - IsAny extends true ? false : unknown extends T ? true : false; +type IsUnknown = IsAny extends true ? false : unknown extends T ? true : false; type Primitive = string | number | boolean | bigint | symbol | undefined | null; type isBuiltin = Primitive | Function | Date | Error | RegExp; type NonReadonly = T extends Exclude - ? T - : T extends Map - ? Map, NonReadonly> - : T extends ReadonlyMap - ? Map, NonReadonly> - : T extends WeakMap - ? WeakMap, NonReadonly> - : T extends Set - ? Set> - : T extends ReadonlySet - ? Set> - : T extends WeakSet - ? WeakSet> - : T extends Promise - ? Promise> - : T extends {} - ? { -readonly [Key in keyof T]: NonReadonly } - : IsUnknown extends true - ? unknown - : T; + ? T + : T extends Map + ? Map, NonReadonly> + : T extends ReadonlyMap + ? Map, NonReadonly> + : T extends WeakMap + ? WeakMap, NonReadonly> + : T extends Set + ? Set> + : T extends ReadonlySet + ? Set> + : T extends WeakSet + ? WeakSet> + : T extends Promise + ? Promise> + : T extends {} + ? { -readonly [Key in keyof T]: NonReadonly } + : IsUnknown extends true + ? unknown + : T; -import { ListPetsParams, CreatePetsBodyItem, Pet } from './petstore.schemas'; +import { ListPetsParams, +CreatePetsBodyItem, +Pet } from './petstore.schemas'; -export type ListPetsContext = Context< - E, - '/pets', - { in: { query: ListPetsParams } } ->; -export type CreatePetsContext = Context< - E, - '/pets', - { in: { json: CreatePetsBodyItem[] } } ->; -export type UpdatePetsContext = Context< - E, - '/pets', - { in: { json: NonReadonly } } ->; -export type ShowPetByIdContext = Context< - E, - '/pets/{petId}', - { - in: { - param: { - petId: string; - }; - }; - } ->; +export type ListPetsContext = Context +export type CreatePetsContext = Context +export type UpdatePetsContext = Context, }}> +export type ShowPetByIdContext = Context \ No newline at end of file diff --git a/samples/hono/src/petstore.schemas.ts b/samples/hono/src/petstore.schemas.ts index fb8a774f1..2602b0162 100644 --- a/samples/hono/src/petstore.schemas.ts +++ b/samples/hono/src/petstore.schemas.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.25.0 🍺 + * Generated by orval v6.26.0 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 @@ -10,10 +10,10 @@ export type CreatePetsBodyItem = { }; export type ListPetsParams = { - /** - * How many items to return at one time (max 100) - */ - limit?: string; +/** + * How many items to return at one time (max 100) + */ +limit?: string; }; export interface Error { @@ -23,7 +23,8 @@ export interface Error { export type Pets = Pet[]; -export type CatType = (typeof CatType)[keyof typeof CatType]; +export type CatType = typeof CatType[keyof typeof CatType]; + // eslint-disable-next-line @typescript-eslint/no-redeclare export const CatType = { @@ -35,8 +36,8 @@ export interface Cat { type: CatType; } -export type DachshundBreed = - (typeof DachshundBreed)[keyof typeof DachshundBreed]; +export type DachshundBreed = typeof DachshundBreed[keyof typeof DachshundBreed]; + // eslint-disable-next-line @typescript-eslint/no-redeclare export const DachshundBreed = { @@ -48,8 +49,8 @@ export interface Dachshund { length: number; } -export type LabradoodleBreed = - (typeof LabradoodleBreed)[keyof typeof LabradoodleBreed]; +export type LabradoodleBreed = typeof LabradoodleBreed[keyof typeof LabradoodleBreed]; + // eslint-disable-next-line @typescript-eslint/no-redeclare export const LabradoodleBreed = { @@ -61,33 +62,33 @@ export interface Labradoodle { cuteness: number; } -export type DogType = (typeof DogType)[keyof typeof DogType]; +export type DogType = typeof DogType[keyof typeof DogType]; + // eslint-disable-next-line @typescript-eslint/no-redeclare export const DogType = { dog: 'dog', } as const; -export type Dog = - | (Labradoodle & { - barksPerMinute?: number; - type: DogType; - }) - | (Dachshund & { - barksPerMinute?: number; - type: DogType; - }); +export type Dog = (Labradoodle & { + barksPerMinute?: number; + type: DogType; +}) | (Dachshund & { + barksPerMinute?: number; + type: DogType; +}); + +export type PetCountry = typeof PetCountry[keyof typeof PetCountry]; -export type PetCountry = (typeof PetCountry)[keyof typeof PetCountry]; // eslint-disable-next-line @typescript-eslint/no-redeclare export const PetCountry = { - "People's_Republic_of_China": "People's Republic of China", + 'People\'s_Republic_of_China': 'People\'s Republic of China', Uruguay: 'Uruguay', } as const; -export type PetCallingCode = - (typeof PetCallingCode)[keyof typeof PetCallingCode]; +export type PetCallingCode = typeof PetCallingCode[keyof typeof PetCallingCode]; + // eslint-disable-next-line @typescript-eslint/no-redeclare export const PetCallingCode = { @@ -95,22 +96,21 @@ export const PetCallingCode = { '+420': '+420', } as const; -export type Pet = - | (Dog & { - '@id'?: string; - callingCode?: PetCallingCode; - country?: PetCountry; - email?: string; - id: number; - name: string; - tag?: string; - }) - | (Cat & { - '@id'?: string; - callingCode?: PetCallingCode; - country?: PetCountry; - email?: string; - id: number; - name: string; - tag?: string; - }); +export type Pet = (Dog & { + '@id'?: string; + callingCode?: PetCallingCode; + country?: PetCountry; + email?: string; + id: number; + name: string; + tag?: string; +}) | (Cat & { + '@id'?: string; + callingCode?: PetCallingCode; + country?: PetCountry; + email?: string; + id: number; + name: string; + tag?: string; +}); + diff --git a/samples/hono/src/petstore.ts b/samples/hono/src/petstore.ts index 95385809f..f728ba264 100644 --- a/samples/hono/src/petstore.ts +++ b/samples/hono/src/petstore.ts @@ -1,40 +1,47 @@ /** - * Generated by orval v6.25.0 🍺 + * Generated by orval v6.26.0 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 */ -import { Hono } from 'hono'; +import { + Hono +} from 'hono' import { listPetsHandlers } from './handlers/listPets'; import { createPetsHandlers } from './handlers/createPets'; import { updatePetsHandlers } from './handlers/updatePets'; import { showPetByIdHandlers } from './handlers/showPetById'; -const app = new Hono(); + +const app = new Hono() /** * @summary List all pets */ -app.get('/pets', ...listPetsHandlers); +app.get('/pets',...listPetsHandlers) + /** * @summary Create a pet */ -app.post('/pets', ...createPetsHandlers); +app.post('/pets',...createPetsHandlers) + /** * @summary Update a pet */ -app.put('/pets', ...updatePetsHandlers); +app.put('/pets',...updatePetsHandlers) + /** * @summary Info for a specific pet */ -app.get('/pets/:petId', ...showPetByIdHandlers); +app.get('/pets/:petId',...showPetByIdHandlers) + -export default app; +export default app \ No newline at end of file diff --git a/samples/hono/src/petstore.validator.ts b/samples/hono/src/petstore.validator.ts new file mode 100644 index 000000000..623ab34e6 --- /dev/null +++ b/samples/hono/src/petstore.validator.ts @@ -0,0 +1,135 @@ +/** + * Generated by orval v6.26.0 🍺 + * Do not edit manually. + * Swagger Petstore + * OpenAPI spec version: 1.0.0 + */ + +// based on https://github.com/honojs/middleware/blob/main/packages/zod-validator/src/index.ts +import type { z, ZodSchema, ZodError } from 'zod'; +import { + Context, + Env, + Input, + MiddlewareHandler, + TypedResponse, + ValidationTargets, +} from 'hono'; + +type HasUndefined = undefined extends T ? true : false; + +type Hook = ( + result: + | { success: true; data: T } + | { success: false; error: ZodError; data: T }, + c: Context, +) => + | Response + | Promise + | void + | Promise + | TypedResponse; +import { zValidator as zValidatorBase } from '@hono/zod-validator'; + +type ValidationTargetsWithResponse = ValidationTargets & { response: any }; + +export const zValidator = + < + T extends ZodSchema, + Target extends keyof ValidationTargetsWithResponse, + E extends Env, + P extends string, + In = z.input, + Out = z.output, + I extends Input = { + in: HasUndefined extends true + ? { + [K in Target]?: K extends 'json' + ? In + : HasUndefined< + keyof ValidationTargetsWithResponse[K] + > extends true + ? { [K2 in keyof In]?: ValidationTargetsWithResponse[K][K2] } + : { [K2 in keyof In]: ValidationTargetsWithResponse[K][K2] }; + } + : { + [K in Target]: K extends 'json' + ? In + : HasUndefined< + keyof ValidationTargetsWithResponse[K] + > extends true + ? { [K2 in keyof In]?: ValidationTargetsWithResponse[K][K2] } + : { [K2 in keyof In]: ValidationTargetsWithResponse[K][K2] }; + }; + out: { [K in Target]: Out }; + }, + V extends I = I, + >( + target: Target, + schema: T, + hook?: Hook, E, P>, + ): MiddlewareHandler => + async (c, next) => { + if (target !== 'response') { + const value = await zValidatorBase< + T, + keyof ValidationTargets, + E, + P, + In, + Out, + I, + V + >( + target, + schema, + hook, + )(c, next); + + if (value instanceof Response) { + return value; + } + } else { + await next(); + + const clonedResponse = c.res.clone(); + + let value: unknown; + try { + value = await clonedResponse.json(); + } catch { + const message = 'Malformed JSON in response'; + c.res = new Response(message, { status: 400 }); + } + + const result = await schema.safeParseAsync(value); + + if (hook) { + const hookResult = hook({ data: value, ...result }, c); + if (hookResult) { + if (hookResult instanceof Response || hookResult instanceof Promise) { + const hookResponse = await hookResult; + + if (hookResponse instanceof Response) { + c.res = new Response(hookResponse.body, hookResponse); + } + } + if ( + 'response' in hookResult && + hookResult.response instanceof Response + ) { + c.res = new Response(hookResult.response.body, hookResult.response); + } + } + } + + if (!result.success) { + c.res = new Response(JSON.stringify(result), { + status: 400, + headers: { + 'Content-Type': 'application/json', + }, + }); + } + } + }; diff --git a/samples/hono/src/petstore.zod.ts b/samples/hono/src/petstore.zod.ts index 578a42b27..1cecdeba4 100644 --- a/samples/hono/src/petstore.zod.ts +++ b/samples/hono/src/petstore.zod.ts @@ -1,5 +1,5 @@ /** - * Generated by orval v6.25.0 🍺 + * Generated by orval v6.26.0 🍺 * Do not edit manually. * Swagger Petstore * OpenAPI spec version: 1.0.0 @@ -7,67 +7,71 @@ import { z as zod } from 'zod'; export const listPetsQueryParams = zod.object({ - limit: zod.string().optional(), -}); + "limit": zod.string().optional() +}) export const listPetsResponseItem = zod.object({ - '@id': zod.string().optional(), - id: zod.number().optional(), - name: zod.string().optional(), - tag: zod.string().optional(), - email: zod.string().email().optional(), - callingCode: zod.enum(['+33', '+420', '+33']).optional(), - country: zod.enum(["People's Republic of China", 'Uruguay']).optional(), -}); -export const listPetsResponse = zod.array(listPetsResponseItem); + "@id": zod.string().optional(), + "id": zod.number().optional(), + "name": zod.string().optional(), + "tag": zod.string().optional(), + "email": zod.string().email().optional(), + "callingCode": zod.enum(['+33', '+420', '+33']).optional(), + "country": zod.enum(['People\'s Republic of China', 'Uruguay']).optional() +}) +export const listPetsResponse = zod.array(listPetsResponseItem) + export const createPetsBodyItem = zod.object({ - name: zod.string().optional(), - tag: zod.string().optional(), -}); -export const createPetsBody = zod.array(createPetsBodyItem); + "name": zod.string().optional(), + "tag": zod.string().optional() +}) +export const createPetsBody = zod.array(createPetsBodyItem) export const createPetsResponse = zod.object({ - '@id': zod.string().optional(), - id: zod.number(), - name: zod.string(), - tag: zod.string().optional(), - email: zod.string().email().optional(), - callingCode: zod.enum(['+33', '+420', '+33']).optional(), - country: zod.enum(["People's Republic of China", 'Uruguay']).optional(), -}); + "@id": zod.string().optional(), + "id": zod.number(), + "name": zod.string(), + "tag": zod.string().optional(), + "email": zod.string().email().optional(), + "callingCode": zod.enum(['+33', '+420', '+33']).optional(), + "country": zod.enum(['People\'s Republic of China', 'Uruguay']).optional() +}) + export const updatePetsBody = zod.object({ - '@id': zod.string().optional(), - id: zod.number(), - name: zod.string(), - tag: zod.string().optional(), - email: zod.string().email().optional(), - callingCode: zod.enum(['+33', '+420', '+33']).optional(), - country: zod.enum(["People's Republic of China", 'Uruguay']).optional(), -}); + "@id": zod.string().optional(), + "id": zod.number(), + "name": zod.string(), + "tag": zod.string().optional(), + "email": zod.string().email().optional(), + "callingCode": zod.enum(['+33', '+420', '+33']).optional(), + "country": zod.enum(['People\'s Republic of China', 'Uruguay']).optional() +}) export const updatePetsResponse = zod.object({ - '@id': zod.string().optional(), - id: zod.number(), - name: zod.string(), - tag: zod.string().optional(), - email: zod.string().email().optional(), - callingCode: zod.enum(['+33', '+420', '+33']).optional(), - country: zod.enum(["People's Republic of China", 'Uruguay']).optional(), -}); + "@id": zod.string().optional(), + "id": zod.number(), + "name": zod.string(), + "tag": zod.string().optional(), + "email": zod.string().email().optional(), + "callingCode": zod.enum(['+33', '+420', '+33']).optional(), + "country": zod.enum(['People\'s Republic of China', 'Uruguay']).optional() +}) + export const showPetByIdParams = zod.object({ - petId: zod.string(), - testId: zod.string(), -}); + "petId": zod.string(), + "testId": zod.string() +}) export const showPetByIdResponse = zod.object({ - '@id': zod.string().optional(), - id: zod.number(), - name: zod.string(), - tag: zod.string().optional(), - email: zod.string().email().optional(), - callingCode: zod.enum(['+33', '+420', '+33']).optional(), - country: zod.enum(["People's Republic of China", 'Uruguay']).optional(), -}); + "@id": zod.string().optional(), + "id": zod.number(), + "name": zod.string(), + "tag": zod.string().optional(), + "email": zod.string().email().optional(), + "callingCode": zod.enum(['+33', '+420', '+33']).optional(), + "country": zod.enum(['People\'s Republic of China', 'Uruguay']).optional() +}) +