Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zod-openapi): use z.input to infer input types of the request #286

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nasty-gorillas-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---

use z.input to infer input types of the request
2 changes: 1 addition & 1 deletion packages/zod-openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dist"
],
"scripts": {
"test": "vitest run",
"test": "vitest run && vitest typecheck --run --passWithNoTests",
"build": "tsup ./src/index.ts --format esm,cjs --dts",
"publint": "publint",
"release": "yarn build && yarn test && yarn publint && yarn publish"
Expand Down
6 changes: 3 additions & 3 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type InputTypeBase<
> = R['request'] extends RequestTypes
? RequestPart<R, Part> extends AnyZodObject
? {
in: { [K in Type]: z.infer<RequestPart<R, Part>> }
in: { [K in Type]: z.input<RequestPart<R, Part>> }
out: { [K in Type]: z.output<RequestPart<R, Part>> }
}
: {}
Expand All @@ -79,7 +79,7 @@ type InputTypeJson<R extends RouteConfig> = R['request'] extends RequestTypes
: R['request']['body']['content'][keyof R['request']['body']['content']]['schema'] extends ZodSchema<any>
? {
in: {
json: z.infer<
json: z.input<
R['request']['body']['content'][keyof R['request']['body']['content']]['schema']
>
}
Expand All @@ -102,7 +102,7 @@ type InputTypeForm<R extends RouteConfig> = R['request'] extends RequestTypes
: R['request']['body']['content'][keyof R['request']['body']['content']]['schema'] extends ZodSchema<any>
? {
in: {
form: z.infer<
form: z.input<
R['request']['body']['content'][keyof R['request']['body']['content']]['schema']
>
}
Expand Down
2 changes: 1 addition & 1 deletion packages/zod-openapi/test/createRoute.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable node/no-extraneous-import */
import { describe, it, expect, expectTypeOf } from 'vitest'
import { createRoute, z } from '../src'
import { createRoute, z } from '../src/index'

describe('createRoute', () => {
it.each([
Expand Down
166 changes: 166 additions & 0 deletions packages/zod-openapi/test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import type { Hono, Env, ToSchema } from 'hono'
import { describe, it, expectTypeOf, assertType } from 'vitest'
import { OpenAPIHono, createRoute, z } from '../src/index'

describe('Types', () => {
const RequestSchema = z.object({
id: z.number().openapi({}),
title: z.string().openapi({}),
})

const PostSchema = z
.object({
id: z.number().openapi({}),
message: z.string().openapi({}),
})
.openapi('Post')

const route = createRoute({
method: 'post',
path: '/posts',
request: {
body: {
content: {
'application/json': {
schema: RequestSchema,
},
},
},
},
responses: {
200: {
content: {
'application/json': {
schema: PostSchema,
},
},
description: 'Post a post',
},
},
})

const app = new OpenAPIHono()

const appRoutes = app.openapi(route, (c) => {
const data = c.req.valid('json')
assertType<number>(data.id)
return c.jsonT({
id: data.id,
message: 'Success',
})
})

it('Should return correct types', () => {
type H = Hono<
Env,
ToSchema<
'post',
'/posts',
{
json: {
title: string
id: number
}
},
{
id: number
message: string
}
>,
'/'
>
expectTypeOf(appRoutes).toMatchTypeOf<H>()
})
})

describe('Input types', () => {
const ParamsSchema = z.object({
id: z.string().transform(Number).openapi({
param: {
name: 'id',
in: 'path',
},
example: 123,
}),
})

const QuerySchema = z.object({
age: z.string().transform(Number).openapi({
param: {
name: 'age',
in: 'query',
},
example: 42
}),
})

const BodySchema = z.object({
sex: z.enum(['male', 'female']).openapi({})
}).openapi('User')

const UserSchema = z
.object({
id: z.number().openapi({
example: 123,
}),
name: z.string().openapi({
example: 'John Doe',
}),
age: z.number().openapi({
example: 42,
}),
sex: z.enum(['male', 'female']).openapi({
example: 'male',
})
})
.openapi('User')

const route = createRoute({
method: 'patch',
path: '/users/{id}',
request: {
params: ParamsSchema,
query: QuerySchema,
body: {
content: {
'application/json': {
schema: BodySchema,
},
},
},
},
responses: {
200: {
content: {
'application/json': {
schema: UserSchema,
},
},
description: 'Update a user',
},
},
})


it('Should return correct types', () => {
const app = new OpenAPIHono()

app.openapi(route, (c) => {
const { id } = c.req.valid('param')
assertType<number>(id)

const { age } = c.req.valid('query')
assertType<number>(age)

const { sex } = c.req.valid('json')
assertType<'male' | 'female'>(sex)

return c.jsonT({
id,
age,
sex,
name: 'Ultra-man',
})
})
})
})
Loading