Skip to content

Commit 700deca

Browse files
committed
fix(experimental): remove query param set to undefined
1 parent 75d57a6 commit 700deca

2 files changed

Lines changed: 49 additions & 9 deletions

File tree

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { toValue } from 'vue'
22
import type {
3-
EmptyParams,
43
MatcherParamsFormatted,
54
MatcherPattern,
65
MatcherQueryParams,
@@ -114,14 +113,12 @@ export class MatcherPatternQueryParam<
114113
build(params: Record<ParamName, T>): MatcherQueryParams {
115114
const paramValue = params[this.paramName]
116115

117-
if (paramValue === undefined) {
118-
return {} as EmptyParams
119-
}
120-
121116
return {
122-
[this.queryKey]: (this.parser.set ?? PARAM_PARSER_DEFAULTS.set)(
123-
paramValue as any
124-
),
117+
// undefined removes the key from the current query
118+
[this.queryKey]:
119+
paramValue === undefined
120+
? undefined
121+
: (this.parser.set ?? PARAM_PARSER_DEFAULTS.set)(paramValue as any),
125122
}
126123
}
127124
}

‎packages/router/src/experimental/route-resolver/resolver-fixed.spec.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import type {
1010
MatcherQueryParams,
1111
} from './matchers/matcher-pattern'
1212
import { MatcherPatternPathStatic } from './matchers/matcher-pattern'
13-
import type { MatcherPatternQuery } from './matchers/matcher-pattern-query'
13+
import {
14+
MatcherPatternQueryParam,
15+
type MatcherPatternQuery,
16+
} from './matchers/matcher-pattern-query'
17+
import { PARAM_PARSER_INT } from './matchers/param-parsers'
1418
import {
1519
EMPTY_PATH_PATTERN_MATCHER,
1620
USER_ID_PATH_PATTERN_MATCHER,
@@ -460,6 +464,45 @@ describe('fixed resolver', () => {
460464
fullPath: '/?existing=value&page=10#current',
461465
})
462466
})
467+
468+
describe('removes a query param set to undefined', () => {
469+
const resolver = createFixedResolver([
470+
{
471+
name: 'list',
472+
path: EMPTY_PATH_PATTERN_MATCHER,
473+
query: [
474+
new MatcherPatternQueryParam(
475+
'page',
476+
'page',
477+
'value',
478+
PARAM_PARSER_INT,
479+
1
480+
),
481+
new MatcherPatternQueryParam('q', 'q', 'value'),
482+
],
483+
hash: ANY_HASH_PATTERN_MATCHER,
484+
},
485+
])
486+
const current = resolver.resolve('/?page=5&q=a&other=x#h')
487+
488+
it('with a default', () => {
489+
expect(
490+
resolver.resolve({ params: { page: undefined } }, current)
491+
).toMatchObject({
492+
fullPath: '/?q=a&other=x#h',
493+
params: { page: 1, q: 'a' },
494+
})
495+
})
496+
497+
it('without a default', () => {
498+
expect(
499+
resolver.resolve({ params: { q: undefined } }, current)
500+
).toMatchObject({
501+
fullPath: '/?page=5&other=x#h',
502+
params: { page: 5, q: undefined },
503+
})
504+
})
505+
})
463506
})
464507

465508
describe('absolute locations', () => {

0 commit comments

Comments
 (0)