Skip to content

Commit 89a26bd

Browse files
committed
fix: handle missing parser
1 parent 2929584 commit 89a26bd

File tree

2 files changed

+113
-6
lines changed

2 files changed

+113
-6
lines changed

packages/router/src/experimental/route-resolver/matchers/matcher-pattern-query.spec.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,4 +400,109 @@ describe('MatcherPatternQueryParam', () => {
400400
expect(matcher.match({ other: 'value' })).toEqual({ missing: 'default' })
401401
})
402402
})
403+
404+
describe('defaultValue', () => {
405+
describe('match', () => {
406+
it('should fallback to PARAM_PARSER_DEFAULTS.get when parser.get is undefined', () => {
407+
const matcher = new MatcherPatternQueryParam(
408+
'test',
409+
'test_param',
410+
'value',
411+
{}
412+
)
413+
// Should use PARAM_PARSER_DEFAULTS.get which returns value ?? null
414+
expect(matcher.match({ test_param: 'value' })).toEqual({
415+
test: 'value',
416+
})
417+
expect(matcher.match({ test_param: null })).toEqual({ test: null })
418+
expect(matcher.match({})).toEqual({ test: undefined })
419+
})
420+
421+
it('should handle array format with missing get method', () => {
422+
const matcher = new MatcherPatternQueryParam(
423+
'test',
424+
'test_param',
425+
'array',
426+
{}
427+
)
428+
// Should use PARAM_PARSER_DEFAULTS.get which returns value ?? null
429+
expect(matcher.match({ test_param: ['a', 'b'] })).toEqual({
430+
test: ['a', 'b'],
431+
})
432+
expect(matcher.match({ test_param: 'single' })).toEqual({
433+
test: ['single'],
434+
})
435+
})
436+
437+
it('should handle both format with missing get method', () => {
438+
const matcher = new MatcherPatternQueryParam(
439+
'test',
440+
'test_param',
441+
'both',
442+
{}
443+
)
444+
// Should use PARAM_PARSER_DEFAULTS.get which returns value ?? null
445+
expect(matcher.match({ test_param: 'value' })).toEqual({
446+
test: 'value',
447+
})
448+
expect(matcher.match({ test_param: ['a', 'b'] })).toEqual({
449+
test: ['a', 'b'],
450+
})
451+
})
452+
})
453+
454+
describe('build', () => {
455+
it('should fallback to PARAM_PARSER_DEFAULTS.set when parser.set is undefined', () => {
456+
const matcher = new MatcherPatternQueryParam(
457+
'test',
458+
'test_param',
459+
'value',
460+
{}
461+
)
462+
// Should use PARAM_PARSER_DEFAULTS.set which converts to string
463+
expect(matcher.build({ test: 'value' })).toEqual({
464+
test_param: 'value',
465+
})
466+
expect(matcher.build({ test: 123 })).toEqual({ test_param: '123' })
467+
expect(matcher.build({ test: true })).toEqual({ test_param: 'true' })
468+
expect(matcher.build({ test: null })).toEqual({ test_param: null })
469+
expect(matcher.build({ test: undefined })).toEqual({})
470+
})
471+
472+
it('should handle array values with missing set method', () => {
473+
const matcher = new MatcherPatternQueryParam(
474+
'test',
475+
'test_param',
476+
'array',
477+
{}
478+
)
479+
// Should use PARAM_PARSER_DEFAULTS.set which handles arrays
480+
expect(matcher.build({ test: ['a', 'b'] })).toEqual({
481+
test_param: ['a', 'b'],
482+
})
483+
expect(matcher.build({ test: [1, 2] })).toEqual({
484+
test_param: ['1', '2'],
485+
})
486+
expect(matcher.build({ test: [1, true] })).toEqual({
487+
test_param: ['1', 'true'],
488+
})
489+
})
490+
491+
it('should handle both format with missing set method', () => {
492+
const matcher = new MatcherPatternQueryParam(
493+
'test',
494+
'test_param',
495+
'both',
496+
{}
497+
)
498+
// Should use PARAM_PARSER_DEFAULTS.set
499+
expect(matcher.build({ test: 'value' })).toEqual({
500+
test_param: 'value',
501+
})
502+
expect(matcher.build({ test: ['a', 'b'] })).toEqual({
503+
test_param: ['a', 'b'],
504+
})
505+
})
506+
})
507+
})
403508
})

packages/router/src/experimental/route-resolver/matchers/matcher-pattern-query.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
MatcherPattern,
66
MatcherQueryParams,
77
} from './matcher-pattern'
8-
import { ParamParser } from './param-parsers'
8+
import { ParamParser, PARAM_PARSER_DEFAULTS } from './param-parsers'
99

1010
/**
1111
* Handles the `query` part of a URL. It can transform a query object into an
@@ -55,7 +55,7 @@ export class MatcherPatternQueryParam<T, ParamName extends string>
5555
try {
5656
;(value as unknown[]).push(
5757
// for ts errors
58-
this.parser.get!(v)
58+
(this.parser.get ?? PARAM_PARSER_DEFAULTS.get)(v) as T
5959
)
6060
} catch (error) {
6161
// we skip the invalid value unless there is no defaultValue
@@ -75,12 +75,13 @@ export class MatcherPatternQueryParam<T, ParamName extends string>
7575
}
7676
} else {
7777
try {
78-
// FIXME: fallback to default getter
7978
value =
8079
// non existing query param should falll back to defaultValue
8180
valueBeforeParse === undefined
8281
? valueBeforeParse
83-
: this.parser.get!(valueBeforeParse)
82+
: ((this.parser.get ?? PARAM_PARSER_DEFAULTS.get)(
83+
valueBeforeParse
84+
) as T)
8485
} catch (error) {
8586
if (this.defaultValue === undefined) {
8687
throw error
@@ -103,8 +104,9 @@ export class MatcherPatternQueryParam<T, ParamName extends string>
103104
}
104105

105106
return {
106-
// FIXME: default setter
107-
[this.queryKey]: this.parser.set!(paramValue),
107+
[this.queryKey]: (this.parser.set ?? PARAM_PARSER_DEFAULTS.set)(
108+
paramValue as any
109+
),
108110
}
109111
}
110112
}

0 commit comments

Comments
 (0)