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",
"typescript": "^5.4.4",
"vitest": "^1.4.0",
"yaml": "^2.4.3",
"zod": "^3.22.1"
},
"dependencies": {
"@asteasolutions/zod-to-openapi": "^7.0.0",
"@hono/zod-validator": "0.2.1"
"@hono/zod-validator": "0.2.2"
},
"engines": {
"node": ">=16.0.0"

View File

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

View File

@ -1,10 +1,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"
describe('Constructor', () => {
it('Should not require init object', () => {
@ -1513,3 +1515,43 @@ describe('RouteConfigToTypedResponse', () => {
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:
"@asteasolutions/zod-to-openapi": "npm:^7.0.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"
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"
yaml: "npm:^2.4.3"
zod: "npm:^3.22.1"
peerDependencies:
hono: ">=4.3.6"
@ -607,13 +608,13 @@ __metadata:
languageName: unknown
linkType: soft
"@hono/zod-validator@npm:0.2.1":
version: 0.2.1
resolution: "@hono/zod-validator@npm:0.2.1"
"@hono/zod-validator@npm:0.2.2":
version: 0.2.2
resolution: "@hono/zod-validator@npm:0.2.2"
peerDependencies:
hono: ">=3.9.0"
zod: ^3.19.1
checksum: de399abc8ab26552111c745ae0ecad714af549e79b0412c2285d057bde72b434b09b8f9b65940d3a4b2dd82dcfb52e1601ac505effb410e4439e9c8835a99d7d
checksum: 3d6d03d28287e6f05e4cf5b86f3fa5fa386429a4212881f7344fe93272a69732ca8dd98634eb51df434b002230d2be2f3c6822f3b1ab320676932583856b30a5
languageName: node
linkType: hard
@ -4835,6 +4836,15 @@ __metadata:
languageName: node
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":
version: 21.1.1
resolution: "yargs-parser@npm:21.1.1"

View File

@ -2168,13 +2168,14 @@ __metadata:
dependencies:
"@asteasolutions/zod-to-openapi": "npm:^7.0.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"
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"
yaml: "npm:^2.4.3"
zod: "npm:^3.22.1"
peerDependencies:
hono: ">=4.3.6"
@ -2182,17 +2183,7 @@ __metadata:
languageName: unknown
linkType: soft
"@hono/zod-validator@npm:0.2.1":
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":
"@hono/zod-validator@npm:0.2.2, @hono/zod-validator@workspace:packages/zod-validator":
version: 0.0.0-use.local
resolution: "@hono/zod-validator@workspace:packages/zod-validator"
dependencies:
@ -19176,6 +19167,15 @@ __metadata:
languageName: node
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":
version: 18.1.3
resolution: "yargs-parser@npm:18.1.3"