@ember-data/serializer 5.4.0-beta.1 → 5.4.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/README.md +11 -1
  2. package/addon-main.cjs +5 -0
  3. package/blueprints/serializer/index.js +71 -5
  4. package/blueprints/serializer-test/index.js +13 -7
  5. package/blueprints/serializer-test/qunit-files/__root__/__path__/__test__.js +8 -9
  6. package/blueprints/transform/index.js +14 -4
  7. package/blueprints/transform-test/index.js +13 -7
  8. package/blueprints/transform-test/qunit-files/__root__/__path__/__test__.js +3 -4
  9. package/dist/index.js +373 -0
  10. package/dist/index.js.map +1 -0
  11. package/{addon/json.js → dist/json-BwMH6O_R.js} +83 -63
  12. package/dist/json-BwMH6O_R.js.map +1 -0
  13. package/{addon → dist}/json-api.js +86 -186
  14. package/dist/json-api.js.map +1 -0
  15. package/dist/json.js +6 -0
  16. package/dist/json.js.map +1 -0
  17. package/dist/rest.js +1270 -0
  18. package/dist/rest.js.map +1 -0
  19. package/{addon/string-22572f80.js → dist/transform.js} +144 -20
  20. package/dist/transform.js.map +1 -0
  21. package/package.json +69 -40
  22. package/unstable-preview-types/-private/embedded-records-mixin.d.ts +103 -0
  23. package/unstable-preview-types/-private/embedded-records-mixin.d.ts.map +1 -0
  24. package/unstable-preview-types/-private/transforms/boolean.d.ts +52 -0
  25. package/unstable-preview-types/-private/transforms/boolean.d.ts.map +1 -0
  26. package/unstable-preview-types/-private/transforms/date.d.ts +33 -0
  27. package/unstable-preview-types/-private/transforms/date.d.ts.map +1 -0
  28. package/unstable-preview-types/-private/transforms/number.d.ts +34 -0
  29. package/unstable-preview-types/-private/transforms/number.d.ts.map +1 -0
  30. package/unstable-preview-types/-private/transforms/string.d.ts +34 -0
  31. package/unstable-preview-types/-private/transforms/string.d.ts.map +1 -0
  32. package/unstable-preview-types/-private/transforms/transform.d.ts +127 -0
  33. package/unstable-preview-types/-private/transforms/transform.d.ts.map +1 -0
  34. package/unstable-preview-types/-private/utils.d.ts +6 -0
  35. package/unstable-preview-types/-private/utils.d.ts.map +1 -0
  36. package/unstable-preview-types/index.d.ts +277 -0
  37. package/unstable-preview-types/index.d.ts.map +1 -0
  38. package/unstable-preview-types/json-api.d.ts +515 -0
  39. package/unstable-preview-types/json-api.d.ts.map +1 -0
  40. package/unstable-preview-types/json.d.ts +1094 -0
  41. package/unstable-preview-types/json.d.ts.map +1 -0
  42. package/unstable-preview-types/rest.d.ts +571 -0
  43. package/unstable-preview-types/rest.d.ts.map +1 -0
  44. package/unstable-preview-types/transform.d.ts +11 -0
  45. package/unstable-preview-types/transform.d.ts.map +1 -0
  46. package/addon/-private.js +0 -4
  47. package/addon/-private.js.map +0 -1
  48. package/addon/embedded-records-mixin-d75385ff.js +0 -575
  49. package/addon/embedded-records-mixin-d75385ff.js.map +0 -1
  50. package/addon/index.js +0 -180
  51. package/addon/index.js.map +0 -1
  52. package/addon/json-api.js.map +0 -1
  53. package/addon/json.js.map +0 -1
  54. package/addon/rest.js +0 -680
  55. package/addon/rest.js.map +0 -1
  56. package/addon/string-22572f80.js.map +0 -1
  57. package/addon/transform.js +0 -2
  58. package/addon/transform.js.map +0 -1
  59. package/addon/utils-075c5b79.js +0 -12
  60. package/addon/utils-075c5b79.js.map +0 -1
  61. package/addon-main.js +0 -93
@@ -0,0 +1,277 @@
1
+ /// <reference path="./json-api.d.ts" />
2
+ /// <reference path="./json.d.ts" />
3
+ /// <reference path="./rest.d.ts" />
4
+ /// <reference path="./transform.d.ts" />
5
+ /// <reference path="./-private/utils.d.ts" />
6
+ /// <reference path="./-private/embedded-records-mixin.d.ts" />
7
+ /// <reference path="./-private/transforms/string.d.ts" />
8
+ /// <reference path="./-private/transforms/boolean.d.ts" />
9
+ /// <reference path="./-private/transforms/date.d.ts" />
10
+ /// <reference path="./-private/transforms/number.d.ts" />
11
+ /// <reference path="./-private/transforms/transform.d.ts" />
12
+ declare module '@ember-data/serializer' {
13
+ /**
14
+ ## Overview
15
+
16
+ <blockquote style="margin: 1em; padding: .1em 1em .1em 1em; border-left: solid 1em #E34C32; background: #e0e0e0;">
17
+ <p>
18
+ ⚠️ <strong>This is LEGACY documentation</strong> for a feature that is no longer encouraged to be used.
19
+ If starting a new app or thinking of implementing a new serializer, consider writing a
20
+ <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>
21
+ </p>
22
+ </blockquote>
23
+
24
+ In order to properly manage and present your data, EmberData
25
+ needs to understand the structure of data it receives.
26
+
27
+ `Serializers` convert data between the server's API format and
28
+ the format EmberData understands.
29
+
30
+ Data received from an API response is **normalized** into
31
+ [JSON:API](https://jsonapi.org/) (the format used internally
32
+ by EmberData), while data sent to an API is **serialized**
33
+ into the format the API expects.
34
+
35
+ ### Implementing a Serializer
36
+
37
+ There are only two required serializer methods, one for
38
+ normalizing data from the server API format into JSON:API, and
39
+ another for serializing records via `Snapshots` into the expected
40
+ server API format.
41
+
42
+ To implement a serializer, export a class that conforms to the structure
43
+ described by [<Interface> Serializer](/ember-data/release/classes/%3CInterface%3E%20Serializer)
44
+ from the `app/serializers/` directory. An example is below.
45
+
46
+ ```ts
47
+ import EmberObject from '@ember/object';
48
+
49
+ export default class ApplicationSerializer extends EmberObject {
50
+ normalizeResponse(store, schema, rawPayload) {
51
+ return rawPayload;
52
+ }
53
+
54
+ serialize(snapshot, options) {
55
+ const serializedResource = {
56
+ id: snapshot.id,
57
+ type: snapshot.modelName,
58
+ attributes: snapshot.attributes()
59
+ };
60
+
61
+ return serializedResource;
62
+ }
63
+ }
64
+ ```
65
+
66
+
67
+ ### Serializer Resolution
68
+
69
+ `store.serializerFor(name)` will lookup serializers defined in
70
+ `app/serializers/` and return an instance. If no serializer is found, an
71
+ error will be thrown.
72
+
73
+ `serializerFor` first attempts to find a serializer with an exact match on `name`,
74
+ then falls back to checking for the presence of a serializer named `application`.
75
+
76
+ ```ts
77
+ store.serializerFor('author');
78
+
79
+ // lookup paths (in order) =>
80
+ // app/serializers/author.js
81
+ // app/serializers/application.js
82
+ ```
83
+
84
+ Most requests in EmberData are made with respect to a particular `type` (or `modelName`)
85
+ (e.g., "get me the full collection of **books**" or "get me the **employee** whose id is 37"). We
86
+ refer to this as the **primary** resource `type`.
87
+
88
+ Typically `serializerFor` will be used to find a serializer with a name matching that of the primary
89
+ resource `type` for the request, falling back to the `application` serializer for those types that
90
+ do not have a defined serializer. This is often described as a `per-model` or `per-type` strategy
91
+ for defining serializers. However, because APIs rarely format payloads per-type but rather
92
+ per-API-version, this may not be a desired strategy.
93
+
94
+ It is recommended that applications define only a single `application` adapter and serializer
95
+ where possible.
96
+
97
+ If you have multiple API formats and the per-type strategy is not viable, one strategy is to
98
+ write an `application` adapter and serializer that make use of `options` to specify the desired
99
+ format when making a request.
100
+
101
+ ### Using a Serializer
102
+
103
+ Any serializer in `app/serializers/` can be looked up by `name` using `store.serializerFor(name)`.
104
+
105
+ ### Default Serializers
106
+
107
+ For applications whose APIs are *very close to* or *exactly* the **REST** format or **JSON:API**
108
+ format the `@ember-data/serializer` package contains implementations these applications can
109
+ extend. It also contains a simple `JSONSerializer` for serializing to/from very basic JSON objects.
110
+
111
+ Many applications will find writing their own serializer to be more performant and less
112
+ complex than extending these classes even when their API format is very close to that expected
113
+ by these serializers.
114
+
115
+ It is recommended that apps write their own serializer to best suit the needs of their API and
116
+ application.
117
+
118
+ @module @ember-data/serializer
119
+ @main @ember-data/serializer
120
+ */
121
+ /// <reference types="ember-source/types" />
122
+ import EmberObject from '@ember/object';
123
+ import type Store from '@ember-data/store';
124
+ import type { ModelSchema } from '@ember-data/store/types';
125
+ import type { EmptyResourceDocument, SingleResourceDocument } from '@warp-drive/core-types/spec/json-api-raw';
126
+ /**
127
+ > ⚠️ CAUTION you likely want the docs for [<Interface> Serializer](/ember-data/release/classes/%3CInterface%3E%20Serializer)
128
+ > as extending this abstract class is unnecessary.
129
+
130
+ `Serializer` is an abstract base class that you may override in your
131
+ application to customize it for your backend. The minimum set of methods
132
+ that you should implement is:
133
+
134
+ * `normalizeResponse()`
135
+ * `serialize()`
136
+
137
+ And you can optionally override the following methods:
138
+
139
+ * `normalize()`
140
+
141
+ For an example implementation, see
142
+ [JSONSerializer](JSONSerializer), the included JSON serializer.
143
+
144
+ @class Serializer
145
+ @public
146
+ @extends Ember.EmberObject
147
+ */
148
+ export default class extends EmberObject {
149
+ store: Store;
150
+ /**
151
+ The `store` property is the application's `store` that contains
152
+ all records. It can be used to look up serializers for other model
153
+ types that may be nested inside the payload response.
154
+
155
+ Example:
156
+
157
+ ```js
158
+ Serializer.extend({
159
+ extractRelationship(relationshipModelName, relationshipHash) {
160
+ let modelClass = this.store.modelFor(relationshipModelName);
161
+ let relationshipSerializer = this.store.serializerFor(relationshipModelName);
162
+ return relationshipSerializer.normalize(modelClass, relationshipHash);
163
+ }
164
+ });
165
+ ```
166
+
167
+ @property store
168
+ @type {Store}
169
+ @public
170
+ */
171
+ /**
172
+ The `normalizeResponse` method is used to normalize a payload from the
173
+ server to a JSON-API Document.
174
+
175
+ http://jsonapi.org/format/#document-structure
176
+
177
+ Example:
178
+
179
+ ```js
180
+ Serializer.extend({
181
+ normalizeResponse(store, primaryModelClass, payload, id, requestType) {
182
+ if (requestType === 'findRecord') {
183
+ return this.normalize(primaryModelClass, payload);
184
+ } else {
185
+ return payload.reduce(function(documentHash, item) {
186
+ let { data, included } = this.normalize(primaryModelClass, item);
187
+ documentHash.included.push(...included);
188
+ documentHash.data.push(data);
189
+ return documentHash;
190
+ }, { data: [], included: [] })
191
+ }
192
+ }
193
+ });
194
+ ```
195
+
196
+ @since 1.13.0
197
+ @method normalizeResponse
198
+ @public
199
+ @param {Store} store
200
+ @param {Model} primaryModelClass
201
+ @param {Object} payload
202
+ @param {String|Number} id
203
+ @param {String} requestType
204
+ @return {Object} JSON-API Document
205
+ */
206
+ /**
207
+ The `serialize` method is used when a record is saved in order to convert
208
+ the record into the form that your external data source expects.
209
+
210
+ `serialize` takes an optional `options` hash with a single option:
211
+
212
+ - `includeId`: If this is `true`, `serialize` should include the ID
213
+ in the serialized object it builds.
214
+
215
+ Example:
216
+
217
+ ```js
218
+ Serializer.extend({
219
+ serialize(snapshot, options) {
220
+ let json = {
221
+ id: snapshot.id
222
+ };
223
+
224
+ snapshot.eachAttribute((key, attribute) => {
225
+ json[key] = snapshot.attr(key);
226
+ });
227
+
228
+ snapshot.eachRelationship((key, relationship) => {
229
+ if (relationship.kind === 'belongsTo') {
230
+ json[key] = snapshot.belongsTo(key, { id: true });
231
+ } else if (relationship.kind === 'hasMany') {
232
+ json[key] = snapshot.hasMany(key, { ids: true });
233
+ }
234
+ });
235
+
236
+ return json;
237
+ },
238
+ });
239
+ ```
240
+
241
+ @method serialize
242
+ @public
243
+ @param {Snapshot} snapshot
244
+ @param {Object} [options]
245
+ @return {Object}
246
+ */
247
+ /**
248
+ The `normalize` method is used to convert a payload received from your
249
+ external data source into the normalized form `store.push()` expects. You
250
+ should override this method, munge the hash and return the normalized
251
+ payload.
252
+
253
+ Example:
254
+
255
+ ```js
256
+ Serializer.extend({
257
+ normalize(modelClass, resourceHash) {
258
+ let data = {
259
+ id: resourceHash.id,
260
+ type: modelClass.modelName,
261
+ attributes: resourceHash
262
+ };
263
+ return { data: data };
264
+ }
265
+ })
266
+ ```
267
+
268
+ @method normalize
269
+ @public
270
+ @param {Model} typeClass
271
+ @param {Object} hash
272
+ @return {Object}
273
+ */
274
+ normalize(_typeClass: ModelSchema, hash: Record<string, unknown>): SingleResourceDocument | EmptyResourceDocument;
275
+ }
276
+ }
277
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA2GE;;AAEF,OAAO,WAAW,MAAM,eAAe,CAAC;AAGxC,OAAO,KAAK,KAAK,MAAM,mBAAmB,CAAC;AAC3C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,KAAK,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,0CAA0C,CAAC;AAE9G;;;;;;;;;;;;;;;;;;;;;EAqBE;AAEF,MAAM,CAAC,OAAO,MAAO,SAAQ,WAAW;IACrB,KAAK,EAAE,KAAK,CAAC;IAC9B;;;;;;;;;;;;;;;;;;;;MAoBE;IAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAkCE;IAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAwCE;IAEF;;;;;;;;;;;;;;;;;;;;;;;;;;MA0BE;IACF,SAAS,CAAC,UAAU,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,sBAAsB,GAAG,qBAAqB;CAGlH"}