diff --git a/.changeset/beige-rice-roll.md b/.changeset/beige-rice-roll.md new file mode 100644 index 00000000..56470994 --- /dev/null +++ b/.changeset/beige-rice-roll.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-openapi': patch +--- + +fix: relax types to support `.refine()` for an object diff --git a/package.json b/package.json index 5ea5f0ab..b986daf7 100644 --- a/package.json +++ b/package.json @@ -63,4 +63,4 @@ "typescript": "^5.2.2" }, "packageManager": "yarn@4.0.2" -} \ No newline at end of file +} diff --git a/packages/react-compat/package.json b/packages/react-compat/package.json index f8e5d926..4871c924 100644 --- a/packages/react-compat/package.json +++ b/packages/react-compat/package.json @@ -30,4 +30,4 @@ "hono": "4.5.0-rc.1", "tsup": "^8.0.1" } -} \ No newline at end of file +} diff --git a/packages/zod-openapi/.yarn/install-state.gz b/packages/zod-openapi/.yarn/install-state.gz index 3543b184..09de68ea 100644 Binary files a/packages/zod-openapi/.yarn/install-state.gz and b/packages/zod-openapi/.yarn/install-state.gz differ diff --git a/packages/zod-openapi/package.json b/packages/zod-openapi/package.json index a5ee7cb6..6d1b56c1 100644 --- a/packages/zod-openapi/package.json +++ b/packages/zod-openapi/package.json @@ -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", diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index 3c4f19c9..95dd29f1 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -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' @@ -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 = Promise | T @@ -48,10 +48,10 @@ export type RouteConfig = RouteConfigBase & { type RequestTypes = { body?: ZodRequestBody - params?: AnyZodObject - query?: AnyZodObject - cookies?: AnyZodObject - headers?: AnyZodObject | ZodType[] + params?: ZodType + query?: ZodType + cookies?: ZodType + headers?: ZodType | ZodType[] } type IsJson = T extends string @@ -77,7 +77,7 @@ type InputTypeBase< Part extends string, Type extends string > = R['request'] extends RequestTypes - ? RequestPart extends AnyZodObject + ? RequestPart extends ZodType ? { in: { [K in Type]: z.input> } out: { [K in Type]: z.output> } @@ -299,7 +299,7 @@ export class OpenAPIHono< InputTypeJson, P extends string = ConvertPathType >( - {middleware: routeMiddleware, ...route}: R, + { middleware: routeMiddleware, ...route }: R, handler: Handler< E, P, diff --git a/packages/zod-openapi/test/index.test.ts b/packages/zod-openapi/test/index.test.ts index 3fe43bf4..c8339ff1 100644 --- a/packages/zod-openapi/test/index.test.ts +++ b/packages/zod-openapi/test/index.test.ts @@ -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', () => { @@ -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> }) }) -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', @@ -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() }) -}); +}) diff --git a/packages/zod-openapi/yarn.lock b/packages/zod-openapi/yarn.lock index 4a98cd3a..ba7f563a 100644 --- a/packages/zod-openapi/yarn.lock +++ b/packages/zod-openapi/yarn.lock @@ -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" @@ -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