@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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../src/json.
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../src/json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,IAAI,OAAO,EAAE,MAAM,oCAAoC,CAAC"}
|
|
@@ -1,570 +1,4 @@
|
|
|
1
1
|
declare module '@ember-data-mirror/serializer/rest' {
|
|
2
|
-
export { EmbeddedRecordsMixin } from
|
|
3
|
-
export default RESTSerializer;
|
|
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
|
-
Normally, applications will use the `RESTSerializer` by implementing
|
|
14
|
-
the `normalize` method.
|
|
15
|
-
|
|
16
|
-
This allows you to do whatever kind of munging you need and is
|
|
17
|
-
especially useful if your server is inconsistent and you need to
|
|
18
|
-
do munging differently for many different kinds of responses.
|
|
19
|
-
|
|
20
|
-
See the `normalize` documentation for more information.
|
|
21
|
-
|
|
22
|
-
## Across the Board Normalization
|
|
23
|
-
|
|
24
|
-
There are also a number of hooks that you might find useful to define
|
|
25
|
-
across-the-board rules for your payload. These rules will be useful
|
|
26
|
-
if your server is consistent, or if you're building an adapter for
|
|
27
|
-
an infrastructure service, like Firebase, and want to encode service
|
|
28
|
-
conventions.
|
|
29
|
-
|
|
30
|
-
For example, if all of your keys are underscored and all-caps, but
|
|
31
|
-
otherwise consistent with the names you use in your models, you
|
|
32
|
-
can implement across-the-board rules for how to convert an attribute
|
|
33
|
-
name in your model to a key in your JSON.
|
|
34
|
-
|
|
35
|
-
```app/serializers/application.js
|
|
36
|
-
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
37
|
-
import { underscore } from '<app-name>/utils/string-utils';
|
|
38
|
-
|
|
39
|
-
export default class ApplicationSerializer extends RESTSerializer {
|
|
40
|
-
keyForAttribute(attr, method) {
|
|
41
|
-
return underscore(attr).toUpperCase();
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
You can also implement `keyForRelationship`, which takes the name
|
|
47
|
-
of the relationship as the first parameter, the kind of
|
|
48
|
-
relationship (`hasMany` or `belongsTo`) as the second parameter, and
|
|
49
|
-
the method (`serialize` or `deserialize`) as the third parameter.
|
|
50
|
-
|
|
51
|
-
@class RESTSerializer
|
|
52
|
-
@main @ember-data-mirror/serializer/rest
|
|
53
|
-
@public
|
|
54
|
-
@extends JSONSerializer
|
|
55
|
-
*/
|
|
56
|
-
const RESTSerializer: Readonly<Readonly<typeof import("@ember-data-mirror/serializer").default> & (new (owner?: import("@ember/-internals/owner").default) => import(".").default) & {
|
|
57
|
-
primaryKey: string;
|
|
58
|
-
mergedProperties: Object;
|
|
59
|
-
applyTransforms(typeClass: Model, data: Object): Object;
|
|
60
|
-
normalizeResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
61
|
-
normalizeFindRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
62
|
-
normalizeQueryRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
63
|
-
normalizeFindAllResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
64
|
-
normalizeFindBelongsToResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
65
|
-
normalizeFindHasManyResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
66
|
-
normalizeFindManyResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
67
|
-
normalizeQueryResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
68
|
-
normalizeCreateRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
69
|
-
normalizeDeleteRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
70
|
-
normalizeUpdateRecordResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
71
|
-
normalizeSaveResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, ...args: any[]): Object;
|
|
72
|
-
normalizeSingleResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string): Object;
|
|
73
|
-
normalizeArrayResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string): Object;
|
|
74
|
-
_normalizeResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, isSingle: boolean): Object;
|
|
75
|
-
normalize(modelClass: any, resourceHash: any): Object;
|
|
76
|
-
extractId(modelClass: Object, resourceHash: Object): string;
|
|
77
|
-
extractAttributes(modelClass: Object, resourceHash: Object): Object;
|
|
78
|
-
extractRelationship(relationshipModelName: Object, relationshipHash: Object): Object;
|
|
79
|
-
extractPolymorphicRelationship(relationshipModelName: Object, relationshipHash: Object, relationshipOptions: Object): Object;
|
|
80
|
-
extractRelationships(modelClass: Object, resourceHash: Object): Object;
|
|
81
|
-
modelNameFromPayloadKey(key: string): string;
|
|
82
|
-
normalizeRelationships(typeClass: any, hash: any): void;
|
|
83
|
-
normalizeUsingDeclaredMapping(modelClass: any, hash: any): void;
|
|
84
|
-
_getMappedKey(key: string, modelClass: any): string;
|
|
85
|
-
_canSerialize(key: string): boolean;
|
|
86
|
-
_mustSerialize(key: string): boolean;
|
|
87
|
-
shouldSerializeHasMany(snapshot: Snapshot, key: string, relationship: RelationshipSchema): boolean;
|
|
88
|
-
serialize(snapshot: Snapshot, options: Object): Object;
|
|
89
|
-
serializeIntoHash(hash: Object, typeClass: Model, snapshot: Snapshot, options: Object): void;
|
|
90
|
-
serializeAttribute(snapshot: Snapshot, json: Object, key: string, attribute: Object): void;
|
|
91
|
-
serializeBelongsTo(snapshot: Snapshot, json: Object, relationship: Object): void;
|
|
92
|
-
serializeHasMany(snapshot: Snapshot, json: Object, relationship: Object): void;
|
|
93
|
-
serializePolymorphicType(): void;
|
|
94
|
-
extractMeta(store: Store, modelClass: Model, payload: Object): any;
|
|
95
|
-
extractErrors(store: Store, typeClass: Model, payload: Object, id: (string | number)): Object;
|
|
96
|
-
keyForAttribute(key: string, method: string): string;
|
|
97
|
-
keyForRelationship(key: string, typeClass: string, method: string): string;
|
|
98
|
-
keyForLink(key: string, kind: string): string;
|
|
99
|
-
transformFor(attributeType: string, skipAssertion: boolean): Transform;
|
|
100
|
-
}> & (new (owner?: import("@ember-data-mirror/serializer/@ember/-internals/owner").default) => import(".").default) & {
|
|
101
|
-
/**
|
|
102
|
-
`keyForPolymorphicType` can be used to define a custom key when
|
|
103
|
-
serializing and deserializing a polymorphic type. By default, the
|
|
104
|
-
returned key is `${key}Type`.
|
|
105
|
-
|
|
106
|
-
Example
|
|
107
|
-
|
|
108
|
-
```app/serializers/post.js
|
|
109
|
-
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
110
|
-
|
|
111
|
-
export default class ApplicationSerializer extends RESTSerializer {
|
|
112
|
-
keyForPolymorphicType(key, relationship) {
|
|
113
|
-
let relationshipKey = this.keyForRelationship(key);
|
|
114
|
-
|
|
115
|
-
return 'type-' + relationshipKey;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
@method keyForPolymorphicType
|
|
121
|
-
@public
|
|
122
|
-
@param {String} key
|
|
123
|
-
@param {String} typeClass
|
|
124
|
-
@param {String} method
|
|
125
|
-
@return {String} normalized key
|
|
126
|
-
*/
|
|
127
|
-
keyForPolymorphicType(key: string, typeClass: string, method: string): string;
|
|
128
|
-
/**
|
|
129
|
-
Normalizes a part of the JSON payload returned by
|
|
130
|
-
the server. You should override this method, munge the hash
|
|
131
|
-
and call super if you have generic normalization to do.
|
|
132
|
-
|
|
133
|
-
It takes the type of the record that is being normalized
|
|
134
|
-
(as a Model class), the property where the hash was
|
|
135
|
-
originally found, and the hash to normalize.
|
|
136
|
-
|
|
137
|
-
For example, if you have a payload that looks like this:
|
|
138
|
-
|
|
139
|
-
```js
|
|
140
|
-
{
|
|
141
|
-
"post": {
|
|
142
|
-
"id": 1,
|
|
143
|
-
"title": "Rails is omakase",
|
|
144
|
-
"comments": [ 1, 2 ]
|
|
145
|
-
},
|
|
146
|
-
"comments": [{
|
|
147
|
-
"id": 1,
|
|
148
|
-
"body": "FIRST"
|
|
149
|
-
}, {
|
|
150
|
-
"id": 2,
|
|
151
|
-
"body": "Rails is unagi"
|
|
152
|
-
}]
|
|
153
|
-
}
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
The `normalize` method will be called three times:
|
|
157
|
-
|
|
158
|
-
* With `App.Post`, `"posts"` and `{ id: 1, title: "Rails is omakase", ... }`
|
|
159
|
-
* With `App.Comment`, `"comments"` and `{ id: 1, body: "FIRST" }`
|
|
160
|
-
* With `App.Comment`, `"comments"` and `{ id: 2, body: "Rails is unagi" }`
|
|
161
|
-
|
|
162
|
-
You can use this method, for example, to normalize underscored keys to camelized
|
|
163
|
-
or other general-purpose normalizations. You will only need to implement
|
|
164
|
-
`normalize` and manipulate the payload as desired.
|
|
165
|
-
|
|
166
|
-
For example, if the `IDs` under `"comments"` are provided as `_id` instead of
|
|
167
|
-
`id`, you can specify how to normalize just the comments:
|
|
168
|
-
|
|
169
|
-
```app/serializers/post.js
|
|
170
|
-
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
171
|
-
|
|
172
|
-
export default class ApplicationSerializer extends RESTSerializer {
|
|
173
|
-
normalize(model, hash, prop) {
|
|
174
|
-
if (prop === 'comments') {
|
|
175
|
-
hash.id = hash._id;
|
|
176
|
-
delete hash._id;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
return super.normalize(...arguments);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
On each call to the `normalize` method, the third parameter (`prop`) is always
|
|
185
|
-
one of the keys that were in the original payload or in the result of another
|
|
186
|
-
normalization as `normalizeResponse`.
|
|
187
|
-
|
|
188
|
-
@method normalize
|
|
189
|
-
@public
|
|
190
|
-
@param {Model} modelClass
|
|
191
|
-
@param {Object} resourceHash
|
|
192
|
-
@param {String} prop
|
|
193
|
-
@return {Object}
|
|
194
|
-
*/
|
|
195
|
-
/**
|
|
196
|
-
Normalizes an array of resource payloads and returns a JSON-API Document
|
|
197
|
-
with primary data and, if any, included data as `{ data, included }`.
|
|
198
|
-
|
|
199
|
-
@method _normalizeArray
|
|
200
|
-
@param {Store} store
|
|
201
|
-
@param {String} modelName
|
|
202
|
-
@param {Object} arrayHash
|
|
203
|
-
@param {String} prop
|
|
204
|
-
@return {Object}
|
|
205
|
-
@private
|
|
206
|
-
*/
|
|
207
|
-
_normalizeArray(store: Store, modelName: string, arrayHash: Object, prop: string): Object;
|
|
208
|
-
_normalizePolymorphicRecord(store: any, hash: any, prop: any, primaryModelClass: any, primarySerializer: any): any;
|
|
209
|
-
/**
|
|
210
|
-
@method _normalizeResponse
|
|
211
|
-
@param {Store} store
|
|
212
|
-
@param {Model} primaryModelClass
|
|
213
|
-
@param {Object} payload
|
|
214
|
-
@param {String|Number} id
|
|
215
|
-
@param {String} requestType
|
|
216
|
-
@param {Boolean} isSingle
|
|
217
|
-
@return {Object} JSON-API Document
|
|
218
|
-
@private
|
|
219
|
-
*/
|
|
220
|
-
_normalizeResponse(store: Store, primaryModelClass: Model, payload: Object, id: string | number, requestType: string, isSingle: boolean): Object;
|
|
221
|
-
isPrimaryType(store: any, modelName: any, primaryModelClass: any): boolean;
|
|
222
|
-
/**
|
|
223
|
-
This method allows you to push a payload containing top-level
|
|
224
|
-
collections of records organized per type.
|
|
225
|
-
|
|
226
|
-
```js
|
|
227
|
-
{
|
|
228
|
-
"posts": [{
|
|
229
|
-
"id": "1",
|
|
230
|
-
"title": "Rails is omakase",
|
|
231
|
-
"author", "1",
|
|
232
|
-
"comments": [ "1" ]
|
|
233
|
-
}],
|
|
234
|
-
"comments": [{
|
|
235
|
-
"id": "1",
|
|
236
|
-
"body": "FIRST"
|
|
237
|
-
}],
|
|
238
|
-
"users": [{
|
|
239
|
-
"id": "1",
|
|
240
|
-
"name": "@d2h"
|
|
241
|
-
}]
|
|
242
|
-
}
|
|
243
|
-
```
|
|
244
|
-
|
|
245
|
-
It will first normalize the payload, so you can use this to push
|
|
246
|
-
in data streaming in from your server structured the same way
|
|
247
|
-
that fetches and saves are structured.
|
|
248
|
-
|
|
249
|
-
@method pushPayload
|
|
250
|
-
@public
|
|
251
|
-
@param {Store} store
|
|
252
|
-
@param {Object} payload
|
|
253
|
-
*/
|
|
254
|
-
pushPayload(store: Store, payload: Object): void;
|
|
255
|
-
/**
|
|
256
|
-
This method is used to convert each JSON root key in the payload
|
|
257
|
-
into a modelName that it can use to look up the appropriate model for
|
|
258
|
-
that part of the payload.
|
|
259
|
-
|
|
260
|
-
For example, your server may send a model name that does not correspond with
|
|
261
|
-
the name of the model in your app. Let's take a look at an example model,
|
|
262
|
-
and an example payload:
|
|
263
|
-
|
|
264
|
-
```app/models/post.js
|
|
265
|
-
import Model from '@ember-data-mirror/model';
|
|
266
|
-
|
|
267
|
-
export default class Post extends Model {}
|
|
268
|
-
```
|
|
269
|
-
|
|
270
|
-
```javascript
|
|
271
|
-
{
|
|
272
|
-
"blog/post": {
|
|
273
|
-
"id": "1
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
Ember Data is going to normalize the payload's root key for the modelName. As a result,
|
|
279
|
-
it will try to look up the "blog/post" model. Since we don't have a model called "blog/post"
|
|
280
|
-
(or a file called app/models/blog/post.js in ember-cli), Ember Data will throw an error
|
|
281
|
-
because it cannot find the "blog/post" model.
|
|
282
|
-
|
|
283
|
-
Since we want to remove this namespace, we can define a serializer for the application that will
|
|
284
|
-
remove "blog/" from the payload key whenver it's encountered by Ember Data:
|
|
285
|
-
|
|
286
|
-
```app/serializers/application.js
|
|
287
|
-
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
288
|
-
|
|
289
|
-
export default class ApplicationSerializer extends RESTSerializer {
|
|
290
|
-
modelNameFromPayloadKey(payloadKey) {
|
|
291
|
-
if (payloadKey === 'blog/post') {
|
|
292
|
-
return super.modelNameFromPayloadKey(payloadKey.replace('blog/', ''));
|
|
293
|
-
} else {
|
|
294
|
-
return super.modelNameFromPayloadKey(payloadKey);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
After refreshing, Ember Data will appropriately look up the "post" model.
|
|
301
|
-
|
|
302
|
-
By default the modelName for a model is its
|
|
303
|
-
name in dasherized form. This means that a payload key like "blogPost" would be
|
|
304
|
-
normalized to "blog-post" when Ember Data looks up the model. Usually, Ember Data
|
|
305
|
-
can use the correct inflection to do this for you. Most of the time, you won't
|
|
306
|
-
need to override `modelNameFromPayloadKey` for this purpose.
|
|
307
|
-
|
|
308
|
-
@method modelNameFromPayloadKey
|
|
309
|
-
@public
|
|
310
|
-
@param {String} key
|
|
311
|
-
@return {String} the model's modelName
|
|
312
|
-
*/
|
|
313
|
-
modelNameFromPayloadKey(key: string): string;
|
|
314
|
-
/**
|
|
315
|
-
Called when a record is saved in order to convert the
|
|
316
|
-
record into JSON.
|
|
317
|
-
|
|
318
|
-
By default, it creates a JSON object with a key for
|
|
319
|
-
each attribute and belongsTo relationship.
|
|
320
|
-
|
|
321
|
-
For example, consider this model:
|
|
322
|
-
|
|
323
|
-
```app/models/comment.js
|
|
324
|
-
import Model, { attr, belongsTo } from '@ember-data-mirror/model';
|
|
325
|
-
|
|
326
|
-
export default class Comment extends Model {
|
|
327
|
-
@attr title
|
|
328
|
-
@attr body
|
|
329
|
-
|
|
330
|
-
@belongsTo('user') author
|
|
331
|
-
}
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
The default serialization would create a JSON object like:
|
|
335
|
-
|
|
336
|
-
```js
|
|
337
|
-
{
|
|
338
|
-
"title": "Rails is unagi",
|
|
339
|
-
"body": "Rails? Omakase? O_O",
|
|
340
|
-
"author": 12
|
|
341
|
-
}
|
|
342
|
-
```
|
|
343
|
-
|
|
344
|
-
By default, attributes are passed through as-is, unless
|
|
345
|
-
you specified an attribute type (`attr('date')`). If
|
|
346
|
-
you specify a transform, the JavaScript value will be
|
|
347
|
-
serialized when inserted into the JSON hash.
|
|
348
|
-
|
|
349
|
-
By default, belongs-to relationships are converted into
|
|
350
|
-
IDs when inserted into the JSON hash.
|
|
351
|
-
|
|
352
|
-
## IDs
|
|
353
|
-
|
|
354
|
-
`serialize` takes an options hash with a single option:
|
|
355
|
-
`includeId`. If this option is `true`, `serialize` will,
|
|
356
|
-
by default include the ID in the JSON object it builds.
|
|
357
|
-
|
|
358
|
-
The adapter passes in `includeId: true` when serializing
|
|
359
|
-
a record for `createRecord`, but not for `updateRecord`.
|
|
360
|
-
|
|
361
|
-
## Customization
|
|
362
|
-
|
|
363
|
-
Your server may expect a different JSON format than the
|
|
364
|
-
built-in serialization format.
|
|
365
|
-
|
|
366
|
-
In that case, you can implement `serialize` yourself and
|
|
367
|
-
return a JSON hash of your choosing.
|
|
368
|
-
|
|
369
|
-
```app/serializers/post.js
|
|
370
|
-
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
371
|
-
|
|
372
|
-
export default class ApplicationSerializer extends RESTSerializer {
|
|
373
|
-
serialize(snapshot, options) {
|
|
374
|
-
let json = {
|
|
375
|
-
POST_TTL: snapshot.attr('title'),
|
|
376
|
-
POST_BDY: snapshot.attr('body'),
|
|
377
|
-
POST_CMS: snapshot.hasMany('comments', { ids: true })
|
|
378
|
-
};
|
|
379
|
-
|
|
380
|
-
if (options.includeId) {
|
|
381
|
-
json.POST_ID_ = snapshot.id;
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
return json;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
```
|
|
388
|
-
|
|
389
|
-
## Customizing an App-Wide Serializer
|
|
390
|
-
|
|
391
|
-
If you want to define a serializer for your entire
|
|
392
|
-
application, you'll probably want to use `eachAttribute`
|
|
393
|
-
and `eachRelationship` on the record.
|
|
394
|
-
|
|
395
|
-
```app/serializers/application.js
|
|
396
|
-
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
397
|
-
import { pluralize } from '<app-name>/utils/string-utils';
|
|
398
|
-
|
|
399
|
-
export default class ApplicationSerializer extends RESTSerializer {
|
|
400
|
-
serialize(snapshot, options) {
|
|
401
|
-
let json = {};
|
|
402
|
-
|
|
403
|
-
snapshot.eachAttribute(function(name) {
|
|
404
|
-
json[serverAttributeName(name)] = snapshot.attr(name);
|
|
405
|
-
});
|
|
406
|
-
|
|
407
|
-
snapshot.eachRelationship(function(name, relationship) {
|
|
408
|
-
if (relationship.kind === 'hasMany') {
|
|
409
|
-
json[serverHasManyName(name)] = snapshot.hasMany(name, { ids: true });
|
|
410
|
-
}
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
if (options.includeId) {
|
|
414
|
-
json.ID_ = snapshot.id;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
return json;
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
function serverAttributeName(attribute) {
|
|
422
|
-
return attribute.underscore().toUpperCase();
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
function serverHasManyName(name) {
|
|
426
|
-
return serverAttributeName(singularize(name)) + "_IDS";
|
|
427
|
-
}
|
|
428
|
-
```
|
|
429
|
-
|
|
430
|
-
This serializer will generate JSON that looks like this:
|
|
431
|
-
|
|
432
|
-
```js
|
|
433
|
-
{
|
|
434
|
-
"TITLE": "Rails is omakase",
|
|
435
|
-
"BODY": "Yep. Omakase.",
|
|
436
|
-
"COMMENT_IDS": [ 1, 2, 3 ]
|
|
437
|
-
}
|
|
438
|
-
```
|
|
439
|
-
|
|
440
|
-
## Tweaking the Default JSON
|
|
441
|
-
|
|
442
|
-
If you just want to do some small tweaks on the default JSON,
|
|
443
|
-
you can call super first and make the tweaks on the returned
|
|
444
|
-
JSON.
|
|
445
|
-
|
|
446
|
-
```app/serializers/post.js
|
|
447
|
-
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
448
|
-
|
|
449
|
-
export default class ApplicationSerializer extends RESTSerializer {
|
|
450
|
-
serialize(snapshot, options) {
|
|
451
|
-
let json = super.serialize(snapshot, options);
|
|
452
|
-
|
|
453
|
-
json.subject = json.title;
|
|
454
|
-
delete json.title;
|
|
455
|
-
|
|
456
|
-
return json;
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
```
|
|
460
|
-
|
|
461
|
-
@method serialize
|
|
462
|
-
@public
|
|
463
|
-
@param {Snapshot} snapshot
|
|
464
|
-
@param {Object} options
|
|
465
|
-
@return {Object} json
|
|
466
|
-
*/
|
|
467
|
-
serialize(snapshot: Snapshot, options: Object, ...args: any[]): Object;
|
|
468
|
-
/**
|
|
469
|
-
You can use this method to customize the root keys serialized into the JSON.
|
|
470
|
-
The hash property should be modified by reference (possibly using something like _.extend)
|
|
471
|
-
By default the REST Serializer sends the modelName of a model, which is a camelized
|
|
472
|
-
version of the name.
|
|
473
|
-
|
|
474
|
-
For example, your server may expect underscored root objects.
|
|
475
|
-
|
|
476
|
-
```app/serializers/application.js
|
|
477
|
-
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
478
|
-
import { underscore } from '<app-name>/utils/string-utils';
|
|
479
|
-
|
|
480
|
-
export default class ApplicationSerializer extends RESTSerializer {
|
|
481
|
-
serializeIntoHash(data, type, record, options) {
|
|
482
|
-
let root = underscore(type.modelName);
|
|
483
|
-
data[root] = this.serialize(record, options);
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
```
|
|
487
|
-
|
|
488
|
-
@method serializeIntoHash
|
|
489
|
-
@public
|
|
490
|
-
@param {Object} hash
|
|
491
|
-
@param {Model} typeClass
|
|
492
|
-
@param {Snapshot} snapshot
|
|
493
|
-
@param {Object} options
|
|
494
|
-
*/
|
|
495
|
-
serializeIntoHash(hash: Object, typeClass: Model, snapshot: Snapshot, options: Object): void;
|
|
496
|
-
/**
|
|
497
|
-
You can use `payloadKeyFromModelName` to override the root key for an outgoing
|
|
498
|
-
request. By default, the RESTSerializer returns a camelized version of the
|
|
499
|
-
model's name.
|
|
500
|
-
|
|
501
|
-
For a model called TacoParty, its `modelName` would be the string `taco-party`. The RESTSerializer
|
|
502
|
-
will send it to the server with `tacoParty` as the root key in the JSON payload:
|
|
503
|
-
|
|
504
|
-
```js
|
|
505
|
-
{
|
|
506
|
-
"tacoParty": {
|
|
507
|
-
"id": "1",
|
|
508
|
-
"location": "Matthew Beale's House"
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
```
|
|
512
|
-
|
|
513
|
-
For example, your server may expect dasherized root objects:
|
|
514
|
-
|
|
515
|
-
```app/serializers/application.js
|
|
516
|
-
import RESTSerializer from '@ember-data-mirror/serializer/rest';
|
|
517
|
-
import { dasherize } from '<app-name>/utils/string-utils';
|
|
518
|
-
|
|
519
|
-
export default class ApplicationSerializer extends RESTSerializer {
|
|
520
|
-
payloadKeyFromModelName(modelName) {
|
|
521
|
-
return dasherize(modelName);
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
```
|
|
525
|
-
|
|
526
|
-
Given a `TacoParty` model, calling `save` on it would produce an outgoing
|
|
527
|
-
request like:
|
|
528
|
-
|
|
529
|
-
```js
|
|
530
|
-
{
|
|
531
|
-
"taco-party": {
|
|
532
|
-
"id": "1",
|
|
533
|
-
"location": "Matthew Beale's House"
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
```
|
|
537
|
-
|
|
538
|
-
@method payloadKeyFromModelName
|
|
539
|
-
@public
|
|
540
|
-
@param {String} modelName
|
|
541
|
-
@return {String}
|
|
542
|
-
*/
|
|
543
|
-
payloadKeyFromModelName(modelName: string): string;
|
|
544
|
-
/**
|
|
545
|
-
You can use this method to customize how polymorphic objects are serialized.
|
|
546
|
-
By default the REST Serializer creates the key by appending `Type` to
|
|
547
|
-
the attribute and value from the model's camelcased model name.
|
|
548
|
-
|
|
549
|
-
@method serializePolymorphicType
|
|
550
|
-
@public
|
|
551
|
-
@param {Snapshot} snapshot
|
|
552
|
-
@param {Object} json
|
|
553
|
-
@param {Object} relationship
|
|
554
|
-
*/
|
|
555
|
-
serializePolymorphicType(snapshot: Snapshot, json: Object, relationship: Object): void;
|
|
556
|
-
/**
|
|
557
|
-
You can use this method to customize how a polymorphic relationship should
|
|
558
|
-
be extracted.
|
|
559
|
-
|
|
560
|
-
@method extractPolymorphicRelationship
|
|
561
|
-
@public
|
|
562
|
-
@param {Object} relationshipType
|
|
563
|
-
@param {Object} relationshipHash
|
|
564
|
-
@param {Object} relationshipOptions
|
|
565
|
-
@return {Object}
|
|
566
|
-
*/
|
|
567
|
-
extractPolymorphicRelationship(relationshipType: Object, relationshipHash: Object, relationshipOptions: Object, ...args: any[]): Object;
|
|
568
|
-
};
|
|
2
|
+
export { RESTSerializer as default, EmbeddedRecordsMixin } from '@warp-drive-mirror/legacy/serializer/rest';
|
|
569
3
|
}
|
|
570
4
|
//# sourceMappingURL=rest.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rest.d.ts","sourceRoot":"","sources":["../src/rest.
|
|
1
|
+
{"version":3,"file":"rest.d.ts","sourceRoot":"","sources":["../src/rest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,IAAI,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC"}
|
|
@@ -1,11 +1,4 @@
|
|
|
1
1
|
declare module '@ember-data-mirror/serializer/transform' {
|
|
2
|
-
|
|
3
|
-
@module @ember-data-mirror/serializer
|
|
4
|
-
*/
|
|
5
|
-
export { Transform as default } from '@ember-data-mirror/serializer/-private/transforms/transform';
|
|
6
|
-
export { BooleanTransform } from '@ember-data-mirror/serializer/-private/transforms/boolean';
|
|
7
|
-
export { DateTransform } from '@ember-data-mirror/serializer/-private/transforms/date';
|
|
8
|
-
export { NumberTransform } from '@ember-data-mirror/serializer/-private/transforms/number';
|
|
9
|
-
export { StringTransform } from '@ember-data-mirror/serializer/-private/transforms/string';
|
|
2
|
+
export { Transform as default, BooleanTransform, StringTransform, NumberTransform, DateTransform, } from '@warp-drive-mirror/legacy/serializer/transform';
|
|
10
3
|
}
|
|
11
4
|
//# sourceMappingURL=transform.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../src/transform.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../src/transform.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,IAAI,OAAO,EACpB,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,aAAa,GACd,MAAM,yCAAyC,CAAC"}
|