Extended resource lookup in {{get}} helper

refs #10061

- prep task to add authors for content API
This commit is contained in:
kirrg001 2018-11-06 14:08:13 +01:00
parent 3345618731
commit 46c806358b
2 changed files with 90 additions and 10 deletions

View File

@ -12,12 +12,33 @@ var proxy = require('./proxy'),
api = proxy.api,
labs = proxy.labs,
resources,
pathAliases,
get;
// Endpoints that the helper is able to access
resources = ['posts', 'tags', 'users', 'pages'];
/**
* v0.1: users, posts, tags
* v2: authors, pages, posts, tags
*
* @NOTE: if you use "users" in v2, we should fallback to authors
*/
const RESOURCES = {
posts: {
alias: 'posts',
resource: 'posts'
},
tags: {
alias: 'tags',
resource: 'tags'
},
users: {
alias: 'authors',
resource: 'users'
},
pages: {
alias: 'pages',
resource: 'posts'
}
};
// Short forms of paths which we should understand
pathAliases = {
@ -32,7 +53,7 @@ pathAliases = {
* @param {Object} options
* @returns {boolean}
*/
function isBrowse(resource, options) {
function isBrowse(options) {
var browse = true;
if (options.id || options.slug) {
@ -108,7 +129,6 @@ get = function get(resource, options) {
const data = createFrame(options.data);
const apiVersion = data.root._locals.apiVersion;
let apiOptions = options.hash;
let apiMethod;
if (!options.fn) {
data.error = i18n.t('warnings.helpers.mustBeCalledAsBlock', {helperName: 'get'});
@ -116,18 +136,19 @@ get = function get(resource, options) {
return Promise.resolve();
}
if (!_.includes(resources, resource)) {
if (!RESOURCES[resource]) {
data.error = i18n.t('warnings.helpers.get.invalidResource');
logging.warn(data.error);
return Promise.resolve(options.inverse(self, {data: data}));
}
// Determine if this is a read or browse
apiMethod = isBrowse(resource, apiOptions) ? api[apiVersion][resource].browse : api[apiVersion][resource].read;
const controller = api[apiVersion][RESOURCES[resource].alias] ? RESOURCES[resource].alias : RESOURCES[resource].resource;
const action = isBrowse(apiOptions) ? 'browse' : 'read';
// Parse the options we're going to pass to the API
apiOptions = parseOptions(this, apiOptions);
return apiMethod(apiOptions).then(function success(result) {
return api[apiVersion][controller][action](apiOptions).then(function success(result) {
var blockParams;
// block params allows the theme developer to name the data using something like

View File

@ -56,7 +56,7 @@ describe('{{#get}} helper', function () {
}).catch(done);
});
describe('posts', function () {
describe('posts v0.1', function () {
var browsePostsStub, readPostsStub, readTagsStub, readUsersStub, testPostsArr = [
{id: 1, title: 'Test Post 1', author: {slug: 'cameron'}},
{id: 2, title: 'Test Post 2', author: {slug: 'cameron'}, featured: true},
@ -256,6 +256,65 @@ describe('{{#get}} helper', function () {
});
});
describe('users v0.1', function () {
let browseUsersStub;
const meta = {pagination: {}};
beforeEach(function () {
browseUsersStub = sandbox.stub(api["v0.1"].users, 'browse');
browseUsersStub.returns(new Promise.resolve({users: [], meta: meta}));
});
it('browse users v0.1', function (done) {
helpers.get.call(
{},
'users',
{hash: {}, data: locals, fn: fn, inverse: inverse}
).then(function () {
labsStub.calledOnce.should.be.true();
fn.called.should.be.true();
fn.firstCall.args[0].should.be.an.Object().with.property('users');
fn.firstCall.args[0].users.should.eql([]);
inverse.called.should.be.false();
done();
}).catch(done);
});
});
describe('users v2', function () {
let browseUsersStub;
const meta = {pagination: {}};
beforeEach(function () {
locals = {root: {_locals: {apiVersion: 'v2'}}};
browseUsersStub = sandbox.stub(api["v2"], 'users').get(() => {
return {
browse: sandbox.stub().resolves({users: [], meta: meta})
};
});
});
it('browse users', function (done) {
helpers.get.call(
{},
'users',
{hash: {}, data: locals, fn: fn, inverse: inverse}
).then(function () {
labsStub.calledOnce.should.be.true();
fn.called.should.be.true();
fn.firstCall.args[0].should.be.an.Object().with.property('users');
fn.firstCall.args[0].users.should.eql([]);
inverse.called.should.be.false();
done();
}).catch(done);
});
});
describe('general error handling', function () {
it('should return an error for an unknown resource', function (done) {
helpers.get.call(