@@ -21,23 +21,20 @@ exports.formatObject = formatObject;
21
21
exports . formatCollection = formatCollection ;
22
22
exports . toAPI = toAPI ;
23
23
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
+ }
41
38
} ;
42
39
43
40
const internals = {
@@ -54,7 +51,7 @@ const internals = {
54
51
* @return {Hapi.ResponseObject }
55
52
*/
56
53
function replyJsonApi ( type , objects , options ) {
57
- return replyFunction . bind ( this ) ( type , objects , options ) ; // jshint ignore:line
54
+ return replyFunction . bind ( this ) ( type , objects , options ) ;
58
55
}
59
56
60
57
/**
@@ -67,7 +64,7 @@ function replyJsonApi(type, objects, options) {
67
64
* @return {Function } returns curried replyFunction that accepts objects to format as a parameter.
68
65
*/
69
66
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 ) ;
71
68
}
72
69
73
70
/**
@@ -84,13 +81,13 @@ function replyFunction(type, objects, options, code) {
84
81
let resp ;
85
82
options = options || { } ;
86
83
if ( objects instanceof Array ) {
87
- resp = this . response ( formatCollection ( type , objects , options ) ) ; // jshint ignore:line
84
+ resp = this . response ( formatCollection ( type , objects , options ) ) ;
88
85
} else {
89
- resp = this . response ( formatObject ( type , objects , options ) ) ; // jshint ignore:line
86
+ resp = this . response ( formatObject ( type , objects , options ) ) ;
90
87
}
91
88
92
89
if ( code ) {
93
- resp . code ( code ) ; // jshint ignore:line
90
+ resp . code ( code ) ;
94
91
}
95
92
96
93
return resp ;
@@ -99,19 +96,14 @@ function replyFunction(type, objects, options, code) {
99
96
/**
100
97
* onPreResponse handler for formatting error objects according to JSONAPI spec.
101
98
* @param {Hapi.Request } req
102
- * @param {Hapi.Reply } reply
99
+ * @param {Hapi.Toolkit } h
103
100
* @return {Hapi.ResponseObject }
104
101
*/
105
- function formatError ( req , reply ) {
106
- let response = req . response ;
102
+ function formatError ( req , h ) {
103
+ const response = req . response ;
107
104
108
105
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 ;
115
107
}
116
108
117
109
const errorObject = {
@@ -137,7 +129,7 @@ function formatError(req, reply) {
137
129
}
138
130
}
139
131
140
- return reply ( {
132
+ return h . response ( {
141
133
errors : [ errorObject ]
142
134
} ) . code ( response . output . statusCode ) ;
143
135
}
0 commit comments