mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
58 lines
1.8 KiB
JavaScript
58 lines
1.8 KiB
JavaScript
|
const debug = require('ghost-ignition').debug('api:shared:http');
|
||
|
const shared = require('../shared');
|
||
|
const models = require('../../models');
|
||
|
|
||
|
const http = (apiImpl) => {
|
||
|
return (req, res, next) => {
|
||
|
debug('request');
|
||
|
|
||
|
const frame = new shared.Frame({
|
||
|
body: req.body,
|
||
|
file: req.file,
|
||
|
files: req.files,
|
||
|
query: req.query,
|
||
|
params: req.params,
|
||
|
user: req.user,
|
||
|
context: {
|
||
|
user: ((req.user && req.user.id) || (req.user && models.User.isExternalUser(req.user.id))) ? req.user.id : null,
|
||
|
client: (req.client && req.client.slug) ? req.client.slug : null,
|
||
|
client_id: (req.client && req.client.id) ? req.client.id : null
|
||
|
}
|
||
|
});
|
||
|
|
||
|
frame.configure({
|
||
|
options: apiImpl.options,
|
||
|
data: apiImpl.data
|
||
|
});
|
||
|
|
||
|
apiImpl(frame)
|
||
|
.then((result) => {
|
||
|
debug(result);
|
||
|
|
||
|
// CASE: api ctrl wants to handle the express response (e.g. streams)
|
||
|
if (typeof result === 'function') {
|
||
|
debug('ctrl function call');
|
||
|
return result(req, res, next);
|
||
|
}
|
||
|
|
||
|
res.status(apiImpl.statusCode || 200);
|
||
|
|
||
|
// CASE: generate headers based on the api ctrl configuration
|
||
|
res.set(shared.headers.get(result, apiImpl.headers));
|
||
|
|
||
|
if (apiImpl.response && apiImpl.response.format === 'plain') {
|
||
|
debug('plain text response');
|
||
|
return res.send(result);
|
||
|
}
|
||
|
|
||
|
debug('json response');
|
||
|
res.json(result || {});
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
next(err);
|
||
|
});
|
||
|
};
|
||
|
};
|
||
|
|
||
|
module.exports = http;
|