Skip to content

Commit

Permalink
fix(zod-openapi): relax types to support .refine() for an object (#574
Browse files Browse the repository at this point in the history
)

* fix(zod-openapi): relax types to support `.refine()` for an object

* add a chengeset
  • Loading branch information
yusukebe authored Jun 14, 2024
1 parent 5a0488a commit ef9f45a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-rice-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---

fix: relax types to support `.refine()` for an object
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@
"typescript": "^5.2.2"
},
"packageManager": "[email protected]"
}
}
2 changes: 1 addition & 1 deletion packages/react-compat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
"hono": "4.5.0-rc.1",
"tsup": "^8.0.1"
}
}
}
Binary file modified packages/zod-openapi/.yarn/install-state.gz
Binary file not shown.
1 change: 0 additions & 1 deletion packages/zod-openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@cloudflare/workers-types": "^4.20240117.0",
"hono": "^4.3.6",
"jest": "^29.7.0",
"openapi3-ts": "^4.1.2",
"tsup": "^8.0.1",
"typescript": "^5.4.4",
"vitest": "^1.4.0",
Expand Down
22 changes: 11 additions & 11 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import type {
ZodRequestBody,
} from '@asteasolutions/zod-to-openapi'
import {
OpenAPIRegistry,
OpenApiGeneratorV3,
OpenApiGeneratorV31,
OpenAPIRegistry,
extendZodWithOpenApi,
} from '@asteasolutions/zod-to-openapi'
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi'
import type { OpenAPIObjectConfig } from '@asteasolutions/zod-to-openapi/dist/v3.0/openapi-generator'
import { zValidator } from '@hono/zod-validator'
import { Hono } from 'hono'
Expand All @@ -35,10 +35,10 @@ import type {
StatusCode,
SuccessStatusCode,
} from 'hono/utils/http-status'
import type { Prettify, RemoveBlankRecord } from 'hono/utils/types'
import type { RemoveBlankRecord } from 'hono/utils/types'
import { mergePath } from 'hono/utils/url'
import type { AnyZodObject, ZodSchema, ZodError } from 'zod'
import { z, ZodType } from 'zod'
import type { AnyZodObject, ZodError, ZodSchema } from 'zod'
import { ZodType, z } from 'zod'

type MaybePromise<T> = Promise<T> | T

Expand All @@ -48,10 +48,10 @@ export type RouteConfig = RouteConfigBase & {

type RequestTypes = {
body?: ZodRequestBody
params?: AnyZodObject
query?: AnyZodObject
cookies?: AnyZodObject
headers?: AnyZodObject | ZodType<unknown>[]
params?: ZodType
query?: ZodType
cookies?: ZodType
headers?: ZodType | ZodType[]
}

type IsJson<T> = T extends string
Expand All @@ -77,7 +77,7 @@ type InputTypeBase<
Part extends string,
Type extends string
> = R['request'] extends RequestTypes
? RequestPart<R, Part> extends AnyZodObject
? RequestPart<R, Part> extends ZodType
? {
in: { [K in Type]: z.input<RequestPart<R, Part>> }
out: { [K in Type]: z.output<RequestPart<R, Part>> }
Expand Down Expand Up @@ -299,7 +299,7 @@ export class OpenAPIHono<
InputTypeJson<R>,
P extends string = ConvertPathType<R['path']>
>(
{middleware: routeMiddleware, ...route}: R,
{ middleware: routeMiddleware, ...route }: R,
handler: Handler<
E,
P,
Expand Down
73 changes: 38 additions & 35 deletions packages/zod-openapi/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import type { RouteConfig } from '@asteasolutions/zod-to-openapi'
import type { Context, TypedResponse } from 'hono'
import { bearerAuth } from 'hono/bearer-auth'
import { hc } from 'hono/client'
import { describe, it, expect, expectTypeOf } from 'vitest'
import { OpenAPIHono, createRoute, z, RouteConfigToTypedResponse } from '../src/index'
import { Expect, Equal } from 'hono/utils/types'
import { ServerErrorStatusCode } from 'hono/utils/http-status'
import { stringify } from "yaml"
import { describe, expect, expectTypeOf, it } from 'vitest'
import type { RouteConfigToTypedResponse } from '../src/index'
import { OpenAPIHono, createRoute, z } from '../src/index'
import type { Equal, Expect } from 'hono/utils/types'
import type { ServerErrorStatusCode } from 'hono/utils/http-status'
import { stringify } from 'yaml'

describe('Constructor', () => {
it('Should not require init object', () => {
Expand Down Expand Up @@ -1491,43 +1492,45 @@ describe('RouteConfigToTypedResponse', () => {

type Expected =
| TypedResponse<
{
name: string
age: number
},
200,
'json'
>
{
name: string
age: number
},
200,
'json'
>
| TypedResponse<
{
ok: boolean
},
400,
'json'
>
{
ok: boolean
},
400,
'json'
>
| TypedResponse<
{
ok: boolean
},
ServerErrorStatusCode,
'json'
>
{
ok: boolean
},
ServerErrorStatusCode,
'json'
>
type verify = Expect<Equal<Expected, Actual>>
})
})

describe("Generate YAML", () => {
it("Should generate YAML with Middleware", async () => {
describe('Generate YAML', () => {
it('Should generate YAML with Middleware', async () => {
const app = new OpenAPIHono()
app.openapi(
createRoute({
method: 'get',
path: '/books',
middleware: [bearerAuth({
verifyToken: (_, __) => {
return true;
},
})],
middleware: [
bearerAuth({
verifyToken: (_, __) => {
return true
},
}),
],
responses: {
200: {
description: 'Books',
Expand All @@ -1550,8 +1553,8 @@ describe("Generate YAML", () => {
info: {
title: 'My API',
version: '1.0.0',
}
});
expect(() => stringify(doc)).to.not.throw();
},
})
expect(() => stringify(doc)).to.not.throw()
})
});
})
7 changes: 3 additions & 4 deletions packages/zod-openapi/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ __metadata:
"@hono/zod-validator": "npm:0.2.2"
hono: "npm:^4.3.6"
jest: "npm:^29.7.0"
openapi3-ts: "npm:^4.1.2"
tsup: "npm:^8.0.1"
typescript: "npm:^5.4.4"
vitest: "npm:^1.4.0"
Expand Down Expand Up @@ -2381,9 +2380,9 @@ __metadata:
linkType: hard

"hono@npm:^4.3.6":
version: 4.3.6
resolution: "hono@npm:4.3.6"
checksum: 2e27eb1e90b392a5884af573179d29e3f717f5e803c2b90f1383488f42bc986810e8e714d5bb1205935fda1d3e9944b3262aed88e852ea44d0e13d799474fa5b
version: 4.4.6
resolution: "hono@npm:4.4.6"
checksum: 065318f3fe021320b59f3daddacf7d74bfc3303de55f415a999b6967a9f09714e136528bc86cc880a45633cd85dec5428e41e902b5e3a3809f3cd17204302668
languageName: node
linkType: hard

Expand Down

0 comments on commit ef9f45a

Please sign in to comment.