Skip to content

Commit

Permalink
feat(zod-openapi): Add 'middleware' property for route (#435)
Browse files Browse the repository at this point in the history
* feat: Add 'middlewares' property for route

* fix: Rename `middlewares` to `middleware`

* fix: Update README.md

* fix: Fix changeset
  • Loading branch information
RomanNabukhotnyi authored Apr 11, 2024
1 parent 56b1166 commit 4660092
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-plums-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': minor
---

Add 'middleware' property for route
26 changes: 26 additions & 0 deletions packages/zod-openapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,32 @@ app.use(route.getRoutingPath(), prettyJSON(), cache({ cacheName: 'my-cache' }))
app.openapi(route, handler)
```

Or you can use the `middleware` property in the route definition.

```ts
const route = createRoute({
method: 'get',
path: '/users/{id}',
request: {
params: ParamsSchema,
},
middleware: [
prettyJSON(),
cache({ cacheName: 'my-cache' })
],
responses: {
200: {
content: {
'application/json': {
schema: UserSchema,
},
},
description: 'Retrieve the user',
},
},
})
```

### RPC Mode

Zod OpenAPI Hono supports Hono's RPC mode. You can define types for the Hono Client as follows:
Expand Down
10 changes: 8 additions & 2 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import type {
ResponseConfig,
RouteConfig,
RouteConfig as RouteConfigBase,
ZodContentObject,
ZodMediaTypeObject,
ZodRequestBody,
Expand Down Expand Up @@ -32,6 +32,10 @@ import { mergePath } from 'hono/utils/url'
import type { AnyZodObject, ZodSchema, ZodError } from 'zod'
import { z, ZodType } from 'zod'

type RouteConfig = RouteConfigBase & {
middleware?: MiddlewareHandler | MiddlewareHandler[]
}

type RequestTypes = {
body?: ZodRequestBody
params?: AnyZodObject
Expand Down Expand Up @@ -310,7 +314,9 @@ export class OpenAPIHono<
}
}

this.on([route.method], route.path.replaceAll(/\/{(.+?)}/g, '/:$1'), ...validators, handler)
const middleware = route.middleware ? (Array.isArray(route.middleware) ? route.middleware : [route.middleware]) : []

this.on([route.method], route.path.replaceAll(/\/{(.+?)}/g, '/:$1'), ...middleware, ...validators, handler)
return this
}

Expand Down
26 changes: 26 additions & 0 deletions packages/zod-openapi/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1361,3 +1361,29 @@ describe('Handle "Conflicting names for parameter"', () => {
})
})
})

describe('Middleware', () => {
const app = new OpenAPIHono()
app.openapi(
createRoute({
method: 'get',
path: '/books',
middleware: [(c, next) => {
c.header('x-foo', 'bar')
return next()
}],
responses: {
200: {
description: 'response',
},
},
}),
(c) => c.text('foo')
)

it('Should have the header set by the middleware', async () => {
const res = await app.request('/books')
expect(res.status).toBe(200)
expect(res.headers.get('x-foo')).toBe('bar')
})
})

0 comments on commit 4660092

Please sign in to comment.