More granular control over query handling

This commit is contained in:
Michael Jackson
2019-08-08 10:00:34 -07:00
parent a718d90549
commit dfe2f29fe0
7 changed files with 82 additions and 54 deletions

View File

@ -0,0 +1,27 @@
import createSearch from '../utils/createSearch.js';
/**
* Reject URLs with invalid query parameters to increase cache hit rates.
*/
export default function allowQuery(validKeys = []) {
if (!Array.isArray(validKeys)) {
validKeys = [validKeys];
}
return (req, res, next) => {
const keys = Object.keys(req.query);
if (!keys.every(key => validKeys.includes(key))) {
const newQuery = keys
.filter(key => validKeys.includes(key))
.reduce((query, key) => {
query[key] = req.query[key];
return query;
}, {});
return res.redirect(302, req.baseUrl + req.path + createSearch(newQuery));
}
next();
};
}