fix(zod-openapi): support multiple params (#155)
* fix(zod-openapi): support multiple params * changesetpull/156/head
parent
be7d4a6916
commit
804caac191
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@hono/zod-openapi': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(zod-openapi): support multiple params
|
|
@ -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
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -576,3 +576,43 @@ describe('Types', () => {
|
||||||
expectTypeOf(appRoutes).toMatchTypeOf<H>
|
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',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "es2020",
|
"target": "ES2022",
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"moduleResolution": "Node",
|
"moduleResolution": "Node",
|
||||||
|
|
Loading…
Reference in New Issue