From 52b170ffd26134769274930564c522413f2bc3be Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Mon, 11 Sep 2023 11:39:50 +0900 Subject: [PATCH] fix(zod-openapi): support multiple params --- packages/zod-openapi/src/index.ts | 2 +- packages/zod-openapi/test/index.test.ts | 40 +++++++++++++++++++++++++ tsconfig.json | 2 +- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index 9143dbf1..03de18cd 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -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 } diff --git a/packages/zod-openapi/test/index.test.ts b/packages/zod-openapi/test/index.test.ts index 19b20b4e..7783af59 100644 --- a/packages/zod-openapi/test/index.test.ts +++ b/packages/zod-openapi/test/index.test.ts @@ -576,3 +576,43 @@ describe('Types', () => { expectTypeOf(appRoutes).toMatchTypeOf }) }) + +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', + }) + }) +}) diff --git a/tsconfig.json b/tsconfig.json index cfd6287c..ea416791 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es2020", + "target": "ES2022", "module": "commonjs", "declaration": true, "moduleResolution": "Node",