0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Updated api.http handling of controller returned fns (#9907)

refs #9865

This is to ensure that if a controller returns a function, it will
always get called regardless of method.

Also cleaned up top level const usage
This commit is contained in:
Fabien O'Carroll 2018-09-26 13:13:41 +07:00 committed by Katharina Irrgang
parent 6ccb2debe2
commit 6c35de7d95

View file

@ -4,41 +4,32 @@
// Ghost's JSON API is integral to the workings of Ghost, regardless of whether you want to access data internally, // Ghost's JSON API is integral to the workings of Ghost, regardless of whether you want to access data internally,
// from a theme, an app, or from an external app, you'll use the Ghost JSON API to do so. // from a theme, an app, or from an external app, you'll use the Ghost JSON API to do so.
const {isEmpty} = require('lodash'), const {isEmpty} = require('lodash');
Promise = require('bluebird'), const Promise = require('bluebird');
models = require('../models'), const models = require('../models');
urlService = require('../services/url'), const urlService = require('../services/url');
configuration = require('./configuration'), const configuration = require('./configuration');
db = require('./db'), const db = require('./db');
mail = require('./mail'), const mail = require('./mail');
notifications = require('./notifications'), const notifications = require('./notifications');
posts = require('./posts'), const posts = require('./posts');
schedules = require('./schedules'), const schedules = require('./schedules');
roles = require('./roles'), const roles = require('./roles');
settings = require('./settings'), const settings = require('./settings');
tags = require('./tags'), const tags = require('./tags');
invites = require('./invites'), const invites = require('./invites');
redirects = require('./redirects'), const redirects = require('./redirects');
clients = require('./clients'), const clients = require('./clients');
users = require('./users'), const users = require('./users');
slugs = require('./slugs'), const slugs = require('./slugs');
themes = require('./themes'), const themes = require('./themes');
subscribers = require('./subscribers'), const subscribers = require('./subscribers');
authentication = require('./authentication'), const authentication = require('./authentication');
uploads = require('./upload'), const uploads = require('./upload');
exporter = require('../data/exporter'), const exporter = require('../data/exporter');
slack = require('./slack'), const slack = require('./slack');
webhooks = require('./webhooks'), const webhooks = require('./webhooks');
oembed = require('./oembed'); const oembed = require('./oembed');
let http,
addHeaders,
cacheInvalidationHeader,
locationHeader,
contentDispositionHeaderExport,
contentDispositionHeaderSubscribers,
contentDispositionHeaderRedirects,
contentDispositionHeaderRoutes;
function isActiveThemeUpdate(method, endpoint, result) { function isActiveThemeUpdate(method, endpoint, result) {
if (endpoint === 'themes') { if (endpoint === 'themes') {
@ -67,7 +58,7 @@ function isActiveThemeUpdate(method, endpoint, result) {
* @param {Object} result API method result * @param {Object} result API method result
* @return {String} Resolves to header string * @return {String} Resolves to header string
*/ */
cacheInvalidationHeader = (req, result) => { const cacheInvalidationHeader = (req, result) => {
const parsedUrl = req._parsedUrl.pathname.replace(/^\/|\/$/g, '').split('/'), const parsedUrl = req._parsedUrl.pathname.replace(/^\/|\/$/g, '').split('/'),
method = req.method, method = req.method,
endpoint = parsedUrl[0], endpoint = parsedUrl[0],
@ -123,7 +114,7 @@ cacheInvalidationHeader = (req, result) => {
* @param {Object} result API method result * @param {Object} result API method result
* @return {String} Resolves to header string * @return {String} Resolves to header string
*/ */
locationHeader = (req, result) => { const locationHeader = (req, result) => {
const apiRoot = urlService.utils.urlFor('api', {version: 'stable'}); const apiRoot = urlService.utils.urlFor('api', {version: 'stable'});
let location, let location,
newObject, newObject,
@ -167,26 +158,26 @@ locationHeader = (req, result) => {
* @return {string} * @return {string}
*/ */
contentDispositionHeaderExport = () => { const contentDispositionHeaderExport = () => {
return exporter.fileName().then((filename) => { return exporter.fileName().then((filename) => {
return `Attachment; filename="${filename}"`; return `Attachment; filename="${filename}"`;
}); });
}; };
contentDispositionHeaderSubscribers = () => { const contentDispositionHeaderSubscribers = () => {
const datetime = (new Date()).toJSON().substring(0, 10); const datetime = (new Date()).toJSON().substring(0, 10);
return Promise.resolve(`Attachment; filename="subscribers.${datetime}.csv"`); return Promise.resolve(`Attachment; filename="subscribers.${datetime}.csv"`);
}; };
contentDispositionHeaderRedirects = () => { const contentDispositionHeaderRedirects = () => {
return Promise.resolve('Attachment; filename="redirects.json"'); return Promise.resolve('Attachment; filename="redirects.json"');
}; };
contentDispositionHeaderRoutes = () => { const contentDispositionHeaderRoutes = () => {
return Promise.resolve('Attachment; filename="routes.yaml"'); return Promise.resolve('Attachment; filename="routes.yaml"');
}; };
addHeaders = (apiMethod, req, res, result) => { const addHeaders = (apiMethod, req, res, result) => {
let cacheInvalidation, let cacheInvalidation,
location, location,
contentDisposition; contentDisposition;
@ -264,7 +255,7 @@ addHeaders = (apiMethod, req, res, result) => {
* @param {Function} apiMethod API method to call * @param {Function} apiMethod API method to call
* @return {Function} middleware format function to be called by the route when a matching request is made * @return {Function} middleware format function to be called by the route when a matching request is made
*/ */
http = (apiMethod) => { const http = (apiMethod) => {
return function apiHandler(req, res, next) { return function apiHandler(req, res, next) {
// We define 2 properties for using as arguments in API calls: // We define 2 properties for using as arguments in API calls:
let object = req.body, let object = req.body,
@ -292,6 +283,12 @@ http = (apiMethod) => {
// Add X-Cache-Invalidate, Location, and Content-Disposition headers // Add X-Cache-Invalidate, Location, and Content-Disposition headers
return addHeaders(apiMethod, req, res, (response || {})); return addHeaders(apiMethod, req, res, (response || {}));
}).then((response) => { }).then((response) => {
// CASE: api method response wants to handle the express response
// example: serve files (stream)
if (typeof response === 'function') {
return response(req, res, next);
}
if (req.method === 'DELETE') { if (req.method === 'DELETE') {
return res.status(204).end(); return res.status(204).end();
} }
@ -302,12 +299,6 @@ http = (apiMethod) => {
return res.status(200).send(response); return res.status(200).send(response);
} }
// CASE: api method response wants to handle the express response
// example: serve files (stream)
if (typeof response === 'function') {
return response(req, res, next);
}
// Send a properly formatting HTTP response containing the data with correct headers // Send a properly formatting HTTP response containing the data with correct headers
res.json(response || {}); res.json(response || {});
}).catch((error) => { }).catch((error) => {