@ember-data-mirror/serializer 5.4.0-alpha.49

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 (58) hide show
  1. package/LICENSE.md +11 -0
  2. package/README.md +98 -0
  3. package/addon/-private.js +3 -0
  4. package/addon/-private.js.map +1 -0
  5. package/addon/embedded-records-mixin-QJe_8jrF.js +572 -0
  6. package/addon/embedded-records-mixin-QJe_8jrF.js.map +1 -0
  7. package/addon/index.js +181 -0
  8. package/addon/index.js.map +1 -0
  9. package/addon/json-api.js +508 -0
  10. package/addon/json-api.js.map +1 -0
  11. package/addon/json.js +1375 -0
  12. package/addon/json.js.map +1 -0
  13. package/addon/rest.js +684 -0
  14. package/addon/rest.js.map +1 -0
  15. package/addon/string-Juwz4cu0.js +345 -0
  16. package/addon/string-Juwz4cu0.js.map +1 -0
  17. package/addon/transform.js +1 -0
  18. package/addon/transform.js.map +1 -0
  19. package/addon/utils-NcVD2Jb5.js +12 -0
  20. package/addon/utils-NcVD2Jb5.js.map +1 -0
  21. package/addon-main.js +94 -0
  22. package/blueprints/serializer/files/__root__/__path__/__name__.js +4 -0
  23. package/blueprints/serializer/index.js +14 -0
  24. package/blueprints/serializer/native-files/__root__/__path__/__name__.js +4 -0
  25. package/blueprints/serializer-test/index.js +29 -0
  26. package/blueprints/serializer-test/qunit-files/__root__/__path__/__test__.js +24 -0
  27. package/blueprints/transform/files/__root__/__path__/__name__.js +13 -0
  28. package/blueprints/transform/index.js +7 -0
  29. package/blueprints/transform/native-files/__root__/__path__/__name__.js +13 -0
  30. package/blueprints/transform-test/index.js +29 -0
  31. package/blueprints/transform-test/qunit-files/__root__/__path__/__test__.js +13 -0
  32. package/package.json +113 -0
  33. package/unstable-preview-types/-private/embedded-records-mixin.d.ts +7 -0
  34. package/unstable-preview-types/-private/embedded-records-mixin.d.ts.map +1 -0
  35. package/unstable-preview-types/-private/transforms/boolean.d.ts +52 -0
  36. package/unstable-preview-types/-private/transforms/boolean.d.ts.map +1 -0
  37. package/unstable-preview-types/-private/transforms/date.d.ts +33 -0
  38. package/unstable-preview-types/-private/transforms/date.d.ts.map +1 -0
  39. package/unstable-preview-types/-private/transforms/number.d.ts +34 -0
  40. package/unstable-preview-types/-private/transforms/number.d.ts.map +1 -0
  41. package/unstable-preview-types/-private/transforms/string.d.ts +34 -0
  42. package/unstable-preview-types/-private/transforms/string.d.ts.map +1 -0
  43. package/unstable-preview-types/-private/transforms/transform.d.ts +128 -0
  44. package/unstable-preview-types/-private/transforms/transform.d.ts.map +1 -0
  45. package/unstable-preview-types/-private/utils.d.ts +6 -0
  46. package/unstable-preview-types/-private/utils.d.ts.map +1 -0
  47. package/unstable-preview-types/-private.d.ts +13 -0
  48. package/unstable-preview-types/-private.d.ts.map +1 -0
  49. package/unstable-preview-types/index.d.ts +278 -0
  50. package/unstable-preview-types/index.d.ts.map +1 -0
  51. package/unstable-preview-types/json-api.d.ts +515 -0
  52. package/unstable-preview-types/json-api.d.ts.map +1 -0
  53. package/unstable-preview-types/json.d.ts +1094 -0
  54. package/unstable-preview-types/json.d.ts.map +1 -0
  55. package/unstable-preview-types/rest.d.ts +602 -0
  56. package/unstable-preview-types/rest.d.ts.map +1 -0
  57. package/unstable-preview-types/transform.d.ts +7 -0
  58. package/unstable-preview-types/transform.d.ts.map +1 -0
@@ -0,0 +1,278 @@
1
+ /// <reference path="./json-api.d.ts" />
2
+ /// <reference path="./transform.d.ts" />
3
+ /// <reference path="./json.d.ts" />
4
+ /// <reference path="./rest.d.ts" />
5
+ /// <reference path="./-private.d.ts" />
6
+ /// <reference path="./-private/utils.d.ts" />
7
+ /// <reference path="./-private/embedded-records-mixin.d.ts" />
8
+ /// <reference path="./-private/transforms/date.d.ts" />
9
+ /// <reference path="./-private/transforms/number.d.ts" />
10
+ /// <reference path="./-private/transforms/transform.d.ts" />
11
+ /// <reference path="./-private/transforms/string.d.ts" />
12
+ /// <reference path="./-private/transforms/boolean.d.ts" />
13
+ declare module '@ember-data-mirror/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-mirror/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-mirror/serializer
120
+ @main @ember-data-mirror/serializer
121
+ */
122
+ /// <reference types="ember-source/types" />
123
+ import EmberObject from '@ember/object';
124
+ import type Store from '@ember-data-mirror/store';
125
+ import type { ModelSchema } from '@ember-data-mirror/store/-types/q/ds-model';
126
+ import type { EmptyResourceDocument, SingleResourceDocument } from '@warp-drive-mirror/core-types/spec/raw';
127
+ /**
128
+ > ⚠️ CAUTION you likely want the docs for [<Interface> Serializer](/ember-data/release/classes/%3CInterface%3E%20Serializer)
129
+ > as extending this abstract class is unnecessary.
130
+
131
+ `Serializer` is an abstract base class that you may override in your
132
+ application to customize it for your backend. The minimum set of methods
133
+ that you should implement is:
134
+
135
+ * `normalizeResponse()`
136
+ * `serialize()`
137
+
138
+ And you can optionally override the following methods:
139
+
140
+ * `normalize()`
141
+
142
+ For an example implementation, see
143
+ [JSONSerializer](JSONSerializer), the included JSON serializer.
144
+
145
+ @class Serializer
146
+ @public
147
+ @extends Ember.EmberObject
148
+ */
149
+ export default class extends EmberObject {
150
+ store: Store;
151
+ /**
152
+ The `store` property is the application's `store` that contains
153
+ all records. It can be used to look up serializers for other model
154
+ types that may be nested inside the payload response.
155
+
156
+ Example:
157
+
158
+ ```js
159
+ Serializer.extend({
160
+ extractRelationship(relationshipModelName, relationshipHash) {
161
+ let modelClass = this.store.modelFor(relationshipModelName);
162
+ let relationshipSerializer = this.store.serializerFor(relationshipModelName);
163
+ return relationshipSerializer.normalize(modelClass, relationshipHash);
164
+ }
165
+ });
166
+ ```
167
+
168
+ @property store
169
+ @type {Store}
170
+ @public
171
+ */
172
+ /**
173
+ The `normalizeResponse` method is used to normalize a payload from the
174
+ server to a JSON-API Document.
175
+
176
+ http://jsonapi.org/format/#document-structure
177
+
178
+ Example:
179
+
180
+ ```js
181
+ Serializer.extend({
182
+ normalizeResponse(store, primaryModelClass, payload, id, requestType) {
183
+ if (requestType === 'findRecord') {
184
+ return this.normalize(primaryModelClass, payload);
185
+ } else {
186
+ return payload.reduce(function(documentHash, item) {
187
+ let { data, included } = this.normalize(primaryModelClass, item);
188
+ documentHash.included.push(...included);
189
+ documentHash.data.push(data);
190
+ return documentHash;
191
+ }, { data: [], included: [] })
192
+ }
193
+ }
194
+ });
195
+ ```
196
+
197
+ @since 1.13.0
198
+ @method normalizeResponse
199
+ @public
200
+ @param {Store} store
201
+ @param {Model} primaryModelClass
202
+ @param {Object} payload
203
+ @param {String|Number} id
204
+ @param {String} requestType
205
+ @return {Object} JSON-API Document
206
+ */
207
+ /**
208
+ The `serialize` method is used when a record is saved in order to convert
209
+ the record into the form that your external data source expects.
210
+
211
+ `serialize` takes an optional `options` hash with a single option:
212
+
213
+ - `includeId`: If this is `true`, `serialize` should include the ID
214
+ in the serialized object it builds.
215
+
216
+ Example:
217
+
218
+ ```js
219
+ Serializer.extend({
220
+ serialize(snapshot, options) {
221
+ let json = {
222
+ id: snapshot.id
223
+ };
224
+
225
+ snapshot.eachAttribute((key, attribute) => {
226
+ json[key] = snapshot.attr(key);
227
+ });
228
+
229
+ snapshot.eachRelationship((key, relationship) => {
230
+ if (relationship.kind === 'belongsTo') {
231
+ json[key] = snapshot.belongsTo(key, { id: true });
232
+ } else if (relationship.kind === 'hasMany') {
233
+ json[key] = snapshot.hasMany(key, { ids: true });
234
+ }
235
+ });
236
+
237
+ return json;
238
+ },
239
+ });
240
+ ```
241
+
242
+ @method serialize
243
+ @public
244
+ @param {Snapshot} snapshot
245
+ @param {Object} [options]
246
+ @return {Object}
247
+ */
248
+ /**
249
+ The `normalize` method is used to convert a payload received from your
250
+ external data source into the normalized form `store.push()` expects. You
251
+ should override this method, munge the hash and return the normalized
252
+ payload.
253
+
254
+ Example:
255
+
256
+ ```js
257
+ Serializer.extend({
258
+ normalize(modelClass, resourceHash) {
259
+ let data = {
260
+ id: resourceHash.id,
261
+ type: modelClass.modelName,
262
+ attributes: resourceHash
263
+ };
264
+ return { data: data };
265
+ }
266
+ })
267
+ ```
268
+
269
+ @method normalize
270
+ @public
271
+ @param {Model} typeClass
272
+ @param {Object} hash
273
+ @return {Object}
274
+ */
275
+ normalize(_typeClass: ModelSchema, hash: Record<string, unknown>): SingleResourceDocument | EmptyResourceDocument;
276
+ }
277
+ }
278
+ //# 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,qCAAqC,CAAC;AACvE,OAAO,KAAK,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AAErG;;;;;;;;;;;;;;;;;;;;;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"}