2018-10-05 01:50:45 +03:00
|
|
|
const debug = require('ghost-ignition').debug('api:shared:http');
|
|
|
|
const shared = require('../shared');
|
|
|
|
const models = require('../../models');
|
|
|
|
|
2019-05-06 15:24:12 +03:00
|
|
|
/**
|
|
|
|
* @description HTTP wrapper.
|
|
|
|
*
|
|
|
|
* This wrapper is used in the routes definition (see web/).
|
|
|
|
* The wrapper receives the express request, prepares the frame and forwards the request to the pipeline.
|
|
|
|
*
|
|
|
|
* @param {Function} apiImpl - Pipeline wrapper, which executes the target ctrl function.
|
|
|
|
* @return {Function}
|
|
|
|
*/
|
2019-02-25 21:52:45 +03:00
|
|
|
const http = (apiImpl) => {
|
2018-10-05 01:50:45 +03:00
|
|
|
return (req, res, next) => {
|
2019-10-15 17:07:38 +03:00
|
|
|
debug(`External API request to ${req.url}`);
|
2019-01-24 17:19:34 +03:00
|
|
|
let apiKey = null;
|
|
|
|
let integration = null;
|
|
|
|
let user = null;
|
|
|
|
|
|
|
|
if (req.api_key) {
|
|
|
|
apiKey = {
|
|
|
|
id: req.api_key.get('id'),
|
|
|
|
type: req.api_key.get('type')
|
|
|
|
};
|
2019-02-06 22:11:44 +03:00
|
|
|
integration = {
|
|
|
|
id: req.api_key.get('integration_id')
|
|
|
|
};
|
2019-01-24 17:19:34 +03:00
|
|
|
}
|
|
|
|
|
2019-05-06 15:24:12 +03:00
|
|
|
// NOTE: "external user" is only used in the subscriber app. External user is ID "0".
|
2019-01-24 17:19:34 +03:00
|
|
|
if ((req.user && req.user.id) || (req.user && models.User.isExternalUser(req.user.id))) {
|
|
|
|
user = req.user.id;
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:50:45 +03:00
|
|
|
const frame = new shared.Frame({
|
|
|
|
body: req.body,
|
|
|
|
file: req.file,
|
|
|
|
files: req.files,
|
|
|
|
query: req.query,
|
|
|
|
params: req.params,
|
|
|
|
user: req.user,
|
|
|
|
context: {
|
2019-01-24 17:19:34 +03:00
|
|
|
api_key: apiKey,
|
|
|
|
user: user,
|
|
|
|
integration: integration,
|
2018-11-07 14:01:27 +03:00
|
|
|
member: (req.member || null)
|
2019-02-25 21:52:45 +03:00
|
|
|
}
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
frame.configure({
|
|
|
|
options: apiImpl.options,
|
|
|
|
data: apiImpl.data
|
|
|
|
});
|
|
|
|
|
|
|
|
apiImpl(frame)
|
|
|
|
.then((result) => {
|
2019-10-15 17:07:38 +03:00
|
|
|
debug(`External API request to ${frame.docName}.${frame.method}`);
|
2018-12-17 14:47:19 +03:00
|
|
|
return shared.headers.get(result, apiImpl.headers, frame)
|
|
|
|
.then(headers => ({result, headers}));
|
|
|
|
})
|
|
|
|
.then(({result, headers}) => {
|
2018-10-05 01:50:45 +03:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2018-10-12 20:44:01 +03:00
|
|
|
let statusCode = 200;
|
|
|
|
if (typeof apiImpl.statusCode === 'function') {
|
|
|
|
statusCode = apiImpl.statusCode(result);
|
|
|
|
} else if (apiImpl.statusCode) {
|
|
|
|
statusCode = apiImpl.statusCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(statusCode);
|
2018-10-05 01:50:45 +03:00
|
|
|
|
|
|
|
// CASE: generate headers based on the api ctrl configuration
|
2018-12-17 14:47:19 +03:00
|
|
|
res.set(headers);
|
2018-10-05 01:50:45 +03:00
|
|
|
|
|
|
|
if (apiImpl.response && apiImpl.response.format === 'plain') {
|
|
|
|
debug('plain text response');
|
|
|
|
return res.send(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('json response');
|
|
|
|
res.json(result || {});
|
|
|
|
})
|
2019-02-25 21:11:16 +03:00
|
|
|
.catch((err) => {
|
|
|
|
req.frameOptions = {
|
|
|
|
docName: frame.docName,
|
|
|
|
method: frame.method
|
|
|
|
};
|
|
|
|
|
2018-10-05 01:50:45 +03:00
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = http;
|