mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
9eadeb9fbb
refs #5942, #6150 There were a few key problems I was looking to solve with this: - Introduce a single point of truth for what the URL for accessing the API should be - Provide a simple way to configure the utility (much like a true SDK) As of this commit, this utility is still automatically available in a Ghost theme. To use it on an external site, the code would look like: ``` <script type="text/javascript" src="http://my-ghost-blog.com/shared/ghost-url.min.js"></script> <script type="text/javascript"> ghost.init({ clientId: "<your-client-id>", clientSecret: "<your-client-secret>" }); </script> ``` To achieve this, there have been a number of changes: - A new `apiUrl` function has been added to config, which calculates the correct URL. This needs to be unified with the other url generation functions as a separate piece of work. - The serveSharedFile middleware has been updated, so that it can serve files from / or /shared and to substitute `{{api-url}}` as it does `{{blog-url}}`. - ghost-url.js and ghost-url.min.js have been updated to be served via the serveSharedFile middleware - ghost-url.js has been changed slightly, to take the url from an inline variable which is substituted the first time it is served - `{{ghost_head}}` has been updated, removing the api url handling which is now in config/url.js and removing the configuration of the utility in favour of calling `init()` after the script is required - `{{ghost_head}}` has also had the meta tags for client id and secret removed - tests have been updated
72 lines
1.7 KiB
JavaScript
72 lines
1.7 KiB
JavaScript
(function () {
|
|
'use strict';
|
|
|
|
var apiUrl = '{{api-url}}',
|
|
clientId,
|
|
clientSecret,
|
|
url,
|
|
init;
|
|
|
|
function generateQueryString(object) {
|
|
var queries = [],
|
|
i;
|
|
|
|
if (!object) {
|
|
return '';
|
|
}
|
|
|
|
for (i in object) {
|
|
if (object.hasOwnProperty(i) && (!!object[i] || object[i] === false)) {
|
|
queries.push(i + '=' + encodeURIComponent(object[i]));
|
|
}
|
|
}
|
|
|
|
if (queries.length) {
|
|
return '?' + queries.join('&');
|
|
}
|
|
return '';
|
|
}
|
|
|
|
url = {
|
|
api: function () {
|
|
var args = Array.prototype.slice.call(arguments),
|
|
queryOptions;
|
|
|
|
if (args.length && typeof args[args.length - 1] === 'object') {
|
|
queryOptions = args.pop();
|
|
} else {
|
|
queryOptions = {};
|
|
}
|
|
|
|
queryOptions.client_id = clientId;
|
|
queryOptions.client_secret = clientSecret;
|
|
|
|
if (args.length) {
|
|
args.forEach(function (el) {
|
|
apiUrl += el.replace(/^\/|\/$/g, '') + '/';
|
|
});
|
|
}
|
|
|
|
return apiUrl + generateQueryString(queryOptions);
|
|
}
|
|
};
|
|
|
|
init = function (options) {
|
|
clientId = options.clientId ? options.clientId : '';
|
|
clientSecret = options.clientSecret ? options.clientSecret : '';
|
|
apiUrl = options.url ? options.url : '';
|
|
};
|
|
|
|
if (typeof window !== 'undefined') {
|
|
window.ghost = window.ghost || {};
|
|
window.ghost.url = url;
|
|
window.ghost.init = init;
|
|
}
|
|
|
|
if (typeof module !== 'undefined') {
|
|
module.exports = {
|
|
url: url,
|
|
init: init
|
|
};
|
|
}
|
|
})();
|