From 46c806358b02c21f51d0f8490f3a2cd38f73171f Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Tue, 6 Nov 2018 14:08:13 +0100 Subject: [PATCH] Extended resource lookup in {{get}} helper refs #10061 - prep task to add authors for content API --- core/server/helpers/get.js | 39 ++++++++++++++----- core/test/unit/helpers/get_spec.js | 61 +++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 10 deletions(-) diff --git a/core/server/helpers/get.js b/core/server/helpers/get.js index 3bd6c7ca84..e061aadb36 100644 --- a/core/server/helpers/get.js +++ b/core/server/helpers/get.js @@ -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 diff --git a/core/test/unit/helpers/get_spec.js b/core/test/unit/helpers/get_spec.js index 89c49c2344..f7ff0ad4bc 100644 --- a/core/test/unit/helpers/get_spec.js +++ b/core/test/unit/helpers/get_spec.js @@ -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(