@ember-data-mirror/serializer 5.6.0-alpha.5 → 5.6.0-beta.1
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.
- package/addon-main.cjs +1 -1
- package/dist/index.js +1 -301
- package/dist/json-api.js +1 -514
- package/dist/json.js +1 -6
- package/dist/rest.js +1 -1245
- package/dist/transform.js +1 -313
- package/package.json +7 -20
- package/unstable-preview-types/index.d.ts +81 -238
- package/unstable-preview-types/json-api.d.ts +2 -497
- package/unstable-preview-types/json.d.ts +2 -1048
- package/unstable-preview-types/rest.d.ts +2 -555
- package/unstable-preview-types/transform.d.ts +3 -7
- package/dist/index.js.map +0 -1
- package/dist/json-CYP2BhR9.js +0 -1349
- package/dist/json-CYP2BhR9.js.map +0 -1
- package/dist/json-api.js.map +0 -1
- package/dist/json.js.map +0 -1
- package/dist/rest.js.map +0 -1
- package/dist/transform.js.map +0 -1
- package/unstable-preview-types/-private/embedded-records-mixin.d.ts +0 -99
- package/unstable-preview-types/-private/embedded-records-mixin.d.ts.map +0 -1
- package/unstable-preview-types/-private/transforms/boolean.d.ts +0 -49
- package/unstable-preview-types/-private/transforms/boolean.d.ts.map +0 -1
- package/unstable-preview-types/-private/transforms/boolean.type-test.d.ts +0 -4
- package/unstable-preview-types/-private/transforms/boolean.type-test.d.ts.map +0 -1
- package/unstable-preview-types/-private/transforms/date.d.ts +0 -30
- package/unstable-preview-types/-private/transforms/date.d.ts.map +0 -1
- package/unstable-preview-types/-private/transforms/number.d.ts +0 -31
- package/unstable-preview-types/-private/transforms/number.d.ts.map +0 -1
- package/unstable-preview-types/-private/transforms/string.d.ts +0 -31
- package/unstable-preview-types/-private/transforms/string.d.ts.map +0 -1
- package/unstable-preview-types/-private/transforms/transform.d.ts +0 -121
- package/unstable-preview-types/-private/transforms/transform.d.ts.map +0 -1
- package/unstable-preview-types/-private/utils.d.ts +0 -6
- package/unstable-preview-types/-private/utils.d.ts.map +0 -1
- package/unstable-preview-types/index.d.ts.map +0 -1
- package/unstable-preview-types/json-api.d.ts.map +0 -1
- package/unstable-preview-types/json.d.ts.map +0 -1
- package/unstable-preview-types/rest.d.ts.map +0 -1
- package/unstable-preview-types/transform.d.ts.map +0 -1
package/addon-main.cjs
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,301 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import * as s from '@ember/service';
|
|
3
|
-
const deferred = /* @__PURE__ */new WeakMap();
|
|
4
|
-
function deferDecorator(proto, prop, desc) {
|
|
5
|
-
let map = deferred.get(proto);
|
|
6
|
-
if (!map) {
|
|
7
|
-
map = /* @__PURE__ */new Map();
|
|
8
|
-
deferred.set(proto, map);
|
|
9
|
-
}
|
|
10
|
-
map.set(prop, desc);
|
|
11
|
-
}
|
|
12
|
-
function findDeferredDecorator(target, prop) {
|
|
13
|
-
var _a;
|
|
14
|
-
let cursor = target.prototype;
|
|
15
|
-
while (cursor) {
|
|
16
|
-
let desc = (_a = deferred.get(cursor)) == null ? void 0 : _a.get(prop);
|
|
17
|
-
if (desc) {
|
|
18
|
-
return desc;
|
|
19
|
-
}
|
|
20
|
-
cursor = cursor.prototype;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
function decorateFieldV2(prototype, prop, decorators, initializer) {
|
|
24
|
-
let desc = {
|
|
25
|
-
configurable: true,
|
|
26
|
-
enumerable: true,
|
|
27
|
-
writable: true,
|
|
28
|
-
initializer: null
|
|
29
|
-
};
|
|
30
|
-
if (initializer) {
|
|
31
|
-
desc.initializer = initializer;
|
|
32
|
-
}
|
|
33
|
-
for (let decorator of decorators) {
|
|
34
|
-
desc = decorator(prototype, prop, desc) || desc;
|
|
35
|
-
}
|
|
36
|
-
if (desc.initializer === void 0) {
|
|
37
|
-
Object.defineProperty(prototype, prop, desc);
|
|
38
|
-
} else {
|
|
39
|
-
deferDecorator(prototype, prop, desc);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
function initializeDeferredDecorator(target, prop) {
|
|
43
|
-
let desc = findDeferredDecorator(target.constructor, prop);
|
|
44
|
-
if (desc) {
|
|
45
|
-
Object.defineProperty(target, prop, {
|
|
46
|
-
enumerable: desc.enumerable,
|
|
47
|
-
configurable: desc.configurable,
|
|
48
|
-
writable: desc.writable,
|
|
49
|
-
value: desc.initializer ? desc.initializer.call(target) : void 0
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
## Overview
|
|
56
|
-
|
|
57
|
-
<blockquote style="margin: 1em; padding: .1em 1em .1em 1em; border-left: solid 1em #E34C32; background: #e0e0e0;">
|
|
58
|
-
<p>
|
|
59
|
-
⚠️ <strong>This is LEGACY documentation</strong> for a feature that is no longer encouraged to be used.
|
|
60
|
-
If starting a new app or thinking of implementing a new serializer, consider writing a
|
|
61
|
-
<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>
|
|
62
|
-
</p>
|
|
63
|
-
</blockquote>
|
|
64
|
-
|
|
65
|
-
In order to properly manage and present your data, EmberData
|
|
66
|
-
needs to understand the structure of data it receives.
|
|
67
|
-
|
|
68
|
-
`Serializers` convert data between the server's API format and
|
|
69
|
-
the format EmberData understands.
|
|
70
|
-
|
|
71
|
-
Data received from an API response is **normalized** into
|
|
72
|
-
[JSON:API](https://jsonapi.org/) (the format used internally
|
|
73
|
-
by EmberData), while data sent to an API is **serialized**
|
|
74
|
-
into the format the API expects.
|
|
75
|
-
|
|
76
|
-
### Implementing a Serializer
|
|
77
|
-
|
|
78
|
-
There are only two required serializer methods, one for
|
|
79
|
-
normalizing data from the server API format into JSON:API, and
|
|
80
|
-
another for serializing records via `Snapshots` into the expected
|
|
81
|
-
server API format.
|
|
82
|
-
|
|
83
|
-
To implement a serializer, export a class that conforms to the structure
|
|
84
|
-
described by {@link MinimumSerializerInterface}
|
|
85
|
-
from the `app/serializers/` directory. An example is below.
|
|
86
|
-
|
|
87
|
-
```ts
|
|
88
|
-
import EmberObject from '@ember/object';
|
|
89
|
-
|
|
90
|
-
export default class ApplicationSerializer extends EmberObject {
|
|
91
|
-
normalizeResponse(store, schema, rawPayload) {
|
|
92
|
-
return rawPayload;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
serialize(snapshot, options) {
|
|
96
|
-
const serializedResource = {
|
|
97
|
-
id: snapshot.id,
|
|
98
|
-
type: snapshot.modelName,
|
|
99
|
-
attributes: snapshot.attributes()
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
return serializedResource;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
### Serializer Resolution
|
|
109
|
-
|
|
110
|
-
`store.serializerFor(name)` will lookup serializers defined in
|
|
111
|
-
`app/serializers/` and return an instance. If no serializer is found, an
|
|
112
|
-
error will be thrown.
|
|
113
|
-
|
|
114
|
-
`serializerFor` first attempts to find a serializer with an exact match on `name`,
|
|
115
|
-
then falls back to checking for the presence of a serializer named `application`.
|
|
116
|
-
|
|
117
|
-
```ts
|
|
118
|
-
store.serializerFor('author');
|
|
119
|
-
|
|
120
|
-
// lookup paths (in order) =>
|
|
121
|
-
// app/serializers/author.js
|
|
122
|
-
// app/serializers/application.js
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
Most requests in EmberData are made with respect to a particular `type` (or `modelName`)
|
|
126
|
-
(e.g., "get me the full collection of **books**" or "get me the **employee** whose id is 37"). We
|
|
127
|
-
refer to this as the **primary** resource `type`.
|
|
128
|
-
|
|
129
|
-
Typically `serializerFor` will be used to find a serializer with a name matching that of the primary
|
|
130
|
-
resource `type` for the request, falling back to the `application` serializer for those types that
|
|
131
|
-
do not have a defined serializer. This is often described as a `per-model` or `per-type` strategy
|
|
132
|
-
for defining serializers. However, because APIs rarely format payloads per-type but rather
|
|
133
|
-
per-API-version, this may not be a desired strategy.
|
|
134
|
-
|
|
135
|
-
It is recommended that applications define only a single `application` adapter and serializer
|
|
136
|
-
where possible.
|
|
137
|
-
|
|
138
|
-
If you have multiple API formats and the per-type strategy is not viable, one strategy is to
|
|
139
|
-
write an `application` adapter and serializer that make use of `options` to specify the desired
|
|
140
|
-
format when making a request.
|
|
141
|
-
|
|
142
|
-
### Using a Serializer
|
|
143
|
-
|
|
144
|
-
Any serializer in `app/serializers/` can be looked up by `name` using `store.serializerFor(name)`.
|
|
145
|
-
|
|
146
|
-
### Default Serializers
|
|
147
|
-
|
|
148
|
-
For applications whose APIs are *very close to* or *exactly* the **REST** format or **JSON:API**
|
|
149
|
-
format the `@ember-data-mirror/serializer` package contains implementations these applications can
|
|
150
|
-
extend. It also contains a simple `JSONSerializer` for serializing to/from very basic JSON objects.
|
|
151
|
-
|
|
152
|
-
Many applications will find writing their own serializer to be more performant and less
|
|
153
|
-
complex than extending these classes even when their API format is very close to that expected
|
|
154
|
-
by these serializers.
|
|
155
|
-
|
|
156
|
-
It is recommended that apps write their own serializer to best suit the needs of their API and
|
|
157
|
-
application.
|
|
158
|
-
|
|
159
|
-
@module
|
|
160
|
-
*/
|
|
161
|
-
|
|
162
|
-
const service = s.service ?? s.inject;
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
> ⚠️ CAUTION you likely want the docs for {@link MinimumSerializerInterface}
|
|
166
|
-
> as extending this abstract class is unnecessary.
|
|
167
|
-
|
|
168
|
-
`Serializer` is an abstract base class that you may override in your
|
|
169
|
-
application to customize it for your backend. The minimum set of methods
|
|
170
|
-
that you should implement is:
|
|
171
|
-
|
|
172
|
-
* `normalizeResponse()`
|
|
173
|
-
* `serialize()`
|
|
174
|
-
|
|
175
|
-
And you can optionally override the following methods:
|
|
176
|
-
|
|
177
|
-
* `normalize()`
|
|
178
|
-
|
|
179
|
-
For an example implementation, see
|
|
180
|
-
[JSONSerializer](JSONSerializer), the included JSON serializer.
|
|
181
|
-
|
|
182
|
-
@class Serializer
|
|
183
|
-
@public
|
|
184
|
-
*/
|
|
185
|
-
|
|
186
|
-
class Serializer extends EmberObject {
|
|
187
|
-
static {
|
|
188
|
-
decorateFieldV2(this.prototype, "store", [service]);
|
|
189
|
-
}
|
|
190
|
-
#store = (initializeDeferredDecorator(this, "store"), void 0);
|
|
191
|
-
/**
|
|
192
|
-
The `store` property is the application's `store` that contains
|
|
193
|
-
all records. It can be used to look up serializers for other model
|
|
194
|
-
types that may be nested inside the payload response.
|
|
195
|
-
Example:
|
|
196
|
-
```js
|
|
197
|
-
Serializer.extend({
|
|
198
|
-
extractRelationship(relationshipModelName, relationshipHash) {
|
|
199
|
-
let modelClass = this.store.modelFor(relationshipModelName);
|
|
200
|
-
let relationshipSerializer = this.store.serializerFor(relationshipModelName);
|
|
201
|
-
return relationshipSerializer.normalize(modelClass, relationshipHash);
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
```
|
|
205
|
-
@property store
|
|
206
|
-
@type {Store}
|
|
207
|
-
@public
|
|
208
|
-
*/
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
The `normalizeResponse` method is used to normalize a payload from the
|
|
212
|
-
server to a JSON-API Document.
|
|
213
|
-
http://jsonapi.org/format/#document-structure
|
|
214
|
-
Example:
|
|
215
|
-
```js
|
|
216
|
-
Serializer.extend({
|
|
217
|
-
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
|
|
218
|
-
if (requestType === 'findRecord') {
|
|
219
|
-
return this.normalize(primaryModelClass, payload);
|
|
220
|
-
} else {
|
|
221
|
-
return payload.reduce(function(documentHash, item) {
|
|
222
|
-
let { data, included } = this.normalize(primaryModelClass, item);
|
|
223
|
-
documentHash.included.push(...included);
|
|
224
|
-
documentHash.data.push(data);
|
|
225
|
-
return documentHash;
|
|
226
|
-
}, { data: [], included: [] })
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
```
|
|
231
|
-
@since 1.13.0
|
|
232
|
-
@public
|
|
233
|
-
@param {Store} store
|
|
234
|
-
@param {Model} primaryModelClass
|
|
235
|
-
@param {Object} payload
|
|
236
|
-
@param {String|Number} id
|
|
237
|
-
@param {String} requestType
|
|
238
|
-
@return {Object} JSON-API Document
|
|
239
|
-
*/
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
The `serialize` method is used when a record is saved in order to convert
|
|
243
|
-
the record into the form that your external data source expects.
|
|
244
|
-
`serialize` takes an optional `options` hash with a single option:
|
|
245
|
-
- `includeId`: If this is `true`, `serialize` should include the ID
|
|
246
|
-
in the serialized object it builds.
|
|
247
|
-
Example:
|
|
248
|
-
```js
|
|
249
|
-
Serializer.extend({
|
|
250
|
-
serialize(snapshot, options) {
|
|
251
|
-
let json = {
|
|
252
|
-
id: snapshot.id
|
|
253
|
-
};
|
|
254
|
-
snapshot.eachAttribute((key, attribute) => {
|
|
255
|
-
json[key] = snapshot.attr(key);
|
|
256
|
-
});
|
|
257
|
-
snapshot.eachRelationship((key, relationship) => {
|
|
258
|
-
if (relationship.kind === 'belongsTo') {
|
|
259
|
-
json[key] = snapshot.belongsTo(key, { id: true });
|
|
260
|
-
} else if (relationship.kind === 'hasMany') {
|
|
261
|
-
json[key] = snapshot.hasMany(key, { ids: true });
|
|
262
|
-
}
|
|
263
|
-
});
|
|
264
|
-
return json;
|
|
265
|
-
},
|
|
266
|
-
});
|
|
267
|
-
```
|
|
268
|
-
@public
|
|
269
|
-
@param {Snapshot} snapshot
|
|
270
|
-
@param {Object} [options]
|
|
271
|
-
@return {Object}
|
|
272
|
-
*/
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
The `normalize` method is used to convert a payload received from your
|
|
276
|
-
external data source into the normalized form `store.push()` expects. You
|
|
277
|
-
should override this method, munge the hash and return the normalized
|
|
278
|
-
payload.
|
|
279
|
-
Example:
|
|
280
|
-
```js
|
|
281
|
-
Serializer.extend({
|
|
282
|
-
normalize(modelClass, resourceHash) {
|
|
283
|
-
let data = {
|
|
284
|
-
id: resourceHash.id,
|
|
285
|
-
type: modelClass.modelName,
|
|
286
|
-
attributes: resourceHash
|
|
287
|
-
};
|
|
288
|
-
return { data: data };
|
|
289
|
-
}
|
|
290
|
-
})
|
|
291
|
-
```
|
|
292
|
-
@public
|
|
293
|
-
@param {Model} typeClass
|
|
294
|
-
@param {Object} hash
|
|
295
|
-
@return {Object}
|
|
296
|
-
*/
|
|
297
|
-
normalize(_typeClass, hash) {
|
|
298
|
-
return hash;
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
export { Serializer as default };
|
|
1
|
+
export { Serializer as default } from '@warp-drive-mirror/legacy/serializer';
|