@@ -7,6 +7,10 @@ import { dasherize } from '@ember/string';
7
7
import { pluralize , singularize } from 'ember-inflector' ;
8
8
9
9
import { DEBUG } from '@ember-data/env' ;
10
+ import { type Snapshot , upgradeStore } from '@ember-data/legacy-compat/-private' ;
11
+ import type Store from '@ember-data/store' ;
12
+ import type { ModelSchema } from '@ember-data/store/-types/q/ds-model' ;
13
+ import type { RelationshipSchema } from '@warp-drive/core-types/schema' ;
10
14
11
15
import JSONSerializer from './json' ;
12
16
@@ -135,7 +139,7 @@ import JSONSerializer from './json';
135
139
@public
136
140
@extends JSONSerializer
137
141
*/
138
- const JSONAPISerializer = JSONSerializer . extend ( {
142
+ class JSONAPISerializer extends JSONSerializer {
139
143
/**
140
144
@method _normalizeDocumentHelper
141
145
@param {Object } documentHash
@@ -157,7 +161,7 @@ const JSONAPISerializer = JSONSerializer.extend({
157
161
}
158
162
159
163
if ( Array . isArray ( documentHash . included ) ) {
160
- const ret = new Array ( ) ;
164
+ const ret = [ ] ;
161
165
for ( let i = 0 ; i < documentHash . included . length ; i ++ ) {
162
166
const included = documentHash . included [ i ] ;
163
167
const normalized = this . _normalizeResourceHelper ( included ) ;
@@ -171,7 +175,7 @@ const JSONAPISerializer = JSONSerializer.extend({
171
175
}
172
176
173
177
return documentHash ;
174
- } ,
178
+ }
175
179
176
180
/**
177
181
@method _normalizeRelationshipDataHelper
@@ -183,7 +187,7 @@ const JSONAPISerializer = JSONSerializer.extend({
183
187
relationshipDataHash . type = this . modelNameFromPayloadKey ( relationshipDataHash . type ) ;
184
188
185
189
return relationshipDataHash ;
186
- } ,
190
+ }
187
191
188
192
/**
189
193
@method _normalizeResourceHelper
@@ -192,22 +196,24 @@ const JSONAPISerializer = JSONSerializer.extend({
192
196
@private
193
197
*/
194
198
_normalizeResourceHelper ( resourceHash ) {
199
+ const { store } = this ;
200
+ upgradeStore ( store ) ;
195
201
assert ( this . warnMessageForUndefinedType ( ) , resourceHash . type ) ;
196
202
197
203
const modelName = this . modelNameFromPayloadKey ( resourceHash . type ) ;
198
204
199
- if ( ! this . store . getSchemaDefinitionService ( ) . doesTypeExist ( modelName ) ) {
205
+ if ( ! store . getSchemaDefinitionService ( ) . doesTypeExist ( modelName ) ) {
200
206
warn ( this . warnMessageNoModelForType ( modelName , resourceHash . type , 'modelNameFromPayloadKey' ) , false , {
201
207
id : 'ds.serializer.model-for-type-missing' ,
202
208
} ) ;
203
209
return null ;
204
210
}
205
211
206
- const modelClass = this . store . modelFor ( modelName ) ;
207
- const serializer = this . store . serializerFor ( modelName ) ;
212
+ const modelClass = store . modelFor ( modelName ) ;
213
+ const serializer = store . serializerFor ( modelName ) ;
208
214
const { data } = serializer . normalize ( modelClass , resourceHash ) ;
209
215
return data ;
210
- } ,
216
+ }
211
217
212
218
/**
213
219
Normalize some data and push it into the store.
@@ -217,10 +223,10 @@ const JSONAPISerializer = JSONSerializer.extend({
217
223
@param {Store } store
218
224
@param {Object } payload
219
225
*/
220
- pushPayload ( store , payload ) {
226
+ pushPayload ( store : Store , payload ) {
221
227
const normalizedPayload = this . _normalizeDocumentHelper ( payload ) ;
222
228
store . push ( normalizedPayload ) ;
223
- } ,
229
+ }
224
230
225
231
/**
226
232
@method _normalizeResponse
@@ -233,10 +239,10 @@ const JSONAPISerializer = JSONSerializer.extend({
233
239
@return {Object } JSON-API Document
234
240
@private
235
241
*/
236
- _normalizeResponse ( store , primaryModelClass , payload , id , requestType , isSingle ) {
242
+ _normalizeResponse ( store : Store , primaryModelClass : ModelSchema , payload , id , requestType , isSingle ) {
237
243
const normalizedPayload = this . _normalizeDocumentHelper ( payload ) ;
238
244
return normalizedPayload ;
239
- } ,
245
+ }
240
246
241
247
normalizeQueryRecordResponse ( ) {
242
248
const normalized = this . _super ( ...arguments ) ;
@@ -247,9 +253,9 @@ const JSONAPISerializer = JSONSerializer.extend({
247
253
) ;
248
254
249
255
return normalized ;
250
- } ,
256
+ }
251
257
252
- extractAttributes ( modelClass , resourceHash ) {
258
+ extractAttributes ( modelClass : ModelSchema , resourceHash ) {
253
259
const attributes = { } ;
254
260
255
261
if ( resourceHash . attributes ) {
@@ -270,7 +276,7 @@ const JSONAPISerializer = JSONSerializer.extend({
270
276
}
271
277
272
278
return attributes ;
273
- } ,
279
+ }
274
280
275
281
/**
276
282
Returns a relationship formatted as a JSON-API "relationship object".
@@ -297,7 +303,7 @@ const JSONAPISerializer = JSONSerializer.extend({
297
303
}
298
304
299
305
return relationshipHash ;
300
- } ,
306
+ }
301
307
302
308
/**
303
309
Returns the resource's relationships formatted as a JSON-API "relationships object".
@@ -310,12 +316,12 @@ const JSONAPISerializer = JSONSerializer.extend({
310
316
@param {Object } resourceHash
311
317
@return {Object }
312
318
*/
313
- extractRelationships ( modelClass , resourceHash ) {
319
+ extractRelationships ( modelClass : ModelSchema , resourceHash ) {
314
320
const relationships = { } ;
315
321
316
322
if ( resourceHash . relationships ) {
317
323
modelClass . eachRelationship ( ( key , relationshipMeta ) => {
318
- const relationshipKey = this . keyForRelationship ( key , relationshipMeta . kind , 'deserialize' ) ;
324
+ const relationshipKey = this . keyForRelationship ( key , modelClass , 'deserialize' ) ;
319
325
if ( resourceHash . relationships [ relationshipKey ] !== undefined ) {
320
326
const relationshipHash = resourceHash . relationships [ relationshipKey ] ;
321
327
relationships [ key ] = this . extractRelationship ( relationshipHash ) ;
@@ -326,7 +332,7 @@ const JSONAPISerializer = JSONSerializer.extend({
326
332
resourceHash . relationships [ key ] !== undefined
327
333
) {
328
334
assert (
329
- `Your payload for '${ modelClass . modelName } ' contains '${ key } ', but your serializer is setup to look for '${ relationshipKey } '. This is most likely because Ember Data's JSON API serializer dasherizes relationship keys by default. You should subclass JSONAPISerializer and implement 'keyForRelationship(key) { return key; }' to prevent Ember Data from customizing your relationship keys.` ,
335
+ `Your payload for '${ modelClass . modelName } ' contains '${ key } ', but your serializer is setup to look for '${ relationshipKey } '. This is most likely because Ember Data's JSON API serializer dasherizes relationship keys by default. You should subclass JSONAPISerializer and implement 'keyForRelationship(key) { return key; }' to prevent EmberData from customizing your relationship keys.` ,
330
336
false
331
337
) ;
332
338
}
@@ -335,7 +341,7 @@ const JSONAPISerializer = JSONSerializer.extend({
335
341
}
336
342
337
343
return relationships ;
338
- } ,
344
+ }
339
345
340
346
/**
341
347
@method _extractType
@@ -344,9 +350,9 @@ const JSONAPISerializer = JSONSerializer.extend({
344
350
@return {String }
345
351
@private
346
352
*/
347
- _extractType ( modelClass , resourceHash ) {
353
+ _extractType ( modelClass : ModelSchema , resourceHash ) {
348
354
return this . modelNameFromPayloadKey ( resourceHash . type ) ;
349
- } ,
355
+ }
350
356
351
357
/**
352
358
Dasherizes and singularizes the model name in the payload to match
@@ -360,9 +366,9 @@ const JSONAPISerializer = JSONSerializer.extend({
360
366
@param {String } key
361
367
@return {String } the model's modelName
362
368
*/
363
- modelNameFromPayloadKey ( key ) {
369
+ modelNameFromPayloadKey ( key : string ) : string {
364
370
return dasherize ( singularize ( key ) ) ;
365
- } ,
371
+ }
366
372
367
373
/**
368
374
Converts the model name to a pluralized version of the model name.
@@ -375,11 +381,11 @@ const JSONAPISerializer = JSONSerializer.extend({
375
381
@param {String } modelName
376
382
@return {String }
377
383
*/
378
- payloadKeyFromModelName ( modelName ) {
384
+ payloadKeyFromModelName ( modelName : string ) : string {
379
385
return pluralize ( modelName ) ;
380
- } ,
386
+ }
381
387
382
- normalize ( modelClass , resourceHash ) {
388
+ normalize ( modelClass : ModelSchema , resourceHash ) {
383
389
if ( resourceHash . attributes ) {
384
390
this . normalizeUsingDeclaredMapping ( modelClass , resourceHash . attributes ) ;
385
391
}
@@ -402,7 +408,7 @@ const JSONAPISerializer = JSONSerializer.extend({
402
408
this . applyTransforms ( modelClass , data . attributes ) ;
403
409
404
410
return { data } ;
405
- } ,
411
+ }
406
412
407
413
/**
408
414
`keyForAttribute` can be used to define rules for how to convert an
@@ -432,9 +438,9 @@ const JSONAPISerializer = JSONSerializer.extend({
432
438
@param {String } method
433
439
@return {String } normalized key
434
440
*/
435
- keyForAttribute ( key , method ) {
441
+ keyForAttribute ( key : string , method ) {
436
442
return dasherize ( key ) ;
437
- } ,
443
+ }
438
444
439
445
/**
440
446
`keyForRelationship` can be used to define a custom key when
@@ -460,13 +466,13 @@ const JSONAPISerializer = JSONSerializer.extend({
460
466
@method keyForRelationship
461
467
@public
462
468
@param {String } key
463
- @param {String } typeClass
469
+ @param {ModelSchema } schema
464
470
@param {String } method
465
471
@return {String } normalized key
466
472
*/
467
- keyForRelationship ( key , typeClass , method ) {
473
+ keyForRelationship ( key : string , schema : ModelSchema , method : 'serialize' | 'deserialize' ) : string {
468
474
return dasherize ( key ) ;
469
- } ,
475
+ }
470
476
471
477
/**
472
478
Called when a record is saved in order to convert the
@@ -637,14 +643,14 @@ const JSONAPISerializer = JSONSerializer.extend({
637
643
@param {Object } options
638
644
@return {Object } json
639
645
*/
640
- serialize ( snapshot , options ) {
646
+ serialize ( snapshot : Snapshot , options ) {
641
647
const data = this . _super ( ...arguments ) ;
642
648
data . type = this . payloadKeyFromModelName ( snapshot . modelName ) ;
643
649
644
650
return { data } ;
645
- } ,
651
+ }
646
652
647
- serializeAttribute ( snapshot , json , key , attribute ) {
653
+ serializeAttribute ( snapshot : Snapshot , json , key : string , attribute ) {
648
654
const type = attribute . type ;
649
655
650
656
if ( this . _canSerialize ( key ) ) {
@@ -665,9 +671,9 @@ const JSONAPISerializer = JSONSerializer.extend({
665
671
666
672
json . attributes [ payloadKey ] = value ;
667
673
}
668
- } ,
674
+ }
669
675
670
- serializeBelongsTo ( snapshot , json , relationship ) {
676
+ serializeBelongsTo ( snapshot : Snapshot , json , relationship : RelationshipSchema ) {
671
677
const name = relationship . name ;
672
678
673
679
if ( this . _canSerialize ( name ) ) {
@@ -680,7 +686,7 @@ const JSONAPISerializer = JSONSerializer.extend({
680
686
const schema = this . store . modelFor ( snapshot . modelName ) ;
681
687
let payloadKey = this . _getMappedKey ( name , schema ) ;
682
688
if ( payloadKey === name ) {
683
- payloadKey = this . keyForRelationship ( name , 'belongsTo' , 'serialize' ) ;
689
+ payloadKey = this . keyForRelationship ( name , schema , 'serialize' ) ;
684
690
}
685
691
686
692
let data = null ;
@@ -696,9 +702,9 @@ const JSONAPISerializer = JSONSerializer.extend({
696
702
json . relationships [ payloadKey ] = { data } ;
697
703
}
698
704
}
699
- } ,
705
+ }
700
706
701
- serializeHasMany ( snapshot , json , relationship ) {
707
+ serializeHasMany ( snapshot : Snapshot , json , relationship : RelationshipSchema ) {
702
708
const name = relationship . name ;
703
709
704
710
if ( this . shouldSerializeHasMany ( snapshot , name , relationship ) ) {
@@ -709,7 +715,7 @@ const JSONAPISerializer = JSONSerializer.extend({
709
715
const schema = this . store . modelFor ( snapshot . modelName ) ;
710
716
let payloadKey = this . _getMappedKey ( name , schema ) ;
711
717
if ( payloadKey === name && this . keyForRelationship ) {
712
- payloadKey = this . keyForRelationship ( name , 'hasMany' , 'serialize' ) ;
718
+ payloadKey = this . keyForRelationship ( name , schema , 'serialize' ) ;
713
719
}
714
720
715
721
// only serialize has many relationships that are not new
@@ -729,8 +735,8 @@ const JSONAPISerializer = JSONSerializer.extend({
729
735
json . relationships [ payloadKey ] = { data } ;
730
736
}
731
737
}
732
- } ,
733
- } ) ;
738
+ }
739
+ }
734
740
735
741
if ( DEBUG ) {
736
742
JSONAPISerializer . reopen ( {
0 commit comments