-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
static.js
47 lines (37 loc) · 1.47 KB
/
static.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const _ = require('lodash'),
Promise = require('bluebird'),
debug = require('ghost-ignition').debug('services:routing:controllers:static'),
helpers = require('../helpers');
function processQuery(query, locals) {
const api = require('../../../api')[locals.apiVersion];
query = _.cloneDeep(query);
// Return a promise for the api query
return api[query.resource][query.type](query.options);
}
module.exports = function staticController(req, res, next) {
debug('staticController', res.routerOptions);
let props = {};
_.each(res.routerOptions.data, function (query, name) {
props[name] = processQuery(query, res.locals);
});
return Promise.props(props)
.then(function handleResult(result) {
let response = {};
if (res.routerOptions.data) {
response.data = {};
_.each(res.routerOptions.data, function (config, name) {
if (config.type === 'browse') {
response.data[name] = result[name];
} else {
response.data[name] = result[name][config.resource];
}
});
}
// @TODO: get rid of this O_O
_.each(response.data, function (data) {
helpers.secure(req, data);
});
helpers.renderer(req, res, helpers.formatResponse.entries(response));
})
.catch(helpers.handleError(next));
};