@@ -2,39 +2,82 @@ var _ = require('lodash');
22var RelUtils = require ( './utilities/relationship-utils' ) ;
33var utils = require ( './utils' ) ;
44
5- module . exports = function serializer ( type , data , relations , options ) {
5+ function defaultBeforeSerialize ( options , cb ) {
6+ cb ( null , options ) ;
7+ }
68
9+ function defaultSerialize ( options , cb ) {
710 var result = null ;
811 var resultData = { } ;
9- options . attributes = options . attributes || { } ;
10-
11- options . isRelationshipRequest = false ;
12-
13- if ( options . topLevelLinks . self . match ( / \/ r e l a t i o n s h i p s \/ / ) ) {
14- options . topLevelLinks . related = options . topLevelLinks . self . replace ( '/relationships/' , '/' ) ;
15- options . isRelationshipRequest = true ;
16- }
1712
18- if ( _ . isArray ( data ) ) {
19- result = parseCollection ( type , data , relations , options ) ;
20- } else if ( _ . isPlainObject ( data ) ) {
21- result = parseResource ( type , data , relations , options ) ;
13+ if ( _ . isArray ( options . results ) ) {
14+ result = parseCollection ( options . type , options . results , options . relationships , options ) ;
15+ } else if ( _ . isPlainObject ( options . results ) ) {
16+ result = parseResource ( options . type , options . results , options . relationships , options ) ;
2217 }
2318
24- resultData . data = result ;
25-
2619 if ( options . topLevelLinks ) {
2720 resultData . links = makeLinks ( options . topLevelLinks ) ;
2821 }
2922
23+ resultData . data = result ;
24+
3025 /**
3126 * If we're requesting to sideload relationships...
3227 */
3328 if ( options . requestedIncludes ) {
34- handleIncludes ( resultData , options . requestedIncludes , relations ) ;
29+ try {
30+ handleIncludes ( resultData , options . requestedIncludes , options . relationships ) ;
31+ } catch ( err ) {
32+ cb ( err ) ;
33+ }
34+ }
35+
36+ options . results = resultData ;
37+ cb ( null , options ) ;
38+ }
39+
40+ function defaultAfterSerialize ( options , cb ) {
41+ cb ( null , options ) ;
42+ }
43+
44+ module . exports = function serializer ( type , data , relations , options , cb ) {
45+ options = _ . clone ( options ) ;
46+ options . attributes = options . attributes || { } ;
47+
48+ options . isRelationshipRequest = false ;
49+
50+ if ( options . topLevelLinks . self . match ( / \/ r e l a t i o n s h i p s \/ / ) ) {
51+ options . topLevelLinks . related = options . topLevelLinks . self . replace ( '/relationships/' , '/' ) ;
52+ options . isRelationshipRequest = true ;
3553 }
54+ var serializeOptions ;
55+ var model = options . model ;
56+
57+ var beforeSerialize = ( typeof model . beforeJsonApiSerialize === 'function' ) ?
58+ model . beforeJsonApiSerialize : defaultBeforeSerialize ;
59+
60+ var serialize = ( typeof model . jsonApiSerialize === 'function' ) ?
61+ model . jsonApiSerialize : defaultSerialize ;
62+
63+ var afterSerialize = ( typeof model . afterJsonApiSerialize === 'function' ) ?
64+ model . afterJsonApiSerialize : defaultAfterSerialize ;
3665
37- return resultData ;
66+ serializeOptions = _ . defaults ( options , {
67+ type : type ,
68+ results : data ,
69+ relationships : relations
70+ } ) ;
71+ beforeSerialize ( serializeOptions , function ( err , serializeOptions ) {
72+ if ( err ) return cb ( err ) ;
73+ serialize ( serializeOptions , function ( err , serializeOptions ) {
74+ if ( err ) return cb ( err ) ;
75+ afterSerialize ( serializeOptions , function ( err , serializeOptions ) {
76+ if ( err ) return cb ( err ) ;
77+ return cb ( null , serializeOptions . results ) ;
78+ } ) ;
79+ } ) ;
80+ } ) ;
3881} ;
3982
4083/**
0 commit comments