@ember-data-mirror/serializer 5.4.0-alpha.49

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.
Files changed (58) hide show
  1. package/LICENSE.md +11 -0
  2. package/README.md +98 -0
  3. package/addon/-private.js +3 -0
  4. package/addon/-private.js.map +1 -0
  5. package/addon/embedded-records-mixin-QJe_8jrF.js +572 -0
  6. package/addon/embedded-records-mixin-QJe_8jrF.js.map +1 -0
  7. package/addon/index.js +181 -0
  8. package/addon/index.js.map +1 -0
  9. package/addon/json-api.js +508 -0
  10. package/addon/json-api.js.map +1 -0
  11. package/addon/json.js +1375 -0
  12. package/addon/json.js.map +1 -0
  13. package/addon/rest.js +684 -0
  14. package/addon/rest.js.map +1 -0
  15. package/addon/string-Juwz4cu0.js +345 -0
  16. package/addon/string-Juwz4cu0.js.map +1 -0
  17. package/addon/transform.js +1 -0
  18. package/addon/transform.js.map +1 -0
  19. package/addon/utils-NcVD2Jb5.js +12 -0
  20. package/addon/utils-NcVD2Jb5.js.map +1 -0
  21. package/addon-main.js +94 -0
  22. package/blueprints/serializer/files/__root__/__path__/__name__.js +4 -0
  23. package/blueprints/serializer/index.js +14 -0
  24. package/blueprints/serializer/native-files/__root__/__path__/__name__.js +4 -0
  25. package/blueprints/serializer-test/index.js +29 -0
  26. package/blueprints/serializer-test/qunit-files/__root__/__path__/__test__.js +24 -0
  27. package/blueprints/transform/files/__root__/__path__/__name__.js +13 -0
  28. package/blueprints/transform/index.js +7 -0
  29. package/blueprints/transform/native-files/__root__/__path__/__name__.js +13 -0
  30. package/blueprints/transform-test/index.js +29 -0
  31. package/blueprints/transform-test/qunit-files/__root__/__path__/__test__.js +13 -0
  32. package/package.json +113 -0
  33. package/unstable-preview-types/-private/embedded-records-mixin.d.ts +7 -0
  34. package/unstable-preview-types/-private/embedded-records-mixin.d.ts.map +1 -0
  35. package/unstable-preview-types/-private/transforms/boolean.d.ts +52 -0
  36. package/unstable-preview-types/-private/transforms/boolean.d.ts.map +1 -0
  37. package/unstable-preview-types/-private/transforms/date.d.ts +33 -0
  38. package/unstable-preview-types/-private/transforms/date.d.ts.map +1 -0
  39. package/unstable-preview-types/-private/transforms/number.d.ts +34 -0
  40. package/unstable-preview-types/-private/transforms/number.d.ts.map +1 -0
  41. package/unstable-preview-types/-private/transforms/string.d.ts +34 -0
  42. package/unstable-preview-types/-private/transforms/string.d.ts.map +1 -0
  43. package/unstable-preview-types/-private/transforms/transform.d.ts +128 -0
  44. package/unstable-preview-types/-private/transforms/transform.d.ts.map +1 -0
  45. package/unstable-preview-types/-private/utils.d.ts +6 -0
  46. package/unstable-preview-types/-private/utils.d.ts.map +1 -0
  47. package/unstable-preview-types/-private.d.ts +13 -0
  48. package/unstable-preview-types/-private.d.ts.map +1 -0
  49. package/unstable-preview-types/index.d.ts +278 -0
  50. package/unstable-preview-types/index.d.ts.map +1 -0
  51. package/unstable-preview-types/json-api.d.ts +515 -0
  52. package/unstable-preview-types/json-api.d.ts.map +1 -0
  53. package/unstable-preview-types/json.d.ts +1094 -0
  54. package/unstable-preview-types/json.d.ts.map +1 -0
  55. package/unstable-preview-types/rest.d.ts +602 -0
  56. package/unstable-preview-types/rest.d.ts.map +1 -0
  57. package/unstable-preview-types/transform.d.ts +7 -0
  58. package/unstable-preview-types/transform.d.ts.map +1 -0
@@ -0,0 +1,1094 @@
1
+ declare module '@ember-data-mirror/serializer/json' {
2
+ /// <reference types="ember-source/types" />
3
+ export default JSONSerializer;
4
+ /**
5
+ * <blockquote style="margin: 1em; padding: .1em 1em .1em 1em; border-left: solid 1em #E34C32; background: #e0e0e0;">
6
+ <p>
7
+ ⚠️ <strong>This is LEGACY documentation</strong> for a feature that is no longer encouraged to be used.
8
+ If starting a new app or thinking of implementing a new adapter, consider writing a
9
+ <a href="/ember-data/release/classes/%3CInterface%3E%20Handler">Handler</a> instead to be used with the <a href="https://github.com/emberjs/data/tree/main/packages/request#readme">RequestManager</a>
10
+ </p>
11
+ </blockquote>
12
+
13
+ In EmberData a Serializer is used to serialize and deserialize
14
+ records when they are transferred in and out of an external source.
15
+ This process involves normalizing property names, transforming
16
+ attribute values and serializing relationships.
17
+
18
+ By default, EmberData uses and recommends the `JSONAPISerializer`.
19
+
20
+ `JSONSerializer` is useful for simpler or legacy backends that may
21
+ not support the http://jsonapi.org/ spec.
22
+
23
+ For example, given the following `User` model and JSON payload:
24
+
25
+ ```app/models/user.js
26
+ import Model, { attr, belongsTo, hasMany } from '@ember-data-mirror/model';
27
+
28
+ export default class UserModel extends Model {
29
+ @hasMany('user') friends;
30
+ @belongsTo('location') house;
31
+
32
+ @attr('string') name;
33
+ }
34
+ ```
35
+
36
+ ```js
37
+ {
38
+ id: 1,
39
+ name: 'Sebastian',
40
+ friends: [3, 4],
41
+ links: {
42
+ house: '/houses/lefkada'
43
+ }
44
+ }
45
+ ```
46
+
47
+ `JSONSerializer` will normalize the JSON payload to the JSON API format that the
48
+ Ember Data store expects.
49
+
50
+ You can customize how JSONSerializer processes its payload by passing options in
51
+ the `attrs` hash or by subclassing the `JSONSerializer` and overriding hooks:
52
+
53
+ - To customize how a single record is normalized, use the `normalize` hook.
54
+ - To customize how `JSONSerializer` normalizes the whole server response, use the
55
+ `normalizeResponse` hook.
56
+ - To customize how `JSONSerializer` normalizes a specific response from the server,
57
+ use one of the many specific `normalizeResponse` hooks.
58
+ - To customize how `JSONSerializer` normalizes your id, attributes or relationships,
59
+ use the `extractId`, `extractAttributes` and `extractRelationships` hooks.
60
+
61
+ The `JSONSerializer` normalization process follows these steps:
62
+
63
+ 1. `normalizeResponse`
64
+ - entry method to the serializer.
65
+ 2. `normalizeCreateRecordResponse`
66
+ - a `normalizeResponse` for a specific operation is called.
67
+ 3. `normalizeSingleResponse`|`normalizeArrayResponse`
68
+ - for methods like `createRecord` we expect a single record back, while for methods like `findAll` we expect multiple records back.
69
+ 4. `normalize`
70
+ - `normalizeArrayResponse` iterates and calls `normalize` for each of its records while `normalizeSingle`
71
+ calls it once. This is the method you most likely want to subclass.
72
+ 5. `extractId` | `extractAttributes` | `extractRelationships`
73
+ - `normalize` delegates to these methods to
74
+ turn the record payload into the JSON API format.
75
+
76
+ @main @ember-data-mirror/serializer/json
77
+ @class JSONSerializer
78
+ @public
79
+ @extends Serializer
80
+ */
81
+ const JSONSerializer: Readonly<typeof Serializer> & (new (owner?: import("@ember/-internals/owner").default | undefined) => Serializer) & {
82
+ /**
83
+ The `primaryKey` is used when serializing and deserializing
84
+ data. Ember Data always uses the `id` property to store the id of
85
+ the record. The external source may not always follow this
86
+ convention. In these cases it is useful to override the
87
+ `primaryKey` property to match the `primaryKey` of your external
88
+ store.
89
+
90
+ Example
91
+
92
+ ```app/serializers/application.js
93
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
94
+
95
+ export default class ApplicationSerializer extends JSONSerializer {
96
+ primaryKey = '_id'
97
+ }
98
+ ```
99
+
100
+ @property primaryKey
101
+ @type {String}
102
+ @public
103
+ @default 'id'
104
+ */
105
+ primaryKey: string;
106
+ /**
107
+ The `attrs` object can be used to a simple mapping between
108
+ property names on `Model` records and payload keys in the
109
+ serialized JSON object representing the record. An object with the
110
+ property `key` can also be used to designate the attribute's key on
111
+ the response payload.
112
+
113
+ Example
114
+
115
+ ```app/models/person.js
116
+ import Model, { attr } from '@ember-data-mirror/model';
117
+
118
+ export default class PersonModel extends Model {
119
+ @attr('string') firstName;
120
+ @attr('string') lastName;
121
+ @attr('string') occupation;
122
+ @attr('boolean') admin;
123
+ }
124
+ ```
125
+
126
+ ```app/serializers/person.js
127
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
128
+
129
+ export default class PersonSerializer extends JSONSerializer {
130
+ attrs = {
131
+ admin: 'is_admin',
132
+ occupation: { key: 'career' }
133
+ }
134
+ }
135
+ ```
136
+
137
+ You can also remove attributes and relationships by setting the `serialize`
138
+ key to `false` in your mapping object.
139
+
140
+ Example
141
+
142
+ ```app/serializers/person.js
143
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
144
+
145
+ export default class PostSerializer extends JSONSerializer {
146
+ attrs = {
147
+ admin: { serialize: false },
148
+ occupation: { key: 'career' }
149
+ }
150
+ }
151
+ ```
152
+
153
+ When serialized:
154
+
155
+ ```javascript
156
+ {
157
+ "firstName": "Harry",
158
+ "lastName": "Houdini",
159
+ "career": "magician"
160
+ }
161
+ ```
162
+
163
+ Note that the `admin` is now not included in the payload.
164
+
165
+ Setting `serialize` to `true` enforces serialization for hasMany
166
+ relationships even if it's neither a many-to-many nor many-to-none
167
+ relationship.
168
+
169
+ @property attrs
170
+ @public
171
+ @type {Object}
172
+ */
173
+ mergedProperties: Object;
174
+ /**
175
+ Given a subclass of `Model` and a JSON object this method will
176
+ iterate through each attribute of the `Model` and invoke the
177
+ `Transform#deserialize` method on the matching property of the
178
+ JSON object. This method is typically called after the
179
+ serializer's `normalize` method.
180
+
181
+ @method applyTransforms
182
+ @private
183
+ @param {Model} typeClass
184
+ @param {Object} data The data to transform
185
+ @return {Object} data The transformed data object
186
+ */
187
+ applyTransforms(typeClass: Model, data: Object): Object;
188
+ /**
189
+ The `normalizeResponse` method is used to normalize a payload from the
190
+ server to a JSON-API Document.
191
+
192
+ http://jsonapi.org/format/#document-structure
193
+
194
+ This method delegates to a more specific normalize method based on
195
+ the `requestType`.
196
+
197
+ To override this method with a custom one, make sure to call
198
+ `return super.normalizeResponse(store, primaryModelClass, payload, id, requestType)` with your
199
+ pre-processed data.
200
+
201
+ Here's an example of using `normalizeResponse` manually:
202
+
203
+ ```javascript
204
+ socket.on('message', function(message) {
205
+ let data = message.data;
206
+ let modelClass = store.modelFor(data.modelName);
207
+ let serializer = store.serializerFor(data.modelName);
208
+ let normalized = serializer.normalizeSingleResponse(store, modelClass, data, data.id);
209
+
210
+ store.push(normalized);
211
+ });
212
+ ```
213
+
214
+ @since 1.13.0
215
+ @method normalizeResponse
216
+ @public
217
+ @param {Store} store
218
+ @param {Model} primaryModelClass
219
+ @param {Object} payload
220
+ @param {String|Number} id
221
+ @param {String} requestType
222
+ @return {Object} JSON-API Document
223
+ */
224
+ normalizeResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
225
+ /**
226
+ Called by the default normalizeResponse implementation when the
227
+ type of request is `findRecord`
228
+
229
+ @since 1.13.0
230
+ @method normalizeFindRecordResponse
231
+ @public
232
+ @param {Store} store
233
+ @param {Model} primaryModelClass
234
+ @param {Object} payload
235
+ @param {String|Number} id
236
+ @param {String} requestType
237
+ @return {Object} JSON-API Document
238
+ */
239
+ normalizeFindRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
240
+ /**
241
+ Called by the default normalizeResponse implementation when the
242
+ type of request is `queryRecord`
243
+
244
+ @since 1.13.0
245
+ @method normalizeQueryRecordResponse
246
+ @public
247
+ @param {Store} store
248
+ @param {Model} primaryModelClass
249
+ @param {Object} payload
250
+ @param {String|Number} id
251
+ @param {String} requestType
252
+ @return {Object} JSON-API Document
253
+ */
254
+ normalizeQueryRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
255
+ /**
256
+ Called by the default normalizeResponse implementation when the
257
+ type of request is `findAll`
258
+
259
+ @since 1.13.0
260
+ @method normalizeFindAllResponse
261
+ @public
262
+ @param {Store} store
263
+ @param {Model} primaryModelClass
264
+ @param {Object} payload
265
+ @param {String|Number} id
266
+ @param {String} requestType
267
+ @return {Object} JSON-API Document
268
+ */
269
+ normalizeFindAllResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
270
+ /**
271
+ Called by the default normalizeResponse implementation when the
272
+ type of request is `findBelongsTo`
273
+
274
+ @since 1.13.0
275
+ @method normalizeFindBelongsToResponse
276
+ @public
277
+ @param {Store} store
278
+ @param {Model} primaryModelClass
279
+ @param {Object} payload
280
+ @param {String|Number} id
281
+ @param {String} requestType
282
+ @return {Object} JSON-API Document
283
+ */
284
+ normalizeFindBelongsToResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
285
+ /**
286
+ Called by the default normalizeResponse implementation when the
287
+ type of request is `findHasMany`
288
+
289
+ @since 1.13.0
290
+ @method normalizeFindHasManyResponse
291
+ @public
292
+ @param {Store} store
293
+ @param {Model} primaryModelClass
294
+ @param {Object} payload
295
+ @param {String|Number} id
296
+ @param {String} requestType
297
+ @return {Object} JSON-API Document
298
+ */
299
+ normalizeFindHasManyResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
300
+ /**
301
+ Called by the default normalizeResponse implementation when the
302
+ type of request is `findMany`
303
+
304
+ @since 1.13.0
305
+ @method normalizeFindManyResponse
306
+ @public
307
+ @param {Store} store
308
+ @param {Model} primaryModelClass
309
+ @param {Object} payload
310
+ @param {String|Number} id
311
+ @param {String} requestType
312
+ @return {Object} JSON-API Document
313
+ */
314
+ normalizeFindManyResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
315
+ /**
316
+ Called by the default normalizeResponse implementation when the
317
+ type of request is `query`
318
+
319
+ @since 1.13.0
320
+ @method normalizeQueryResponse
321
+ @public
322
+ @param {Store} store
323
+ @param {Model} primaryModelClass
324
+ @param {Object} payload
325
+ @param {String|Number} id
326
+ @param {String} requestType
327
+ @return {Object} JSON-API Document
328
+ */
329
+ normalizeQueryResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
330
+ /**
331
+ Called by the default normalizeResponse implementation when the
332
+ type of request is `createRecord`
333
+
334
+ @since 1.13.0
335
+ @method normalizeCreateRecordResponse
336
+ @public
337
+ @param {Store} store
338
+ @param {Model} primaryModelClass
339
+ @param {Object} payload
340
+ @param {String|Number} id
341
+ @param {String} requestType
342
+ @return {Object} JSON-API Document
343
+ */
344
+ normalizeCreateRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
345
+ /**
346
+ Called by the default normalizeResponse implementation when the
347
+ type of request is `deleteRecord`
348
+
349
+ @since 1.13.0
350
+ @method normalizeDeleteRecordResponse
351
+ @public
352
+ @param {Store} store
353
+ @param {Model} primaryModelClass
354
+ @param {Object} payload
355
+ @param {String|Number} id
356
+ @param {String} requestType
357
+ @return {Object} JSON-API Document
358
+ */
359
+ normalizeDeleteRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
360
+ /**
361
+ Called by the default normalizeResponse implementation when the
362
+ type of request is `updateRecord`
363
+
364
+ @since 1.13.0
365
+ @method normalizeUpdateRecordResponse
366
+ @public
367
+ @param {Store} store
368
+ @param {Model} primaryModelClass
369
+ @param {Object} payload
370
+ @param {String|Number} id
371
+ @param {String} requestType
372
+ @return {Object} JSON-API Document
373
+ */
374
+ normalizeUpdateRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
375
+ /**
376
+ normalizeUpdateRecordResponse, normalizeCreateRecordResponse and
377
+ normalizeDeleteRecordResponse delegate to this method by default.
378
+
379
+ @since 1.13.0
380
+ @method normalizeSaveResponse
381
+ @public
382
+ @param {Store} store
383
+ @param {Model} primaryModelClass
384
+ @param {Object} payload
385
+ @param {String|Number} id
386
+ @param {String} requestType
387
+ @return {Object} JSON-API Document
388
+ */
389
+ normalizeSaveResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
390
+ /**
391
+ normalizeQueryResponse and normalizeFindRecordResponse delegate to this
392
+ method by default.
393
+
394
+ @since 1.13.0
395
+ @method normalizeSingleResponse
396
+ @public
397
+ @param {Store} store
398
+ @param {Model} primaryModelClass
399
+ @param {Object} payload
400
+ @param {String|Number} id
401
+ @param {String} requestType
402
+ @return {Object} JSON-API Document
403
+ */
404
+ normalizeSingleResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string): Object;
405
+ /**
406
+ normalizeQueryResponse, normalizeFindManyResponse, and normalizeFindHasManyResponse delegate
407
+ to this method by default.
408
+
409
+ @since 1.13.0
410
+ @method normalizeArrayResponse
411
+ @public
412
+ @param {Store} store
413
+ @param {Model} primaryModelClass
414
+ @param {Object} payload
415
+ @param {String|Number} id
416
+ @param {String} requestType
417
+ @return {Object} JSON-API Document
418
+ */
419
+ normalizeArrayResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string): Object;
420
+ /**
421
+ @method _normalizeResponse
422
+ @param {Store} store
423
+ @param {Model} primaryModelClass
424
+ @param {Object} payload
425
+ @param {String|Number} id
426
+ @param {String} requestType
427
+ @param {Boolean} isSingle
428
+ @return {Object} JSON-API Document
429
+ @private
430
+ */
431
+ _normalizeResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, isSingle: boolean): Object;
432
+ /**
433
+ Normalizes a part of the JSON payload returned by
434
+ the server. You should override this method, munge the hash
435
+ and call super if you have generic normalization to do.
436
+
437
+ It takes the type of the record that is being normalized
438
+ (as a Model class), the property where the hash was
439
+ originally found, and the hash to normalize.
440
+
441
+ You can use this method, for example, to normalize underscored keys to camelized
442
+ or other general-purpose normalizations.
443
+
444
+ Example
445
+
446
+ ```app/serializers/application.js
447
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
448
+ import { underscore } from '<app-name>/utils/string-utils';
449
+ import { get } from '@ember/object';
450
+
451
+ export default class ApplicationSerializer extends JSONSerializer {
452
+ normalize(typeClass, hash) {
453
+ let fields = typeClass.fields;
454
+
455
+ fields.forEach(function(type, field) {
456
+ let payloadField = underscore(field);
457
+ if (field === payloadField) { return; }
458
+
459
+ hash[field] = hash[payloadField];
460
+ delete hash[payloadField];
461
+ });
462
+
463
+ return super.normalize(...arguments);
464
+ }
465
+ }
466
+ ```
467
+
468
+ @method normalize
469
+ @public
470
+ @param {Model} typeClass
471
+ @param {Object} hash
472
+ @return {Object}
473
+ */
474
+ normalize(modelClass: any, resourceHash: any): Object;
475
+ /**
476
+ Returns the resource's ID.
477
+
478
+ @method extractId
479
+ @public
480
+ @param {Object} modelClass
481
+ @param {Object} resourceHash
482
+ @return {String}
483
+ */
484
+ extractId(modelClass: Object, resourceHash: Object): string;
485
+ /**
486
+ Returns the resource's attributes formatted as a JSON-API "attributes object".
487
+
488
+ http://jsonapi.org/format/#document-resource-object-attributes
489
+
490
+ @method extractAttributes
491
+ @public
492
+ @param {Object} modelClass
493
+ @param {Object} resourceHash
494
+ @return {Object}
495
+ */
496
+ extractAttributes(modelClass: Object, resourceHash: Object): Object;
497
+ /**
498
+ Returns a relationship formatted as a JSON-API "relationship object".
499
+
500
+ http://jsonapi.org/format/#document-resource-object-relationships
501
+
502
+ @method extractRelationship
503
+ @public
504
+ @param {Object} relationshipModelName
505
+ @param {Object} relationshipHash
506
+ @return {Object}
507
+ */
508
+ extractRelationship(relationshipModelName: Object, relationshipHash: Object): Object;
509
+ /**
510
+ Returns a polymorphic relationship formatted as a JSON-API "relationship object".
511
+
512
+ http://jsonapi.org/format/#document-resource-object-relationships
513
+
514
+ `relationshipOptions` is a hash which contains more information about the
515
+ polymorphic relationship which should be extracted:
516
+ - `resourceHash` complete hash of the resource the relationship should be
517
+ extracted from
518
+ - `relationshipKey` key under which the value for the relationship is
519
+ extracted from the resourceHash
520
+ - `relationshipMeta` meta information about the relationship
521
+
522
+ @method extractPolymorphicRelationship
523
+ @public
524
+ @param {Object} relationshipModelName
525
+ @param {Object} relationshipHash
526
+ @param {Object} relationshipOptions
527
+ @return {Object}
528
+ */
529
+ extractPolymorphicRelationship(relationshipModelName: Object, relationshipHash: Object, relationshipOptions: Object): Object;
530
+ /**
531
+ Returns the resource's relationships formatted as a JSON-API "relationships object".
532
+
533
+ http://jsonapi.org/format/#document-resource-object-relationships
534
+
535
+ @method extractRelationships
536
+ @public
537
+ @param {Object} modelClass
538
+ @param {Object} resourceHash
539
+ @return {Object}
540
+ */
541
+ extractRelationships(modelClass: Object, resourceHash: Object): Object;
542
+ /**
543
+ Dasherizes the model name in the payload
544
+
545
+ @method modelNameFromPayloadKey
546
+ @public
547
+ @param {String} key
548
+ @return {String} the model's modelName
549
+ */
550
+ modelNameFromPayloadKey(key: string): string;
551
+ /**
552
+ @method normalizeRelationships
553
+ @private
554
+ */
555
+ normalizeRelationships(typeClass: any, hash: any): void;
556
+ /**
557
+ @method normalizeUsingDeclaredMapping
558
+ @private
559
+ */
560
+ normalizeUsingDeclaredMapping(modelClass: any, hash: any): void;
561
+ /**
562
+ Looks up the property key that was set by the custom `attr` mapping
563
+ passed to the serializer.
564
+
565
+ @method _getMappedKey
566
+ @private
567
+ @param {String} key
568
+ @return {String} key
569
+ */
570
+ _getMappedKey(key: string, modelClass: any): string;
571
+ /**
572
+ Check attrs.key.serialize property to inform if the `key`
573
+ can be serialized
574
+
575
+ @method _canSerialize
576
+ @private
577
+ @param {String} key
578
+ @return {boolean} true if the key can be serialized
579
+ */
580
+ _canSerialize(key: string): boolean;
581
+ /**
582
+ When attrs.key.serialize is set to true then
583
+ it takes priority over the other checks and the related
584
+ attribute/relationship will be serialized
585
+
586
+ @method _mustSerialize
587
+ @private
588
+ @param {String} key
589
+ @return {boolean} true if the key must be serialized
590
+ */
591
+ _mustSerialize(key: string): boolean;
592
+ /**
593
+ Check if the given hasMany relationship should be serialized
594
+
595
+ By default only many-to-many and many-to-none relationships are serialized.
596
+ This could be configured per relationship by Serializer's `attrs` object.
597
+
598
+ @method shouldSerializeHasMany
599
+ @public
600
+ @param {Snapshot} snapshot
601
+ @param {String} key
602
+ @param {RelationshipSchema} relationship
603
+ @return {boolean} true if the hasMany relationship should be serialized
604
+ */
605
+ shouldSerializeHasMany(snapshot: Snapshot, key: string, relationship: RelationshipSchema): boolean;
606
+ /**
607
+ Called when a record is saved in order to convert the
608
+ record into JSON.
609
+
610
+ By default, it creates a JSON object with a key for
611
+ each attribute and belongsTo relationship.
612
+
613
+ For example, consider this model:
614
+
615
+ ```app/models/comment.js
616
+ import Model, { attr, belongsTo } from '@ember-data-mirror/model';
617
+
618
+ export default class CommentModel extends Model {
619
+ @attr title;
620
+ @attr body;
621
+
622
+ @belongsTo('user') author;
623
+ }
624
+ ```
625
+
626
+ The default serialization would create a JSON object like:
627
+
628
+ ```javascript
629
+ {
630
+ "title": "Rails is unagi",
631
+ "body": "Rails? Omakase? O_O",
632
+ "author": 12
633
+ }
634
+ ```
635
+
636
+ By default, attributes are passed through as-is, unless
637
+ you specified an attribute type (`attr('date')`). If
638
+ you specify a transform, the JavaScript value will be
639
+ serialized when inserted into the JSON hash.
640
+
641
+ By default, belongs-to relationships are converted into
642
+ IDs when inserted into the JSON hash.
643
+
644
+ ## IDs
645
+
646
+ `serialize` takes an options hash with a single option:
647
+ `includeId`. If this option is `true`, `serialize` will,
648
+ by default include the ID in the JSON object it builds.
649
+
650
+ The adapter passes in `includeId: true` when serializing
651
+ a record for `createRecord`, but not for `updateRecord`.
652
+
653
+ ## Customization
654
+
655
+ Your server may expect a different JSON format than the
656
+ built-in serialization format.
657
+
658
+ In that case, you can implement `serialize` yourself and
659
+ return a JSON hash of your choosing.
660
+
661
+ ```app/serializers/post.js
662
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
663
+
664
+ export default class PostSerializer extends JSONSerializer {
665
+ serialize(snapshot, options) {
666
+ let json = {
667
+ POST_TTL: snapshot.attr('title'),
668
+ POST_BDY: snapshot.attr('body'),
669
+ POST_CMS: snapshot.hasMany('comments', { ids: true })
670
+ };
671
+
672
+ if (options.includeId) {
673
+ json.POST_ID_ = snapshot.id;
674
+ }
675
+
676
+ return json;
677
+ }
678
+ }
679
+ ```
680
+
681
+ ## Customizing an App-Wide Serializer
682
+
683
+ If you want to define a serializer for your entire
684
+ application, you'll probably want to use `eachAttribute`
685
+ and `eachRelationship` on the record.
686
+
687
+ ```app/serializers/application.js
688
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
689
+ import { singularize } from '<app-name>/utils/string-utils';
690
+
691
+ export default class ApplicationSerializer extends JSONSerializer {
692
+ serialize(snapshot, options) {
693
+ let json = {};
694
+
695
+ snapshot.eachAttribute((name) => {
696
+ json[serverAttributeName(name)] = snapshot.attr(name);
697
+ });
698
+
699
+ snapshot.eachRelationship((name, relationship) => {
700
+ if (relationship.kind === 'hasMany') {
701
+ json[serverHasManyName(name)] = snapshot.hasMany(name, { ids: true });
702
+ }
703
+ });
704
+
705
+ if (options.includeId) {
706
+ json.ID_ = snapshot.id;
707
+ }
708
+
709
+ return json;
710
+ }
711
+ }
712
+
713
+ function serverAttributeName(attribute) {
714
+ return attribute.underscore().toUpperCase();
715
+ }
716
+
717
+ function serverHasManyName(name) {
718
+ return serverAttributeName(singularize(name)) + "_IDS";
719
+ }
720
+ ```
721
+
722
+ This serializer will generate JSON that looks like this:
723
+
724
+ ```javascript
725
+ {
726
+ "TITLE": "Rails is omakase",
727
+ "BODY": "Yep. Omakase.",
728
+ "COMMENT_IDS": [ "1", "2", "3" ]
729
+ }
730
+ ```
731
+
732
+ ## Tweaking the Default JSON
733
+
734
+ If you just want to do some small tweaks on the default JSON,
735
+ you can call `super.serialize` first and make the tweaks on
736
+ the returned JSON.
737
+
738
+ ```app/serializers/post.js
739
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
740
+
741
+ export default class PostSerializer extends JSONSerializer {
742
+ serialize(snapshot, options) {
743
+ let json = super.serialize(...arguments);
744
+
745
+ json.subject = json.title;
746
+ delete json.title;
747
+
748
+ return json;
749
+ }
750
+ }
751
+ ```
752
+
753
+ @method serialize
754
+ @public
755
+ @param {Snapshot} snapshot
756
+ @param {Object} options
757
+ @return {Object} json
758
+ */
759
+ serialize(snapshot: Snapshot, options: Object): Object;
760
+ /**
761
+ You can use this method to customize how a serialized record is added to the complete
762
+ JSON hash to be sent to the server. By default the JSON Serializer does not namespace
763
+ the payload and just sends the raw serialized JSON object.
764
+ If your server expects namespaced keys, you should consider using the RESTSerializer.
765
+ Otherwise you can override this method to customize how the record is added to the hash.
766
+ The hash property should be modified by reference.
767
+
768
+ For example, your server may expect underscored root objects.
769
+
770
+ ```app/serializers/application.js
771
+ import RESTSerializer from '@ember-data-mirror/serializer/rest';
772
+ import { decamelize } from '<app-name>/utils/string-utils';
773
+
774
+ export default class ApplicationSerializer extends RESTSerializer {
775
+ serializeIntoHash(data, type, snapshot, options) {
776
+ let root = decamelize(type.modelName);
777
+ data[root] = this.serialize(snapshot, options);
778
+ }
779
+ }
780
+ ```
781
+
782
+ @method serializeIntoHash
783
+ @public
784
+ @param {Object} hash
785
+ @param {Model} typeClass
786
+ @param {Snapshot} snapshot
787
+ @param {Object} options
788
+ */
789
+ serializeIntoHash(hash: Object, typeClass: Model, snapshot: Snapshot, options: Object): void;
790
+ /**
791
+ `serializeAttribute` can be used to customize how `attr`
792
+ properties are serialized
793
+
794
+ For example if you wanted to ensure all your attributes were always
795
+ serialized as properties on an `attributes` object you could
796
+ write:
797
+
798
+ ```app/serializers/application.js
799
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
800
+
801
+ export default class ApplicationSerializer extends JSONSerializer {
802
+ serializeAttribute(snapshot, json, key, attributes) {
803
+ json.attributes = json.attributes || {};
804
+ super.serializeAttribute(snapshot, json.attributes, key, attributes);
805
+ }
806
+ }
807
+ ```
808
+
809
+ @method serializeAttribute
810
+ @public
811
+ @param {Snapshot} snapshot
812
+ @param {Object} json
813
+ @param {String} key
814
+ @param {Object} attribute
815
+ */
816
+ serializeAttribute(snapshot: Snapshot, json: Object, key: string, attribute: Object): void;
817
+ /**
818
+ `serializeBelongsTo` can be used to customize how `belongsTo`
819
+ properties are serialized.
820
+
821
+ Example
822
+
823
+ ```app/serializers/post.js
824
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
825
+
826
+ export default class PostSerializer extends JSONSerializer {
827
+ serializeBelongsTo(snapshot, json, relationship) {
828
+ let key = relationship.name;
829
+ let belongsTo = snapshot.belongsTo(key);
830
+
831
+ key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo", "serialize") : key;
832
+
833
+ json[key] = !belongsTo ? null : belongsTo.record.toJSON();
834
+ }
835
+ }
836
+ ```
837
+
838
+ @method serializeBelongsTo
839
+ @public
840
+ @param {Snapshot} snapshot
841
+ @param {Object} json
842
+ @param {Object} relationship
843
+ */
844
+ serializeBelongsTo(snapshot: Snapshot, json: Object, relationship: Object): void;
845
+ /**
846
+ `serializeHasMany` can be used to customize how `hasMany`
847
+ properties are serialized.
848
+
849
+ Example
850
+
851
+ ```app/serializers/post.js
852
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
853
+
854
+ export default class PostSerializer extends JSONSerializer {
855
+ serializeHasMany(snapshot, json, relationship) {
856
+ let key = relationship.name;
857
+ if (key === 'comments') {
858
+ return;
859
+ } else {
860
+ super.serializeHasMany(...arguments);
861
+ }
862
+ }
863
+ }
864
+ ```
865
+
866
+ @method serializeHasMany
867
+ @public
868
+ @param {Snapshot} snapshot
869
+ @param {Object} json
870
+ @param {Object} relationship
871
+ */
872
+ serializeHasMany(snapshot: Snapshot, json: Object, relationship: Object): void;
873
+ /**
874
+ You can use this method to customize how polymorphic objects are
875
+ serialized. Objects are considered to be polymorphic if
876
+ `{ polymorphic: true }` is pass as the second argument to the
877
+ `belongsTo` function.
878
+
879
+ Example
880
+
881
+ ```app/serializers/comment.js
882
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
883
+
884
+ export default class CommentSerializer extends JSONSerializer {
885
+ serializePolymorphicType(snapshot, json, relationship) {
886
+ let key = relationship.name;
887
+ let belongsTo = snapshot.belongsTo(key);
888
+
889
+ key = this.keyForAttribute ? this.keyForAttribute(key, 'serialize') : key;
890
+
891
+ if (!belongsTo) {
892
+ json[key + '_type'] = null;
893
+ } else {
894
+ json[key + '_type'] = belongsTo.modelName;
895
+ }
896
+ }
897
+ }
898
+ ```
899
+
900
+ @method serializePolymorphicType
901
+ @public
902
+ @param {Snapshot} snapshot
903
+ @param {Object} json
904
+ @param {Object} relationship
905
+ */
906
+ serializePolymorphicType(): void;
907
+ /**
908
+ `extractMeta` is used to deserialize any meta information in the
909
+ adapter payload. By default Ember Data expects meta information to
910
+ be located on the `meta` property of the payload object.
911
+
912
+ Example
913
+
914
+ ```app/serializers/post.js
915
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
916
+
917
+ export default class PostSerializer extends JSONSerializer {
918
+ extractMeta(store, typeClass, payload) {
919
+ if (payload && payload.hasOwnProperty('_pagination')) {
920
+ let meta = payload._pagination;
921
+ delete payload._pagination;
922
+ return meta;
923
+ }
924
+ }
925
+ }
926
+ ```
927
+
928
+ @method extractMeta
929
+ @public
930
+ @param {Store} store
931
+ @param {Model} modelClass
932
+ @param {Object} payload
933
+ */
934
+ extractMeta(store: Store, modelClass: Model, payload: Object): any;
935
+ /**
936
+ `extractErrors` is used to extract model errors when a call
937
+ to `Model#save` fails with an `InvalidError`. By default
938
+ Ember Data expects error information to be located on the `errors`
939
+ property of the payload object.
940
+
941
+ This serializer expects this `errors` object to be an Array similar
942
+ to the following, compliant with the https://jsonapi.org/format/#errors specification:
943
+
944
+ ```js
945
+ {
946
+ "errors": [
947
+ {
948
+ "detail": "This username is already taken!",
949
+ "source": {
950
+ "pointer": "data/attributes/username"
951
+ }
952
+ }, {
953
+ "detail": "Doesn't look like a valid email.",
954
+ "source": {
955
+ "pointer": "data/attributes/email"
956
+ }
957
+ }
958
+ ]
959
+ }
960
+ ```
961
+
962
+ The key `detail` provides a textual description of the problem.
963
+ Alternatively, the key `title` can be used for the same purpose.
964
+
965
+ The nested keys `source.pointer` detail which specific element
966
+ of the request data was invalid.
967
+
968
+ Note that JSON-API also allows for object-level errors to be placed
969
+ in an object with pointer `data`, signifying that the problem
970
+ cannot be traced to a specific attribute:
971
+
972
+ ```javascript
973
+ {
974
+ "errors": [
975
+ {
976
+ "detail": "Some generic non property error message",
977
+ "source": {
978
+ "pointer": "data"
979
+ }
980
+ }
981
+ ]
982
+ }
983
+ ```
984
+
985
+ When turn into a `Errors` object, you can read these errors
986
+ through the property `base`:
987
+
988
+ ```handlebars
989
+ {{#each @model.errors.base as |error|}}
990
+ <div class="error">
991
+ {{error.message}}
992
+ </div>
993
+ {{/each}}
994
+ ```
995
+
996
+ Example of alternative implementation, overriding the default
997
+ behavior to deal with a different format of errors:
998
+
999
+ ```app/serializers/post.js
1000
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
1001
+
1002
+ export default class PostSerializer extends JSONSerializer {
1003
+ extractErrors(store, typeClass, payload, id) {
1004
+ if (payload && typeof payload === 'object' && payload._problems) {
1005
+ payload = payload._problems;
1006
+ this.normalizeErrors(typeClass, payload);
1007
+ }
1008
+ return payload;
1009
+ }
1010
+ }
1011
+ ```
1012
+
1013
+ @method extractErrors
1014
+ @public
1015
+ @param {Store} store
1016
+ @param {Model} typeClass
1017
+ @param {Object} payload
1018
+ @param {(String|Number)} id
1019
+ @return {Object} json The deserialized errors
1020
+ */
1021
+ extractErrors(store: Store, typeClass: Model, payload: Object, id: (string | number)): Object;
1022
+ /**
1023
+ `keyForAttribute` can be used to define rules for how to convert an
1024
+ attribute name in your model to a key in your JSON.
1025
+
1026
+ Example
1027
+
1028
+ ```app/serializers/application.js
1029
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
1030
+ import { underscore } from '<app-name>/utils/string-utils';
1031
+
1032
+ export default class ApplicationSerializer extends JSONSerializer {
1033
+ keyForAttribute(attr, method) {
1034
+ return underscore(attr).toUpperCase();
1035
+ }
1036
+ }
1037
+ ```
1038
+
1039
+ @method keyForAttribute
1040
+ @public
1041
+ @param {String} key
1042
+ @param {String} method
1043
+ @return {String} normalized key
1044
+ */
1045
+ keyForAttribute(key: string, method: string): string;
1046
+ /**
1047
+ `keyForRelationship` can be used to define a custom key when
1048
+ serializing and deserializing relationship properties. By default
1049
+ `JSONSerializer` does not provide an implementation of this method.
1050
+
1051
+ Example
1052
+
1053
+ ```app/serializers/post.js
1054
+ import JSONSerializer from '@ember-data-mirror/serializer/json';
1055
+ import { underscore } from '<app-name>/utils/string-utils';
1056
+
1057
+ export default class PostSerializer extends JSONSerializer {
1058
+ keyForRelationship(key, relationship, method) {
1059
+ return `rel_${underscore(key)}`;
1060
+ }
1061
+ }
1062
+ ```
1063
+
1064
+ @method keyForRelationship
1065
+ @public
1066
+ @param {String} key
1067
+ @param {String} typeClass
1068
+ @param {String} method
1069
+ @return {String} normalized key
1070
+ */
1071
+ keyForRelationship(key: string, typeClass: string, method: string): string;
1072
+ /**
1073
+ `keyForLink` can be used to define a custom key when deserializing link
1074
+ properties.
1075
+
1076
+ @method keyForLink
1077
+ @public
1078
+ @param {String} key
1079
+ @param {String} kind `belongsTo` or `hasMany`
1080
+ @return {String} normalized key
1081
+ */
1082
+ keyForLink(key: string, kind: string): string;
1083
+ /**
1084
+ @method transformFor
1085
+ @private
1086
+ @param {String} attributeType
1087
+ @param {Boolean} skipAssertion
1088
+ @return {Transform} transform
1089
+ */
1090
+ transformFor(attributeType: string, skipAssertion: boolean): Transform;
1091
+ };
1092
+ import Serializer from '@ember-data-mirror/serializer';
1093
+ }
1094
+ //# sourceMappingURL=json.d.ts.map