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

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