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

add themes ajax helper

closes #5942
- adds helper script for calling the api in themes
- adds tests for said helper script
This commit is contained in:
Austin Burdine 2015-11-10 22:33:19 -06:00
parent 67129fe2cf
commit 250edf2b06
5 changed files with 274 additions and 2 deletions

View file

@ -130,7 +130,6 @@ var _ = require('lodash'),
'!config*.js', // note: i added this, do we want this linted? '!config*.js', // note: i added this, do we want this linted?
'core/*.js', 'core/*.js',
'core/server/**/*.js', 'core/server/**/*.js',
'core/shared/**/*.js',
'core/test/**/*.js', 'core/test/**/*.js',
'!core/test/coverage/**', '!core/test/coverage/**',
'!core/shared/vendor/**/*.js' '!core/shared/vendor/**/*.js'
@ -167,7 +166,6 @@ var _ = require('lodash'),
'!config*.js', // note: i added this, do we want this linted? '!config*.js', // note: i added this, do we want this linted?
'core/*.js', 'core/*.js',
'core/server/**/*.js', 'core/server/**/*.js',
'core/shared/**/*.js',
'core/test/**/*.js', 'core/test/**/*.js',
'!core/test/coverage/**', '!core/test/coverage/**',
'!core/shared/vendor/**/*.js' '!core/shared/vendor/**/*.js'

View file

@ -10,6 +10,8 @@ var hbs = require('express-hbs'),
moment = require('moment'), moment = require('moment'),
_ = require('lodash'), _ = require('lodash'),
Promise = require('bluebird'), Promise = require('bluebird'),
fs = require('fs'),
path = require('path'),
config = require('../config'), config = require('../config'),
filters = require('../filters'), filters = require('../filters'),
@ -279,6 +281,23 @@ function finaliseSchema(schema, head) {
return head; return head;
} }
function getAjaxHelper() {
var ghostUrlScript = fs.readFileSync(path.join(config.paths.corePath, 'shared', 'ghost-url.js'), 'utf8'),
template = hbs.compile(ghostUrlScript, path.join(config.paths.subdir || '/', 'shared', 'ghost-url.js')),
apiPath = require('../routes').apiBaseUri,
url, useOrigin;
if (config.forceAdminSSL) {
url = 'https://' + (config.urlSSL || config.url).replace(/.*?:\/\//g, '').replace(/\/$/, '') + apiPath;
useOrigin = false;
} else {
url = config.paths.subdir + apiPath;
useOrigin = true;
}
return '<script type="text/javascript">' + template({api_url: url, useOrigin: useOrigin ? 'true' : 'false'}) + '</script>';
}
ghost_head = function (options) { ghost_head = function (options) {
// create a shortcut for theme config // create a shortcut for theme config
blog = config.theme; blog = config.theme;
@ -341,6 +360,7 @@ ghost_head = function (options) {
if (metaData.clientId && metaData.clientSecret) { if (metaData.clientId && metaData.clientSecret) {
head.push(writeMetaTag('ghost:client_id', metaData.clientId)); head.push(writeMetaTag('ghost:client_id', metaData.clientId));
head.push(writeMetaTag('ghost:client_secret', metaData.clientSecret)); head.push(writeMetaTag('ghost:client_secret', metaData.clientSecret));
head.push(getAjaxHelper());
} }
} }

64
core/shared/ghost-url.js Normal file
View file

@ -0,0 +1,64 @@
(function () {
'use strict';
function generateQueryString(object) {
var url = '?',
i;
if (!object) {
return '';
}
for (i in object) {
if (object.hasOwnProperty(i) && (!!object[i] || object[i] === false)) {
url += i + '=' + encodeURIComponent(object[i]) + '&';
}
}
return url.substring(0, url.length - 1);
}
var url = {
config: {
url: '{{api_url}}',
useOrigin: '{{useOrigin}}',
origin: '',
clientId: '',
clientSecret: ''
},
api: function () {
var args = Array.prototype.slice.call(arguments),
url = ((this.config.useOrigin === 'true')) ? this.config.origin + this.config.url : this.config.url,
queryOptions;
if (args.length && typeof args[args.length - 1] === 'object') {
queryOptions = args.pop();
} else {
queryOptions = {};
}
queryOptions.client_id = this.config.clientId;
queryOptions.client_secret = this.config.clientSecret;
if (args.length) {
args.forEach(function (el) {
url += el.replace(/^\/|\/$/g, '') + '/';
});
}
return url + generateQueryString(queryOptions);
}
};
if (typeof window !== 'undefined') {
url.config.origin = window.location.origin;
url.config.clientId = document.querySelector('meta[property=\'ghost:client_id\']').content;
url.config.clientSecret = document.querySelector('meta[property=\'ghost:client_secret\']').content;
window.ghost = window.ghost || {};
window.ghost.url = url;
}
if (typeof module !== 'undefined') {
module.exports = url;
}
})();

View file

@ -0,0 +1,90 @@
/* globals describe, afterEach, it */
/*jshint expr:true*/
var url = require('../../shared/ghost-url');
describe('Ghost Ajax Helper', function () {
afterEach(function () {
url.config = {};
});
it('renders basic url correctly when no arguments are presented & useOrigin is set to false', function () {
url.config = {
url: 'http://testblog.com/',
useOrigin: 'false',
clientId: '',
clientSecret: ''
};
url.api().should.equal('http://testblog.com/');
});
it('renders basic url correctly when no arguments are presented & useOrigin is set to true', function () {
url.config = {
url: '/url/',
useOrigin: 'true',
origin: 'http://originblog.com',
clientId: '',
clientSecret: ''
};
url.api().should.equal('http://originblog.com/url/');
});
it('strips arguments of forward and trailing slashes correctly', function () {
url.config = {
url: 'http://testblog.com/',
useOrigin: 'false',
clientId: '',
clientSecret: ''
};
url.api('a/', '/b', '/c/').should.equal('http://testblog.com/a/b/c/');
});
it('appends client_id & client_secret to query string automatically', function () {
url.config = {
url: 'http://testblog.com/',
useOrigin: 'false',
clientId: 'ghost-frontend',
clientSecret: 'notasecret'
};
url.api().should.equal('http://testblog.com/?client_id=ghost-frontend&client_secret=notasecret');
});
it('generates query parameters correctly', function () {
url.config = {
url: 'http://testblog.com/',
useOrigin: 'false',
clientId: 'ghost-frontend',
clientSecret: 'notasecret'
};
var rendered = url.api({a: 'string', b: 5, c: 'en coded'});
rendered.should.match(/http:\/\/testblog\.com\/\?/);
rendered.should.match(/client_id=ghost-frontend/);
rendered.should.match(/client_secret=notasecret/);
rendered.should.match(/a/);
rendered.should.match(/b=5/);
rendered.should.match(/c=en\%20coded/);
});
it('generates complex query correctly', function () {
url.config = {
url: '/blog/ghost/api/v0.1/',
useOrigin: 'true',
origin: 'https://testblog.com',
clientId: 'ghost-frontend',
clientSecret: 'notasecret'
};
var rendered = url.api('posts/', '/tags/', '/count', {include: 'tags,tests', page: 2});
rendered.should.match(/https:\/\/testblog\.com\/blog\/ghost\/api\/v0\.1\/posts\/tags\/count\/\?/);
rendered.should.match(/client_id=ghost-frontend/);
rendered.should.match(/client_secret=notasecret/);
rendered.should.match(/include=tags%2Ctests/);
rendered.should.match(/page=2/);
});
});

View file

@ -778,4 +778,104 @@ describe('{{ghost_head}} helper', function () {
}).catch(done); }).catch(done);
}); });
}); });
describe('with Ajax Helper', function () {
beforeEach(function () {
utils.overrideConfig({
url: '',
urlSSL: '',
forceAdminSSL: false
});
});
it('renders script tags with basic configuration', function (done) {
helpers.ghost_head.call(
{safeVersion: '0.3', context: ['paged', 'index'], post: false},
{data: {root: {context: []}}}
).then(function (rendered) {
should.exist(rendered);
expectGhostClientMeta(rendered);
rendered.string.should.match(/<script type="text\/javascript">\(function \(\) \{/);
rendered.string.should.match(/'use strict';/);
rendered.string.should.match(/<\/script>/);
done();
});
});
it('renders basic url correctly', function (done) {
utils.overrideConfig({
url: 'http://testurl.com/'
});
helpers.ghost_head.call(
{safeVersion: '0.3', context: ['paged', 'index'], post: false},
{data: {root: {context: []}}}
).then(function (rendered) {
should.exist(rendered);
expectGhostClientMeta(rendered);
rendered.string.should.match(/url: '\/ghost\/api\/v0\.1\/'/);
rendered.string.should.match(/useOrigin: 'true'/);
done();
});
});
it('renders basic url correctly with subdirectory', function (done) {
utils.overrideConfig({
url: 'http://testurl.com/blog/'
});
helpers.ghost_head.call(
{safeVersion: '0.3', context: ['paged', 'index'], post: false},
{data: {root: {context: []}}}
).then(function (rendered) {
should.exist(rendered);
expectGhostClientMeta(rendered);
rendered.string.should.match(/url: '\/blog\/ghost\/api\/v0\.1\/'/);
rendered.string.should.match(/useOrigin: 'true'/);
done();
});
});
it('renders correct https url with forceAdminSSL set', function (done) {
utils.overrideConfig({
url: 'http://testurl.com/',
forceAdminSSL: true
});
helpers.ghost_head.call(
{safeVersion: '0.3', context: ['paged', 'index'], post: false},
{data: {root: {context: []}}}
).then(function (rendered) {
should.exist(rendered);
expectGhostClientMeta(rendered);
rendered.string.should.match(/url: 'https:\/\/testurl\.com\/ghost\/api\/v0\.1\/'/);
rendered.string.should.match(/useOrigin: 'false'/);
done();
});
});
it('renders correct https url if urlSSL is set and forceAdminSSL is also set', function (done) {
utils.overrideConfig({
url: 'http://testurl.com/',
urlSSL: 'https://sslurl.com/',
forceAdminSSL: true
});
helpers.ghost_head.call(
{safeVersion: '0.3', context: ['paged', 'index'], post: false},
{data: {root: {context: []}}}
).then(function (rendered) {
should.exist(rendered);
expectGhostClientMeta(rendered);
rendered.string.should.match(/url: 'https:\/\/sslurl\.com\/ghost\/api\/v0\.1\/'/);
rendered.string.should.match(/useOrigin: 'false'/);
done();
});
});
});
}); });