@ember-data/serializer 5.4.0-alpha.32 → 5.4.0-alpha.33

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