common: remove falsy params when using app.route()

This commit is contained in:
David Sevilla Martin 2020-03-15 09:39:22 -04:00
parent c615fb96c9
commit e86940b6a3
No known key found for this signature in database
GPG Key ID: F764F1417E16B15F
2 changed files with 11 additions and 6 deletions

View File

@ -200,6 +200,13 @@ export default abstract class Application {
if (!route) throw new Error(`Route '${name}' does not exist`);
const url = route.path.replace(/:([^\/]+)/g, (m, key) => extract(params, key));
// Remove falsy values in params to avoid
// having urls like '/?sort&q'
for (const key in params) {
if (params.hasOwnProperty(key) && !params[key]) delete params[key];
}
const queryString = m.buildQueryString(params);
const prefix = m.route.prefix === '' ? this.forum.attribute('basePath') : '';

View File

@ -30,13 +30,11 @@ export default app => {
app.route.discussion = (discussion: Discussion, near?: number): string => {
const slug = discussion?.slug();
const hasNear = near && near !== 1;
const params = {
return app.route(hasNear ? 'discussion.near' : 'discussion', {
id: discussion.id() + (slug.trim() ? '-' + slug : ''),
};
if (hasNear) params['near'] = near;
return app.route(hasNear ? 'discussion.near' : 'discussion', params);
near: hasNear && near,
});
};
/**