33module . exports = function ( app ) {
44 var remotes = app . remotes ( ) ;
55
6- //register a handler to run before all remote methods so that we can
7- //transform JSON API structured JSON payload into something loopback
8- //can work with.
9- remotes . before ( '**' , function ( ctx , next ) {
6+ function deserializeJSONAPIPayload ( ctx , next ) {
107
11- //JSON api doesn't support PUT so we only need to apply our changes to
12- //POST and PATCH operations.
13- if ( ctx . req . method === 'POST' || ctx . req . method === 'PATCH' ) {
8+ var regexs = [
9+ / \. c r e a t e / ,
10+ / p r o t o t y p e \. u p d a t e A t t r i b u t e s / ,
11+ / p r o t o t y p e \. _ _ c r e a t e R e l a t i o n s h i p s _ _ / ,
12+ / p r o t o t y p e \. _ _ u p d a t e R e l a t i o n s h i p s _ _ /
13+ ] ;
14+
15+ var matches = regexs . filter ( function ( regex ) {
16+ return ctx . methodString . match ( regex ) ;
17+ } ) ;
18+
19+ if ( matches . length > 0 ) {
1420
1521 //set the JSON API Content-Type response header
1622 ctx . res . set ( { 'Content-Type' : 'application/vnd.api+json' } ) ;
1723
1824 //check the incoming payload data to ensure it is valid according to the
1925 //JSON API spec.
2026 var data = ctx . args . data ;
27+
28+ if ( ! data ) return next ( ) ;
29+
2130 if ( ! data . data ) return next ( new Error ( 'JSON API resource object must contain `data` property' ) ) ;
2231 if ( ! data . data . type ) return next ( new Error ( 'JSON API resource object must contain `data.type` property' ) ) ;
2332 if ( ctx . req . method === 'PATCH' ) {
@@ -29,5 +38,10 @@ module.exports = function (app) {
2938 }
3039
3140 next ( ) ;
32- } ) ;
41+ }
42+
43+ //register a handler to run before all remote methods so that we can
44+ //transform JSON API structured JSON payload into something loopback
45+ //can work with.
46+ remotes . before ( '**' , deserializeJSONAPIPayload ) ;
3347} ;
0 commit comments