fix(zod-openapi): support openapi yaml with middleware (#557)

pull/558/head
arjunyel 2024-06-04 09:48:47 -05:00 committed by GitHub
parent 1b641bee14
commit 69e5364464
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 100 additions and 42 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---
Fix OpenAPI yaml with route middleware

View File

@ -48,11 +48,12 @@
"tsup": "^8.0.1", "tsup": "^8.0.1",
"typescript": "^5.4.4", "typescript": "^5.4.4",
"vitest": "^1.4.0", "vitest": "^1.4.0",
"yaml": "^2.4.3",
"zod": "^3.22.1" "zod": "^3.22.1"
}, },
"dependencies": { "dependencies": {
"@asteasolutions/zod-to-openapi": "^7.0.0", "@asteasolutions/zod-to-openapi": "^7.0.0",
"@hono/zod-validator": "0.2.1" "@hono/zod-validator": "0.2.2"
}, },
"engines": { "engines": {
"node": ">=16.0.0" "node": ">=16.0.0"

View File

@ -299,7 +299,7 @@ export class OpenAPIHono<
InputTypeJson<R>, InputTypeJson<R>,
P extends string = ConvertPathType<R['path']> P extends string = ConvertPathType<R['path']>
>( >(
route: R, {middleware: routeMiddleware, ...route}: R,
handler: Handler< handler: Handler<
E, E,
P, P,
@ -379,10 +379,10 @@ export class OpenAPIHono<
} }
} }
const middleware = route.middleware const middleware = routeMiddleware
? Array.isArray(route.middleware) ? Array.isArray(routeMiddleware)
? route.middleware ? routeMiddleware
: [route.middleware] : [routeMiddleware]
: [] : []
this.on( this.on(

View File

@ -1,10 +1,12 @@
import type { RouteConfig } from '@asteasolutions/zod-to-openapi' import type { RouteConfig } from '@asteasolutions/zod-to-openapi'
import type { Context, TypedResponse } from 'hono' import type { Context, TypedResponse } from 'hono'
import { bearerAuth } from 'hono/bearer-auth'
import { hc } from 'hono/client' import { hc } from 'hono/client'
import { describe, it, expect, expectTypeOf } from 'vitest' import { describe, it, expect, expectTypeOf } from 'vitest'
import { OpenAPIHono, createRoute, z, RouteConfigToTypedResponse } from '../src/index' import { OpenAPIHono, createRoute, z, RouteConfigToTypedResponse } from '../src/index'
import { Expect, Equal } from 'hono/utils/types' import { Expect, Equal } from 'hono/utils/types'
import { ServerErrorStatusCode } from 'hono/utils/http-status' import { ServerErrorStatusCode } from 'hono/utils/http-status'
import { stringify } from "yaml"
describe('Constructor', () => { describe('Constructor', () => {
it('Should not require init object', () => { it('Should not require init object', () => {
@ -1489,27 +1491,67 @@ describe('RouteConfigToTypedResponse', () => {
type Expected = type Expected =
| TypedResponse< | TypedResponse<
{ {
name: string name: string
age: number age: number
}, },
200, 200,
'json' 'json'
> >
| TypedResponse< | TypedResponse<
{ {
ok: boolean ok: boolean
}, },
400, 400,
'json' 'json'
> >
| TypedResponse< | TypedResponse<
{ {
ok: boolean ok: boolean
}, },
ServerErrorStatusCode, ServerErrorStatusCode,
'json' 'json'
> >
type verify = Expect<Equal<Expected, Actual>> type verify = Expect<Equal<Expected, Actual>>
}) })
}) })
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;
},
})],
responses: {
200: {
description: 'Books',
content: {
'application/json': {
schema: z.array(
z.object({
title: z.string(),
})
),
},
},
},
},
}),
(c) => c.json([{ title: 'foo' }])
)
const doc = app.getOpenAPI31Document({
openapi: '3.1.0',
info: {
title: 'My API',
version: '1.0.0',
}
});
expect(() => stringify(doc)).to.not.throw();
})
});

View File

@ -593,13 +593,14 @@ __metadata:
dependencies: dependencies:
"@asteasolutions/zod-to-openapi": "npm:^7.0.0" "@asteasolutions/zod-to-openapi": "npm:^7.0.0"
"@cloudflare/workers-types": "npm:^4.20240117.0" "@cloudflare/workers-types": "npm:^4.20240117.0"
"@hono/zod-validator": "npm:0.2.1" "@hono/zod-validator": "npm:0.2.2"
hono: "npm:^4.3.6" hono: "npm:^4.3.6"
jest: "npm:^29.7.0" jest: "npm:^29.7.0"
openapi3-ts: "npm:^4.1.2" openapi3-ts: "npm:^4.1.2"
tsup: "npm:^8.0.1" tsup: "npm:^8.0.1"
typescript: "npm:^5.4.4" typescript: "npm:^5.4.4"
vitest: "npm:^1.4.0" vitest: "npm:^1.4.0"
yaml: "npm:^2.4.3"
zod: "npm:^3.22.1" zod: "npm:^3.22.1"
peerDependencies: peerDependencies:
hono: ">=4.3.6" hono: ">=4.3.6"
@ -607,13 +608,13 @@ __metadata:
languageName: unknown languageName: unknown
linkType: soft linkType: soft
"@hono/zod-validator@npm:0.2.1": "@hono/zod-validator@npm:0.2.2":
version: 0.2.1 version: 0.2.2
resolution: "@hono/zod-validator@npm:0.2.1" resolution: "@hono/zod-validator@npm:0.2.2"
peerDependencies: peerDependencies:
hono: ">=3.9.0" hono: ">=3.9.0"
zod: ^3.19.1 zod: ^3.19.1
checksum: de399abc8ab26552111c745ae0ecad714af549e79b0412c2285d057bde72b434b09b8f9b65940d3a4b2dd82dcfb52e1601ac505effb410e4439e9c8835a99d7d checksum: 3d6d03d28287e6f05e4cf5b86f3fa5fa386429a4212881f7344fe93272a69732ca8dd98634eb51df434b002230d2be2f3c6822f3b1ab320676932583856b30a5
languageName: node languageName: node
linkType: hard linkType: hard
@ -4835,6 +4836,15 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"yaml@npm:^2.4.3":
version: 2.4.3
resolution: "yaml@npm:2.4.3"
bin:
yaml: bin.mjs
checksum: b4a9dea34265f000402c909144ac310be42c4526dfd16dff1aee2b04a0d94051713651c0cd2b0a3d8109266997422120f16a7934629d12f22dc215839ebbeccf
languageName: node
linkType: hard
"yargs-parser@npm:^21.1.1": "yargs-parser@npm:^21.1.1":
version: 21.1.1 version: 21.1.1
resolution: "yargs-parser@npm:21.1.1" resolution: "yargs-parser@npm:21.1.1"

View File

@ -2168,13 +2168,14 @@ __metadata:
dependencies: dependencies:
"@asteasolutions/zod-to-openapi": "npm:^7.0.0" "@asteasolutions/zod-to-openapi": "npm:^7.0.0"
"@cloudflare/workers-types": "npm:^4.20240117.0" "@cloudflare/workers-types": "npm:^4.20240117.0"
"@hono/zod-validator": "npm:0.2.1" "@hono/zod-validator": "npm:0.2.2"
hono: "npm:^4.3.6" hono: "npm:^4.3.6"
jest: "npm:^29.7.0" jest: "npm:^29.7.0"
openapi3-ts: "npm:^4.1.2" openapi3-ts: "npm:^4.1.2"
tsup: "npm:^8.0.1" tsup: "npm:^8.0.1"
typescript: "npm:^5.4.4" typescript: "npm:^5.4.4"
vitest: "npm:^1.4.0" vitest: "npm:^1.4.0"
yaml: "npm:^2.4.3"
zod: "npm:^3.22.1" zod: "npm:^3.22.1"
peerDependencies: peerDependencies:
hono: ">=4.3.6" hono: ">=4.3.6"
@ -2182,17 +2183,7 @@ __metadata:
languageName: unknown languageName: unknown
linkType: soft linkType: soft
"@hono/zod-validator@npm:0.2.1": "@hono/zod-validator@npm:0.2.2, @hono/zod-validator@workspace:packages/zod-validator":
version: 0.2.1
resolution: "@hono/zod-validator@npm:0.2.1"
peerDependencies:
hono: ">=3.9.0"
zod: ^3.19.1
checksum: de399abc8ab26552111c745ae0ecad714af549e79b0412c2285d057bde72b434b09b8f9b65940d3a4b2dd82dcfb52e1601ac505effb410e4439e9c8835a99d7d
languageName: node
linkType: hard
"@hono/zod-validator@workspace:packages/zod-validator":
version: 0.0.0-use.local version: 0.0.0-use.local
resolution: "@hono/zod-validator@workspace:packages/zod-validator" resolution: "@hono/zod-validator@workspace:packages/zod-validator"
dependencies: dependencies:
@ -19176,6 +19167,15 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"yaml@npm:^2.4.3":
version: 2.4.3
resolution: "yaml@npm:2.4.3"
bin:
yaml: bin.mjs
checksum: b4a9dea34265f000402c909144ac310be42c4526dfd16dff1aee2b04a0d94051713651c0cd2b0a3d8109266997422120f16a7934629d12f22dc215839ebbeccf
languageName: node
linkType: hard
"yargs-parser@npm:^18.1.2, yargs-parser@npm:^18.1.3": "yargs-parser@npm:^18.1.2, yargs-parser@npm:^18.1.3":
version: 18.1.3 version: 18.1.3
resolution: "yargs-parser@npm:18.1.3" resolution: "yargs-parser@npm:18.1.3"