@@ -32,75 +32,19 @@ export class JsonApiSerializer {
32
32
}
33
33
34
34
buildCreatePayload ( model : Model & Partial < JsonApiMapping > ) : JsonApiPayload {
35
- const ModelClass = this . modelMap [ model . type ] ;
36
-
37
- if ( ! ModelClass ) {
38
- console . warn ( `No model class found for type: ${ model . type } ` ) ;
39
- return this . buildDefaultPayload ( model ) ;
40
- }
41
-
42
- const prototype = ModelClass . prototype ;
43
-
44
- if ( typeof prototype . jsonApiMapping === 'function' ) {
45
- const mapping = prototype . jsonApiMapping . call ( model ) ;
46
-
47
- const payload : JsonApiPayload = {
48
- data : {
49
- type : model . type ,
50
- attributes : { } ,
51
- relationships : { } ,
52
- } ,
53
- } ;
54
-
55
- prototype . constructor . relationships . forEach ( ( relationship : {
56
- name : string ;
57
- type : string ;
58
- modelType : string ;
59
- } ) => {
60
- if ( relationship . type === 'array' ) {
61
- const value = ( model as any ) [ relationship . name ] ;
62
- if ( value ) {
63
- payload . data . relationships ! [ relationship . name ] = {
64
- data : value . map ( ( item : any ) => ( {
65
- type : relationship . modelType ,
66
- id : item . id ,
67
- } ) ) ,
68
- } ;
69
- }
70
- } else {
71
- const value = ( model as any ) [ relationship . name ] ;
72
- if ( value ) {
73
- payload . data . relationships ! [ relationship . name ] = {
74
- data : {
75
- type : relationship . modelType ,
76
- id : value . id ?? value ,
77
- } ,
78
- } ;
79
- }
80
- }
81
- } ) ;
82
-
83
- if ( mapping . attributes ) {
84
- mapping . attributes . forEach ( ( attr : string ) => {
85
- const value = ( model as any ) [ attr ] ;
86
- if ( value !== undefined && value !== '' ) {
87
- payload . data . attributes [ attr ] = value ;
88
- }
89
- } ) ;
90
- }
91
-
92
- return payload ;
93
- }
94
-
95
- return this . buildDefaultPayload ( model ) ;
35
+ return this . buildPayload ( model , false ) ;
96
36
}
97
37
98
38
buildUpdatePayload ( model : Model & Partial < JsonApiMapping > ) : JsonApiPayload {
39
+ return this . buildPayload ( model , true ) ;
40
+ }
41
+
42
+ private buildPayload ( model : Model & Partial < JsonApiMapping > , isUpdate : boolean ) : JsonApiPayload {
99
43
const ModelClass = this . modelMap [ model . type ] ;
100
44
101
45
if ( ! ModelClass ) {
102
46
console . warn ( `No model class found for type: ${ model . type } ` ) ;
103
- return this . buildDefaultPayload ( model ) ;
47
+ return this . buildDefaultPayload ( model , isUpdate ) ;
104
48
}
105
49
106
50
const prototype = ModelClass . prototype ;
@@ -110,13 +54,16 @@ export class JsonApiSerializer {
110
54
111
55
const payload : JsonApiPayload = {
112
56
data : {
113
- id : model . id ,
114
57
type : model . type ,
115
58
attributes : { } ,
116
59
relationships : { } ,
117
60
} ,
118
61
} ;
119
62
63
+ if ( isUpdate && model . id ) {
64
+ payload . data . id = model . id ;
65
+ }
66
+
120
67
prototype . constructor . relationships . forEach ( ( relationship : {
121
68
name : string ;
122
69
type : string ;
@@ -138,7 +85,7 @@ export class JsonApiSerializer {
138
85
payload . data . relationships ! [ relationship . name ] = {
139
86
data : {
140
87
type : relationship . modelType ,
141
- id : value . id ?? value ,
88
+ id : typeof value === 'string' ? value : value . id ,
142
89
} ,
143
90
} ;
144
91
}
@@ -157,7 +104,7 @@ export class JsonApiSerializer {
157
104
return payload ;
158
105
}
159
106
160
- return this . buildDefaultPayload ( model ) ;
107
+ return this . buildDefaultPayload ( model , isUpdate ) ;
161
108
}
162
109
163
110
buildRelationshipPayload ( model : Model , relationships : Array < Model > ) : JsonApiRelationshipsPayload {
@@ -174,20 +121,24 @@ export class JsonApiSerializer {
174
121
type : model . type ,
175
122
id : relationship . id ! ,
176
123
} ) ) ;
177
- const payload : JsonApiRelationshipsPayload = {
178
- data : data ,
179
- } ;
180
124
181
- return payload ;
125
+ return { data } ;
182
126
}
183
127
184
- private buildDefaultPayload ( model : Model ) : JsonApiPayload {
128
+ private buildDefaultPayload ( model : Model , includeId : boolean ) : JsonApiPayload {
185
129
const { type, id, meta, links, included, _relationships, ...attributes } = model ;
186
- return {
130
+
131
+ const payload : JsonApiPayload = {
187
132
data : {
188
133
type : model . type ,
189
134
attributes,
190
135
} ,
191
136
} ;
137
+
138
+ if ( includeId && id ) {
139
+ payload . data . id = id ;
140
+ }
141
+
142
+ return payload ;
192
143
}
193
144
}
0 commit comments