@ember-data-mirror/serializer 5.6.0-alpha.5 → 5.6.0-beta.1

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 (40) hide show
  1. package/addon-main.cjs +1 -1
  2. package/dist/index.js +1 -301
  3. package/dist/json-api.js +1 -514
  4. package/dist/json.js +1 -6
  5. package/dist/rest.js +1 -1245
  6. package/dist/transform.js +1 -313
  7. package/package.json +7 -20
  8. package/unstable-preview-types/index.d.ts +81 -238
  9. package/unstable-preview-types/json-api.d.ts +2 -497
  10. package/unstable-preview-types/json.d.ts +2 -1048
  11. package/unstable-preview-types/rest.d.ts +2 -555
  12. package/unstable-preview-types/transform.d.ts +3 -7
  13. package/dist/index.js.map +0 -1
  14. package/dist/json-CYP2BhR9.js +0 -1349
  15. package/dist/json-CYP2BhR9.js.map +0 -1
  16. package/dist/json-api.js.map +0 -1
  17. package/dist/json.js.map +0 -1
  18. package/dist/rest.js.map +0 -1
  19. package/dist/transform.js.map +0 -1
  20. package/unstable-preview-types/-private/embedded-records-mixin.d.ts +0 -99
  21. package/unstable-preview-types/-private/embedded-records-mixin.d.ts.map +0 -1
  22. package/unstable-preview-types/-private/transforms/boolean.d.ts +0 -49
  23. package/unstable-preview-types/-private/transforms/boolean.d.ts.map +0 -1
  24. package/unstable-preview-types/-private/transforms/boolean.type-test.d.ts +0 -4
  25. package/unstable-preview-types/-private/transforms/boolean.type-test.d.ts.map +0 -1
  26. package/unstable-preview-types/-private/transforms/date.d.ts +0 -30
  27. package/unstable-preview-types/-private/transforms/date.d.ts.map +0 -1
  28. package/unstable-preview-types/-private/transforms/number.d.ts +0 -31
  29. package/unstable-preview-types/-private/transforms/number.d.ts.map +0 -1
  30. package/unstable-preview-types/-private/transforms/string.d.ts +0 -31
  31. package/unstable-preview-types/-private/transforms/string.d.ts.map +0 -1
  32. package/unstable-preview-types/-private/transforms/transform.d.ts +0 -121
  33. package/unstable-preview-types/-private/transforms/transform.d.ts.map +0 -1
  34. package/unstable-preview-types/-private/utils.d.ts +0 -6
  35. package/unstable-preview-types/-private/utils.d.ts.map +0 -1
  36. package/unstable-preview-types/index.d.ts.map +0 -1
  37. package/unstable-preview-types/json-api.d.ts.map +0 -1
  38. package/unstable-preview-types/json.d.ts.map +0 -1
  39. package/unstable-preview-types/rest.d.ts.map +0 -1
  40. package/unstable-preview-types/transform.d.ts.map +0 -1
@@ -1,499 +1,4 @@
1
1
  declare module '@ember-data-mirror/serializer/json-api' {
2
- export default JSONAPISerializer;
3
- /**
4
- * <blockquote style="margin: 1em; padding: .1em 1em .1em 1em; border-left: solid 1em #E34C32; background: #e0e0e0;">
5
- <p>
6
- ⚠️ <strong>This is LEGACY documentation</strong> for a feature that is no longer encouraged to be used.
7
- If starting a new app or thinking of implementing a new adapter, consider writing a
8
- <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>
9
- </p>
10
- </blockquote>
2
+ export { JSONAPISerializer as default } from "@warp-drive-mirror/legacy/serializer/json-api";
11
3
 
12
- In EmberData a Serializer is used to serialize and deserialize
13
- records when they are transferred in and out of an external source.
14
- This process involves normalizing property names, transforming
15
- attribute values and serializing relationships.
16
-
17
- `JSONAPISerializer` supports the http://jsonapi.org/ spec and is the
18
- serializer recommended by Ember Data.
19
-
20
- This serializer normalizes a JSON API payload that looks like:
21
-
22
- ```js [app/models/player.js]
23
- import Model, { attr, belongsTo } from '@ember-data-mirror/model';
24
-
25
- export default class Player extends Model {
26
- @attr('string') name;
27
- @attr('string') skill;
28
- @attr('number') gamesPlayed;
29
- @belongsTo('club') club;
30
- }
31
- ```
32
-
33
- ```js [app/models/club.js]
34
- import Model, { attr, hasMany } from '@ember-data-mirror/model';
35
-
36
- export default class Club extends Model {
37
- @attr('string') name;
38
- @attr('string') location;
39
- @hasMany('player') players;
40
- }
41
- ```
42
-
43
- ```js
44
- {
45
- "data": [
46
- {
47
- "attributes": {
48
- "name": "Benfica",
49
- "location": "Portugal"
50
- },
51
- "id": "1",
52
- "relationships": {
53
- "players": {
54
- "data": [
55
- {
56
- "id": "3",
57
- "type": "players"
58
- }
59
- ]
60
- }
61
- },
62
- "type": "clubs"
63
- }
64
- ],
65
- "included": [
66
- {
67
- "attributes": {
68
- "name": "Eusebio Silva Ferreira",
69
- "skill": "Rocket shot",
70
- "games-played": 431
71
- },
72
- "id": "3",
73
- "relationships": {
74
- "club": {
75
- "data": {
76
- "id": "1",
77
- "type": "clubs"
78
- }
79
- }
80
- },
81
- "type": "players"
82
- }
83
- ]
84
- }
85
- ```
86
-
87
- to the format that the Ember Data store expects.
88
-
89
- ### Customizing meta
90
-
91
- Since a JSON API Document can have meta defined in multiple locations you can
92
- use the specific serializer hooks if you need to customize the meta.
93
-
94
- One scenario would be to camelCase the meta keys of your payload. The example
95
- below shows how this could be done using `normalizeArrayResponse` and
96
- `extractRelationship`.
97
-
98
- ```js [app/serializers/application.js]
99
- import JSONAPISerializer from '@ember-data-mirror/serializer/json-api';
100
-
101
- export default class ApplicationSerializer extends JSONAPISerializer {
102
- normalizeArrayResponse(store, primaryModelClass, payload, id, requestType) {
103
- let normalizedDocument = super.normalizeArrayResponse(...arguments);
104
-
105
- // Customize document meta
106
- normalizedDocument.meta = camelCaseKeys(normalizedDocument.meta);
107
-
108
- return normalizedDocument;
109
- }
110
-
111
- extractRelationship(relationshipHash) {
112
- let normalizedRelationship = super.extractRelationship(...arguments);
113
-
114
- // Customize relationship meta
115
- normalizedRelationship.meta = camelCaseKeys(normalizedRelationship.meta);
116
-
117
- return normalizedRelationship;
118
- }
119
- }
120
- ```
121
-
122
- @since 1.13.0
123
- @class JSONAPISerializer
124
- @public
125
- */
126
- const JSONAPISerializer: Readonly<Readonly<typeof import("@ember-data-mirror/serializer").default> & (new (owner?: import("@ember/-internals/owner").default) => import(".").default) & {
127
- primaryKey: string;
128
- mergedProperties: Object;
129
- applyTransforms(typeClass: Model, data: Object): Object;
130
- normalizeResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
131
- normalizeFindRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
132
- normalizeQueryRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
133
- normalizeFindAllResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
134
- normalizeFindBelongsToResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
135
- normalizeFindHasManyResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
136
- normalizeFindManyResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
137
- normalizeQueryResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
138
- normalizeCreateRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
139
- normalizeDeleteRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
140
- normalizeUpdateRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
141
- normalizeSaveResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
142
- normalizeSingleResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string): Object;
143
- normalizeArrayResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string): Object;
144
- _normalizeResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, isSingle: boolean): Object;
145
- normalize(modelClass: any, resourceHash: any): Object;
146
- extractId(modelClass: Object, resourceHash: Object): string;
147
- extractAttributes(modelClass: Object, resourceHash: Object): Object;
148
- extractRelationship(relationshipModelName: Object, relationshipHash: Object): Object;
149
- extractPolymorphicRelationship(relationshipModelName: Object, relationshipHash: Object, relationshipOptions: Object): Object;
150
- extractRelationships(modelClass: Object, resourceHash: Object): Object;
151
- modelNameFromPayloadKey(key: string): string;
152
- normalizeRelationships(typeClass: any, hash: any): void;
153
- normalizeUsingDeclaredMapping(modelClass: any, hash: any): void;
154
- _getMappedKey(key: string, modelClass: any): string;
155
- _canSerialize(key: string): boolean;
156
- _mustSerialize(key: string): boolean;
157
- shouldSerializeHasMany(snapshot: Snapshot, key: string, relationship: RelationshipSchema): boolean;
158
- serialize(snapshot: Snapshot, options: Object): Object;
159
- serializeIntoHash(hash: Object, typeClass: Model, snapshot: Snapshot, options: Object): void;
160
- serializeAttribute(snapshot: Snapshot, json: Object, key: string, attribute: Object): void;
161
- serializeBelongsTo(snapshot: Snapshot, json: Object, relationship: Object): void;
162
- serializeHasMany(snapshot: Snapshot, json: Object, relationship: Object): void;
163
- serializePolymorphicType(): void;
164
- extractMeta(store: Store, modelClass: Model, payload: Object): any;
165
- extractErrors(store: Store, typeClass: Model, payload: Object, id: (string | number)): Object;
166
- keyForAttribute(key: string, method: string): string;
167
- keyForRelationship(key: string, typeClass: string, method: string): string;
168
- keyForLink(key: string, kind: string): string;
169
- transformFor(attributeType: string, skipAssertion: boolean): Transform;
170
- }> & (new (owner?: import("@ember-data-mirror/serializer/@ember/-internals/owner").default) => import(".").default) & {
171
- /**
172
- @param {Object} documentHash
173
- @return {Object}
174
- @private
175
- */
176
- _normalizeDocumentHelper(documentHash: Object): Object;
177
- /**
178
- @param {Object} relationshipDataHash
179
- @return {Object}
180
- @private
181
- */
182
- _normalizeRelationshipDataHelper(relationshipDataHash: Object): Object;
183
- /**
184
- @param {Object} resourceHash
185
- @return {Object}
186
- @private
187
- */
188
- _normalizeResourceHelper(resourceHash: Object): Object;
189
- /**
190
- Normalize some data and push it into the store.
191
-
192
- @public
193
- @param {Store} store
194
- @param {Object} payload
195
- */
196
- pushPayload(store: Store, payload: Object): void;
197
- /**
198
- @param {Store} store
199
- @param {Model} primaryModelClass
200
- @param {Object} payload
201
- @param {String|Number} id
202
- @param {String} requestType
203
- @param {Boolean} isSingle
204
- @return {Object} JSON-API Document
205
- @private
206
- */
207
- _normalizeResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, isSingle: boolean): Object;
208
- normalizeQueryRecordResponse(...args: any[]): any;
209
- extractAttributes(modelClass: any, resourceHash: any): {};
210
- /**
211
- Returns a relationship formatted as a JSON-API "relationship object".
212
-
213
- http://jsonapi.org/format/#document-resource-object-relationships
214
-
215
- @public
216
- @param {Object} relationshipHash
217
- @return {Object}
218
- */
219
- extractRelationship(relationshipHash: Object): Object;
220
- /**
221
- Returns the resource's relationships formatted as a JSON-API "relationships object".
222
-
223
- http://jsonapi.org/format/#document-resource-object-relationships
224
-
225
- @public
226
- @param {Object} modelClass
227
- @param {Object} resourceHash
228
- @return {Object}
229
- */
230
- extractRelationships(modelClass: Object, resourceHash: Object): Object;
231
- /**
232
- @param {Model} modelClass
233
- @param {Object} resourceHash
234
- @return {String}
235
- @private
236
- */
237
- _extractType(modelClass: Model, resourceHash: Object): string;
238
- /**
239
- Dasherizes and singularizes the model name in the payload to match
240
- the format Ember Data uses internally for the model name.
241
-
242
- For example the key `posts` would be converted to `post` and the
243
- key `studentAssesments` would be converted to `student-assesment`.
244
-
245
- @public
246
- @param {String} key
247
- @return {String} the model's modelName
248
- */
249
- modelNameFromPayloadKey(key: string): string;
250
- /**
251
- Converts the model name to a pluralized version of the model name.
252
-
253
- For example `post` would be converted to `posts` and
254
- `student-assesment` would be converted to `student-assesments`.
255
-
256
- @public
257
- @param {String} modelName
258
- @return {String}
259
- */
260
- payloadKeyFromModelName(modelName: string): string;
261
- normalize(modelClass: any, resourceHash: any): {
262
- data: {
263
- id: any;
264
- type: string;
265
- attributes: {};
266
- relationships: Object;
267
- };
268
- };
269
- /**
270
- `keyForAttribute` can be used to define rules for how to convert an
271
- attribute name in your model to a key in your JSON.
272
- By default `JSONAPISerializer` follows the format used on the examples of
273
- http://jsonapi.org/format and uses dashes as the word separator in the JSON
274
- attribute keys.
275
-
276
- This behaviour can be easily customized by extending this method.
277
-
278
- Example
279
-
280
- ```js [app/serializers/application.js]
281
- import JSONAPISerializer from '@ember-data-mirror/serializer/json-api';
282
- import { dasherize } from '<app-name>/utils/string-utils';
283
-
284
- export default class ApplicationSerializer extends JSONAPISerializer {
285
- keyForAttribute(attr, method) {
286
- return dasherize(attr).toUpperCase();
287
- }
288
- }
289
- ```
290
-
291
- @public
292
- @param {String} key
293
- @param {String} method
294
- @return {String} normalized key
295
- */
296
- keyForAttribute(key: string, method: string): string;
297
- /**
298
- `keyForRelationship` can be used to define a custom key when
299
- serializing and deserializing relationship properties.
300
- By default `JSONAPISerializer` follows the format used on the examples of
301
- http://jsonapi.org/format and uses dashes as word separators in
302
- relationship properties.
303
-
304
- This behaviour can be easily customized by extending this method.
305
-
306
- Example
307
-
308
- ```js [app/serializers/post.js]
309
- import JSONAPISerializer from '@ember-data-mirror/serializer/json-api';
310
- import { underscore } from '<app-name>/utils/string-utils';
311
-
312
- export default class ApplicationSerializer extends JSONAPISerializer {
313
- keyForRelationship(key, relationship, method) {
314
- return underscore(key);
315
- }
316
- }
317
- ```
318
- @public
319
- @param {String} key
320
- @param {String} typeClass
321
- @param {String} method
322
- @return {String} normalized key
323
- */
324
- keyForRelationship(key: string, typeClass: string, method: string): string;
325
- /**
326
- Called when a record is saved in order to convert the
327
- record into JSON.
328
-
329
- For example, consider this model:
330
-
331
- ```js [app/models/comment.js]
332
- import Model, { attr, belongsTo } from '@ember-data-mirror/model';
333
-
334
- export default class CommentModel extends Model {
335
- @attr title;
336
- @attr body;
337
-
338
- @belongsTo('user', { async: false, inverse: null })
339
- author;
340
- }
341
- ```
342
-
343
- The default serialization would create a JSON-API resource object like:
344
-
345
- ```javascript
346
- {
347
- "data": {
348
- "type": "comments",
349
- "attributes": {
350
- "title": "Rails is unagi",
351
- "body": "Rails? Omakase? O_O",
352
- },
353
- "relationships": {
354
- "author": {
355
- "data": {
356
- "id": "12",
357
- "type": "users"
358
- }
359
- }
360
- }
361
- }
362
- }
363
- ```
364
-
365
- By default, attributes are passed through as-is, unless
366
- you specified an attribute type (`attr('date')`). If
367
- you specify a transform, the JavaScript value will be
368
- serialized when inserted into the attributes hash.
369
-
370
- Belongs-to relationships are converted into JSON-API
371
- resource identifier objects.
372
-
373
- ## IDs
374
-
375
- `serialize` takes an options hash with a single option:
376
- `includeId`. If this option is `true`, `serialize` will,
377
- by default include the ID in the JSON object it builds.
378
-
379
- The JSONAPIAdapter passes in `includeId: true` when serializing a record
380
- for `createRecord` or `updateRecord`.
381
-
382
- ## Customization
383
-
384
- Your server may expect data in a different format than the
385
- built-in serialization format.
386
-
387
- In that case, you can implement `serialize` yourself and
388
- return data formatted to match your API's expectations, or override
389
- the invoked adapter method and do the serialization in the adapter directly
390
- by using the provided snapshot.
391
-
392
- If your API's format differs greatly from the JSON:API spec, you should
393
- consider authoring your own adapter and serializer instead of extending
394
- this class.
395
-
396
- ```js [app/serializers/post.js]
397
- import JSONAPISerializer from '@ember-data-mirror/serializer/json-api';
398
-
399
- export default class PostSerializer extends JSONAPISerializer {
400
- serialize(snapshot, options) {
401
- let json = {
402
- POST_TTL: snapshot.attr('title'),
403
- POST_BDY: snapshot.attr('body'),
404
- POST_CMS: snapshot.hasMany('comments', { ids: true })
405
- };
406
-
407
- if (options.includeId) {
408
- json.POST_ID_ = snapshot.id;
409
- }
410
-
411
- return json;
412
- }
413
- }
414
- ```
415
-
416
- ## Customizing an App-Wide Serializer
417
-
418
- If you want to define a serializer for your entire
419
- application, you'll probably want to use `eachAttribute`
420
- and `eachRelationship` on the record.
421
-
422
- ```js [app/serializers/application.js]
423
- import JSONAPISerializer from '@ember-data-mirror/serializer/json-api';
424
- import { underscore, singularize } from '<app-name>/utils/string-utils';
425
-
426
- export default class ApplicationSerializer extends JSONAPISerializer {
427
- serialize(snapshot, options) {
428
- let json = {};
429
-
430
- snapshot.eachAttribute((name) => {
431
- json[serverAttributeName(name)] = snapshot.attr(name);
432
- });
433
-
434
- snapshot.eachRelationship((name, relationship) => {
435
- if (relationship.kind === 'hasMany') {
436
- json[serverHasManyName(name)] = snapshot.hasMany(name, { ids: true });
437
- }
438
- });
439
-
440
- if (options.includeId) {
441
- json.ID_ = snapshot.id;
442
- }
443
-
444
- return json;
445
- }
446
- }
447
-
448
- function serverAttributeName(attribute) {
449
- return underscore(attribute).toUpperCase();
450
- }
451
-
452
- function serverHasManyName(name) {
453
- return serverAttributeName(singularize(name)) + '_IDS';
454
- }
455
- ```
456
-
457
- This serializer will generate JSON that looks like this:
458
-
459
- ```javascript
460
- {
461
- "TITLE": "Rails is omakase",
462
- "BODY": "Yep. Omakase.",
463
- "COMMENT_IDS": [ "1", "2", "3" ]
464
- }
465
- ```
466
-
467
- ## Tweaking the Default Formatting
468
-
469
- If you just want to do some small tweaks on the default JSON:API formatted response,
470
- you can call `super.serialize` first and make the tweaks
471
- on the returned object.
472
-
473
- ```js [app/serializers/post.js]
474
- import JSONAPISerializer from '@ember-data-mirror/serializer/json-api';
475
-
476
- export default class PostSerializer extends JSONAPISerializer {
477
- serialize(snapshot, options) {
478
- let json = super.serialize(...arguments);
479
-
480
- json.data.attributes.subject = json.data.attributes.title;
481
- delete json.data.attributes.title;
482
-
483
- return json;
484
- }
485
- }
486
- ```
487
-
488
- @public
489
- @param {Snapshot} snapshot
490
- @param {Object} options
491
- @return {Object} json
492
- */
493
- serialize(snapshot: Snapshot, options: Object, ...args: any[]): Object;
494
- serializeAttribute(snapshot: any, json: any, key: any, attribute: any): void;
495
- serializeBelongsTo(snapshot: any, json: any, relationship: any): void;
496
- serializeHasMany(snapshot: any, json: any, relationship: any): void;
497
- };
498
- }
499
- //# sourceMappingURL=json-api.d.ts.map
4
+ }