Skip to content

Support optional query params in routing configuration #901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions src/routing/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,30 +124,28 @@ export class Router extends Evented<{ nav: NavEvent; route: RouteEvent; outlet:
}

let linkPath: string | undefined = route.fullPath;
if (route.fullQueryParams.length > 0) {
let queryString = route.fullQueryParams.reduce((queryParamString, param, index) => {
if (index > 0) {
return `${queryParamString}&${param}={${param}}`;
}
return `?${param}={${param}}`;
}, '');
linkPath = `${linkPath}${queryString}`;
}
params = { ...route.defaultParams, ...this._currentQueryParams, ...this._currentParams, ...params };

if (Object.keys(params).length === 0 && route.fullParams.length > 0) {
return undefined;
}

const fullParams = [...route.fullParams, ...route.fullQueryParams];
for (let i = 0; i < fullParams.length; i++) {
const param = fullParams[i];
for (let i = 0; i < route.fullParams.length; i++) {
const param = route.fullParams[i];
if (params[param]) {
linkPath = linkPath.replace(`{${param}}`, params[param]);
} else {
return undefined;
}
}

let paramSeparator = '?';
for (let i = 0; i < route.fullQueryParams.length; i++) {
const { param, optional } = route.fullQueryParams[i];
if (params[param]) {
linkPath = `${linkPath}${paramSeparator}${param}=${params[param]}`;
paramSeparator = '&';
} else if (!optional) {
return undefined;
}
}

return this._history.prefix(linkPath);
}

Expand Down Expand Up @@ -198,8 +196,12 @@ export class Router extends Evented<{ nav: NavEvent; route: RouteEvent; outlet:
routes = routes ? routes : this._routes;
for (let i = 0; i < config.length; i++) {
let { path, outlet, children, defaultRoute = false, defaultParams = {}, id, title, redirect } = config[i];
let [parsedPath, queryParamString] = path.split('?');
let queryParams: string[] = [];
let [parsedPath, ...queryParamParts] = path.split('?');
let queryParamString = queryParamParts.join('?');
let queryParams: {
param: string;
optional: boolean;
}[] = [];
parsedPath = this._stripLeadingSlash(parsedPath);

const segments: string[] = parsedPath.split('/');
Expand Down Expand Up @@ -239,7 +241,9 @@ export class Router extends Evented<{ nav: NavEvent; route: RouteEvent; outlet:
}
if (queryParamString) {
queryParams = queryParamString.split('&').map((queryParam) => {
return queryParam.replace('{', '').replace('}', '');
const param = queryParam.replace('{', '').replace(/\??}$/, '');
const optional = /\?}/.test(queryParam);
return { param, optional };
});
}
route.fullQueryParams = parentRoute ? [...parentRoute.fullQueryParams, ...queryParams] : queryParams;
Expand Down
5 changes: 4 additions & 1 deletion src/routing/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export interface Route {
redirect?: string;
fullPath: string;
fullParams: string[];
fullQueryParams: string[];
fullQueryParams: {
param: string;
optional: boolean;
}[];
defaultParams: Params;
score: number;
}
Expand Down
15 changes: 14 additions & 1 deletion tests/routing/unit/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const routeConfigWithParamsAndQueryParams = [
},
children: [
{
path: '/bar/{bar}?{barQuery}',
path: '/bar/{bar}?{barQuery}&{optionalQuery?}',
outlet: 'bar',
id: 'bar',
defaultParams: {
Expand Down Expand Up @@ -578,6 +578,8 @@ describe('Router', () => {
router.setPath('foo/bar/bar/foo?fooQuery=bar&barQuery=foo');
assert.strictEqual(router.link('foo'), 'foo/bar?fooQuery=bar');
assert.strictEqual(router.link('bar'), 'foo/bar/bar/foo?fooQuery=bar&barQuery=foo');
router.setPath('foo/bar/bar/foo?fooQuery=bar&barQuery=foo&optionalQuery=optional');
assert.strictEqual(router.link('bar'), 'foo/bar/bar/foo?fooQuery=bar&barQuery=foo&optionalQuery=optional');
});

it('Should create link with params and query params with specified params', () => {
Expand All @@ -587,6 +589,17 @@ describe('Router', () => {
router.link('bar', { foo: 'qux', bar: 'baz', fooQuery: 'quxQuery', barQuery: 'bazQuery' }),
'foo/qux/bar/baz?fooQuery=quxQuery&barQuery=bazQuery'
);
assert.strictEqual(router.link('foo', { foo: 'qux', fooQuery: 'quxQuery' }), 'foo/qux?fooQuery=quxQuery');
assert.strictEqual(
router.link('bar', {
foo: 'qux',
bar: 'baz',
fooQuery: 'quxQuery',
barQuery: 'bazQuery',
optionalQuery: 'optional'
}),
'foo/qux/bar/baz?fooQuery=quxQuery&barQuery=bazQuery&optionalQuery=optional'
);
});

it('Cannot generate link for an unknown route', () => {
Expand Down