@ember-data/serializer 4.6.1 → 4.7.0

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.
@@ -1,6 +1,5 @@
1
1
  import { A } from '@ember/array';
2
2
  import { warn } from '@ember/debug';
3
- import { get, set } from '@ember/object';
4
3
  import Mixin from '@ember/object/mixin';
5
4
  import { camelize } from '@ember/string';
6
5
  import { typeOf } from '@ember/utils';
@@ -544,7 +543,7 @@ export default Mixin.create({
544
543
  },
545
544
 
546
545
  attrsOption(attr) {
547
- let attrs = this.get('attrs');
546
+ let attrs = this.attrs;
548
547
  return attrs && (attrs[camelize(attr)] || attrs[attr]);
549
548
  },
550
549
 
@@ -571,7 +570,7 @@ export default Mixin.create({
571
570
  @private
572
571
  */
573
572
  _extractEmbeddedHasMany(store, key, hash, relationshipMeta) {
574
- let relationshipHash = get(hash, `data.relationships.${key}.data`);
573
+ let relationshipHash = hash.data?.relationships?.[key]?.data;
575
574
 
576
575
  if (!relationshipHash) {
577
576
  return;
@@ -592,7 +591,7 @@ export default Mixin.create({
592
591
  }
593
592
 
594
593
  let relationship = { data: hasMany };
595
- set(hash, `data.relationships.${key}`, relationship);
594
+ hash.data.relationships[key] = relationship;
596
595
  },
597
596
 
598
597
  /**
@@ -600,7 +599,7 @@ export default Mixin.create({
600
599
  @private
601
600
  */
602
601
  _extractEmbeddedBelongsTo(store, key, hash, relationshipMeta) {
603
- let relationshipHash = get(hash, `data.relationships.${key}.data`);
602
+ let relationshipHash = hash.data?.relationships?.[key]?.data;
604
603
  if (!relationshipHash) {
605
604
  return;
606
605
  }
@@ -615,7 +614,7 @@ export default Mixin.create({
615
614
  let belongsTo = { id: data.id, type: data.type };
616
615
  let relationship = { data: belongsTo };
617
616
 
618
- set(hash, `data.relationships.${key}`, relationship);
617
+ hash.data.relationships[key] = relationship;
619
618
  },
620
619
 
621
620
  /**
@@ -3,7 +3,6 @@
3
3
  */
4
4
 
5
5
  export { default as EmbeddedRecordsMixin } from './embedded-records-mixin';
6
- export { modelHasAttributeOrRelationshipNamedType } from './utils';
7
6
 
8
7
  export { default as Transform } from './transforms/transform';
9
8
  export { default as BooleanTransform } from './transforms/boolean';
package/addon/json-api.js CHANGED
@@ -9,7 +9,6 @@ import { DEBUG } from '@glimmer/env';
9
9
  import { pluralize, singularize } from 'ember-inflector';
10
10
 
11
11
  import JSONSerializer from '@ember-data/serializer/json';
12
- import { normalizeModelName } from '@ember-data/store';
13
12
 
14
13
  /**
15
14
  Ember Data 2.0 Serializer:
@@ -361,7 +360,7 @@ const JSONAPISerializer = JSONSerializer.extend({
361
360
  @return {String} the model's modelName
362
361
  */
363
362
  modelNameFromPayloadKey(key) {
364
- return singularize(normalizeModelName(key));
363
+ return singularize(dasherize(key));
365
364
  },
366
365
 
367
366
  /**
@@ -375,7 +374,6 @@ const JSONAPISerializer = JSONSerializer.extend({
375
374
  @param {String} modelName
376
375
  @return {String}
377
376
  */
378
- // TODO @deprecated Use payloadTypeFromModelName instead
379
377
  payloadKeyFromModelName(modelName) {
380
378
  return pluralize(modelName);
381
379
  },
@@ -414,7 +412,7 @@ const JSONAPISerializer = JSONSerializer.extend({
414
412
 
415
413
  ```app/serializers/application.js
416
414
  import JSONAPISerializer from '@ember-data/serializer/json-api';
417
- import { dasherize } from '@ember/string';
415
+ import { dasherize } from '<app-name>/utils/string-utils';
418
416
 
419
417
  export default class ApplicationSerializer extends JSONAPISerializer {
420
418
  keyForAttribute(attr, method) {
@@ -446,7 +444,7 @@ const JSONAPISerializer = JSONSerializer.extend({
446
444
 
447
445
  ```app/serializers/post.js
448
446
  import JSONAPISerializer from '@ember-data/serializer/json-api';
449
- import { underscore } from '@ember/string';
447
+ import { underscore } from '<app-name>/utils/string-utils';
450
448
 
451
449
  export default class ApplicationSerializer extends JSONAPISerializer {
452
450
  keyForRelationship(key, relationship, method) {
@@ -564,8 +562,7 @@ const JSONAPISerializer = JSONSerializer.extend({
564
562
 
565
563
  ```app/serializers/application.js
566
564
  import JSONAPISerializer from '@ember-data/serializer/json-api';
567
- import { singularize } from 'ember-inflector';
568
- import { underscore } from '@ember/string';
565
+ import { underscore, singularize } from '<app-name>/utils/string-utils';
569
566
 
570
567
  export default class ApplicationSerializer extends JSONAPISerializer {
571
568
  serialize(snapshot, options) {
@@ -711,7 +708,7 @@ const JSONAPISerializer = JSONSerializer.extend({
711
708
  }
712
709
 
713
710
  // only serialize has many relationships that are not new
714
- let nonNewHasMany = hasMany.filter((item) => item.record && !item.record.get('isNew'));
711
+ let nonNewHasMany = hasMany.filter((item) => item.record && !item.record.isNew);
715
712
  let data = new Array(nonNewHasMany.length);
716
713
 
717
714
  for (let i = 0; i < nonNewHasMany.length; i++) {
package/addon/json.js CHANGED
@@ -3,14 +3,15 @@
3
3
  */
4
4
  import { getOwner } from '@ember/application';
5
5
  import { assert, warn } from '@ember/debug';
6
- import { get } from '@ember/object';
6
+ import { dasherize } from '@ember/string';
7
7
  import { isNone, typeOf } from '@ember/utils';
8
8
 
9
9
  import Serializer from '@ember-data/serializer';
10
- import { normalizeModelName } from '@ember-data/store';
11
- import { coerceId, errorsArrayToHash } from '@ember-data/store/-private';
10
+ import { coerceId } from '@ember-data/store/-private';
12
11
 
13
- import { modelHasAttributeOrRelationshipNamedType } from './-private';
12
+ const SOURCE_POINTER_REGEXP = /^\/?data\/(attributes|relationships)\/(.*)/;
13
+ const SOURCE_POINTER_PRIMARY_REGEXP = /^\/?data/;
14
+ const PRIMARY_ATTRIBUTE_KEY = 'base';
14
15
 
15
16
  /**
16
17
  Ember Data 2.0 Serializer:
@@ -192,7 +193,7 @@ const JSONSerializer = Serializer.extend({
192
193
  @return {Object} data The transformed data object
193
194
  */
194
195
  applyTransforms(typeClass, data) {
195
- let attributes = get(typeClass, 'attributes');
196
+ let attributes = typeClass.attributes;
196
197
 
197
198
  typeClass.eachTransformedAttribute((key, typeClass) => {
198
199
  if (data[key] === undefined) {
@@ -567,12 +568,12 @@ const JSONSerializer = Serializer.extend({
567
568
 
568
569
  ```app/serializers/application.js
569
570
  import JSONSerializer from '@ember-data/serializer/json';
570
- import { underscore } from '@ember/string';
571
+ import { underscore } from '<app-name>/utils/string-utils';
571
572
  import { get } from '@ember/object';
572
573
 
573
574
  export default class ApplicationSerializer extends JSONSerializer {
574
575
  normalize(typeClass, hash) {
575
- let fields = get(typeClass, 'fields');
576
+ let fields = typeClass.fields;
576
577
 
577
578
  fields.forEach(function(type, field) {
578
579
  let payloadField = underscore(field);
@@ -625,7 +626,7 @@ const JSONSerializer = Serializer.extend({
625
626
  @return {String}
626
627
  */
627
628
  extractId(modelClass, resourceHash) {
628
- let primaryKey = get(this, 'primaryKey');
629
+ let primaryKey = this.primaryKey;
629
630
  let id = resourceHash[primaryKey];
630
631
  return coerceId(id);
631
632
  },
@@ -681,7 +682,7 @@ const JSONSerializer = Serializer.extend({
681
682
  }
682
683
 
683
684
  let modelClass = this.store.modelFor(relationshipModelName);
684
- if (relationshipHash.type && !modelHasAttributeOrRelationshipNamedType(modelClass)) {
685
+ if (relationshipHash.type && !modelClass.fields.has('type')) {
685
686
  relationshipHash.type = this.modelNameFromPayloadKey(relationshipHash.type);
686
687
  }
687
688
 
@@ -795,7 +796,7 @@ const JSONSerializer = Serializer.extend({
795
796
  @return {String} the model's modelName
796
797
  */
797
798
  modelNameFromPayloadKey(key) {
798
- return normalizeModelName(key);
799
+ return dasherize(key);
799
800
  },
800
801
 
801
802
  /**
@@ -826,7 +827,7 @@ const JSONSerializer = Serializer.extend({
826
827
  @private
827
828
  */
828
829
  normalizeUsingDeclaredMapping(modelClass, hash) {
829
- let attrs = get(this, 'attrs');
830
+ let attrs = this.attrs;
830
831
  let normalizedKey;
831
832
  let payloadKey;
832
833
 
@@ -838,11 +839,11 @@ const JSONSerializer = Serializer.extend({
838
839
  continue;
839
840
  }
840
841
 
841
- if (get(modelClass, 'attributes').has(key)) {
842
+ if (modelClass.attributes.has(key)) {
842
843
  normalizedKey = this.keyForAttribute(key, 'deserialize');
843
844
  }
844
845
 
845
- if (get(modelClass, 'relationshipsByName').has(key)) {
846
+ if (modelClass.relationshipsByName.has(key)) {
846
847
  normalizedKey = this.keyForRelationship(key, modelClass, 'deserialize');
847
848
  }
848
849
 
@@ -870,13 +871,13 @@ const JSONSerializer = Serializer.extend({
870
871
  '` on `' +
871
872
  modelClass.modelName +
872
873
  '`. Check your serializers attrs hash.',
873
- get(modelClass, 'attributes').has(key) || get(modelClass, 'relationshipsByName').has(key),
874
+ modelClass.attributes.has(key) || modelClass.relationshipsByName.has(key),
874
875
  {
875
876
  id: 'ds.serializer.no-mapped-attrs-key',
876
877
  }
877
878
  );
878
879
 
879
- let attrs = get(this, 'attrs');
880
+ let attrs = this.attrs;
880
881
  let mappedKey;
881
882
  if (attrs && attrs[key]) {
882
883
  mappedKey = attrs[key];
@@ -903,7 +904,7 @@ const JSONSerializer = Serializer.extend({
903
904
  @return {boolean} true if the key can be serialized
904
905
  */
905
906
  _canSerialize(key) {
906
- let attrs = get(this, 'attrs');
907
+ let attrs = this.attrs;
907
908
 
908
909
  return !attrs || !attrs[key] || attrs[key].serialize !== false;
909
910
  },
@@ -919,7 +920,7 @@ const JSONSerializer = Serializer.extend({
919
920
  @return {boolean} true if the key must be serialized
920
921
  */
921
922
  _mustSerialize(key) {
922
- let attrs = get(this, 'attrs');
923
+ let attrs = this.attrs;
923
924
 
924
925
  return attrs && attrs[key] && attrs[key].serialize === true;
925
926
  },
@@ -1030,7 +1031,7 @@ const JSONSerializer = Serializer.extend({
1030
1031
 
1031
1032
  ```app/serializers/application.js
1032
1033
  import JSONSerializer from '@ember-data/serializer/json';
1033
- import { singularize } from 'ember-inflector';
1034
+ import { singularize } from '<app-name>/utils/string-utils';
1034
1035
 
1035
1036
  export default class ApplicationSerializer extends JSONSerializer {
1036
1037
  serialize(snapshot, options) {
@@ -1106,7 +1107,7 @@ const JSONSerializer = Serializer.extend({
1106
1107
  if (options && options.includeId) {
1107
1108
  const id = snapshot.id;
1108
1109
  if (id) {
1109
- json[get(this, 'primaryKey')] = id;
1110
+ json[this.primaryKey] = id;
1110
1111
  }
1111
1112
  }
1112
1113
 
@@ -1137,7 +1138,7 @@ const JSONSerializer = Serializer.extend({
1137
1138
 
1138
1139
  ```app/serializers/application.js
1139
1140
  import RESTSerializer from '@ember-data/serializer/rest';
1140
- import { decamelize } from '@ember/string';
1141
+ import { decamelize } from '<app-name>/utils/string-utils';
1141
1142
 
1142
1143
  export default class ApplicationSerializer extends RESTSerializer {
1143
1144
  serializeIntoHash(data, type, snapshot, options) {
@@ -1467,25 +1468,48 @@ const JSONSerializer = Serializer.extend({
1467
1468
  */
1468
1469
  extractErrors(store, typeClass, payload, id) {
1469
1470
  if (payload && typeof payload === 'object' && payload.errors) {
1470
- payload = errorsArrayToHash(payload.errors);
1471
+ // the default assumption is that errors is already in JSON:API format
1472
+ const extracted = {};
1471
1473
 
1472
- this.normalizeUsingDeclaredMapping(typeClass, payload);
1474
+ payload.errors.forEach((error) => {
1475
+ if (error.source && error.source.pointer) {
1476
+ let key = error.source.pointer.match(SOURCE_POINTER_REGEXP);
1473
1477
 
1478
+ if (key) {
1479
+ key = key[2];
1480
+ } else if (error.source.pointer.search(SOURCE_POINTER_PRIMARY_REGEXP) !== -1) {
1481
+ key = PRIMARY_ATTRIBUTE_KEY;
1482
+ }
1483
+
1484
+ if (key) {
1485
+ extracted[key] = extracted[key] || [];
1486
+ extracted[key].push(error.detail || error.title);
1487
+ }
1488
+ }
1489
+ });
1490
+
1491
+ // if the user has an attrs hash, convert keys using it
1492
+ this.normalizeUsingDeclaredMapping(typeClass, extracted);
1493
+
1494
+ // for each attr and relationship, make sure that we use
1495
+ // the normalized key
1474
1496
  typeClass.eachAttribute((name) => {
1475
1497
  let key = this.keyForAttribute(name, 'deserialize');
1476
- if (key !== name && payload[key] !== undefined) {
1477
- payload[name] = payload[key];
1478
- delete payload[key];
1498
+ if (key !== name && extracted[key] !== undefined) {
1499
+ extracted[name] = extracted[key];
1500
+ delete extracted[key];
1479
1501
  }
1480
1502
  });
1481
1503
 
1482
1504
  typeClass.eachRelationship((name) => {
1483
1505
  let key = this.keyForRelationship(name, 'deserialize');
1484
- if (key !== name && payload[key] !== undefined) {
1485
- payload[name] = payload[key];
1486
- delete payload[key];
1506
+ if (key !== name && extracted[key] !== undefined) {
1507
+ extracted[name] = extracted[key];
1508
+ delete extracted[key];
1487
1509
  }
1488
1510
  });
1511
+
1512
+ return extracted;
1489
1513
  }
1490
1514
 
1491
1515
  return payload;
@@ -1499,7 +1523,7 @@ const JSONSerializer = Serializer.extend({
1499
1523
 
1500
1524
  ```app/serializers/application.js
1501
1525
  import JSONSerializer from '@ember-data/serializer/json';
1502
- import { underscore } from '@ember/string';
1526
+ import { underscore } from '<app-name>/utils/string-utils';
1503
1527
 
1504
1528
  export default class ApplicationSerializer extends JSONSerializer {
1505
1529
  keyForAttribute(attr, method) {
@@ -1527,7 +1551,7 @@ const JSONSerializer = Serializer.extend({
1527
1551
 
1528
1552
  ```app/serializers/post.js
1529
1553
  import JSONSerializer from '@ember-data/serializer/json';
1530
- import { underscore } from '@ember/string';
1554
+ import { underscore } from '<app-name>/utils/string-utils';
1531
1555
 
1532
1556
  export default class PostSerializer extends JSONSerializer {
1533
1557
  keyForRelationship(key, relationship, method) {
package/addon/rest.js CHANGED
@@ -1,19 +1,19 @@
1
1
  /**
2
2
  * @module @ember-data/serializer/rest
3
3
  */
4
- import { makeArray } from '@ember/array';
5
4
  import { assert, warn } from '@ember/debug';
6
- import { camelize } from '@ember/string';
5
+ import { camelize, dasherize } from '@ember/string';
7
6
  import { isNone, typeOf } from '@ember/utils';
8
7
  import { DEBUG } from '@glimmer/env';
9
8
 
10
9
  import { singularize } from 'ember-inflector';
11
10
 
12
11
  import JSONSerializer from '@ember-data/serializer/json';
13
- import { normalizeModelName } from '@ember-data/store';
14
12
  import { coerceId } from '@ember-data/store/-private';
15
13
 
16
- import { modelHasAttributeOrRelationshipNamedType } from './-private';
14
+ function makeArray(value) {
15
+ return Array.isArray(value) ? value : [value];
16
+ }
17
17
 
18
18
  /**
19
19
  Normally, applications will use the `RESTSerializer` by implementing
@@ -40,7 +40,7 @@ import { modelHasAttributeOrRelationshipNamedType } from './-private';
40
40
 
41
41
  ```app/serializers/application.js
42
42
  import RESTSerializer from '@ember-data/serializer/rest';
43
- import { underscore } from '@ember/string';
43
+ import { underscore } from '<app-name>/utils/string-utils';
44
44
 
45
45
  export default class ApplicationSerializer extends RESTSerializer {
46
46
  keyForAttribute(attr, method) {
@@ -196,7 +196,7 @@ const RESTSerializer = JSONSerializer.extend({
196
196
  let serializer = primarySerializer;
197
197
  let modelClass = primaryModelClass;
198
198
 
199
- let primaryHasTypeAttribute = modelHasAttributeOrRelationshipNamedType(primaryModelClass);
199
+ let primaryHasTypeAttribute = primaryModelClass.fields.has('type');
200
200
 
201
201
  if (!primaryHasTypeAttribute && hash.type) {
202
202
  // Support polymorphic records in async relationships
@@ -350,7 +350,7 @@ const RESTSerializer = JSONSerializer.extend({
350
350
  },
351
351
 
352
352
  isPrimaryType(store, modelName, primaryModelClass) {
353
- return normalizeModelName(modelName) === primaryModelClass.modelName;
353
+ return dasherize(modelName) === primaryModelClass.modelName;
354
354
  },
355
355
 
356
356
  /**
@@ -473,7 +473,7 @@ const RESTSerializer = JSONSerializer.extend({
473
473
  @return {String} the model's modelName
474
474
  */
475
475
  modelNameFromPayloadKey(key) {
476
- return singularize(normalizeModelName(key));
476
+ return singularize(dasherize(key));
477
477
  },
478
478
 
479
479
  // SERIALIZE
@@ -561,7 +561,7 @@ const RESTSerializer = JSONSerializer.extend({
561
561
 
562
562
  ```app/serializers/application.js
563
563
  import RESTSerializer from '@ember-data/serializer/rest';
564
- import { pluralize } from 'ember-inflector';
564
+ import { pluralize } from '<app-name>/utils/string-utils';
565
565
 
566
566
  export default class ApplicationSerializer extends RESTSerializer {
567
567
  serialize(snapshot, options) {
@@ -645,7 +645,7 @@ const RESTSerializer = JSONSerializer.extend({
645
645
 
646
646
  ```app/serializers/application.js
647
647
  import RESTSerializer from '@ember-data/serializer/rest';
648
- import { decamelize } from '@ember/string';
648
+ import { decamelize } from '<app-name>/utils/string-utils';
649
649
 
650
650
  export default class ApplicationSerializer extends RESTSerializer {
651
651
  serializeIntoHash(data, type, record, options) {
@@ -688,7 +688,7 @@ const RESTSerializer = JSONSerializer.extend({
688
688
 
689
689
  ```app/serializers/application.js
690
690
  import RESTSerializer from '@ember-data/serializer/rest';
691
- import { dasherize } from '@ember/string';
691
+ import { dasherize } from '<app-name>/utils/string-utils';
692
692
 
693
693
  export default class ApplicationSerializer extends RESTSerializer {
694
694
  payloadKeyFromModelName(modelName) {
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "@ember-data/serializer",
3
- "version": "4.6.1",
4
- "description": "The default blueprint for ember-cli addons.",
3
+ "version": "4.7.0",
4
+ "description": "Provides reference Serializer implementations for use with @ember-data/store",
5
5
  "keywords": [
6
6
  "ember-addon"
7
7
  ],
8
- "repository": "https://github.com/emberjs/data/tree/master/packages/serializer",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+ssh://git@github.com:emberjs/data.git",
11
+ "directory": "packages/serializer"
12
+ },
9
13
  "license": "MIT",
10
14
  "author": "",
11
15
  "directories": {
@@ -18,15 +22,15 @@
18
22
  "test:node": "mocha"
19
23
  },
20
24
  "dependencies": {
21
- "@ember-data/private-build-infra": "4.6.1",
22
- "@ember-data/store": "4.6.1",
25
+ "@ember-data/private-build-infra": "4.7.0",
26
+ "@ember-data/store": "4.7.0",
23
27
  "ember-auto-import": "^2.4.2",
24
28
  "ember-cli-babel": "^7.26.11",
25
29
  "ember-cli-test-info": "^1.0.0",
26
30
  "ember-cli-typescript": "^5.1.0"
27
31
  },
28
32
  "devDependencies": {
29
- "@ember-data/unpublished-test-infra": "4.6.1",
33
+ "@ember-data/unpublished-test-infra": "4.7.0",
30
34
  "@ember/optional-features": "^2.0.0",
31
35
  "@ember/string": "^3.0.0",
32
36
  "@ember/test-helpers": "~2.7.0",
@@ -60,7 +64,7 @@
60
64
  "configPath": "tests/dummy/config"
61
65
  },
62
66
  "volta": {
63
- "node": "16.16.0",
67
+ "node": "16.17.0",
64
68
  "yarn": "1.22.19"
65
69
  }
66
70
  }
@@ -1,10 +0,0 @@
1
- import { get } from '@ember/object';
2
-
3
- /*
4
- Check if the passed model has a `type` attribute or a relationship named `type`.
5
- */
6
- function modelHasAttributeOrRelationshipNamedType(modelClass) {
7
- return get(modelClass, 'attributes').has('type') || get(modelClass, 'relationshipsByName').has('type');
8
- }
9
-
10
- export { modelHasAttributeOrRelationshipNamedType };