@ember-data/serializer 5.4.0-beta.1 → 5.4.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/addon/-private.js +3 -4
- package/addon/-private.js.map +1 -1
- package/addon/{embedded-records-mixin-d75385ff.js → embedded-records-mixin-HTw8JJT1.js} +57 -60
- package/addon/embedded-records-mixin-HTw8JJT1.js.map +1 -0
- package/addon/index.js +1 -0
- package/addon/index.js.map +1 -1
- package/addon/json-api.js +49 -173
- package/addon/json-api.js.map +1 -1
- package/addon/json.js +52 -53
- package/addon/json.js.map +1 -1
- package/addon/rest.js +31 -27
- package/addon/rest.js.map +1 -1
- package/addon/{string-22572f80.js → string-A02hFTMo.js} +139 -20
- package/addon/string-A02hFTMo.js.map +1 -0
- package/addon/transform.js +1 -2
- package/addon/transform.js.map +1 -1
- package/addon/{utils-075c5b79.js.map → utils-N1ERF6HN.js.map} +1 -1
- package/package.json +61 -28
- package/addon/embedded-records-mixin-d75385ff.js.map +0 -1
- package/addon/string-22572f80.js.map +0 -1
- /package/addon/{utils-075c5b79.js → utils-N1ERF6HN.js} +0 -0
package/addon/json.js
CHANGED
|
@@ -3,7 +3,7 @@ import { assert, warn } from '@ember/debug';
|
|
|
3
3
|
import { dasherize } from '@ember/string';
|
|
4
4
|
import { singularize } from 'ember-inflector';
|
|
5
5
|
import _class from "./index";
|
|
6
|
-
import { c as coerceId } from "./utils-
|
|
6
|
+
import { c as coerceId } from "./utils-N1ERF6HN";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* @module @ember-data/serializer/json
|
|
@@ -177,13 +177,13 @@ const JSONSerializer = _class.extend({
|
|
|
177
177
|
@return {Object} data The transformed data object
|
|
178
178
|
*/
|
|
179
179
|
applyTransforms(typeClass, data) {
|
|
180
|
-
|
|
180
|
+
const attributes = typeClass.attributes;
|
|
181
181
|
typeClass.eachTransformedAttribute((key, typeClass) => {
|
|
182
182
|
if (data[key] === undefined) {
|
|
183
183
|
return;
|
|
184
184
|
}
|
|
185
|
-
|
|
186
|
-
|
|
185
|
+
const transform = this.transformFor(typeClass);
|
|
186
|
+
const transformMeta = attributes.get(key);
|
|
187
187
|
data[key] = transform.deserialize(data[key], transformMeta.options);
|
|
188
188
|
});
|
|
189
189
|
return data;
|
|
@@ -461,17 +461,17 @@ const JSONSerializer = _class.extend({
|
|
|
461
461
|
@private
|
|
462
462
|
*/
|
|
463
463
|
_normalizeResponse(store, primaryModelClass, payload, id, requestType, isSingle) {
|
|
464
|
-
|
|
464
|
+
const documentHash = {
|
|
465
465
|
data: null,
|
|
466
466
|
included: []
|
|
467
467
|
};
|
|
468
|
-
|
|
468
|
+
const meta = this.extractMeta(store, primaryModelClass, payload);
|
|
469
469
|
if (meta) {
|
|
470
470
|
assert('The `meta` returned from `extractMeta` has to be an object, not "' + typeof meta + '".', typeof meta === 'object');
|
|
471
471
|
documentHash.meta = meta;
|
|
472
472
|
}
|
|
473
473
|
if (isSingle) {
|
|
474
|
-
|
|
474
|
+
const {
|
|
475
475
|
data,
|
|
476
476
|
included
|
|
477
477
|
} = this.normalize(primaryModelClass, payload);
|
|
@@ -480,10 +480,10 @@ const JSONSerializer = _class.extend({
|
|
|
480
480
|
documentHash.included = included;
|
|
481
481
|
}
|
|
482
482
|
} else {
|
|
483
|
-
|
|
483
|
+
const ret = new Array(payload.length);
|
|
484
484
|
for (let i = 0, l = payload.length; i < l; i++) {
|
|
485
|
-
|
|
486
|
-
|
|
485
|
+
const item = payload[i];
|
|
486
|
+
const {
|
|
487
487
|
data,
|
|
488
488
|
included
|
|
489
489
|
} = this.normalize(primaryModelClass, item);
|
|
@@ -557,8 +557,8 @@ const JSONSerializer = _class.extend({
|
|
|
557
557
|
@return {String}
|
|
558
558
|
*/
|
|
559
559
|
extractId(modelClass, resourceHash) {
|
|
560
|
-
|
|
561
|
-
|
|
560
|
+
const primaryKey = this.primaryKey;
|
|
561
|
+
const id = resourceHash[primaryKey];
|
|
562
562
|
return coerceId(id);
|
|
563
563
|
},
|
|
564
564
|
/**
|
|
@@ -572,7 +572,7 @@ const JSONSerializer = _class.extend({
|
|
|
572
572
|
*/
|
|
573
573
|
extractAttributes(modelClass, resourceHash) {
|
|
574
574
|
let attributeKey;
|
|
575
|
-
|
|
575
|
+
const attributes = {};
|
|
576
576
|
modelClass.eachAttribute(key => {
|
|
577
577
|
attributeKey = this.keyForAttribute(key, 'deserialize');
|
|
578
578
|
if (resourceHash[attributeKey] !== undefined) {
|
|
@@ -603,7 +603,7 @@ const JSONSerializer = _class.extend({
|
|
|
603
603
|
if (relationshipHash.id) {
|
|
604
604
|
relationshipHash.id = coerceId(relationshipHash.id);
|
|
605
605
|
}
|
|
606
|
-
|
|
606
|
+
const modelClass = this.store.modelFor(relationshipModelName);
|
|
607
607
|
if (relationshipHash.type && !modelClass.fields.has('type')) {
|
|
608
608
|
relationshipHash.type = this.modelNameFromPayloadKey(relationshipHash.type);
|
|
609
609
|
}
|
|
@@ -644,13 +644,13 @@ const JSONSerializer = _class.extend({
|
|
|
644
644
|
@return {Object}
|
|
645
645
|
*/
|
|
646
646
|
extractRelationships(modelClass, resourceHash) {
|
|
647
|
-
|
|
647
|
+
const relationships = {};
|
|
648
648
|
modelClass.eachRelationship((key, relationshipMeta) => {
|
|
649
649
|
let relationship = null;
|
|
650
|
-
|
|
650
|
+
const relationshipKey = this.keyForRelationship(key, relationshipMeta.kind, 'deserialize');
|
|
651
651
|
if (resourceHash[relationshipKey] !== undefined) {
|
|
652
652
|
let data = null;
|
|
653
|
-
|
|
653
|
+
const relationshipHash = resourceHash[relationshipKey];
|
|
654
654
|
if (relationshipMeta.kind === 'belongsTo') {
|
|
655
655
|
if (relationshipMeta.options.polymorphic) {
|
|
656
656
|
// extracting a polymorphic belongsTo may need more information
|
|
@@ -670,7 +670,7 @@ const JSONSerializer = _class.extend({
|
|
|
670
670
|
data = new Array(relationshipHash.length);
|
|
671
671
|
if (relationshipMeta.options.polymorphic) {
|
|
672
672
|
for (let i = 0, l = relationshipHash.length; i < l; i++) {
|
|
673
|
-
|
|
673
|
+
const item = relationshipHash[i];
|
|
674
674
|
data[i] = this.extractPolymorphicRelationship(relationshipMeta.type, item, {
|
|
675
675
|
key,
|
|
676
676
|
resourceHash,
|
|
@@ -679,7 +679,7 @@ const JSONSerializer = _class.extend({
|
|
|
679
679
|
}
|
|
680
680
|
} else {
|
|
681
681
|
for (let i = 0, l = relationshipHash.length; i < l; i++) {
|
|
682
|
-
|
|
682
|
+
const item = relationshipHash[i];
|
|
683
683
|
data[i] = this.extractRelationship(relationshipMeta.type, item);
|
|
684
684
|
}
|
|
685
685
|
}
|
|
@@ -689,9 +689,9 @@ const JSONSerializer = _class.extend({
|
|
|
689
689
|
data
|
|
690
690
|
};
|
|
691
691
|
}
|
|
692
|
-
|
|
692
|
+
const linkKey = this.keyForLink(key, relationshipMeta.kind);
|
|
693
693
|
if (resourceHash.links && resourceHash.links[linkKey] !== undefined) {
|
|
694
|
-
|
|
694
|
+
const related = resourceHash.links[linkKey];
|
|
695
695
|
relationship = relationship || {};
|
|
696
696
|
relationship.links = {
|
|
697
697
|
related
|
|
@@ -738,11 +738,11 @@ const JSONSerializer = _class.extend({
|
|
|
738
738
|
@private
|
|
739
739
|
*/
|
|
740
740
|
normalizeUsingDeclaredMapping(modelClass, hash) {
|
|
741
|
-
|
|
741
|
+
const attrs = this.attrs;
|
|
742
742
|
let normalizedKey;
|
|
743
743
|
let payloadKey;
|
|
744
744
|
if (attrs) {
|
|
745
|
-
for (
|
|
745
|
+
for (const key in attrs) {
|
|
746
746
|
normalizedKey = payloadKey = this._getMappedKey(key, modelClass);
|
|
747
747
|
if (hash[payloadKey] === undefined) {
|
|
748
748
|
continue;
|
|
@@ -772,7 +772,7 @@ const JSONSerializer = _class.extend({
|
|
|
772
772
|
warn('There is no attribute or relationship with the name `' + key + '` on `' + modelClass.modelName + '`. Check your serializers attrs hash.', modelClass.attributes.has(key) || modelClass.relationshipsByName.has(key), {
|
|
773
773
|
id: 'ds.serializer.no-mapped-attrs-key'
|
|
774
774
|
});
|
|
775
|
-
|
|
775
|
+
const attrs = this.attrs;
|
|
776
776
|
let mappedKey;
|
|
777
777
|
if (attrs && attrs[key]) {
|
|
778
778
|
mappedKey = attrs[key];
|
|
@@ -796,7 +796,7 @@ const JSONSerializer = _class.extend({
|
|
|
796
796
|
@return {boolean} true if the key can be serialized
|
|
797
797
|
*/
|
|
798
798
|
_canSerialize(key) {
|
|
799
|
-
|
|
799
|
+
const attrs = this.attrs;
|
|
800
800
|
return !attrs || !attrs[key] || attrs[key].serialize !== false;
|
|
801
801
|
},
|
|
802
802
|
/**
|
|
@@ -809,7 +809,7 @@ const JSONSerializer = _class.extend({
|
|
|
809
809
|
@return {boolean} true if the key must be serialized
|
|
810
810
|
*/
|
|
811
811
|
_mustSerialize(key) {
|
|
812
|
-
|
|
812
|
+
const attrs = this.attrs;
|
|
813
813
|
return attrs && attrs[key] && attrs[key].serialize === true;
|
|
814
814
|
},
|
|
815
815
|
/**
|
|
@@ -825,7 +825,7 @@ const JSONSerializer = _class.extend({
|
|
|
825
825
|
*/
|
|
826
826
|
shouldSerializeHasMany(snapshot, key, relationship) {
|
|
827
827
|
const schema = this.store.modelFor(snapshot.modelName);
|
|
828
|
-
|
|
828
|
+
const relationshipType = schema.determineRelationshipType(relationship, this.store);
|
|
829
829
|
if (this._mustSerialize(key)) {
|
|
830
830
|
return true;
|
|
831
831
|
}
|
|
@@ -948,7 +948,7 @@ const JSONSerializer = _class.extend({
|
|
|
948
948
|
@return {Object} json
|
|
949
949
|
*/
|
|
950
950
|
serialize(snapshot, options) {
|
|
951
|
-
|
|
951
|
+
const json = {};
|
|
952
952
|
if (options && options.includeId) {
|
|
953
953
|
const id = snapshot.id;
|
|
954
954
|
if (id) {
|
|
@@ -1019,16 +1019,16 @@ const JSONSerializer = _class.extend({
|
|
|
1019
1019
|
*/
|
|
1020
1020
|
serializeAttribute(snapshot, json, key, attribute) {
|
|
1021
1021
|
if (this._canSerialize(key)) {
|
|
1022
|
-
|
|
1022
|
+
const type = attribute.type;
|
|
1023
1023
|
let value = snapshot.attr(key);
|
|
1024
1024
|
if (type) {
|
|
1025
|
-
|
|
1025
|
+
const transform = this.transformFor(type);
|
|
1026
1026
|
value = transform.serialize(value, attribute.options);
|
|
1027
1027
|
}
|
|
1028
1028
|
|
|
1029
1029
|
// if provided, use the mapping provided by `attrs` in
|
|
1030
1030
|
// the serializer
|
|
1031
|
-
|
|
1031
|
+
const schema = this.store.modelFor(snapshot.modelName);
|
|
1032
1032
|
let payloadKey = this._getMappedKey(key, schema);
|
|
1033
1033
|
if (payloadKey === key && this.keyForAttribute) {
|
|
1034
1034
|
payloadKey = this.keyForAttribute(key, 'serialize');
|
|
@@ -1044,7 +1044,7 @@ const JSONSerializer = _class.extend({
|
|
|
1044
1044
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1045
1045
|
export default class PostSerializer extends JSONSerializer {
|
|
1046
1046
|
serializeBelongsTo(snapshot, json, relationship) {
|
|
1047
|
-
let key = relationship.
|
|
1047
|
+
let key = relationship.name;
|
|
1048
1048
|
let belongsTo = snapshot.belongsTo(key);
|
|
1049
1049
|
key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo", "serialize") : key;
|
|
1050
1050
|
json[key] = !belongsTo ? null : belongsTo.record.toJSON();
|
|
@@ -1058,18 +1058,18 @@ const JSONSerializer = _class.extend({
|
|
|
1058
1058
|
@param {Object} relationship
|
|
1059
1059
|
*/
|
|
1060
1060
|
serializeBelongsTo(snapshot, json, relationship) {
|
|
1061
|
-
|
|
1062
|
-
if (this._canSerialize(
|
|
1063
|
-
|
|
1061
|
+
const name = relationship.name;
|
|
1062
|
+
if (this._canSerialize(name)) {
|
|
1063
|
+
const belongsToId = snapshot.belongsTo(name, {
|
|
1064
1064
|
id: true
|
|
1065
1065
|
});
|
|
1066
1066
|
|
|
1067
1067
|
// if provided, use the mapping provided by `attrs` in
|
|
1068
1068
|
// the serializer
|
|
1069
|
-
|
|
1070
|
-
let payloadKey = this._getMappedKey(
|
|
1071
|
-
if (payloadKey ===
|
|
1072
|
-
payloadKey = this.keyForRelationship(
|
|
1069
|
+
const schema = this.store.modelFor(snapshot.modelName);
|
|
1070
|
+
let payloadKey = this._getMappedKey(name, schema);
|
|
1071
|
+
if (payloadKey === name && this.keyForRelationship) {
|
|
1072
|
+
payloadKey = this.keyForRelationship(name, 'belongsTo', 'serialize');
|
|
1073
1073
|
}
|
|
1074
1074
|
|
|
1075
1075
|
//Need to check whether the id is there for new&async records
|
|
@@ -1091,7 +1091,7 @@ const JSONSerializer = _class.extend({
|
|
|
1091
1091
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1092
1092
|
export default class PostSerializer extends JSONSerializer {
|
|
1093
1093
|
serializeHasMany(snapshot, json, relationship) {
|
|
1094
|
-
let key = relationship.
|
|
1094
|
+
let key = relationship.name;
|
|
1095
1095
|
if (key === 'comments') {
|
|
1096
1096
|
return;
|
|
1097
1097
|
} else {
|
|
@@ -1107,25 +1107,24 @@ const JSONSerializer = _class.extend({
|
|
|
1107
1107
|
@param {Object} relationship
|
|
1108
1108
|
*/
|
|
1109
1109
|
serializeHasMany(snapshot, json, relationship) {
|
|
1110
|
-
|
|
1111
|
-
if (this.shouldSerializeHasMany(snapshot,
|
|
1112
|
-
|
|
1110
|
+
const name = relationship.name;
|
|
1111
|
+
if (this.shouldSerializeHasMany(snapshot, name, relationship)) {
|
|
1112
|
+
const hasMany = snapshot.hasMany(name, {
|
|
1113
1113
|
ids: true
|
|
1114
1114
|
});
|
|
1115
1115
|
if (hasMany !== undefined) {
|
|
1116
1116
|
// if provided, use the mapping provided by `attrs` in
|
|
1117
1117
|
// the serializer
|
|
1118
|
-
|
|
1119
|
-
let payloadKey = this._getMappedKey(
|
|
1120
|
-
if (payloadKey ===
|
|
1121
|
-
payloadKey = this.keyForRelationship(
|
|
1118
|
+
const schema = this.store.modelFor(snapshot.modelName);
|
|
1119
|
+
let payloadKey = this._getMappedKey(name, schema);
|
|
1120
|
+
if (payloadKey === name && this.keyForRelationship) {
|
|
1121
|
+
payloadKey = this.keyForRelationship(name, 'hasMany', 'serialize');
|
|
1122
1122
|
}
|
|
1123
1123
|
json[payloadKey] = hasMany;
|
|
1124
1124
|
// TODO support for polymorphic manyToNone and manyToMany relationships
|
|
1125
1125
|
}
|
|
1126
1126
|
}
|
|
1127
1127
|
},
|
|
1128
|
-
|
|
1129
1128
|
/**
|
|
1130
1129
|
You can use this method to customize how polymorphic objects are
|
|
1131
1130
|
serialized. Objects are considered to be polymorphic if
|
|
@@ -1136,7 +1135,7 @@ const JSONSerializer = _class.extend({
|
|
|
1136
1135
|
import JSONSerializer from '@ember-data/serializer/json';
|
|
1137
1136
|
export default class CommentSerializer extends JSONSerializer {
|
|
1138
1137
|
serializePolymorphicType(snapshot, json, relationship) {
|
|
1139
|
-
let key = relationship.
|
|
1138
|
+
let key = relationship.name;
|
|
1140
1139
|
let belongsTo = snapshot.belongsTo(key);
|
|
1141
1140
|
key = this.keyForAttribute ? this.keyForAttribute(key, 'serialize') : key;
|
|
1142
1141
|
if (!belongsTo) {
|
|
@@ -1179,7 +1178,7 @@ const JSONSerializer = _class.extend({
|
|
|
1179
1178
|
*/
|
|
1180
1179
|
extractMeta(store, modelClass, payload) {
|
|
1181
1180
|
if (payload && payload['meta'] !== undefined) {
|
|
1182
|
-
|
|
1181
|
+
const meta = payload.meta;
|
|
1183
1182
|
delete payload.meta;
|
|
1184
1183
|
return meta;
|
|
1185
1184
|
}
|
|
@@ -1283,14 +1282,14 @@ const JSONSerializer = _class.extend({
|
|
|
1283
1282
|
// for each attr and relationship, make sure that we use
|
|
1284
1283
|
// the normalized key
|
|
1285
1284
|
typeClass.eachAttribute(name => {
|
|
1286
|
-
|
|
1285
|
+
const key = this.keyForAttribute(name, 'deserialize');
|
|
1287
1286
|
if (key !== name && extracted[key] !== undefined) {
|
|
1288
1287
|
extracted[name] = extracted[key];
|
|
1289
1288
|
delete extracted[key];
|
|
1290
1289
|
}
|
|
1291
1290
|
});
|
|
1292
1291
|
typeClass.eachRelationship(name => {
|
|
1293
|
-
|
|
1292
|
+
const key = this.keyForRelationship(name, 'deserialize');
|
|
1294
1293
|
if (key !== name && extracted[key] !== undefined) {
|
|
1295
1294
|
extracted[name] = extracted[key];
|
|
1296
1295
|
delete extracted[key];
|
|
@@ -1368,7 +1367,7 @@ const JSONSerializer = _class.extend({
|
|
|
1368
1367
|
@return {Transform} transform
|
|
1369
1368
|
*/
|
|
1370
1369
|
transformFor(attributeType, skipAssertion) {
|
|
1371
|
-
|
|
1370
|
+
const transform = getOwner(this).lookup('transform:' + attributeType);
|
|
1372
1371
|
assert(`Unable to find the transform for \`attr('${attributeType}')\``, skipAssertion || !!transform);
|
|
1373
1372
|
return transform;
|
|
1374
1373
|
}
|