mirror of
https://github.com/HSLdevcom/digitransit-ui
synced 2025-07-27 07:04:44 +02:00
104 lines
2.7 KiB
JavaScript
104 lines
2.7 KiB
JavaScript
import isFinite from 'lodash/isFinite';
|
|
import oldParamParser from '../app/util/oldParamParser';
|
|
import { getConfiguration } from '../app/config';
|
|
|
|
function formatQuery(query) {
|
|
const params = Object.keys(query)
|
|
.map(k => `${k}=${query[k]}`)
|
|
.join('&');
|
|
|
|
return `?${params}`;
|
|
}
|
|
|
|
function formatUrl(req) {
|
|
const query = formatQuery(req.query);
|
|
return `${req.path}?${query}`;
|
|
}
|
|
|
|
function removeUrlParam(req, param) {
|
|
if (req.query[param]) {
|
|
delete req.query[param];
|
|
}
|
|
|
|
return formatUrl(req);
|
|
}
|
|
|
|
export function validateParams(req, config) {
|
|
let url;
|
|
|
|
if (config.queryMaxAgeDays && req.query.time) {
|
|
const now = Date.now() / 1000; // Convert to seconds
|
|
if (now - req.query.time > config.queryMaxAgeDays * 24 * 3600) {
|
|
url = removeUrlParam(req, 'time');
|
|
}
|
|
}
|
|
|
|
const numericParams = ['time'];
|
|
Object.keys(req.query).forEach(key => {
|
|
if (numericParams.indexOf(key) > -1 && !isFinite(Number(req.query[key]))) {
|
|
url = removeUrlParam(req, key);
|
|
}
|
|
});
|
|
|
|
return url;
|
|
}
|
|
|
|
const fixLocaleParam = (req, lang) => {
|
|
// override locale query param with the selected language
|
|
req.query.locale = lang === 'slangi' ? 'fi' : lang;
|
|
return formatQuery(req.query);
|
|
};
|
|
|
|
export const dropPathLanguageAndFixLocaleParam = (req, lang) => {
|
|
const newPath = req.path.replace(`/${lang}`, '/').replace('//', '/');
|
|
return newPath + fixLocaleParam(req, lang);
|
|
};
|
|
|
|
const dropPathLanguageAndRedirect = (req, res, lang) => {
|
|
const trimmedUrl = dropPathLanguageAndFixLocaleParam(req, lang);
|
|
res.redirect(trimmedUrl);
|
|
};
|
|
|
|
const fixLocaleParamAndRedirect = (req, res, lang) => {
|
|
const fixedUrl = req.path + fixLocaleParam(req, lang);
|
|
res.redirect(fixedUrl);
|
|
};
|
|
|
|
export default function reittiopasParameterMiddleware(req, res, next) {
|
|
const config = getConfiguration(req);
|
|
const newUrl = validateParams(req, config);
|
|
if (newUrl) {
|
|
res.redirect(newUrl);
|
|
} else if (config.redirectReittiopasParams) {
|
|
const parts = req.path.split('/');
|
|
const lang = parts[1];
|
|
if (config.availableLanguages.includes(lang)) {
|
|
res.cookie('lang', lang, {
|
|
// Good up to one year
|
|
maxAge: 365 * 24 * 60 * 60,
|
|
path: '/',
|
|
});
|
|
}
|
|
if (
|
|
req.query.from ||
|
|
req.query.to ||
|
|
req.query.from_in ||
|
|
req.query.to_in
|
|
) {
|
|
oldParamParser(req.query, config).then(url => res.redirect(url));
|
|
} else if (['fi', 'en', 'sv', 'ru', 'slangi'].includes(lang)) {
|
|
dropPathLanguageAndRedirect(req, res, lang);
|
|
} else {
|
|
const { locale } = req.query;
|
|
const cookieLang = req.cookies.lang;
|
|
|
|
if (cookieLang && locale && cookieLang !== locale) {
|
|
fixLocaleParamAndRedirect(req, res, cookieLang);
|
|
} else {
|
|
next();
|
|
}
|
|
}
|
|
} else {
|
|
next();
|
|
}
|
|
}
|