Skip to content

Commit cedb299

Browse files
committed
Add hapi 17 support, require node 8
1 parent b46ce65 commit cedb299

File tree

5 files changed

+155
-220
lines changed

5 files changed

+155
-220
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
coverage
3+
package-lock.json

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: node_js
22
node_js:
3-
- 6
43
- 8
54

65
script:

index.js

+24-32
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,20 @@ exports.formatObject = formatObject;
2121
exports.formatCollection = formatCollection;
2222
exports.toAPI = toAPI;
2323

24-
exports.register = function (server, options, next) {
25-
server.expose('formatObject', exports.formatObject);
26-
server.expose('formatObjectWithOptions', exports.formatObjectWithOptions);
27-
server.expose('formatCollection', exports.formatCollection);
28-
server.expose('formatCollectionWithOptions', exports.formatCollectionWithOptions);
29-
server.expose('toAPI', exports.toAPI);
30-
31-
server.ext('onPreResponse', formatError);
32-
33-
server.decorate('reply', 'jsonApi', replyJsonApi);
34-
server.decorate('reply', 'jsonApiFunction', replyJsonApiFunction);
35-
36-
next();
37-
};
38-
39-
exports.register.attributes = {
40-
name: 'service-jsonapi'
24+
exports.plugin = {
25+
name: 'service-jsonapi',
26+
register(server) {
27+
server.expose('formatObject', exports.formatObject);
28+
server.expose('formatObjectWithOptions', exports.formatObjectWithOptions);
29+
server.expose('formatCollection', exports.formatCollection);
30+
server.expose('formatCollectionWithOptions', exports.formatCollectionWithOptions);
31+
server.expose('toAPI', exports.toAPI);
32+
33+
server.ext('onPreResponse', formatError);
34+
35+
server.decorate('toolkit', 'jsonApi', replyJsonApi);
36+
server.decorate('toolkit', 'jsonApiFunction', replyJsonApiFunction);
37+
}
4138
};
4239

4340
const internals = {
@@ -54,7 +51,7 @@ const internals = {
5451
* @return {Hapi.ResponseObject}
5552
*/
5653
function replyJsonApi(type, objects, options) {
57-
return replyFunction.bind(this)(type, objects, options); // jshint ignore:line
54+
return replyFunction.bind(this)(type, objects, options);
5855
}
5956

6057
/**
@@ -67,7 +64,7 @@ function replyJsonApi(type, objects, options) {
6764
* @return {Function} returns curried replyFunction that accepts objects to format as a parameter.
6865
*/
6966
function replyJsonApiFunction(type, options, code) {
70-
return R.curry(R.bind(replyFunction, this))(type, R.__, options, code); // jshint ignore:line
67+
return R.curry(R.bind(replyFunction, this))(type, R.__, options, code);
7168
}
7269

7370
/**
@@ -84,13 +81,13 @@ function replyFunction(type, objects, options, code) {
8481
let resp;
8582
options = options || {};
8683
if (objects instanceof Array) {
87-
resp = this.response(formatCollection(type, objects, options)); // jshint ignore:line
84+
resp = this.response(formatCollection(type, objects, options));
8885
} else {
89-
resp = this.response(formatObject(type, objects, options)); // jshint ignore:line
86+
resp = this.response(formatObject(type, objects, options));
9087
}
9188

9289
if (code) {
93-
resp.code(code); // jshint ignore:line
90+
resp.code(code);
9491
}
9592

9693
return resp;
@@ -99,19 +96,14 @@ function replyFunction(type, objects, options, code) {
9996
/**
10097
* onPreResponse handler for formatting error objects according to JSONAPI spec.
10198
* @param {Hapi.Request} req
102-
* @param {Hapi.Reply} reply
99+
* @param {Hapi.Toolkit} h
103100
* @return {Hapi.ResponseObject}
104101
*/
105-
function formatError(req, reply) {
106-
let response = req.response;
102+
function formatError(req, h) {
103+
const response = req.response;
107104

108105
if (!response.isBoom) {
109-
return reply.continue();
110-
}
111-
112-
if (response.data && response.data.isBoom) {
113-
// fix for https://github.com/hapijs/hapi/issues/3587
114-
response = response.data;
106+
return h.continue;
115107
}
116108

117109
const errorObject = {
@@ -137,7 +129,7 @@ function formatError(req, reply) {
137129
}
138130
}
139131

140-
return reply({
132+
return h.response({
141133
errors: [errorObject]
142134
}).code(response.output.statusCode);
143135
}

package.json

+10-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"release:major": "npm run ci && release-it -n -i major"
1616
},
1717
"engines": {
18-
"node": ">=6.x.x"
18+
"node": ">=8.x.x"
1919
},
2020
"repository": {
2121
"type": "git",
@@ -33,22 +33,25 @@
3333
"homepage": "https://github.com/aptoma/hapi-json-api",
3434
"eslintConfig": {
3535
"extends": "@aptoma/eslint-config",
36+
"parserOptions": {
37+
"ecmaVersion": "2017"
38+
},
3639
"env": {
3740
"node": true,
3841
"mocha": true,
3942
"es6": true
4043
}
4144
},
4245
"dependencies": {
43-
"ramda": "^0.22.1"
46+
"ramda": "^0.25.0"
4447
},
4548
"devDependencies": {
46-
"@aptoma/eslint-config": "^4.0.0",
47-
"chai": "^3.5.0",
48-
"eslint": "^3.6.1",
49-
"hapi": "^16.6.2",
49+
"@aptoma/eslint-config": "^7.0.1",
50+
"chai": "^4.1.2",
51+
"eslint": "^4.12.1",
52+
"hapi": "^17.1.1",
5053
"istanbul": "^0.4.5",
51-
"mocha": "^3.2.0",
54+
"mocha": "^4.0.1",
5255
"mocha-only-detector": "^0.1.0",
5356
"release-it": "^2.4.3"
5457
}

0 commit comments

Comments
 (0)