Skip to content

Commit c8c7de3

Browse files
committed
fix: in non strictQueryParams mode, serialise extra query params to returned matched params
1 parent bcd2ce9 commit c8c7de3

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

modules/RouteNode.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Path from 'path-parser';
2-
import { getSearch, getPath, omit, withoutBrackets } from 'search-params';
2+
import { getSearch, getPath, omit, withoutBrackets, parse } from 'search-params';
33

44
const noop = () => {};
55

@@ -158,10 +158,14 @@ export default class RouteNode {
158158
segments.push(child);
159159
Object.keys(match).forEach(param => segments.params[param] = match[param]);
160160

161-
if (!isRoot && (!remainingPath.length || // fully matched
162-
!strictQueryParams && remainingPath.indexOf('?') === 0) // unmatched queryParams in non strict mode
163-
) {
164-
return segments;
161+
if (!isRoot && !remainingPath.length) { // fully matched
162+
return segments;
163+
}
164+
if (!isRoot && !strictQueryParams && remainingPath.indexOf('?') === 0) { // unmatched queryParams in non strict mode
165+
const remainingQueryParams = parse(remainingPath.slice(1));
166+
167+
remainingQueryParams.forEach(({ name, value} ) => segments.params[name] = value);
168+
return segments;
165169
}
166170
// If no children to match against but unmatched path left
167171
if (!child.children.length) {

test/main.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ describe('RouteNode', function () {
235235
}
236236
},
237237
name: 'grandParent',
238-
params: {nickname: 'gran'}
238+
params: {nickname: 'gran', name: 'papa'}
239239
});
240240
node.matchPath('/grand-parent/parent/child?nickname=gran&names=papa-maman', { strictQueryParams: false }).should.eql({
241241
_meta: {
@@ -250,7 +250,7 @@ describe('RouteNode', function () {
250250
}
251251
},
252252
name: 'grandParent.parent.child',
253-
params: {nickname: 'gran'}
253+
params: {nickname: 'gran', names: 'papa-maman'}
254254
});
255255
});
256256

@@ -397,6 +397,17 @@ describe('RouteNode', function () {
397397
node.setPath('?param');
398398
node.buildPath('route', { param: '1' }).should.equal('/path?param=1');
399399
});
400+
401+
it('should serialise extra search parameters', function () {
402+
var node = new RouteNode('', '', [
403+
new RouteNode('route', '/path')
404+
]);
405+
406+
withoutMeta(node.matchPath('/path?a=1&b=2&c=3', { strictQueryParams: false })).should.eql({
407+
name: 'route',
408+
params: { a: '1', b: '2', c: '3' }
409+
});
410+
});
400411
});
401412

402413

0 commit comments

Comments
 (0)