@ember-data/serializer 5.5.0-alpha.2 → 5.5.0-alpha.21

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 (78) hide show
  1. package/LICENSE.md +19 -7
  2. package/README.md +16 -10
  3. package/addon-main.cjs +5 -0
  4. package/blueprints/serializer/index.js +71 -5
  5. package/blueprints/serializer-test/index.js +13 -7
  6. package/blueprints/serializer-test/qunit-files/__root__/__path__/__test__.js +8 -9
  7. package/blueprints/transform/index.js +14 -4
  8. package/blueprints/transform-test/index.js +13 -7
  9. package/blueprints/transform-test/qunit-files/__root__/__path__/__test__.js +3 -4
  10. package/dist/index.js +306 -0
  11. package/dist/index.js.map +1 -0
  12. package/{addon/json.js → dist/json-BwMH6O_R.js} +83 -63
  13. package/dist/json-BwMH6O_R.js.map +1 -0
  14. package/{addon → dist}/json-api.js +90 -188
  15. package/dist/json-api.js.map +1 -0
  16. package/dist/json.js +6 -0
  17. package/dist/json.js.map +1 -0
  18. package/dist/rest.js +1274 -0
  19. package/dist/rest.js.map +1 -0
  20. package/{addon/string-22572f80.js → dist/transform.js} +143 -20
  21. package/dist/transform.js.map +1 -0
  22. package/logos/NCC-1701-a-blue.svg +4 -0
  23. package/logos/NCC-1701-a-gold.svg +4 -0
  24. package/logos/NCC-1701-a-gold_100.svg +1 -0
  25. package/logos/NCC-1701-a-gold_base-64.txt +1 -0
  26. package/logos/NCC-1701-a.svg +4 -0
  27. package/logos/README.md +4 -0
  28. package/logos/docs-badge.svg +2 -0
  29. package/logos/github-header.svg +444 -0
  30. package/logos/social1.png +0 -0
  31. package/logos/social2.png +0 -0
  32. package/logos/warp-drive-logo-dark.svg +4 -0
  33. package/logos/warp-drive-logo-gold.svg +4 -0
  34. package/package.json +60 -45
  35. package/unstable-preview-types/-private/embedded-records-mixin.d.ts +102 -0
  36. package/unstable-preview-types/-private/embedded-records-mixin.d.ts.map +1 -0
  37. package/unstable-preview-types/-private/transforms/boolean.d.ts +52 -0
  38. package/unstable-preview-types/-private/transforms/boolean.d.ts.map +1 -0
  39. package/unstable-preview-types/-private/transforms/boolean.type-test.d.ts +4 -0
  40. package/unstable-preview-types/-private/transforms/boolean.type-test.d.ts.map +1 -0
  41. package/unstable-preview-types/-private/transforms/date.d.ts +33 -0
  42. package/unstable-preview-types/-private/transforms/date.d.ts.map +1 -0
  43. package/unstable-preview-types/-private/transforms/number.d.ts +34 -0
  44. package/unstable-preview-types/-private/transforms/number.d.ts.map +1 -0
  45. package/unstable-preview-types/-private/transforms/string.d.ts +34 -0
  46. package/unstable-preview-types/-private/transforms/string.d.ts.map +1 -0
  47. package/unstable-preview-types/-private/transforms/transform.d.ts +126 -0
  48. package/unstable-preview-types/-private/transforms/transform.d.ts.map +1 -0
  49. package/unstable-preview-types/-private/utils.d.ts +6 -0
  50. package/unstable-preview-types/-private/utils.d.ts.map +1 -0
  51. package/unstable-preview-types/index.d.ts +277 -0
  52. package/unstable-preview-types/index.d.ts.map +1 -0
  53. package/unstable-preview-types/json-api.d.ts +514 -0
  54. package/unstable-preview-types/json-api.d.ts.map +1 -0
  55. package/unstable-preview-types/json.d.ts +1093 -0
  56. package/unstable-preview-types/json.d.ts.map +1 -0
  57. package/unstable-preview-types/rest.d.ts +570 -0
  58. package/unstable-preview-types/rest.d.ts.map +1 -0
  59. package/unstable-preview-types/transform.d.ts +11 -0
  60. package/unstable-preview-types/transform.d.ts.map +1 -0
  61. package/addon/-private.js +0 -4
  62. package/addon/-private.js.map +0 -1
  63. package/addon/embedded-records-mixin-d75385ff.js +0 -575
  64. package/addon/embedded-records-mixin-d75385ff.js.map +0 -1
  65. package/addon/index.js +0 -180
  66. package/addon/index.js.map +0 -1
  67. package/addon/json-api.js.map +0 -1
  68. package/addon/json.js.map +0 -1
  69. package/addon/rest.js +0 -680
  70. package/addon/rest.js.map +0 -1
  71. package/addon/string-22572f80.js.map +0 -1
  72. package/addon/transform.js +0 -2
  73. package/addon/transform.js.map +0 -1
  74. package/addon/utils-075c5b79.js +0 -12
  75. package/addon/utils-075c5b79.js.map +0 -1
  76. package/addon-main.js +0 -93
  77. /package/{ember-data-logo-dark.svg → logos/ember-data-logo-dark.svg} +0 -0
  78. /package/{ember-data-logo-light.svg → logos/ember-data-logo-light.svg} +0 -0
@@ -0,0 +1,277 @@
1
+ /// <reference path="./transform.d.ts" />
2
+ /// <reference path="./json-api.d.ts" />
3
+ /// <reference path="./json.d.ts" />
4
+ /// <reference path="./rest.d.ts" />
5
+ /// <reference path="./-private/utils.d.ts" />
6
+ /// <reference path="./-private/embedded-records-mixin.d.ts" />
7
+ /// <reference path="./-private/transforms/date.d.ts" />
8
+ /// <reference path="./-private/transforms/number.d.ts" />
9
+ /// <reference path="./-private/transforms/transform.d.ts" />
10
+ /// <reference path="./-private/transforms/string.d.ts" />
11
+ /// <reference path="./-private/transforms/boolean.d.ts" />
12
+ /// <reference path="./-private/transforms/boolean.type-test.d.ts" />
13
+ declare module '@ember-data/serializer' {
14
+ /**
15
+ ## Overview
16
+
17
+ <blockquote style="margin: 1em; padding: .1em 1em .1em 1em; border-left: solid 1em #E34C32; background: #e0e0e0;">
18
+ <p>
19
+ ⚠️ <strong>This is LEGACY documentation</strong> for a feature that is no longer encouraged to be used.
20
+ If starting a new app or thinking of implementing a new serializer, consider writing a
21
+ <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>
22
+ </p>
23
+ </blockquote>
24
+
25
+ In order to properly manage and present your data, EmberData
26
+ needs to understand the structure of data it receives.
27
+
28
+ `Serializers` convert data between the server's API format and
29
+ the format EmberData understands.
30
+
31
+ Data received from an API response is **normalized** into
32
+ [JSON:API](https://jsonapi.org/) (the format used internally
33
+ by EmberData), while data sent to an API is **serialized**
34
+ into the format the API expects.
35
+
36
+ ### Implementing a Serializer
37
+
38
+ There are only two required serializer methods, one for
39
+ normalizing data from the server API format into JSON:API, and
40
+ another for serializing records via `Snapshots` into the expected
41
+ server API format.
42
+
43
+ To implement a serializer, export a class that conforms to the structure
44
+ described by [<Interface> Serializer](/ember-data/release/classes/%3CInterface%3E%20Serializer)
45
+ from the `app/serializers/` directory. An example is below.
46
+
47
+ ```ts
48
+ import EmberObject from '@ember/object';
49
+
50
+ export default class ApplicationSerializer extends EmberObject {
51
+ normalizeResponse(store, schema, rawPayload) {
52
+ return rawPayload;
53
+ }
54
+
55
+ serialize(snapshot, options) {
56
+ const serializedResource = {
57
+ id: snapshot.id,
58
+ type: snapshot.modelName,
59
+ attributes: snapshot.attributes()
60
+ };
61
+
62
+ return serializedResource;
63
+ }
64
+ }
65
+ ```
66
+
67
+
68
+ ### Serializer Resolution
69
+
70
+ `store.serializerFor(name)` will lookup serializers defined in
71
+ `app/serializers/` and return an instance. If no serializer is found, an
72
+ error will be thrown.
73
+
74
+ `serializerFor` first attempts to find a serializer with an exact match on `name`,
75
+ then falls back to checking for the presence of a serializer named `application`.
76
+
77
+ ```ts
78
+ store.serializerFor('author');
79
+
80
+ // lookup paths (in order) =>
81
+ // app/serializers/author.js
82
+ // app/serializers/application.js
83
+ ```
84
+
85
+ Most requests in EmberData are made with respect to a particular `type` (or `modelName`)
86
+ (e.g., "get me the full collection of **books**" or "get me the **employee** whose id is 37"). We
87
+ refer to this as the **primary** resource `type`.
88
+
89
+ Typically `serializerFor` will be used to find a serializer with a name matching that of the primary
90
+ resource `type` for the request, falling back to the `application` serializer for those types that
91
+ do not have a defined serializer. This is often described as a `per-model` or `per-type` strategy
92
+ for defining serializers. However, because APIs rarely format payloads per-type but rather
93
+ per-API-version, this may not be a desired strategy.
94
+
95
+ It is recommended that applications define only a single `application` adapter and serializer
96
+ where possible.
97
+
98
+ If you have multiple API formats and the per-type strategy is not viable, one strategy is to
99
+ write an `application` adapter and serializer that make use of `options` to specify the desired
100
+ format when making a request.
101
+
102
+ ### Using a Serializer
103
+
104
+ Any serializer in `app/serializers/` can be looked up by `name` using `store.serializerFor(name)`.
105
+
106
+ ### Default Serializers
107
+
108
+ For applications whose APIs are *very close to* or *exactly* the **REST** format or **JSON:API**
109
+ format the `@ember-data/serializer` package contains implementations these applications can
110
+ extend. It also contains a simple `JSONSerializer` for serializing to/from very basic JSON objects.
111
+
112
+ Many applications will find writing their own serializer to be more performant and less
113
+ complex than extending these classes even when their API format is very close to that expected
114
+ by these serializers.
115
+
116
+ It is recommended that apps write their own serializer to best suit the needs of their API and
117
+ application.
118
+
119
+ @module @ember-data/serializer
120
+ @main @ember-data/serializer
121
+ */
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;AAI9G;;;;;;;;;;;;;;;;;;;;;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"}