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
|
|
|
|
*/
|
2024-05-07 12:08:56 +03:00
|
|
|
return async function versionMismatchHandlerMiddleware(err, req, res, next) {
|
2022-04-20 10:42:53 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-07 12:08:56 +03:00
|
|
|
next(err);
|
2022-04-20 10:42:53 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = versionMismatchHandler;
|