fix(zod-openapi): support multiple params (#155)

* fix(zod-openapi): support multiple params

* changeset
pull/156/head
Yusuke Wada 2023-09-11 11:44:47 +09:00 committed by GitHub
parent be7d4a6916
commit 804caac191
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 2 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---
fix(zod-openapi): support multiple params

View File

@ -219,7 +219,7 @@ export class OpenAPIHono<
}
}
this.on([route.method], route.path.replace(/\/{(.+)}/, '/:$1'), ...validators, handler)
this.on([route.method], route.path.replaceAll(/\/{(.+?)}/g, '/:$1'), ...validators, handler)
return this
}

View File

@ -576,3 +576,43 @@ describe('Types', () => {
expectTypeOf(appRoutes).toMatchTypeOf<H>
})
})
describe('Multi params', () => {
const ParamsSchema = z.object({
id: z.string(),
tagName: z.string(),
})
const route = createRoute({
method: 'get',
path: '/users/{id}/tags/{tagName}',
request: {
params: ParamsSchema,
},
responses: {
200: {
// eslint-disable-next-line quotes
description: "Get the user's tag",
},
},
})
const app = new OpenAPIHono()
app.openapi(route, (c) => {
const { id, tagName } = c.req.valid('param')
return c.jsonT({
id,
tagName,
})
})
it('Should return 200 response with correct contents', async () => {
const res = await app.request('/users/123/tags/baseball')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
id: '123',
tagName: 'baseball',
})
})
})

View File

@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2020",
"target": "ES2022",
"module": "commonjs",
"declaration": true,
"moduleResolution": "Node",