diff --git a/.changeset/khaki-cheetahs-provide.md b/.changeset/khaki-cheetahs-provide.md new file mode 100644 index 00000000..29af2bdc --- /dev/null +++ b/.changeset/khaki-cheetahs-provide.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-openapi': minor +--- + +feat: bump `zod-to-openapi` supports ESM diff --git a/packages/zod-openapi/.yarn/install-state.gz b/packages/zod-openapi/.yarn/install-state.gz index 10a1ddc8..43018591 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 cf125812..b684a819 100644 --- a/packages/zod-openapi/package.json +++ b/packages/zod-openapi/package.json @@ -42,16 +42,16 @@ }, "devDependencies": { "@cloudflare/workers-types": "^4.20240117.0", - "hono": "^3.11.7", + "hono": "^4.2.2", "jest": "^29.7.0", "openapi3-ts": "^4.1.2", "tsup": "^8.0.1", - "typescript": "^5.3.3", - "vitest": "^1.0.1", + "typescript": "^5.4.4", + "vitest": "^1.4.0", "zod": "^3.22.1" }, "dependencies": { - "@asteasolutions/zod-to-openapi": "^5.5.0", + "@asteasolutions/zod-to-openapi": "^7.0.0", "@hono/zod-validator": "0.2.1" }, "engines": { diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index 1e4cc3f2..887fafcf 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -76,7 +76,10 @@ type InputTypeJson = R['request'] extends RequestTypes ? R['request']['body']['content'] extends ZodContentObject ? IsJson extends never ? {} - : R['request']['body']['content'][keyof R['request']['body']['content']]['schema'] extends ZodSchema + : R['request']['body']['content'][keyof R['request']['body']['content']] extends Record< + 'schema', + ZodSchema + > ? { in: { json: z.input< @@ -99,7 +102,10 @@ type InputTypeForm = R['request'] extends RequestTypes ? R['request']['body']['content'] extends ZodContentObject ? IsForm extends never ? {} - : R['request']['body']['content'][keyof R['request']['body']['content']]['schema'] extends ZodSchema + : R['request']['body']['content'][keyof R['request']['body']['content']] extends Record< + 'schema', + ZodSchema + > ? { in: { form: z.input< @@ -127,7 +133,7 @@ type OutputType = R['responses'] extends Record extends never ? {} - : C['content'][keyof C['content']]['schema'] extends ZodSchema + : C['content'][keyof C['content']] extends Record<'schema', ZodSchema> ? z.infer : {} : {} @@ -250,7 +256,11 @@ export class OpenAPIHono< : HandlerAllResponse> >, hook: Hook> | undefined = this.defaultHook - ): OpenAPIHono, I['in'], OutputType>, BasePath> => { + ): OpenAPIHono< + E, + S & ToSchema, I['in'], OutputType>, + BasePath + > => { this.openAPIRegistry.registerPath(route) const validators: MiddlewareHandler[] = [] @@ -279,22 +289,23 @@ export class OpenAPIHono< if (bodyContent) { for (const mediaType of Object.keys(bodyContent)) { + if (!bodyContent[mediaType]) { + continue + } + const schema = (bodyContent[mediaType] as ZodMediaTypeObject)['schema'] + if (!(schema instanceof ZodType)) { + continue + } if (mediaType.startsWith('application/json')) { - const schema = bodyContent[mediaType]['schema'] - if (schema instanceof ZodType) { - const validator = zValidator('json', schema as any, hook as any) - validators.push(validator as any) - } + const validator = zValidator('json', schema, hook as any) + validators.push(validator as any) } if ( mediaType.startsWith('multipart/form-data') || mediaType.startsWith('application/x-www-form-urlencoded') ) { - const schema = bodyContent[mediaType]['schema'] - if (schema instanceof ZodType) { - const validator = zValidator('form', schema as any, hook as any) - validators.push(validator as any) - } + const validator = zValidator('form', schema, hook as any) + validators.push(validator as any) } } } @@ -409,7 +420,7 @@ export class OpenAPIHono< } basePath(path: SubPath): OpenAPIHono> { - return new OpenAPIHono({...super.basePath(path) as any, defaultHook: this.defaultHook}) + return new OpenAPIHono({ ...(super.basePath(path) as any), defaultHook: this.defaultHook }) } } diff --git a/packages/zod-openapi/test/index.test-d.ts b/packages/zod-openapi/test/index.test-d.ts index 03051e3a..062c04be 100644 --- a/packages/zod-openapi/test/index.test-d.ts +++ b/packages/zod-openapi/test/index.test-d.ts @@ -70,7 +70,7 @@ describe('Types', () => { '/' > expectTypeOf(appRoutes).toMatchTypeOf() - }); + }) }) describe('Input types', () => { @@ -83,7 +83,7 @@ describe('Input types', () => { name: 'id', in: 'path', }, - example: 123, + example: '123', }), }) @@ -96,7 +96,7 @@ describe('Input types', () => { name: 'age', in: 'query', }, - example: 42, + example: '42', }), }) @@ -162,7 +162,7 @@ describe('Input types', () => { const { sex } = c.req.valid('json') assertType<'male' | 'female'>(sex) - return c.jsonT({ + return c.json({ id, age, sex, diff --git a/packages/zod-openapi/test/index.test.ts b/packages/zod-openapi/test/index.test.ts index 8a86d897..3f24e1b5 100644 --- a/packages/zod-openapi/test/index.test.ts +++ b/packages/zod-openapi/test/index.test.ts @@ -561,7 +561,7 @@ describe('Input types', () => { name: 'id', in: 'path', }, - example: 123, + example: '123', }), }) @@ -574,7 +574,7 @@ describe('Input types', () => { name: 'age', in: 'query', }, - example: 42, + example: '42', }), }) @@ -704,7 +704,7 @@ describe('Routers', () => { }) it('Should include definitions from nested routers', async () => { const router = new OpenAPIHono().openapi(route, (ctx) => { - return ctx.jsonT({ id: 123 }) + return ctx.json({ id: 123 }) }) router.openAPIRegistry.register('Id', z.number()) @@ -827,7 +827,7 @@ describe('basePath()', () => { it('Should retain defaultHook of the parent app', async () => { const defaultHook = () => {} const app = new OpenAPIHono({ - defaultHook + defaultHook, }).basePath('/api') expect(app.defaultHook).toBeDefined() expect(app.defaultHook).toBe(defaultHook) diff --git a/packages/zod-openapi/yarn.lock b/packages/zod-openapi/yarn.lock index 7a9f7d08..f11fb5f9 100644 --- a/packages/zod-openapi/yarn.lock +++ b/packages/zod-openapi/yarn.lock @@ -15,14 +15,14 @@ __metadata: languageName: node linkType: hard -"@asteasolutions/zod-to-openapi@npm:^5.5.0": - version: 5.5.0 - resolution: "@asteasolutions/zod-to-openapi@npm:5.5.0" +"@asteasolutions/zod-to-openapi@npm:^7.0.0": + version: 7.0.0 + resolution: "@asteasolutions/zod-to-openapi@npm:7.0.0" dependencies: openapi3-ts: "npm:^4.1.2" peerDependencies: zod: ^3.20.2 - checksum: 3cac318d4bf2efe88175002d0ee66a299207c29dbb3dd5f353b1fcf46bb7117e9f203c6ae7093d740182c94fd625703bf2ac1d721a6c1d64adfc977d0f00d909 + checksum: 120499b6d997bd3fa21b4e6600f42d6b9201e2a85a7656b722a02c712d6a9fd7c474bf62ad31f76646fbd129d75331408ad3ed5c350c1436856ef306966a33e1 languageName: node linkType: hard @@ -591,15 +591,15 @@ __metadata: version: 0.0.0-use.local resolution: "@hono/zod-openapi@workspace:." dependencies: - "@asteasolutions/zod-to-openapi": "npm:^5.5.0" + "@asteasolutions/zod-to-openapi": "npm:^7.0.0" "@cloudflare/workers-types": "npm:^4.20240117.0" "@hono/zod-validator": "npm:0.2.1" - hono: "npm:^3.11.7" + hono: "npm:^4.2.2" jest: "npm:^29.7.0" openapi3-ts: "npm:^4.1.2" tsup: "npm:^8.0.1" - typescript: "npm:^5.3.3" - vitest: "npm:^1.0.1" + typescript: "npm:^5.4.4" + vitest: "npm:^1.4.0" zod: "npm:^3.22.1" peerDependencies: hono: ">=3.11.3" @@ -1209,57 +1209,57 @@ __metadata: languageName: node linkType: hard -"@vitest/expect@npm:1.2.2": - version: 1.2.2 - resolution: "@vitest/expect@npm:1.2.2" +"@vitest/expect@npm:1.4.0": + version: 1.4.0 + resolution: "@vitest/expect@npm:1.4.0" dependencies: - "@vitest/spy": "npm:1.2.2" - "@vitest/utils": "npm:1.2.2" + "@vitest/spy": "npm:1.4.0" + "@vitest/utils": "npm:1.4.0" chai: "npm:^4.3.10" - checksum: 920e80b956d9d5ef7909cbe2bf883c8556da11c5123ea037396cb672d7038116c9773bd36915a3df7be2ffd76b661d5a6487e7e5ded78f39e2500cb36ae81e59 + checksum: 2d6a657afc674adb78ad6609ecf61a94355b080cf90f922e05193b5b33b37d486c9b66a52270f1f367c16d626bcb8323368519dae096a992190898e03280b5e0 languageName: node linkType: hard -"@vitest/runner@npm:1.2.2": - version: 1.2.2 - resolution: "@vitest/runner@npm:1.2.2" +"@vitest/runner@npm:1.4.0": + version: 1.4.0 + resolution: "@vitest/runner@npm:1.4.0" dependencies: - "@vitest/utils": "npm:1.2.2" + "@vitest/utils": "npm:1.4.0" p-limit: "npm:^5.0.0" pathe: "npm:^1.1.1" - checksum: 25a9c03cca5b40738fe606757b14ee9d60d25193115b4674e3cc402c2b2c3844d234902d48bfa7646cb205455ea27891fef96733e033a570b85fe74ed29ff81c + checksum: 87a5bdde5c48e3258ecd2716994da20c8eec63acaf63a0db724513a42701bc644728009a7301d78b8775d8004c7ce1ddb8bde6495066d864c532bc117783aa91 languageName: node linkType: hard -"@vitest/snapshot@npm:1.2.2": - version: 1.2.2 - resolution: "@vitest/snapshot@npm:1.2.2" +"@vitest/snapshot@npm:1.4.0": + version: 1.4.0 + resolution: "@vitest/snapshot@npm:1.4.0" dependencies: magic-string: "npm:^0.30.5" pathe: "npm:^1.1.1" pretty-format: "npm:^29.7.0" - checksum: 0f8a69a289aa6466c7dd56f8327190d56a0bc7ad10412127de001c94784f6dba5e5bccb757def21f565f4efa3e00c307b92e8b6c302f11fc57889b743ba18a95 + checksum: 6f089d1dbe43556779479bc309b0a8fc7e0229843c40efb4dabcf99ccf9a6fa859dd38c13674616a955801442730aca55151cbd52bb22d41d9a335060e03759b languageName: node linkType: hard -"@vitest/spy@npm:1.2.2": - version: 1.2.2 - resolution: "@vitest/spy@npm:1.2.2" +"@vitest/spy@npm:1.4.0": + version: 1.4.0 + resolution: "@vitest/spy@npm:1.4.0" dependencies: tinyspy: "npm:^2.2.0" - checksum: 5480048d26c0d82b524317552fbdcc05fed6ea626d887620647826453a344798a360f2a75af477512a1569b1b6c918eae62338e8b35575f875fc2d7ef51419f3 + checksum: 847bc3085d0aa2e039aa42d803cf2dc94596aab3a63de7d364933d24ed9e0781b7d3d4bd222df202f92bae83e9c37b2893b9f24a2de7d83b6930b7b1acf43516 languageName: node linkType: hard -"@vitest/utils@npm:1.2.2": - version: 1.2.2 - resolution: "@vitest/utils@npm:1.2.2" +"@vitest/utils@npm:1.4.0": + version: 1.4.0 + resolution: "@vitest/utils@npm:1.4.0" dependencies: diff-sequences: "npm:^29.6.3" estree-walker: "npm:^3.0.3" loupe: "npm:^2.3.7" pretty-format: "npm:^29.7.0" - checksum: 32449cb7eca8ecea56e0fce280c9770f65fa6b60bbba73be06ca2891096818899b4b3220bd3c815df8beb4266034db394fcf235e4de8959cce686b8b360948d1 + checksum: cfa352484f0ea2614444a94fc35979bea94fac64e9756238c685ae74bcd027893a1798b9d6d92c1cdd454b1f7f08f453d0cca108274f0449b6f5efd345822a4c languageName: node linkType: hard @@ -1277,7 +1277,7 @@ __metadata: languageName: node linkType: hard -"acorn@npm:^8.10.0, acorn@npm:^8.11.3": +"acorn@npm:^8.11.3": version: 8.11.3 resolution: "acorn@npm:8.11.3" bin: @@ -2379,10 +2379,10 @@ __metadata: languageName: node linkType: hard -"hono@npm:^3.11.7": - version: 3.12.8 - resolution: "hono@npm:3.12.8" - checksum: 98450510d4e91a1f2ceec6c7d4e7e01c5dd81e9ff1fc68601fcfa64dd95e01e09a27e30a109004d7395f0aaac5a2ba6f975d7350c7c53053f85d8a5ad4cd07b1 +"hono@npm:^4.2.2": + version: 4.2.2 + resolution: "hono@npm:4.2.2" + checksum: 4f423ff808c105341d2802192abe9070c5f6fc4a45da51842a5f5f2111b4c333ef4aea2720a585e16996944cc0cedf68657c57df1d150f1382f21fc18e814b42 languageName: node linkType: hard @@ -3128,6 +3128,13 @@ __metadata: languageName: node linkType: hard +"js-tokens@npm:^9.0.0": + version: 9.0.0 + resolution: "js-tokens@npm:9.0.0" + checksum: 4ad1c12f47b8c8b2a3a99e29ef338c1385c7b7442198a425f3463f3537384dab6032012791bfc2f056ea5ecdb06b1ed4f70e11a3ab3f388d3dcebfe16a52b27d + languageName: node + linkType: hard + "js-yaml@npm:^3.13.1": version: 3.14.1 resolution: "js-yaml@npm:3.14.1" @@ -4292,12 +4299,12 @@ __metadata: languageName: node linkType: hard -"strip-literal@npm:^1.3.0": - version: 1.3.0 - resolution: "strip-literal@npm:1.3.0" +"strip-literal@npm:^2.0.0": + version: 2.1.0 + resolution: "strip-literal@npm:2.1.0" dependencies: - acorn: "npm:^8.10.0" - checksum: 3c0c9ee41eb346e827eede61ef288457f53df30e3e6ff8b94fa81b636933b0c13ca4ea5c97d00a10d72d04be326da99ac819f8769f0c6407ba8177c98344a916 + js-tokens: "npm:^9.0.0" + checksum: bc8b8c8346125ae3c20fcdaf12e10a498ff85baf6f69597b4ab2b5fbf2e58cfd2827f1a44f83606b852da99a5f6c8279770046ddea974c510c17c98934c9cc24 languageName: node linkType: hard @@ -4518,23 +4525,23 @@ __metadata: languageName: node linkType: hard -"typescript@npm:^5.3.3": - version: 5.3.3 - resolution: "typescript@npm:5.3.3" +"typescript@npm:^5.4.4": + version: 5.4.4 + resolution: "typescript@npm:5.4.4" bin: tsc: bin/tsc tsserver: bin/tsserver - checksum: e33cef99d82573624fc0f854a2980322714986bc35b9cb4d1ce736ed182aeab78e2cb32b385efa493b2a976ef52c53e20d6c6918312353a91850e2b76f1ea44f + checksum: 4d8de0291204ed61ca97ad0cba2ce064e09c4988ca1c451c787e4653ba76296ba35177a52694e8a00cf4ef899d0ee83338663b926d8b7d55167ff0ba81549999 languageName: node linkType: hard -"typescript@patch:typescript@npm%3A^5.3.3#optional!builtin": - version: 5.3.3 - resolution: "typescript@patch:typescript@npm%3A5.3.3#optional!builtin::version=5.3.3&hash=e012d7" +"typescript@patch:typescript@npm%3A^5.4.4#optional!builtin": + version: 5.4.4 + resolution: "typescript@patch:typescript@npm%3A5.4.4#optional!builtin::version=5.4.4&hash=e012d7" bin: tsc: bin/tsc tsserver: bin/tsserver - checksum: 1d0a5f4ce496c42caa9a30e659c467c5686eae15d54b027ee7866744952547f1be1262f2d40de911618c242b510029d51d43ff605dba8fb740ec85ca2d3f9500 + checksum: 413a0714a2b554bef8d39a386e5d25df22c625dd677d7260c1a08625016e46ce5f79dae0812641a22ab37c905370b407dc6838e11d4f10845821127d42221876 languageName: node linkType: hard @@ -4595,9 +4602,9 @@ __metadata: languageName: node linkType: hard -"vite-node@npm:1.2.2": - version: 1.2.2 - resolution: "vite-node@npm:1.2.2" +"vite-node@npm:1.4.0": + version: 1.4.0 + resolution: "vite-node@npm:1.4.0" dependencies: cac: "npm:^6.7.14" debug: "npm:^4.3.4" @@ -4606,7 +4613,7 @@ __metadata: vite: "npm:^5.0.0" bin: vite-node: vite-node.mjs - checksum: 39a5b9d9c806a012aab208eee0f59e4e12446ec19a4cf149a6459e7ff86491c289e189fda4f55a63b7e37d713f5edbda0e9efed95af4f7ebefa6d39eee093c0b + checksum: bc8eb01dd03c2cc306be2bf35efe789d6a3e8ca1d89d635d3154a9af0213f7609c94ef849f30a01f04535b31e729aee49468275e267693a42c32845fbd2a6721 languageName: node linkType: hard @@ -4650,17 +4657,16 @@ __metadata: languageName: node linkType: hard -"vitest@npm:^1.0.1": - version: 1.2.2 - resolution: "vitest@npm:1.2.2" +"vitest@npm:^1.4.0": + version: 1.4.0 + resolution: "vitest@npm:1.4.0" dependencies: - "@vitest/expect": "npm:1.2.2" - "@vitest/runner": "npm:1.2.2" - "@vitest/snapshot": "npm:1.2.2" - "@vitest/spy": "npm:1.2.2" - "@vitest/utils": "npm:1.2.2" + "@vitest/expect": "npm:1.4.0" + "@vitest/runner": "npm:1.4.0" + "@vitest/snapshot": "npm:1.4.0" + "@vitest/spy": "npm:1.4.0" + "@vitest/utils": "npm:1.4.0" acorn-walk: "npm:^8.3.2" - cac: "npm:^6.7.14" chai: "npm:^4.3.10" debug: "npm:^4.3.4" execa: "npm:^8.0.1" @@ -4669,17 +4675,17 @@ __metadata: pathe: "npm:^1.1.1" picocolors: "npm:^1.0.0" std-env: "npm:^3.5.0" - strip-literal: "npm:^1.3.0" + strip-literal: "npm:^2.0.0" tinybench: "npm:^2.5.1" tinypool: "npm:^0.8.2" vite: "npm:^5.0.0" - vite-node: "npm:1.2.2" + vite-node: "npm:1.4.0" why-is-node-running: "npm:^2.2.2" peerDependencies: "@edge-runtime/vm": "*" "@types/node": ^18.0.0 || >=20.0.0 - "@vitest/browser": ^1.0.0 - "@vitest/ui": ^1.0.0 + "@vitest/browser": 1.4.0 + "@vitest/ui": 1.4.0 happy-dom: "*" jsdom: "*" peerDependenciesMeta: @@ -4697,7 +4703,7 @@ __metadata: optional: true bin: vitest: vitest.mjs - checksum: 085cb62146191b32dc98fac1a5b0de6d1c63c44cc1e7946a7d38309dd4135539432ec27b4bfad38ce79736688a0ce20d9b93f58de4ce4a41677cb3c5ca6ad980 + checksum: 732ce229341f6777350d36020dc00ccf5dd2ac0da39424cf5c9f6f4116ed1b6f7bb56de5a11270c693214d817b6d121d3d326e8f5a73437ec3f4c65aa07e1f52 languageName: node linkType: hard