@ember-data-mirror/serializer 5.6.0-alpha.1 → 5.6.0-alpha.12
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/addon-main.cjs +1 -1
- package/dist/index.js +2 -306
- package/dist/index.js.map +1 -1
- package/dist/json-api.js +2 -531
- package/dist/json-api.js.map +1 -1
- package/dist/json.js +2 -6
- package/dist/json.js.map +1 -1
- package/dist/rest.js +2 -1274
- package/dist/rest.js.map +1 -1
- package/dist/transform.js +2 -336
- package/dist/transform.js.map +1 -1
- package/package.json +6 -23
- package/unstable-preview-types/index.d.ts +5 -167
- package/unstable-preview-types/index.d.ts.map +1 -1
- package/unstable-preview-types/json-api.d.ts +1 -511
- package/unstable-preview-types/json-api.d.ts.map +1 -1
- package/unstable-preview-types/json.d.ts +1 -1090
- package/unstable-preview-types/json.d.ts.map +1 -1
- package/unstable-preview-types/rest.d.ts +1 -567
- package/unstable-preview-types/rest.d.ts.map +1 -1
- package/unstable-preview-types/transform.d.ts +1 -8
- package/unstable-preview-types/transform.d.ts.map +1 -1
- package/dist/json-CVTR4xWv.js +0 -1396
- package/dist/json-CVTR4xWv.js.map +0 -1
- package/unstable-preview-types/-private/embedded-records-mixin.d.ts +0 -102
- package/unstable-preview-types/-private/embedded-records-mixin.d.ts.map +0 -1
- package/unstable-preview-types/-private/transforms/boolean.d.ts +0 -52
- package/unstable-preview-types/-private/transforms/boolean.d.ts.map +0 -1
- package/unstable-preview-types/-private/transforms/boolean.type-test.d.ts +0 -4
- package/unstable-preview-types/-private/transforms/boolean.type-test.d.ts.map +0 -1
- package/unstable-preview-types/-private/transforms/date.d.ts +0 -33
- package/unstable-preview-types/-private/transforms/date.d.ts.map +0 -1
- package/unstable-preview-types/-private/transforms/number.d.ts +0 -34
- package/unstable-preview-types/-private/transforms/number.d.ts.map +0 -1
- package/unstable-preview-types/-private/transforms/string.d.ts +0 -34
- package/unstable-preview-types/-private/transforms/string.d.ts.map +0 -1
- package/unstable-preview-types/-private/transforms/transform.d.ts +0 -126
- package/unstable-preview-types/-private/transforms/transform.d.ts.map +0 -1
- package/unstable-preview-types/-private/utils.d.ts +0 -6
- package/unstable-preview-types/-private/utils.d.ts.map +0 -1
|
@@ -1,514 +1,4 @@
|
|
|
1
1
|
declare module '@ember-data-mirror/serializer/json-api' {
|
|
2
|
-
export default
|
|
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>
|
|
11
|
-
|
|
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
|
-
```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
|
-
```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
|
-
```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
|
-
@main @ember-data-mirror/serializer/json-api
|
|
123
|
-
@since 1.13.0
|
|
124
|
-
@class JSONAPISerializer
|
|
125
|
-
@public
|
|
126
|
-
@extends JSONSerializer
|
|
127
|
-
*/
|
|
128
|
-
const JSONAPISerializer: Readonly<Readonly<typeof import("@ember-data-mirror/serializer").default> & (new (owner?: import("@ember/-internals/owner").default) => import(".").default) & {
|
|
129
|
-
primaryKey: string;
|
|
130
|
-
mergedProperties: Object;
|
|
131
|
-
applyTransforms(typeClass: Model, data: Object): Object;
|
|
132
|
-
normalizeResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
133
|
-
normalizeFindRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
134
|
-
normalizeQueryRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
135
|
-
normalizeFindAllResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
136
|
-
normalizeFindBelongsToResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
137
|
-
normalizeFindHasManyResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
138
|
-
normalizeFindManyResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
139
|
-
normalizeQueryResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
140
|
-
normalizeCreateRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
141
|
-
normalizeDeleteRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
142
|
-
normalizeUpdateRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
143
|
-
normalizeSaveResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
144
|
-
normalizeSingleResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string): Object;
|
|
145
|
-
normalizeArrayResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string): Object;
|
|
146
|
-
_normalizeResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, isSingle: boolean): Object;
|
|
147
|
-
normalize(modelClass: any, resourceHash: any): Object;
|
|
148
|
-
extractId(modelClass: Object, resourceHash: Object): string;
|
|
149
|
-
extractAttributes(modelClass: Object, resourceHash: Object): Object;
|
|
150
|
-
extractRelationship(relationshipModelName: Object, relationshipHash: Object): Object;
|
|
151
|
-
extractPolymorphicRelationship(relationshipModelName: Object, relationshipHash: Object, relationshipOptions: Object): Object;
|
|
152
|
-
extractRelationships(modelClass: Object, resourceHash: Object): Object;
|
|
153
|
-
modelNameFromPayloadKey(key: string): string;
|
|
154
|
-
normalizeRelationships(typeClass: any, hash: any): void;
|
|
155
|
-
normalizeUsingDeclaredMapping(modelClass: any, hash: any): void;
|
|
156
|
-
_getMappedKey(key: string, modelClass: any): string;
|
|
157
|
-
_canSerialize(key: string): boolean;
|
|
158
|
-
_mustSerialize(key: string): boolean;
|
|
159
|
-
shouldSerializeHasMany(snapshot: Snapshot, key: string, relationship: RelationshipSchema): boolean;
|
|
160
|
-
serialize(snapshot: Snapshot, options: Object): Object;
|
|
161
|
-
serializeIntoHash(hash: Object, typeClass: Model, snapshot: Snapshot, options: Object): void;
|
|
162
|
-
serializeAttribute(snapshot: Snapshot, json: Object, key: string, attribute: Object): void;
|
|
163
|
-
serializeBelongsTo(snapshot: Snapshot, json: Object, relationship: Object): void;
|
|
164
|
-
serializeHasMany(snapshot: Snapshot, json: Object, relationship: Object): void;
|
|
165
|
-
serializePolymorphicType(): void;
|
|
166
|
-
extractMeta(store: Store, modelClass: Model, payload: Object): any;
|
|
167
|
-
extractErrors(store: Store, typeClass: Model, payload: Object, id: (string | number)): Object;
|
|
168
|
-
keyForAttribute(key: string, method: string): string;
|
|
169
|
-
keyForRelationship(key: string, typeClass: string, method: string): string;
|
|
170
|
-
keyForLink(key: string, kind: string): string;
|
|
171
|
-
transformFor(attributeType: string, skipAssertion: boolean): Transform;
|
|
172
|
-
}> & (new (owner?: import("@ember-data-mirror/serializer/@ember/-internals/owner").default) => import(".").default) & {
|
|
173
|
-
/**
|
|
174
|
-
@method _normalizeDocumentHelper
|
|
175
|
-
@param {Object} documentHash
|
|
176
|
-
@return {Object}
|
|
177
|
-
@private
|
|
178
|
-
*/
|
|
179
|
-
_normalizeDocumentHelper(documentHash: Object): Object;
|
|
180
|
-
/**
|
|
181
|
-
@method _normalizeRelationshipDataHelper
|
|
182
|
-
@param {Object} relationshipDataHash
|
|
183
|
-
@return {Object}
|
|
184
|
-
@private
|
|
185
|
-
*/
|
|
186
|
-
_normalizeRelationshipDataHelper(relationshipDataHash: Object): Object;
|
|
187
|
-
/**
|
|
188
|
-
@method _normalizeResourceHelper
|
|
189
|
-
@param {Object} resourceHash
|
|
190
|
-
@return {Object}
|
|
191
|
-
@private
|
|
192
|
-
*/
|
|
193
|
-
_normalizeResourceHelper(resourceHash: Object): Object;
|
|
194
|
-
/**
|
|
195
|
-
Normalize some data and push it into the store.
|
|
196
|
-
|
|
197
|
-
@method pushPayload
|
|
198
|
-
@public
|
|
199
|
-
@param {Store} store
|
|
200
|
-
@param {Object} payload
|
|
201
|
-
*/
|
|
202
|
-
pushPayload(store: Store, payload: Object): void;
|
|
203
|
-
/**
|
|
204
|
-
@method _normalizeResponse
|
|
205
|
-
@param {Store} store
|
|
206
|
-
@param {Model} primaryModelClass
|
|
207
|
-
@param {Object} payload
|
|
208
|
-
@param {String|Number} id
|
|
209
|
-
@param {String} requestType
|
|
210
|
-
@param {Boolean} isSingle
|
|
211
|
-
@return {Object} JSON-API Document
|
|
212
|
-
@private
|
|
213
|
-
*/
|
|
214
|
-
_normalizeResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, isSingle: boolean): Object;
|
|
215
|
-
normalizeQueryRecordResponse(...args: any[]): any;
|
|
216
|
-
extractAttributes(modelClass: any, resourceHash: any): {};
|
|
217
|
-
/**
|
|
218
|
-
Returns a relationship formatted as a JSON-API "relationship object".
|
|
219
|
-
|
|
220
|
-
http://jsonapi.org/format/#document-resource-object-relationships
|
|
221
|
-
|
|
222
|
-
@method extractRelationship
|
|
223
|
-
@public
|
|
224
|
-
@param {Object} relationshipHash
|
|
225
|
-
@return {Object}
|
|
226
|
-
*/
|
|
227
|
-
extractRelationship(relationshipHash: Object): Object;
|
|
228
|
-
/**
|
|
229
|
-
Returns the resource's relationships formatted as a JSON-API "relationships object".
|
|
230
|
-
|
|
231
|
-
http://jsonapi.org/format/#document-resource-object-relationships
|
|
232
|
-
|
|
233
|
-
@method extractRelationships
|
|
234
|
-
@public
|
|
235
|
-
@param {Object} modelClass
|
|
236
|
-
@param {Object} resourceHash
|
|
237
|
-
@return {Object}
|
|
238
|
-
*/
|
|
239
|
-
extractRelationships(modelClass: Object, resourceHash: Object): Object;
|
|
240
|
-
/**
|
|
241
|
-
@method _extractType
|
|
242
|
-
@param {Model} modelClass
|
|
243
|
-
@param {Object} resourceHash
|
|
244
|
-
@return {String}
|
|
245
|
-
@private
|
|
246
|
-
*/
|
|
247
|
-
_extractType(modelClass: Model, resourceHash: Object): string;
|
|
248
|
-
/**
|
|
249
|
-
Dasherizes and singularizes the model name in the payload to match
|
|
250
|
-
the format Ember Data uses internally for the model name.
|
|
251
|
-
|
|
252
|
-
For example the key `posts` would be converted to `post` and the
|
|
253
|
-
key `studentAssesments` would be converted to `student-assesment`.
|
|
254
|
-
|
|
255
|
-
@method modelNameFromPayloadKey
|
|
256
|
-
@public
|
|
257
|
-
@param {String} key
|
|
258
|
-
@return {String} the model's modelName
|
|
259
|
-
*/
|
|
260
|
-
modelNameFromPayloadKey(key: string): string;
|
|
261
|
-
/**
|
|
262
|
-
Converts the model name to a pluralized version of the model name.
|
|
263
|
-
|
|
264
|
-
For example `post` would be converted to `posts` and
|
|
265
|
-
`student-assesment` would be converted to `student-assesments`.
|
|
266
|
-
|
|
267
|
-
@method payloadKeyFromModelName
|
|
268
|
-
@public
|
|
269
|
-
@param {String} modelName
|
|
270
|
-
@return {String}
|
|
271
|
-
*/
|
|
272
|
-
payloadKeyFromModelName(modelName: string): string;
|
|
273
|
-
normalize(modelClass: any, resourceHash: any): {
|
|
274
|
-
data: {
|
|
275
|
-
id: any;
|
|
276
|
-
type: string;
|
|
277
|
-
attributes: {};
|
|
278
|
-
relationships: Object;
|
|
279
|
-
};
|
|
280
|
-
};
|
|
281
|
-
/**
|
|
282
|
-
`keyForAttribute` can be used to define rules for how to convert an
|
|
283
|
-
attribute name in your model to a key in your JSON.
|
|
284
|
-
By default `JSONAPISerializer` follows the format used on the examples of
|
|
285
|
-
http://jsonapi.org/format and uses dashes as the word separator in the JSON
|
|
286
|
-
attribute keys.
|
|
287
|
-
|
|
288
|
-
This behaviour can be easily customized by extending this method.
|
|
289
|
-
|
|
290
|
-
Example
|
|
291
|
-
|
|
292
|
-
```app/serializers/application.js
|
|
293
|
-
import JSONAPISerializer from '@ember-data-mirror/serializer/json-api';
|
|
294
|
-
import { dasherize } from '<app-name>/utils/string-utils';
|
|
295
|
-
|
|
296
|
-
export default class ApplicationSerializer extends JSONAPISerializer {
|
|
297
|
-
keyForAttribute(attr, method) {
|
|
298
|
-
return dasherize(attr).toUpperCase();
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
```
|
|
302
|
-
|
|
303
|
-
@method keyForAttribute
|
|
304
|
-
@public
|
|
305
|
-
@param {String} key
|
|
306
|
-
@param {String} method
|
|
307
|
-
@return {String} normalized key
|
|
308
|
-
*/
|
|
309
|
-
keyForAttribute(key: string, method: string): string;
|
|
310
|
-
/**
|
|
311
|
-
`keyForRelationship` can be used to define a custom key when
|
|
312
|
-
serializing and deserializing relationship properties.
|
|
313
|
-
By default `JSONAPISerializer` follows the format used on the examples of
|
|
314
|
-
http://jsonapi.org/format and uses dashes as word separators in
|
|
315
|
-
relationship properties.
|
|
316
|
-
|
|
317
|
-
This behaviour can be easily customized by extending this method.
|
|
318
|
-
|
|
319
|
-
Example
|
|
320
|
-
|
|
321
|
-
```app/serializers/post.js
|
|
322
|
-
import JSONAPISerializer from '@ember-data-mirror/serializer/json-api';
|
|
323
|
-
import { underscore } from '<app-name>/utils/string-utils';
|
|
324
|
-
|
|
325
|
-
export default class ApplicationSerializer extends JSONAPISerializer {
|
|
326
|
-
keyForRelationship(key, relationship, method) {
|
|
327
|
-
return underscore(key);
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
```
|
|
331
|
-
@method keyForRelationship
|
|
332
|
-
@public
|
|
333
|
-
@param {String} key
|
|
334
|
-
@param {String} typeClass
|
|
335
|
-
@param {String} method
|
|
336
|
-
@return {String} normalized key
|
|
337
|
-
*/
|
|
338
|
-
keyForRelationship(key: string, typeClass: string, method: string): string;
|
|
339
|
-
/**
|
|
340
|
-
Called when a record is saved in order to convert the
|
|
341
|
-
record into JSON.
|
|
342
|
-
|
|
343
|
-
For example, consider this model:
|
|
344
|
-
|
|
345
|
-
```app/models/comment.js
|
|
346
|
-
import Model, { attr, belongsTo } from '@ember-data-mirror/model';
|
|
347
|
-
|
|
348
|
-
export default class CommentModel extends Model {
|
|
349
|
-
@attr title;
|
|
350
|
-
@attr body;
|
|
351
|
-
|
|
352
|
-
@belongsTo('user', { async: false, inverse: null })
|
|
353
|
-
author;
|
|
354
|
-
}
|
|
355
|
-
```
|
|
356
|
-
|
|
357
|
-
The default serialization would create a JSON-API resource object like:
|
|
358
|
-
|
|
359
|
-
```javascript
|
|
360
|
-
{
|
|
361
|
-
"data": {
|
|
362
|
-
"type": "comments",
|
|
363
|
-
"attributes": {
|
|
364
|
-
"title": "Rails is unagi",
|
|
365
|
-
"body": "Rails? Omakase? O_O",
|
|
366
|
-
},
|
|
367
|
-
"relationships": {
|
|
368
|
-
"author": {
|
|
369
|
-
"data": {
|
|
370
|
-
"id": "12",
|
|
371
|
-
"type": "users"
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
```
|
|
378
|
-
|
|
379
|
-
By default, attributes are passed through as-is, unless
|
|
380
|
-
you specified an attribute type (`attr('date')`). If
|
|
381
|
-
you specify a transform, the JavaScript value will be
|
|
382
|
-
serialized when inserted into the attributes hash.
|
|
383
|
-
|
|
384
|
-
Belongs-to relationships are converted into JSON-API
|
|
385
|
-
resource identifier objects.
|
|
386
|
-
|
|
387
|
-
## IDs
|
|
388
|
-
|
|
389
|
-
`serialize` takes an options hash with a single option:
|
|
390
|
-
`includeId`. If this option is `true`, `serialize` will,
|
|
391
|
-
by default include the ID in the JSON object it builds.
|
|
392
|
-
|
|
393
|
-
The JSONAPIAdapter passes in `includeId: true` when serializing a record
|
|
394
|
-
for `createRecord` or `updateRecord`.
|
|
395
|
-
|
|
396
|
-
## Customization
|
|
397
|
-
|
|
398
|
-
Your server may expect data in a different format than the
|
|
399
|
-
built-in serialization format.
|
|
400
|
-
|
|
401
|
-
In that case, you can implement `serialize` yourself and
|
|
402
|
-
return data formatted to match your API's expectations, or override
|
|
403
|
-
the invoked adapter method and do the serialization in the adapter directly
|
|
404
|
-
by using the provided snapshot.
|
|
405
|
-
|
|
406
|
-
If your API's format differs greatly from the JSON:API spec, you should
|
|
407
|
-
consider authoring your own adapter and serializer instead of extending
|
|
408
|
-
this class.
|
|
409
|
-
|
|
410
|
-
```app/serializers/post.js
|
|
411
|
-
import JSONAPISerializer from '@ember-data-mirror/serializer/json-api';
|
|
412
|
-
|
|
413
|
-
export default class PostSerializer extends JSONAPISerializer {
|
|
414
|
-
serialize(snapshot, options) {
|
|
415
|
-
let json = {
|
|
416
|
-
POST_TTL: snapshot.attr('title'),
|
|
417
|
-
POST_BDY: snapshot.attr('body'),
|
|
418
|
-
POST_CMS: snapshot.hasMany('comments', { ids: true })
|
|
419
|
-
};
|
|
420
|
-
|
|
421
|
-
if (options.includeId) {
|
|
422
|
-
json.POST_ID_ = snapshot.id;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
return json;
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
```
|
|
429
|
-
|
|
430
|
-
## Customizing an App-Wide Serializer
|
|
431
|
-
|
|
432
|
-
If you want to define a serializer for your entire
|
|
433
|
-
application, you'll probably want to use `eachAttribute`
|
|
434
|
-
and `eachRelationship` on the record.
|
|
435
|
-
|
|
436
|
-
```app/serializers/application.js
|
|
437
|
-
import JSONAPISerializer from '@ember-data-mirror/serializer/json-api';
|
|
438
|
-
import { underscore, singularize } from '<app-name>/utils/string-utils';
|
|
439
|
-
|
|
440
|
-
export default class ApplicationSerializer extends JSONAPISerializer {
|
|
441
|
-
serialize(snapshot, options) {
|
|
442
|
-
let json = {};
|
|
443
|
-
|
|
444
|
-
snapshot.eachAttribute((name) => {
|
|
445
|
-
json[serverAttributeName(name)] = snapshot.attr(name);
|
|
446
|
-
});
|
|
447
|
-
|
|
448
|
-
snapshot.eachRelationship((name, relationship) => {
|
|
449
|
-
if (relationship.kind === 'hasMany') {
|
|
450
|
-
json[serverHasManyName(name)] = snapshot.hasMany(name, { ids: true });
|
|
451
|
-
}
|
|
452
|
-
});
|
|
453
|
-
|
|
454
|
-
if (options.includeId) {
|
|
455
|
-
json.ID_ = snapshot.id;
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
return json;
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
function serverAttributeName(attribute) {
|
|
463
|
-
return underscore(attribute).toUpperCase();
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
function serverHasManyName(name) {
|
|
467
|
-
return serverAttributeName(singularize(name)) + '_IDS';
|
|
468
|
-
}
|
|
469
|
-
```
|
|
470
|
-
|
|
471
|
-
This serializer will generate JSON that looks like this:
|
|
472
|
-
|
|
473
|
-
```javascript
|
|
474
|
-
{
|
|
475
|
-
"TITLE": "Rails is omakase",
|
|
476
|
-
"BODY": "Yep. Omakase.",
|
|
477
|
-
"COMMENT_IDS": [ "1", "2", "3" ]
|
|
478
|
-
}
|
|
479
|
-
```
|
|
480
|
-
|
|
481
|
-
## Tweaking the Default Formatting
|
|
482
|
-
|
|
483
|
-
If you just want to do some small tweaks on the default JSON:API formatted response,
|
|
484
|
-
you can call `super.serialize` first and make the tweaks
|
|
485
|
-
on the returned object.
|
|
486
|
-
|
|
487
|
-
```app/serializers/post.js
|
|
488
|
-
import JSONAPISerializer from '@ember-data-mirror/serializer/json-api';
|
|
489
|
-
|
|
490
|
-
export default class PostSerializer extends JSONAPISerializer {
|
|
491
|
-
serialize(snapshot, options) {
|
|
492
|
-
let json = super.serialize(...arguments);
|
|
493
|
-
|
|
494
|
-
json.data.attributes.subject = json.data.attributes.title;
|
|
495
|
-
delete json.data.attributes.title;
|
|
496
|
-
|
|
497
|
-
return json;
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
```
|
|
501
|
-
|
|
502
|
-
@method serialize
|
|
503
|
-
@public
|
|
504
|
-
@param {Snapshot} snapshot
|
|
505
|
-
@param {Object} options
|
|
506
|
-
@return {Object} json
|
|
507
|
-
*/
|
|
508
|
-
serialize(snapshot: Snapshot, options: Object, ...args: any[]): Object;
|
|
509
|
-
serializeAttribute(snapshot: any, json: any, key: any, attribute: any): void;
|
|
510
|
-
serializeBelongsTo(snapshot: any, json: any, relationship: any): void;
|
|
511
|
-
serializeHasMany(snapshot: any, json: any, relationship: any): void;
|
|
512
|
-
};
|
|
2
|
+
export { JSONAPISerializer as default } from '@warp-drive-mirror/legacy/serializer/json-api';
|
|
513
3
|
}
|
|
514
4
|
//# sourceMappingURL=json-api.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json-api.d.ts","sourceRoot":"","sources":["../src/json-api.
|
|
1
|
+
{"version":3,"file":"json-api.d.ts","sourceRoot":"","sources":["../src/json-api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,IAAI,OAAO,EAAE,MAAM,wCAAwC,CAAC"}
|