mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 11:22:19 +03:00
5715aa2155
closes #11085 - Ghost has been using YAML format for other configurations (e.g. routes). The plan is to move to this format for all user-edited settings files. By default JSON format is still used in Ghost Admin API v2/v3, but will be changed to YAML in API v4. Check referenced issue for more context. - New format supports all the features available before. The main noticeable change is the structure of config file. It is now grouped by redirect HTTP code instead of specifying `"permanent": true | false` attribute for each config property. Example format for YAML config: ``` 302: /from-url/: /to-url/ 301: /category/([a-z0-9\-]+)/i: /tag/$1/ /v([0-9\.]+)/docs/([a-z0-9\-]+)/i: /docs/$2/ ``` - Added 2 new endpoints: `POST redirects/upload` and `GET redirects/download`. These serve as an alias to current GET/POST `/redirects/json. "upload/download" naming pattern is introduced to match the convention with other resources that can be uploaded and downloaded (images, themes etc.). `/redirects/json` endpoints will be removed in Admin API v4 - The parsing code from `custom-redirects.js` has been moved to `frontend/services/redirects/settings.js`. This location is more appropriate for this logic and eventually `custom-redirects.js` middlewear might be moved into "frontend" as this middlewear plays a role mostly effecting that area.
130 lines
4.2 KiB
JavaScript
130 lines
4.2 KiB
JavaScript
const url = require('url');
|
|
const debug = require('ghost-ignition').debug('api:shared:http');
|
|
const shared = require('../shared');
|
|
const models = require('../../models');
|
|
|
|
/**
|
|
* @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}
|
|
*/
|
|
const http = (apiImpl) => {
|
|
return (req, res, next) => {
|
|
debug(`External API request to ${req.url}`);
|
|
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')
|
|
};
|
|
integration = {
|
|
id: req.api_key.get('integration_id')
|
|
};
|
|
}
|
|
|
|
// NOTE: "external user" is only used in the subscriber app. External user is ID "0".
|
|
if ((req.user && req.user.id) || (req.user && models.User.isExternalUser(req.user.id))) {
|
|
user = req.user.id;
|
|
}
|
|
|
|
const frame = new shared.Frame({
|
|
body: req.body,
|
|
file: req.file,
|
|
files: req.files,
|
|
query: req.query,
|
|
params: req.params,
|
|
user: req.user,
|
|
session: req.session,
|
|
url: {
|
|
host: req.vhost ? req.vhost.host : req.get('host'),
|
|
pathname: url.parse(req.originalUrl || req.url).pathname,
|
|
secure: req.secure
|
|
},
|
|
context: {
|
|
api_key: apiKey,
|
|
user: user,
|
|
integration: integration,
|
|
member: (req.member || null)
|
|
}
|
|
});
|
|
|
|
frame.configure({
|
|
options: apiImpl.options,
|
|
data: apiImpl.data
|
|
});
|
|
|
|
apiImpl(frame)
|
|
.then((result) => {
|
|
debug(`External API request to ${frame.docName}.${frame.method}`);
|
|
return shared.headers.get(result, apiImpl.headers, frame)
|
|
.then(headers => ({result, headers}));
|
|
})
|
|
.then(({result, headers}) => {
|
|
// 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);
|
|
}
|
|
|
|
let statusCode = 200;
|
|
if (typeof apiImpl.statusCode === 'function') {
|
|
statusCode = apiImpl.statusCode(result);
|
|
} else if (apiImpl.statusCode) {
|
|
statusCode = apiImpl.statusCode;
|
|
}
|
|
|
|
res.status(statusCode);
|
|
|
|
// CASE: generate headers based on the api ctrl configuration
|
|
res.set(headers);
|
|
|
|
const send = (format) => {
|
|
if (format === 'plain') {
|
|
debug('plain text response');
|
|
return res.send(result);
|
|
}
|
|
|
|
debug('json response');
|
|
res.json(result || {});
|
|
};
|
|
|
|
let responseFormat;
|
|
|
|
if (apiImpl.response){
|
|
if (typeof apiImpl.response.format === 'function') {
|
|
const apiResponseFormat = apiImpl.response.format();
|
|
|
|
if (apiResponseFormat.then) { // is promise
|
|
return apiResponseFormat.then((formatName) => {
|
|
send(formatName);
|
|
});
|
|
} else {
|
|
responseFormat = apiResponseFormat;
|
|
}
|
|
} else {
|
|
responseFormat = apiImpl.response.format;
|
|
}
|
|
}
|
|
|
|
send(responseFormat);
|
|
})
|
|
.catch((err) => {
|
|
req.frameOptions = {
|
|
docName: frame.docName,
|
|
method: frame.method
|
|
};
|
|
|
|
next(err);
|
|
});
|
|
};
|
|
};
|
|
|
|
module.exports = http;
|