mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-26 20:34:02 +03:00
af6137248d
fixes #1765 fixes #1811 issue #1833 New UrlFor functions - moved body of url helper to config.path.urlFor, which can generate a URL for various scenarios - urlFor can take a string (name) or object (relativeUrl: '/') as the first argument - this is the first step towards issue #1833 - also added config.path.urlForPost which is async and handles getting permalink setting - frontend controller, ghost_head helper, cache invalidation all now use urlFor or urlForPost all urls should be correct and consistent URL Consistency Improvements - refactored invalidateCache into cacheInvalidationHeader which returns a promise so that url can be generated properly by urlForPost - moved isPost from models to schema, and refactored schema to have a tables object - deleted posts now return the whole object, not just id and slug, ensuring cache invalidation header can be set on delete - frontend controller rss and archive page redirects work properly with subdirectory - removes {{url}} helper from admin and client, and replaced with adminUrl helper which also uses urlFor - in res.locals ghostRoot becomes relativeUrl, and path is removed
85 lines
2.7 KiB
JavaScript
85 lines
2.7 KiB
JavaScript
// # Ghost Data API
|
|
// Provides access to the data model
|
|
|
|
var _ = require('underscore'),
|
|
when = require('when'),
|
|
config = require('../config'),
|
|
errors = require('../errorHandling'),
|
|
db = require('./db'),
|
|
settings = require('./settings'),
|
|
notifications = require('./notifications'),
|
|
posts = require('./posts'),
|
|
users = require('./users'),
|
|
tags = require('./tags'),
|
|
requestHandler,
|
|
init;
|
|
|
|
// ## Request Handlers
|
|
|
|
function cacheInvalidationHeader(req, result) {
|
|
var parsedUrl = req._parsedUrl.pathname.replace(/\/$/, '').split('/'),
|
|
method = req.method,
|
|
endpoint = parsedUrl[4],
|
|
id = parsedUrl[5],
|
|
cacheInvalidate,
|
|
jsonResult = result.toJSON ? result.toJSON() : result;
|
|
|
|
if (method === 'POST' || method === 'PUT' || method === 'DELETE') {
|
|
if (endpoint === 'settings' || endpoint === 'users' || endpoint === 'db') {
|
|
cacheInvalidate = "/*";
|
|
} else if (endpoint === 'posts') {
|
|
cacheInvalidate = "/, /page/*, /rss/, /rss/*";
|
|
if (id && jsonResult.slug) {
|
|
return config.paths.urlForPost(settings, jsonResult).then(function (postUrl) {
|
|
return cacheInvalidate + ', ' + postUrl;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
return when(cacheInvalidate);
|
|
}
|
|
|
|
// ### requestHandler
|
|
// decorator for api functions which are called via an HTTP request
|
|
// takes the API method and wraps it so that it gets data from the request and returns a sensible JSON response
|
|
requestHandler = function (apiMethod) {
|
|
return function (req, res) {
|
|
var options = _.extend(req.body, req.query, req.params),
|
|
apiContext = {
|
|
user: req.session && req.session.user
|
|
};
|
|
|
|
return apiMethod.call(apiContext, options).then(function (result) {
|
|
res.json(result || {});
|
|
return cacheInvalidationHeader(req, result).then(function (header) {
|
|
if (header) {
|
|
res.set({
|
|
"X-Cache-Invalidate": header
|
|
});
|
|
}
|
|
});
|
|
}, function (error) {
|
|
var errorCode = error.errorCode || 500,
|
|
errorMsg = {error: _.isString(error) ? error : (_.isObject(error) ? error.message : 'Unknown API Error')};
|
|
res.json(errorCode, errorMsg);
|
|
});
|
|
};
|
|
};
|
|
|
|
init = function () {
|
|
return settings.updateSettingsCache();
|
|
};
|
|
|
|
// Public API
|
|
module.exports = {
|
|
posts: posts,
|
|
users: users,
|
|
tags: tags,
|
|
notifications: notifications,
|
|
settings: settings,
|
|
db: db,
|
|
requestHandler: requestHandler,
|
|
init: init
|
|
};
|