File tree Expand file tree Collapse file tree 1 file changed +15
-5
lines changed Expand file tree Collapse file tree 1 file changed +15
-5
lines changed Original file line number Diff line number Diff line change 22const JSONAPISerializer = require ( 'jsonapi-serializer' ) . Serializer ;
33
44function jsonapify ( data , model , selfUrl ) {
5+ const idAttribute = Object . keys ( model . attributes ) . filter ( function ( attribute ) {
6+ return model . attributes [ attribute ] . primaryKey ;
7+ } ) [ 0 ] ;
8+
9+ const attributes = Object . keys ( model . attributes ) . filter ( function ( attribute ) { return attribute !== idAttribute ; } ) ;
10+
11+ const idParameter = ! Array . isArray ( data ) ? ( '/' + data [ idAttribute ] ) : '' ;
12+
513 return new JSONAPISerializer ( model . name , data , {
614 topLevelLinks : {
7- self : selfUrl
15+ self : '/' + selfUrl + idParameter
816 } ,
9- attributes : Object . keys ( model . attributes ) . filter ( function ( attribute ) {
10- return ! model . attributes [ attribute ] . primaryKey ;
11- } )
17+ attributes : attributes
1218 } ) ;
1319}
1420
1521module . exports = function ( options = { } ) { // eslint-disable-line no-unused-vars
1622 return function ( hook ) {
17- hook . result = jsonapify ( hook . result . data , hook . service . Model , hook . path ) ;
23+ if ( hook . method === 'find' ) {
24+ hook . result = jsonapify ( hook . result . data , hook . service . Model , hook . path ) ;
25+ } else if ( hook . method === 'get' ) {
26+ hook . result = jsonapify ( hook . result . toJSON ( ) , hook . service . Model , hook . path ) ;
27+ }
1828 return Promise . resolve ( hook ) ;
1929 } ;
2030} ;
You can’t perform that action at this time.
0 commit comments