Skip to content

Commit fe07125

Browse files
committed
fix: take last value in query params
1 parent 8930dab commit fe07125

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ describe('MatcherPatternQueryParam', () => {
1919
expect(matcher.match({ user_id: 'abc123' })).toEqual({ userId: 'abc123' })
2020
})
2121

22-
it('takes first value from array', () => {
22+
it('takes last value from array', () => {
2323
const matcher = new MatcherPatternQueryParam(
2424
'userId',
2525
'user_id',
2626
'value',
2727
PARAM_PARSER_DEFAULTS
2828
)
2929
expect(matcher.match({ user_id: ['first', 'second'] })).toEqual({
30-
userId: 'first',
30+
userId: 'second',
3131
})
3232
})
3333

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ export class MatcherPatternQueryParam<T, ParamName extends string>
3232
match(query: MatcherQueryParams): Record<ParamName, T> {
3333
const queryValue: MatcherQueryParamsValue | undefined = query[this.queryKey]
3434

35-
// Check if query param is missing for default value handling
36-
35+
// normalize the value coming from the query based on the expected format
36+
// value => keep the last value if multiple
37+
// array => null becomes [], single value becomes [value]
3738
let valueBeforeParse =
3839
this.format === 'value'
3940
? Array.isArray(queryValue)
40-
? queryValue[0]
41+
? queryValue.at(-1)
4142
: queryValue
4243
: // format === 'array'
4344
Array.isArray(queryValue)

0 commit comments

Comments
 (0)