@ember-data-mirror/serializer 5.6.0-alpha.2 → 5.6.0-alpha.4
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/dist/index.js +5 -10
- package/dist/index.js.map +1 -1
- package/dist/{json-CVTR4xWv.js → json-CYP2BhR9.js} +42 -89
- package/dist/json-CYP2BhR9.js.map +1 -0
- package/dist/json-api.js +14 -31
- package/dist/json-api.js.map +1 -1
- package/dist/json.js +1 -1
- package/dist/rest.js +30 -59
- package/dist/rest.js.map +1 -1
- package/dist/transform.js +9 -32
- package/dist/transform.js.map +1 -1
- package/package.json +13 -16
- package/unstable-preview-types/-private/embedded-records-mixin.d.ts +1 -4
- package/unstable-preview-types/-private/embedded-records-mixin.d.ts.map +1 -1
- package/unstable-preview-types/-private/transforms/boolean.d.ts +2 -5
- package/unstable-preview-types/-private/transforms/boolean.d.ts.map +1 -1
- package/unstable-preview-types/-private/transforms/date.d.ts +1 -4
- package/unstable-preview-types/-private/transforms/date.d.ts.map +1 -1
- package/unstable-preview-types/-private/transforms/number.d.ts +1 -4
- package/unstable-preview-types/-private/transforms/number.d.ts.map +1 -1
- package/unstable-preview-types/-private/transforms/string.d.ts +1 -4
- package/unstable-preview-types/-private/transforms/string.d.ts.map +1 -1
- package/unstable-preview-types/-private/transforms/transform.d.ts +4 -9
- package/unstable-preview-types/-private/transforms/transform.d.ts.map +1 -1
- package/unstable-preview-types/index.d.ts +7 -12
- package/unstable-preview-types/index.d.ts.map +1 -1
- package/unstable-preview-types/json-api.d.ts +9 -24
- package/unstable-preview-types/json-api.d.ts.map +1 -1
- package/unstable-preview-types/json.d.ts +19 -62
- package/unstable-preview-types/json.d.ts.map +1 -1
- package/unstable-preview-types/rest.d.ts +11 -24
- package/unstable-preview-types/rest.d.ts.map +1 -1
- package/unstable-preview-types/transform.d.ts +0 -3
- package/unstable-preview-types/transform.d.ts.map +1 -1
- package/dist/json-CVTR4xWv.js.map +0 -1
package/dist/rest.js
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import { warn } from '@ember/debug';
|
|
2
2
|
import { camelize, dasherize, singularize } from '@ember-data-mirror/request-utils/string';
|
|
3
|
-
import { J as JSONSerializer, c as coerceId } from "./json-
|
|
3
|
+
import { J as JSONSerializer, c as coerceId } from "./json-CYP2BhR9.js";
|
|
4
4
|
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
5
5
|
import Mixin from '@ember/object/mixin';
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
@module @ember-data-mirror/serializer/rest
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
7
|
/**
|
|
12
8
|
## Using Embedded Records
|
|
13
9
|
|
|
@@ -22,7 +18,7 @@ import Mixin from '@ember/object/mixin';
|
|
|
22
18
|
|
|
23
19
|
Below is an example of a per-type serializer (`post` type).
|
|
24
20
|
|
|
25
|
-
```app/serializers/post.js
|
|
21
|
+
```js [app/serializers/post.js]
|
|
26
22
|
import RESTSerializer, { EmbeddedRecordsMixin } from '@ember-data-mirror/serializer/rest';
|
|
27
23
|
|
|
28
24
|
export default class PostSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
|
|
@@ -122,7 +118,6 @@ const EmbeddedRecordsMixin = Mixin.create({
|
|
|
122
118
|
}
|
|
123
119
|
}
|
|
124
120
|
```
|
|
125
|
-
@method normalize
|
|
126
121
|
@public
|
|
127
122
|
@param {Model} typeClass
|
|
128
123
|
@param {Object} hash to be normalized
|
|
@@ -156,7 +151,7 @@ const EmbeddedRecordsMixin = Mixin.create({
|
|
|
156
151
|
});
|
|
157
152
|
```
|
|
158
153
|
Use a custom (type) serializer for the post model to configure embedded author
|
|
159
|
-
```app/serializers/post.js
|
|
154
|
+
```js [app/serializers/post.js]
|
|
160
155
|
import RESTSerializer, { EmbeddedRecordsMixin } from '@ember-data-mirror/serializer/rest';
|
|
161
156
|
export default class PostSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
|
|
162
157
|
attrs = {
|
|
@@ -178,8 +173,7 @@ const EmbeddedRecordsMixin = Mixin.create({
|
|
|
178
173
|
}
|
|
179
174
|
}
|
|
180
175
|
```
|
|
181
|
-
@
|
|
182
|
-
@public
|
|
176
|
+
@public
|
|
183
177
|
@param {Snapshot} snapshot
|
|
184
178
|
@param {Object} json
|
|
185
179
|
@param {Object} relationship
|
|
@@ -246,7 +240,7 @@ const EmbeddedRecordsMixin = Mixin.create({
|
|
|
246
240
|
});
|
|
247
241
|
```
|
|
248
242
|
Use a custom (type) serializer for the post model to configure embedded comments
|
|
249
|
-
```app/serializers/post.js
|
|
243
|
+
```js [app/serializers/post.js]
|
|
250
244
|
import RESTSerializer, { EmbeddedRecordsMixin } from '@ember-data-mirror/serializer/rest';
|
|
251
245
|
export default class PostSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
|
|
252
246
|
attrs = {
|
|
@@ -278,7 +272,7 @@ const EmbeddedRecordsMixin = Mixin.create({
|
|
|
278
272
|
So `{ embedded: 'always' }` is shorthand for:
|
|
279
273
|
`{ serialize: 'records', deserialize: 'records' }`
|
|
280
274
|
To embed the `ids` for a related object (using a hasMany relationship):
|
|
281
|
-
```app/serializers/post.js
|
|
275
|
+
```js [app/serializers/post.js]
|
|
282
276
|
import RESTSerializer, { EmbeddedRecordsMixin } from '@ember-data-mirror/serializer/rest';
|
|
283
277
|
export default class PostSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
|
|
284
278
|
attrs = {
|
|
@@ -316,7 +310,7 @@ const EmbeddedRecordsMixin = Mixin.create({
|
|
|
316
310
|
// ...
|
|
317
311
|
});
|
|
318
312
|
```
|
|
319
|
-
```app/serializers/user.js
|
|
313
|
+
```js [app/serializers/user.js]
|
|
320
314
|
import RESTSerializer, { EmbeddedRecordsMixin } from '@ember-data-mirror/serializer/rest';
|
|
321
315
|
export default class UserSerializer extends RESTSerializer.extend(EmbeddedRecordsMixin) {
|
|
322
316
|
attrs = {
|
|
@@ -336,8 +330,7 @@ const EmbeddedRecordsMixin = Mixin.create({
|
|
|
336
330
|
}
|
|
337
331
|
}
|
|
338
332
|
```
|
|
339
|
-
@
|
|
340
|
-
@public
|
|
333
|
+
@public
|
|
341
334
|
@param {Snapshot} snapshot
|
|
342
335
|
@param {Object} json
|
|
343
336
|
@param {Object} relationship
|
|
@@ -420,8 +413,7 @@ const EmbeddedRecordsMixin = Mixin.create({
|
|
|
420
413
|
parent record
|
|
421
414
|
Serializing a `hasMany` relationship does not remove the property that refers to
|
|
422
415
|
the parent record.
|
|
423
|
-
@
|
|
424
|
-
@public
|
|
416
|
+
@public
|
|
425
417
|
@param {Snapshot} snapshot
|
|
426
418
|
@param {Snapshot} embeddedSnapshot
|
|
427
419
|
@param {Object} relationship
|
|
@@ -482,7 +474,6 @@ const EmbeddedRecordsMixin = Mixin.create({
|
|
|
482
474
|
return attrs && (attrs[camelize(attr)] || attrs[attr]);
|
|
483
475
|
},
|
|
484
476
|
/**
|
|
485
|
-
@method _extractEmbeddedRecords
|
|
486
477
|
@private
|
|
487
478
|
*/
|
|
488
479
|
_extractEmbeddedRecords(serializer, store, typeClass, partial) {
|
|
@@ -499,7 +490,6 @@ const EmbeddedRecordsMixin = Mixin.create({
|
|
|
499
490
|
return partial;
|
|
500
491
|
},
|
|
501
492
|
/**
|
|
502
|
-
@method _extractEmbeddedHasMany
|
|
503
493
|
@private
|
|
504
494
|
*/
|
|
505
495
|
_extractEmbeddedHasMany(store, key, hash, relationshipMeta) {
|
|
@@ -533,7 +523,6 @@ const EmbeddedRecordsMixin = Mixin.create({
|
|
|
533
523
|
hash.data.relationships[key] = relationship;
|
|
534
524
|
},
|
|
535
525
|
/**
|
|
536
|
-
@method _extractEmbeddedBelongsTo
|
|
537
526
|
@private
|
|
538
527
|
*/
|
|
539
528
|
_extractEmbeddedBelongsTo(store, key, hash, relationshipMeta) {
|
|
@@ -563,7 +552,6 @@ const EmbeddedRecordsMixin = Mixin.create({
|
|
|
563
552
|
hash.data.relationships[key] = relationship;
|
|
564
553
|
},
|
|
565
554
|
/**
|
|
566
|
-
@method _normalizeEmbeddedRelationship
|
|
567
555
|
@private
|
|
568
556
|
*/
|
|
569
557
|
_normalizeEmbeddedRelationship(store, relationshipMeta, relationshipHash) {
|
|
@@ -577,10 +565,6 @@ const EmbeddedRecordsMixin = Mixin.create({
|
|
|
577
565
|
},
|
|
578
566
|
isEmbeddedRecordsMixin: true
|
|
579
567
|
});
|
|
580
|
-
|
|
581
|
-
/**
|
|
582
|
-
* @module @ember-data-mirror/serializer/rest
|
|
583
|
-
*/
|
|
584
568
|
function makeArray(value) {
|
|
585
569
|
return Array.isArray(value) ? value : [value];
|
|
586
570
|
}
|
|
@@ -616,7 +600,7 @@ function makeArray(value) {
|
|
|
616
600
|
can implement across-the-board rules for how to convert an attribute
|
|
617
601
|
name in your model to a key in your JSON.
|
|
618
602
|
|
|
619
|
-
```app/serializers/application.js
|
|
603
|
+
```js [app/serializers/application.js]
|
|
620
604
|
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
621
605
|
import { underscore } from '<app-name>/utils/string-utils';
|
|
622
606
|
|
|
@@ -633,9 +617,7 @@ function makeArray(value) {
|
|
|
633
617
|
the method (`serialize` or `deserialize`) as the third parameter.
|
|
634
618
|
|
|
635
619
|
@class RESTSerializer
|
|
636
|
-
@main @ember-data-mirror/serializer/rest
|
|
637
620
|
@public
|
|
638
|
-
@extends JSONSerializer
|
|
639
621
|
*/
|
|
640
622
|
const RESTSerializer = JSONSerializer.extend({
|
|
641
623
|
/**
|
|
@@ -643,7 +625,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
643
625
|
serializing and deserializing a polymorphic type. By default, the
|
|
644
626
|
returned key is `${key}Type`.
|
|
645
627
|
Example
|
|
646
|
-
```app/serializers/post.js
|
|
628
|
+
```js [app/serializers/post.js]
|
|
647
629
|
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
648
630
|
export default class ApplicationSerializer extends RESTSerializer {
|
|
649
631
|
keyForPolymorphicType(key, relationship) {
|
|
@@ -652,8 +634,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
652
634
|
}
|
|
653
635
|
}
|
|
654
636
|
```
|
|
655
|
-
|
|
656
|
-
@public
|
|
637
|
+
@public
|
|
657
638
|
@param {String} key
|
|
658
639
|
@param {String} typeClass
|
|
659
640
|
@param {String} method
|
|
@@ -696,7 +677,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
696
677
|
`normalize` and manipulate the payload as desired.
|
|
697
678
|
For example, if the `IDs` under `"comments"` are provided as `_id` instead of
|
|
698
679
|
`id`, you can specify how to normalize just the comments:
|
|
699
|
-
```app/serializers/post.js
|
|
680
|
+
```js [app/serializers/post.js]
|
|
700
681
|
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
701
682
|
export default class ApplicationSerializer extends RESTSerializer {
|
|
702
683
|
normalize(model, hash, prop) {
|
|
@@ -711,8 +692,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
711
692
|
On each call to the `normalize` method, the third parameter (`prop`) is always
|
|
712
693
|
one of the keys that were in the original payload or in the result of another
|
|
713
694
|
normalization as `normalizeResponse`.
|
|
714
|
-
@
|
|
715
|
-
@public
|
|
695
|
+
@public
|
|
716
696
|
@param {Model} modelClass
|
|
717
697
|
@param {Object} resourceHash
|
|
718
698
|
@param {String} prop
|
|
@@ -722,8 +702,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
722
702
|
/**
|
|
723
703
|
Normalizes an array of resource payloads and returns a JSON-API Document
|
|
724
704
|
with primary data and, if any, included data as `{ data, included }`.
|
|
725
|
-
@
|
|
726
|
-
@param {Store} store
|
|
705
|
+
@param {Store} store
|
|
727
706
|
@param {String} modelName
|
|
728
707
|
@param {Object} arrayHash
|
|
729
708
|
@param {String} prop
|
|
@@ -766,7 +745,6 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
766
745
|
return serializer.normalize(modelClass, hash, prop);
|
|
767
746
|
},
|
|
768
747
|
/**
|
|
769
|
-
@method _normalizeResponse
|
|
770
748
|
@param {Store} store
|
|
771
749
|
@param {Model} primaryModelClass
|
|
772
750
|
@param {Object} payload
|
|
@@ -922,8 +900,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
922
900
|
It will first normalize the payload, so you can use this to push
|
|
923
901
|
in data streaming in from your server structured the same way
|
|
924
902
|
that fetches and saves are structured.
|
|
925
|
-
@
|
|
926
|
-
@public
|
|
903
|
+
@public
|
|
927
904
|
@param {Store} store
|
|
928
905
|
@param {Object} payload
|
|
929
906
|
*/
|
|
@@ -966,7 +943,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
966
943
|
For example, your server may send a model name that does not correspond with
|
|
967
944
|
the name of the model in your app. Let's take a look at an example model,
|
|
968
945
|
and an example payload:
|
|
969
|
-
```app/models/post.js
|
|
946
|
+
```js [app/models/post.js]
|
|
970
947
|
import Model from '@ember-data-mirror/model';
|
|
971
948
|
export default class Post extends Model {}
|
|
972
949
|
```
|
|
@@ -983,7 +960,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
983
960
|
because it cannot find the "blog/post" model.
|
|
984
961
|
Since we want to remove this namespace, we can define a serializer for the application that will
|
|
985
962
|
remove "blog/" from the payload key whenver it's encountered by Ember Data:
|
|
986
|
-
```app/serializers/application.js
|
|
963
|
+
```js [app/serializers/application.js]
|
|
987
964
|
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
988
965
|
export default class ApplicationSerializer extends RESTSerializer {
|
|
989
966
|
modelNameFromPayloadKey(payloadKey) {
|
|
@@ -1001,8 +978,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
1001
978
|
normalized to "blog-post" when Ember Data looks up the model. Usually, Ember Data
|
|
1002
979
|
can use the correct inflection to do this for you. Most of the time, you won't
|
|
1003
980
|
need to override `modelNameFromPayloadKey` for this purpose.
|
|
1004
|
-
@
|
|
1005
|
-
@public
|
|
981
|
+
@public
|
|
1006
982
|
@param {String} key
|
|
1007
983
|
@return {String} the model's modelName
|
|
1008
984
|
*/
|
|
@@ -1017,7 +993,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
1017
993
|
By default, it creates a JSON object with a key for
|
|
1018
994
|
each attribute and belongsTo relationship.
|
|
1019
995
|
For example, consider this model:
|
|
1020
|
-
```app/models/comment.js
|
|
996
|
+
```js [app/models/comment.js]
|
|
1021
997
|
import Model, { attr, belongsTo } from '@ember-data-mirror/model';
|
|
1022
998
|
export default class Comment extends Model {
|
|
1023
999
|
@attr title
|
|
@@ -1050,7 +1026,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
1050
1026
|
built-in serialization format.
|
|
1051
1027
|
In that case, you can implement `serialize` yourself and
|
|
1052
1028
|
return a JSON hash of your choosing.
|
|
1053
|
-
```app/serializers/post.js
|
|
1029
|
+
```js [app/serializers/post.js]
|
|
1054
1030
|
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
1055
1031
|
export default class ApplicationSerializer extends RESTSerializer {
|
|
1056
1032
|
serialize(snapshot, options) {
|
|
@@ -1070,7 +1046,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
1070
1046
|
If you want to define a serializer for your entire
|
|
1071
1047
|
application, you'll probably want to use `eachAttribute`
|
|
1072
1048
|
and `eachRelationship` on the record.
|
|
1073
|
-
```app/serializers/application.js
|
|
1049
|
+
```js [app/serializers/application.js]
|
|
1074
1050
|
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
1075
1051
|
import { pluralize } from '<app-name>/utils/string-utils';
|
|
1076
1052
|
export default class ApplicationSerializer extends RESTSerializer {
|
|
@@ -1109,7 +1085,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
1109
1085
|
If you just want to do some small tweaks on the default JSON,
|
|
1110
1086
|
you can call super first and make the tweaks on the returned
|
|
1111
1087
|
JSON.
|
|
1112
|
-
```app/serializers/post.js
|
|
1088
|
+
```js [app/serializers/post.js]
|
|
1113
1089
|
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
1114
1090
|
export default class ApplicationSerializer extends RESTSerializer {
|
|
1115
1091
|
serialize(snapshot, options) {
|
|
@@ -1120,8 +1096,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
1120
1096
|
}
|
|
1121
1097
|
}
|
|
1122
1098
|
```
|
|
1123
|
-
@
|
|
1124
|
-
@public
|
|
1099
|
+
@public
|
|
1125
1100
|
@param {Snapshot} snapshot
|
|
1126
1101
|
@param {Object} options
|
|
1127
1102
|
@return {Object} json
|
|
@@ -1135,7 +1110,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
1135
1110
|
By default the REST Serializer sends the modelName of a model, which is a camelized
|
|
1136
1111
|
version of the name.
|
|
1137
1112
|
For example, your server may expect underscored root objects.
|
|
1138
|
-
```app/serializers/application.js
|
|
1113
|
+
```js [app/serializers/application.js]
|
|
1139
1114
|
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
1140
1115
|
import { underscore } from '<app-name>/utils/string-utils';
|
|
1141
1116
|
export default class ApplicationSerializer extends RESTSerializer {
|
|
@@ -1145,8 +1120,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
1145
1120
|
}
|
|
1146
1121
|
}
|
|
1147
1122
|
```
|
|
1148
|
-
@
|
|
1149
|
-
@public
|
|
1123
|
+
@public
|
|
1150
1124
|
@param {Object} hash
|
|
1151
1125
|
@param {Model} typeClass
|
|
1152
1126
|
@param {Snapshot} snapshot
|
|
@@ -1171,7 +1145,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
1171
1145
|
}
|
|
1172
1146
|
```
|
|
1173
1147
|
For example, your server may expect dasherized root objects:
|
|
1174
|
-
```app/serializers/application.js
|
|
1148
|
+
```js [app/serializers/application.js]
|
|
1175
1149
|
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
1176
1150
|
import { dasherize } from '<app-name>/utils/string-utils';
|
|
1177
1151
|
export default class ApplicationSerializer extends RESTSerializer {
|
|
@@ -1190,8 +1164,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
1190
1164
|
}
|
|
1191
1165
|
}
|
|
1192
1166
|
```
|
|
1193
|
-
@
|
|
1194
|
-
@public
|
|
1167
|
+
@public
|
|
1195
1168
|
@param {String} modelName
|
|
1196
1169
|
@return {String}
|
|
1197
1170
|
*/
|
|
@@ -1202,8 +1175,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
1202
1175
|
You can use this method to customize how polymorphic objects are serialized.
|
|
1203
1176
|
By default the REST Serializer creates the key by appending `Type` to
|
|
1204
1177
|
the attribute and value from the model's camelcased model name.
|
|
1205
|
-
@
|
|
1206
|
-
@public
|
|
1178
|
+
@public
|
|
1207
1179
|
@param {Snapshot} snapshot
|
|
1208
1180
|
@param {Object} json
|
|
1209
1181
|
@param {Object} relationship
|
|
@@ -1221,8 +1193,7 @@ const RESTSerializer = JSONSerializer.extend({
|
|
|
1221
1193
|
/**
|
|
1222
1194
|
You can use this method to customize how a polymorphic relationship should
|
|
1223
1195
|
be extracted.
|
|
1224
|
-
@
|
|
1225
|
-
@public
|
|
1196
|
+
@public
|
|
1226
1197
|
@param {Object} relationshipType
|
|
1227
1198
|
@param {Object} relationshipHash
|
|
1228
1199
|
@param {Object} relationshipOptions
|