Query string parsing capabilities for deep linking (#510)

* adds query string parsing capabilities to URI handling

* improves query params test case

* adds test with empty query value
This commit is contained in:
Norman Rzepka
2017-04-05 07:03:52 +02:00
committed by Eric Vicenti
parent 5006076352
commit d211492a4c
2 changed files with 41 additions and 3 deletions

View File

@@ -251,7 +251,9 @@ export default (
});
}
// Attempt to match `pathToResolve` with a route in this router's
const [pathNameToResolve, queryString] = pathToResolve.split('?');
// Attempt to match `pathNameToResolve` with a route in this router's
// routeConfigs
let matchedRouteName;
let pathMatch;
@@ -260,7 +262,7 @@ export default (
for (const routeName in paths) {
/* $FlowFixMe */
const { re, keys } = paths[routeName];
pathMatch = re.exec(pathToResolve);
pathMatch = re.exec(pathNameToResolve);
if (pathMatch && pathMatch.length) {
pathMatchKeys = keys;
matchedRouteName = routeName;
@@ -285,6 +287,18 @@ export default (
);
}
// reduce the items of the query string. any query params may
// be overridden by path params
const queryParams = (queryString || '').split('&').reduce((result: *, item: string) => {
if (item !== '') {
const nextResult = result || {};
const [key, value] = item.split('=');
nextResult[key] = value;
return nextResult;
}
return result;
}, null);
// reduce the matched pieces of the path into the params
// of the route. `params` is null if there are no params.
/* $FlowFixMe */
@@ -297,7 +311,8 @@ export default (
const paramName = key.name;
nextResult[paramName] = matchResult;
return nextResult;
}, null);
}, queryParams);
return NavigationActions.navigate({
routeName: matchedRouteName,

View File

@@ -185,6 +185,29 @@ describe('StackRouter', () => {
});
});
test('Parses paths with a query', () => {
expect(TestStackRouter.getActionForPathAndParams('people/foo?code=test&foo=bar')).toEqual({
type: NavigationActions.NAVIGATE,
routeName: 'person',
params: {
id: 'foo',
code: 'test',
foo: 'bar',
},
});
});
test('Parses paths with an empty query value', () => {
expect(TestStackRouter.getActionForPathAndParams('people/foo?code=&foo=bar')).toEqual({
type: NavigationActions.NAVIGATE,
routeName: 'person',
params: {
id: 'foo',
code: '',
foo: 'bar',
},
});
});
test('Correctly parses a path without arguments into an action chain', () => {
const uri = 'auth/login';