Skip to content

Commit

Permalink
fix(zod-openapi): support openapi yaml with middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunyel committed Jun 3, 2024
1 parent 1b641be commit 626a84a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-rings-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': minor
---

Fix OpenAPI yaml with route middleware
1 change: 1 addition & 0 deletions packages/zod-openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"tsup": "^8.0.1",
"typescript": "^5.4.4",
"vitest": "^1.4.0",
"yaml": "^2.4.3",
"zod": "^3.22.1"
},
"dependencies": {
Expand Down
10 changes: 5 additions & 5 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
80 changes: 61 additions & 19 deletions packages/zod-openapi/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -1489,27 +1491,67 @@ 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 () => {
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();
})
});
10 changes: 10 additions & 0 deletions packages/zod-openapi/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ __metadata:
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"
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,7 @@ __metadata:
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"
Expand Down Expand Up @@ -19176,6 +19177,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"
Expand Down

0 comments on commit 626a84a

Please sign in to comment.