2022-05-10 11:44:18 +03:00
|
|
|
const extractApiKey = require('@tryghost/extract-api-key');
|
|
|
|
|
2022-04-20 10:42:53 +03:00
|
|
|
const versionMismatchHandler = (APIVersionCompatibilityService) => {
|
2022-05-09 10:35:00 +03:00
|
|
|
/**
|
|
|
|
* @param {Object} err
|
|
|
|
* @param {import('express').Request} req
|
|
|
|
* @param {import('express').Response} res
|
|
|
|
* @param {import('express').NextFunction} next
|
|
|
|
*/
|
2022-04-20 10:42:53 +03:00
|
|
|
return async (err, req, res, next) => {
|
|
|
|
if (err && err.errorType === 'RequestNotAcceptableError') {
|
|
|
|
if (err.code === 'UPDATE_CLIENT') {
|
2022-05-10 11:44:18 +03:00
|
|
|
const {key, type} = extractApiKey(req);
|
2022-05-11 05:40:09 +03:00
|
|
|
const requestURL = req.originalUrl.split('?').shift();
|
2022-05-10 11:44:18 +03:00
|
|
|
|
2022-04-20 10:42:53 +03:00
|
|
|
await APIVersionCompatibilityService.handleMismatch({
|
|
|
|
acceptVersion: req.headers['accept-version'],
|
|
|
|
contentVersion: `v${res.locals.safeVersion}`,
|
2022-05-11 05:40:09 +03:00
|
|
|
requestURL,
|
2022-05-10 11:44:18 +03:00
|
|
|
userAgent: req.headers['user-agent'],
|
|
|
|
apiKeyValue: key,
|
|
|
|
apiKeyType: type
|
2022-04-20 10:42:53 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next(err, req, res);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = versionMismatchHandler;
|