Skip to content

Commit 731c27c

Browse files
authored
Merge pull request #11 from holidayextras/WEB-10209
WEB-10209 Proxy specific values through json-api to subresources
2 parents d585354 + 2e22e93 commit 731c27c

12 files changed

Lines changed: 672 additions & 581 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,5 @@ build/Release
8181
node_modules
8282

8383
# make-up
84-
.eslintrc
84+
.eslintrc
85+
.eslintcache

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.9.4

circle.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
machine:
22
node:
3-
version: 4.4.4
3+
version: 6.9.4
44
dependencies:
55
pre:
66
- npm install git+ssh://git@github.com:holidayextras/deployment-helpers.git
@@ -11,7 +11,7 @@ test:
1111
- mkdir -p $CIRCLE_TEST_REPORTS/unit
1212
- mkdir -p $CIRCLE_TEST_REPORTS/coverage
1313
override:
14-
- node_modules/.bin/make-up lib test
14+
- npm run lint
1515
- istanbul cover _mocha -- --timeout 15000 --recursive test/**/*Test.js -R xunit > $CIRCLE_TEST_REPORTS/unit/results.xml
1616
post:
1717
- cp -r ./coverage/ $CIRCLE_ARTIFACTS/coverage

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* jslint node: true */
2-
'use strict';
2+
'use strict'
33

44
/*
55
* @name /index.js
@@ -8,4 +8,4 @@
88
* @author Kevin Hodges <kevin.hodges@holidayextras.com>
99
*/
1010

11-
module.exports = require( './lib/pluginJsonapi.js' );
11+
module.exports = require('./lib/pluginJsonapi.js')

lib/pluginJsonapi.js

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,68 @@
1-
'use strict';
1+
'use strict'
22

3-
var Boom = require('boom');
4-
var _ = require('lodash');
5-
var pluginName = 'pluginJsonapi';
6-
var utilities = require('./utilities');
3+
var Boom = require('boom')
4+
var _ = require('lodash')
5+
var pluginName = 'pluginJsonapi'
6+
var utilities = require('./utilities')
77

88
// some paths sinply don't want to be jsonapi'd
99
var ignorePaths = [
1010
'/', // Lout documentation and AWS healthcheck
1111
'/css/{path*}' // Lout documentation
12-
];
12+
]
1313

1414
// certain status codes shouldn't be jsonapi'd
1515
var ignoreStatusCodes = [
1616
201, // Created (used on POST)
1717
204 // No Content (used on DELETE)
18-
];
18+
]
1919

20-
var pluginJsonApi = module.exports = {};
20+
var proxyableKeys = [
21+
'contentVersion',
22+
'lang',
23+
'ticketRates'
24+
]
2125

22-
pluginJsonApi.errorHandler = function errorHandler(request, reply, error) {
23-
request.log( ['error', __filename, pluginName], {
26+
var pluginJsonApi = module.exports = {}
27+
28+
pluginJsonApi.errorHandler = function errorHandler (request, reply, error) {
29+
request.log(['error', __filename, pluginName], {
2430
error: error.toString(),
2531
data: request.data
26-
} );
32+
})
2733

28-
return reply(Boom.internal(error));
29-
};
34+
return reply(Boom.internal(error))
35+
}
3036

31-
pluginJsonApi.alsoMakeItSo = function alsoMakeItSo(request, reply) { // eslint-disable-line consistent-return
37+
pluginJsonApi.alsoMakeItSo = function alsoMakeItSo (request, reply) {
38+
// eslint-disable-line consistent-return
3239

3340
try {
3441
// If there was no error and it's not one of our ignoredPaths or ignored status codes, jsonapi it
3542
if (request.response.isBoom || _.includes(ignorePaths, _.get(request, 'route.path')) || _.includes(ignoreStatusCodes, request.response.statusCode)) {
3643
// return early
37-
return reply.continue();
44+
return reply.continue()
3845
}
3946

4047
// Make the response a bit more accessible
41-
var result = _.get(request, 'response.source');
48+
var result = _.get(request, 'response.source')
4249
// check a bind configuration is present
43-
if ( !_.get(request, 'route.settings.bind.resourceName')) {
44-
throw new Error('configuration bind.resourceName not found on handler');
50+
if (!_.get(request, 'route.settings.bind.resourceName')) {
51+
throw new Error('configuration bind.resourceName not found on handler')
4552
}
4653

4754
// Get the current resource from the result
48-
var resources = result[request.route.settings.bind.resourceName];
55+
var resources = result[request.route.settings.bind.resourceName]
4956
if (!resources) {
5057
// no resources, return early
51-
return reply.continue();
58+
return reply.continue()
5259
}
5360

5461
var stateObject = {
5562
request: request,
56-
resources: resources
57-
};
63+
resources: resources,
64+
proxyableKeys: proxyableKeys
65+
}
5866

5967
/*
6068
* Flow
@@ -64,23 +72,22 @@ pluginJsonApi.alsoMakeItSo = function alsoMakeItSo(request, reply) { // eslint-d
6472
* resolveLinkedData() - resolve the sub resources which we have collected previously
6573
*/
6674
utilities.collectIncludes(stateObject)
75+
.then(utilities.collectProxyableValues)
6776
.then(utilities.getSubResources)
6877
.then(utilities.getResources)
6978
.then(utilities.resolveLinkedData)
70-
.then(function() {
79+
.then(function () {
7180
// all done
72-
return reply.continue();
81+
return reply.continue()
7382
})
74-
.fail(function(error) {
75-
return pluginJsonApi.errorHandler(request, reply, error);
83+
.fail(function (error) {
84+
return pluginJsonApi.errorHandler(request, reply, error)
7685
})
77-
.done();
78-
79-
} catch ( error ) {
80-
return pluginJsonApi.errorHandler(request, reply, error);
86+
.done()
87+
} catch (error) {
88+
return pluginJsonApi.errorHandler(request, reply, error)
8189
}
82-
};
83-
90+
}
8491

8592
/**
8693
* Registers the plugin when the hapi server bootstraps - follows the convention for Hapi.js plugins
@@ -89,23 +96,22 @@ pluginJsonApi.alsoMakeItSo = function alsoMakeItSo(request, reply) { // eslint-d
8996
* @param {function} next - the next plugin to be set up
9097
* @returns {undefined} Nothing - calls the next plugin
9198
*/
92-
pluginJsonApi.register = function(server, options, next) {
93-
94-
function makeItSo(request, reply) {
95-
return pluginJsonApi.alsoMakeItSo(request, reply);
99+
pluginJsonApi.register = function (server, options, next) {
100+
function makeItSo (request, reply) {
101+
return pluginJsonApi.alsoMakeItSo(request, reply)
96102
}
97103

98104
// Extend the onPreResponse for EVERY request through the api
99-
server.ext('onPreResponse', makeItSo);
105+
server.ext('onPreResponse', makeItSo)
100106

101107
// In order to test this functionality it needs to be exposed on the server.
102108
if (process.env.NODE_ENV === 'test') {
103-
server.expose('makeItSo', makeItSo);
109+
server.expose('makeItSo', makeItSo)
104110
}
105111

106-
next();
107-
};
112+
next()
113+
}
108114

109115
pluginJsonApi.register.attributes = {
110116
pkg: require('../package.json')
111-
};
117+
}

0 commit comments

Comments
 (0)