Ghost/core/server/services/api-version-compatibility/index.js
Hannah Wolfe c6ae3c30d8 Moved content-version middleware onto api app
closes: https://github.com/TryGhost/Toolbox/issues/319

- at the moment, content-version is only set if one of our endpoints touches the request
  - this was demonstrated in the e2e tests, where many of the tests that set accept-version did not receive accept-version
- by moving the middleware out of the http module and onto the api app we ensure it's always done
- I put the code in the api-version-compatibility service to keep it all co-located
- ideally we will refactor that service slightly so it only exposes middleware
2022-05-02 19:05:14 +01:00

44 lines
1.8 KiB
JavaScript

const APIVersionCompatibilityService = require('@tryghost/api-version-compatibility-service');
const VersionNotificationsDataService = require('@tryghost/version-notifications-data-service');
const versionMismatchHandler = require('@tryghost/mw-api-version-mismatch');
// const {GhostMailer} = require('../mail');
const settingsService = require('../../services/settings');
const models = require('../../models');
const logging = require('@tryghost/logging');
const ghostVersion = require('@tryghost/version');
let serviceInstance;
const init = () => {
//const ghostMailer = new GhostMailer();
const versionNotificationsDataService = new VersionNotificationsDataService({
UserModel: models.User,
settingsService: settingsService.getSettingsBREADServiceInstance()
});
serviceInstance = new APIVersionCompatibilityService({
sendEmail: (options) => {
// NOTE: not using bind here because mockMailer is having trouble mocking bound methods
//return ghostMailer.send(options);
// For now log a warning, rather than sending an email
logging.warn(options.html);
},
fetchEmailsToNotify: versionNotificationsDataService.getNotificationEmails.bind(versionNotificationsDataService),
fetchHandled: versionNotificationsDataService.fetchNotification.bind(versionNotificationsDataService),
saveHandled: versionNotificationsDataService.saveNotification.bind(versionNotificationsDataService)
});
};
module.exports.errorHandler = (req, res, next) => {
return versionMismatchHandler(serviceInstance)(req, res, next);
};
module.exports.contentVersion = (req, res, next) => {
if (req.header('accept-version')) {
res.header('Content-Version', `v${ghostVersion.safe}`);
}
next();
};
module.exports.init = init;