Skip to content

Commit

Permalink
fix(types): support optional path parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 22, 2024
1 parent 8ddb4ab commit 9bc2bae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/utils/matching/matchRequestUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { normalizePath } from './normalizePath'

export type Path = string | RegExp
export type PathParams<KeyType extends keyof any = string> = {
[ParamName in KeyType]: string | ReadonlyArray<string>
[ParamName in KeyType]?: string | ReadonlyArray<string>
}

export interface Match {
Expand Down
22 changes: 22 additions & 0 deletions test/typings/http.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Params>('/user/:a/:b/:b', ({ params }) => {
Expand Down

0 comments on commit 9bc2bae

Please sign in to comment.