From 9bc2bae8c23cf108fc72c246509a35c39a2d7318 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Fri, 22 Nov 2024 18:44:04 +0100 Subject: [PATCH] fix(types): support optional path parameters --- src/core/utils/matching/matchRequestUrl.ts | 2 +- test/typings/http.test-d.ts | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/core/utils/matching/matchRequestUrl.ts b/src/core/utils/matching/matchRequestUrl.ts index 5ea0115d4..bb3d0c326 100644 --- a/src/core/utils/matching/matchRequestUrl.ts +++ b/src/core/utils/matching/matchRequestUrl.ts @@ -4,7 +4,7 @@ import { normalizePath } from './normalizePath' export type Path = string | RegExp export type PathParams = { - [ParamName in KeyType]: string | ReadonlyArray + [ParamName in KeyType]?: string | ReadonlyArray } export interface Match { diff --git a/test/typings/http.test-d.ts b/test/typings/http.test-d.ts index f40397c1a..e6f1cb348 100644 --- a/test/typings/http.test-d.ts +++ b/test/typings/http.test-d.ts @@ -7,6 +7,28 @@ it('supports a single path parameter', () => { }) }) +it('supports a repeating path parameter', () => { + http.get<{ id?: string }>('/user/id*', ({ params }) => { + expectTypeOf(params).toEqualTypeOf<{ id?: string }>() + }) +}) + +it('supports an optional path parameter', () => { + http.get<{ id?: string }>('/user/:id?', ({ params }) => { + expectTypeOf(params).toEqualTypeOf<{ id?: string }>() + }) +}) + +it('supports optional repeating path parameter', () => { + /** + * @note This is the newest "path-to-regexp" syntax. + * MSW doesn't support this quite yet. + */ + http.get<{ path?: string[] }>('/user{/*path}', ({ params }) => { + expectTypeOf(params).toEqualTypeOf<{ path?: string[] }>() + }) +}) + it('supports multiple path parameters', () => { type Params = { a: string; b: string[] } http.get('/user/:a/:b/:b', ({ params }) => {