0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/services/apps/proxy.js
Fabien O'Carroll 4696d70de0 Refactored app proxy module to not use Constructor
no-issue

Rather than creating a whole instance, we can replace it with a helper
method - his is less memory intensive and a little easier to parse for
something this small.
2019-04-16 11:05:33 +02:00

64 lines
2.1 KiB
JavaScript

const _ = require('lodash');
const api = require('../../api/v0.1');
const helpers = require('../../helpers/register');
const filters = require('../../filters');
const common = require('../../lib/common');
const routingService = require('../routing');
module.exports.getInstance = function getInstance(name) {
if (!name) {
throw new Error(common.i18n.t('errors.apps.mustProvideAppName.error'));
}
const appRouter = routingService.registry.getRouter('appRouter');
const passThruAppContextToApi = (apiMethods) => {
const appContext = {
app: name
};
return _.reduce(apiMethods, function (memo, apiMethod, methodName) {
memo[methodName] = function (...args) {
const options = args[args.length - 1];
if (_.isObject(options)) {
options.context = _.clone(appContext);
}
return apiMethod.apply({}, args);
};
return memo;
}, {});
};
return {
filters: {
register: filters.registerFilter.bind(filters),
deregister: filters.deregisterFilter.bind(filters)
},
helpers: {
register: helpers.registerThemeHelper.bind(helpers),
registerAsync: helpers.registerAsyncThemeHelper.bind(helpers)
},
// Expose the route service...
routeService: {
// This allows for mounting an entirely new Router at a path...
registerRouter: appRouter.mountRouter.bind(appRouter)
},
// Mini proxy to the API - needs review
api: {
posts: passThruAppContextToApi(
_.pick(api.posts, 'browse', 'read', 'edit', 'add', 'destroy')
),
tags: passThruAppContextToApi(
_.pick(api.tags, 'browse')
),
notifications: passThruAppContextToApi(
_.pick(api.notifications, 'browse', 'add', 'destroy')
),
settings: passThruAppContextToApi(
_.pick(api.settings, 'browse', 'read', 'edit')
)
}
};
};