Skip to content

Commit aa236bf

Browse files
committed
stash types work on serializers
1 parent f131a8c commit aa236bf

File tree

4 files changed

+577
-410
lines changed

4 files changed

+577
-410
lines changed

packages/serializer/.eslintrc.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
isolation.rules({
1818
allowedImports: [
1919
'ember-inflector',
20-
'@ember/application',
20+
'@ember/owner',
2121
'@ember/service',
2222
'@ember/debug',
2323
'@ember/object',

packages/serializer/src/json-api.js packages/serializer/src/json-api.ts

+50-44
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { dasherize } from '@ember/string';
77
import { pluralize, singularize } from 'ember-inflector';
88

99
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';
1014

1115
import JSONSerializer from './json';
1216

@@ -135,7 +139,7 @@ import JSONSerializer from './json';
135139
@public
136140
@extends JSONSerializer
137141
*/
138-
const JSONAPISerializer = JSONSerializer.extend({
142+
class JSONAPISerializer extends JSONSerializer {
139143
/**
140144
@method _normalizeDocumentHelper
141145
@param {Object} documentHash
@@ -157,7 +161,7 @@ const JSONAPISerializer = JSONSerializer.extend({
157161
}
158162

159163
if (Array.isArray(documentHash.included)) {
160-
const ret = new Array();
164+
const ret = [];
161165
for (let i = 0; i < documentHash.included.length; i++) {
162166
const included = documentHash.included[i];
163167
const normalized = this._normalizeResourceHelper(included);
@@ -171,7 +175,7 @@ const JSONAPISerializer = JSONSerializer.extend({
171175
}
172176

173177
return documentHash;
174-
},
178+
}
175179

176180
/**
177181
@method _normalizeRelationshipDataHelper
@@ -183,7 +187,7 @@ const JSONAPISerializer = JSONSerializer.extend({
183187
relationshipDataHash.type = this.modelNameFromPayloadKey(relationshipDataHash.type);
184188

185189
return relationshipDataHash;
186-
},
190+
}
187191

188192
/**
189193
@method _normalizeResourceHelper
@@ -192,22 +196,24 @@ const JSONAPISerializer = JSONSerializer.extend({
192196
@private
193197
*/
194198
_normalizeResourceHelper(resourceHash) {
199+
const { store } = this;
200+
upgradeStore(store);
195201
assert(this.warnMessageForUndefinedType(), resourceHash.type);
196202

197203
const modelName = this.modelNameFromPayloadKey(resourceHash.type);
198204

199-
if (!this.store.getSchemaDefinitionService().doesTypeExist(modelName)) {
205+
if (!store.getSchemaDefinitionService().doesTypeExist(modelName)) {
200206
warn(this.warnMessageNoModelForType(modelName, resourceHash.type, 'modelNameFromPayloadKey'), false, {
201207
id: 'ds.serializer.model-for-type-missing',
202208
});
203209
return null;
204210
}
205211

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);
208214
const { data } = serializer.normalize(modelClass, resourceHash);
209215
return data;
210-
},
216+
}
211217

212218
/**
213219
Normalize some data and push it into the store.
@@ -217,10 +223,10 @@ const JSONAPISerializer = JSONSerializer.extend({
217223
@param {Store} store
218224
@param {Object} payload
219225
*/
220-
pushPayload(store, payload) {
226+
pushPayload(store: Store, payload) {
221227
const normalizedPayload = this._normalizeDocumentHelper(payload);
222228
store.push(normalizedPayload);
223-
},
229+
}
224230

225231
/**
226232
@method _normalizeResponse
@@ -233,10 +239,10 @@ const JSONAPISerializer = JSONSerializer.extend({
233239
@return {Object} JSON-API Document
234240
@private
235241
*/
236-
_normalizeResponse(store, primaryModelClass, payload, id, requestType, isSingle) {
242+
_normalizeResponse(store: Store, primaryModelClass: ModelSchema, payload, id, requestType, isSingle) {
237243
const normalizedPayload = this._normalizeDocumentHelper(payload);
238244
return normalizedPayload;
239-
},
245+
}
240246

241247
normalizeQueryRecordResponse() {
242248
const normalized = this._super(...arguments);
@@ -247,9 +253,9 @@ const JSONAPISerializer = JSONSerializer.extend({
247253
);
248254

249255
return normalized;
250-
},
256+
}
251257

252-
extractAttributes(modelClass, resourceHash) {
258+
extractAttributes(modelClass: ModelSchema, resourceHash) {
253259
const attributes = {};
254260

255261
if (resourceHash.attributes) {
@@ -270,7 +276,7 @@ const JSONAPISerializer = JSONSerializer.extend({
270276
}
271277

272278
return attributes;
273-
},
279+
}
274280

275281
/**
276282
Returns a relationship formatted as a JSON-API "relationship object".
@@ -297,7 +303,7 @@ const JSONAPISerializer = JSONSerializer.extend({
297303
}
298304

299305
return relationshipHash;
300-
},
306+
}
301307

302308
/**
303309
Returns the resource's relationships formatted as a JSON-API "relationships object".
@@ -310,12 +316,12 @@ const JSONAPISerializer = JSONSerializer.extend({
310316
@param {Object} resourceHash
311317
@return {Object}
312318
*/
313-
extractRelationships(modelClass, resourceHash) {
319+
extractRelationships(modelClass: ModelSchema, resourceHash) {
314320
const relationships = {};
315321

316322
if (resourceHash.relationships) {
317323
modelClass.eachRelationship((key, relationshipMeta) => {
318-
const relationshipKey = this.keyForRelationship(key, relationshipMeta.kind, 'deserialize');
324+
const relationshipKey = this.keyForRelationship(key, modelClass, 'deserialize');
319325
if (resourceHash.relationships[relationshipKey] !== undefined) {
320326
const relationshipHash = resourceHash.relationships[relationshipKey];
321327
relationships[key] = this.extractRelationship(relationshipHash);
@@ -326,7 +332,7 @@ const JSONAPISerializer = JSONSerializer.extend({
326332
resourceHash.relationships[key] !== undefined
327333
) {
328334
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.`,
330336
false
331337
);
332338
}
@@ -335,7 +341,7 @@ const JSONAPISerializer = JSONSerializer.extend({
335341
}
336342

337343
return relationships;
338-
},
344+
}
339345

340346
/**
341347
@method _extractType
@@ -344,9 +350,9 @@ const JSONAPISerializer = JSONSerializer.extend({
344350
@return {String}
345351
@private
346352
*/
347-
_extractType(modelClass, resourceHash) {
353+
_extractType(modelClass: ModelSchema, resourceHash) {
348354
return this.modelNameFromPayloadKey(resourceHash.type);
349-
},
355+
}
350356

351357
/**
352358
Dasherizes and singularizes the model name in the payload to match
@@ -360,9 +366,9 @@ const JSONAPISerializer = JSONSerializer.extend({
360366
@param {String} key
361367
@return {String} the model's modelName
362368
*/
363-
modelNameFromPayloadKey(key) {
369+
modelNameFromPayloadKey(key: string): string {
364370
return dasherize(singularize(key));
365-
},
371+
}
366372

367373
/**
368374
Converts the model name to a pluralized version of the model name.
@@ -375,11 +381,11 @@ const JSONAPISerializer = JSONSerializer.extend({
375381
@param {String} modelName
376382
@return {String}
377383
*/
378-
payloadKeyFromModelName(modelName) {
384+
payloadKeyFromModelName(modelName: string): string {
379385
return pluralize(modelName);
380-
},
386+
}
381387

382-
normalize(modelClass, resourceHash) {
388+
normalize(modelClass: ModelSchema, resourceHash) {
383389
if (resourceHash.attributes) {
384390
this.normalizeUsingDeclaredMapping(modelClass, resourceHash.attributes);
385391
}
@@ -402,7 +408,7 @@ const JSONAPISerializer = JSONSerializer.extend({
402408
this.applyTransforms(modelClass, data.attributes);
403409

404410
return { data };
405-
},
411+
}
406412

407413
/**
408414
`keyForAttribute` can be used to define rules for how to convert an
@@ -432,9 +438,9 @@ const JSONAPISerializer = JSONSerializer.extend({
432438
@param {String} method
433439
@return {String} normalized key
434440
*/
435-
keyForAttribute(key, method) {
441+
keyForAttribute(key: string, method) {
436442
return dasherize(key);
437-
},
443+
}
438444

439445
/**
440446
`keyForRelationship` can be used to define a custom key when
@@ -460,13 +466,13 @@ const JSONAPISerializer = JSONSerializer.extend({
460466
@method keyForRelationship
461467
@public
462468
@param {String} key
463-
@param {String} typeClass
469+
@param {ModelSchema} schema
464470
@param {String} method
465471
@return {String} normalized key
466472
*/
467-
keyForRelationship(key, typeClass, method) {
473+
keyForRelationship(key: string, schema: ModelSchema, method: 'serialize' | 'deserialize'): string {
468474
return dasherize(key);
469-
},
475+
}
470476

471477
/**
472478
Called when a record is saved in order to convert the
@@ -637,14 +643,14 @@ const JSONAPISerializer = JSONSerializer.extend({
637643
@param {Object} options
638644
@return {Object} json
639645
*/
640-
serialize(snapshot, options) {
646+
serialize(snapshot: Snapshot, options) {
641647
const data = this._super(...arguments);
642648
data.type = this.payloadKeyFromModelName(snapshot.modelName);
643649

644650
return { data };
645-
},
651+
}
646652

647-
serializeAttribute(snapshot, json, key, attribute) {
653+
serializeAttribute(snapshot: Snapshot, json, key: string, attribute) {
648654
const type = attribute.type;
649655

650656
if (this._canSerialize(key)) {
@@ -665,9 +671,9 @@ const JSONAPISerializer = JSONSerializer.extend({
665671

666672
json.attributes[payloadKey] = value;
667673
}
668-
},
674+
}
669675

670-
serializeBelongsTo(snapshot, json, relationship) {
676+
serializeBelongsTo(snapshot: Snapshot, json, relationship: RelationshipSchema) {
671677
const name = relationship.name;
672678

673679
if (this._canSerialize(name)) {
@@ -680,7 +686,7 @@ const JSONAPISerializer = JSONSerializer.extend({
680686
const schema = this.store.modelFor(snapshot.modelName);
681687
let payloadKey = this._getMappedKey(name, schema);
682688
if (payloadKey === name) {
683-
payloadKey = this.keyForRelationship(name, 'belongsTo', 'serialize');
689+
payloadKey = this.keyForRelationship(name, schema, 'serialize');
684690
}
685691

686692
let data = null;
@@ -696,9 +702,9 @@ const JSONAPISerializer = JSONSerializer.extend({
696702
json.relationships[payloadKey] = { data };
697703
}
698704
}
699-
},
705+
}
700706

701-
serializeHasMany(snapshot, json, relationship) {
707+
serializeHasMany(snapshot: Snapshot, json, relationship: RelationshipSchema) {
702708
const name = relationship.name;
703709

704710
if (this.shouldSerializeHasMany(snapshot, name, relationship)) {
@@ -709,7 +715,7 @@ const JSONAPISerializer = JSONSerializer.extend({
709715
const schema = this.store.modelFor(snapshot.modelName);
710716
let payloadKey = this._getMappedKey(name, schema);
711717
if (payloadKey === name && this.keyForRelationship) {
712-
payloadKey = this.keyForRelationship(name, 'hasMany', 'serialize');
718+
payloadKey = this.keyForRelationship(name, schema, 'serialize');
713719
}
714720

715721
// only serialize has many relationships that are not new
@@ -729,8 +735,8 @@ const JSONAPISerializer = JSONSerializer.extend({
729735
json.relationships[payloadKey] = { data };
730736
}
731737
}
732-
},
733-
});
738+
}
739+
}
734740

735741
if (DEBUG) {
736742
JSONAPISerializer.reopen({

0 commit comments

Comments
 (0)