@ember-data/serializer 5.8.0-beta.0 → 5.8.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/README.md +3 -3
- package/package.json +2 -2
- package/unstable-preview-types/index.d.ts +2 -109
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
|
|
20
20
|
> **Caution** ⚠️ **This is LEGACY documentation** for a feature that is no longer encouraged to be used.
|
|
21
21
|
> If starting a new app or thinking of implementing a new serializer, consider writing a [Handler](https://api.emberjs.com/ember-data/release/classes/%3CInterface%3E%20Handler)
|
|
22
|
-
> instead to be used with the [RequestManager](https://github.com/
|
|
22
|
+
> instead to be used with the [RequestManager](https://github.com/warp-drive-data/warp-drive/tree/main/packages/request#readme)
|
|
23
23
|
|
|
24
24
|
## Installation
|
|
25
25
|
|
|
@@ -45,10 +45,10 @@ pnpm add @ember-data/serializer
|
|
|
45
45
|
If using `ember-data` no additional setup is necesssary.
|
|
46
46
|
|
|
47
47
|
> **Note**
|
|
48
|
-
> When using [ember-data](https://github.com/
|
|
48
|
+
> When using [ember-data](https://github.com/warp-drive-data/warp-drive/blob/main/packages/-ember-data) the below
|
|
49
49
|
> configuration is handled for you automatically.
|
|
50
50
|
|
|
51
|
-
To use legacy serializers you will need to have installed and configured the LegacyNetworkHandler from [@ember-data/legacy-compat](https://github.com/
|
|
51
|
+
To use legacy serializers you will need to have installed and configured the LegacyNetworkHandler from [@ember-data/legacy-compat](https://github.com/warp-drive-data/warp-drive/blob/main/packages/-ember-data)
|
|
52
52
|
|
|
53
53
|
```sh
|
|
54
54
|
pnpm add @ember-data/legacy-compat
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ember-data/serializer",
|
|
3
|
-
"version": "5.8.0-beta.
|
|
3
|
+
"version": "5.8.0-beta.1",
|
|
4
4
|
"description": "Provides Legacy JSON, JSON:API and REST Implementations of the Serializer Interface for use with @ember-data/store",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ember-addon"
|
|
7
7
|
],
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
|
-
"url": "git+ssh://git@github.com:
|
|
10
|
+
"url": "git+ssh://git@github.com:warp-drive-data/warp-drive.git",
|
|
11
11
|
"directory": "packages/serializer"
|
|
12
12
|
},
|
|
13
13
|
"license": "MIT",
|
|
@@ -1,115 +1,8 @@
|
|
|
1
|
+
/// <reference path="./rest.d.ts" />
|
|
1
2
|
/// <reference path="./transform.d.ts" />
|
|
2
|
-
/// <reference path="./json-api.d.ts" />
|
|
3
3
|
/// <reference path="./json.d.ts" />
|
|
4
|
-
/// <reference path="./
|
|
4
|
+
/// <reference path="./json-api.d.ts" />
|
|
5
5
|
declare module '@ember-data/serializer' {
|
|
6
|
-
/**
|
|
7
|
-
## Overview
|
|
8
|
-
|
|
9
|
-
<blockquote style="margin: 1em; padding: .1em 1em .1em 1em; border-left: solid 1em #E34C32; background: #e0e0e0;">
|
|
10
|
-
<p>
|
|
11
|
-
⚠️ <strong>This is LEGACY documentation</strong> for a feature that is no longer encouraged to be used.
|
|
12
|
-
If starting a new app or thinking of implementing a new serializer, consider writing a
|
|
13
|
-
<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>
|
|
14
|
-
</p>
|
|
15
|
-
</blockquote>
|
|
16
|
-
|
|
17
|
-
In order to properly manage and present your data, EmberData
|
|
18
|
-
needs to understand the structure of data it receives.
|
|
19
|
-
|
|
20
|
-
`Serializers` convert data between the server's API format and
|
|
21
|
-
the format EmberData understands.
|
|
22
|
-
|
|
23
|
-
Data received from an API response is **normalized** into
|
|
24
|
-
[JSON:API](https://jsonapi.org/) (the format used internally
|
|
25
|
-
by EmberData), while data sent to an API is **serialized**
|
|
26
|
-
into the format the API expects.
|
|
27
|
-
|
|
28
|
-
### Implementing a Serializer
|
|
29
|
-
|
|
30
|
-
There are only two required serializer methods, one for
|
|
31
|
-
normalizing data from the server API format into JSON:API, and
|
|
32
|
-
another for serializing records via `Snapshots` into the expected
|
|
33
|
-
server API format.
|
|
34
|
-
|
|
35
|
-
To implement a serializer, export a class that conforms to the structure
|
|
36
|
-
described by {@link MinimumSerializerInterface}
|
|
37
|
-
from the `app/serializers/` directory. An example is below.
|
|
38
|
-
|
|
39
|
-
```ts
|
|
40
|
-
import EmberObject from '@ember/object';
|
|
41
|
-
|
|
42
|
-
export default class ApplicationSerializer extends EmberObject {
|
|
43
|
-
normalizeResponse(store, schema, rawPayload) {
|
|
44
|
-
return rawPayload;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
serialize(snapshot, options) {
|
|
48
|
-
const serializedResource = {
|
|
49
|
-
id: snapshot.id,
|
|
50
|
-
type: snapshot.modelName,
|
|
51
|
-
attributes: snapshot.attributes()
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
return serializedResource;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
### Serializer Resolution
|
|
61
|
-
|
|
62
|
-
`store.serializerFor(name)` will lookup serializers defined in
|
|
63
|
-
`app/serializers/` and return an instance. If no serializer is found, an
|
|
64
|
-
error will be thrown.
|
|
65
|
-
|
|
66
|
-
`serializerFor` first attempts to find a serializer with an exact match on `name`,
|
|
67
|
-
then falls back to checking for the presence of a serializer named `application`.
|
|
68
|
-
|
|
69
|
-
```ts
|
|
70
|
-
store.serializerFor('author');
|
|
71
|
-
|
|
72
|
-
// lookup paths (in order) =>
|
|
73
|
-
// app/serializers/author.js
|
|
74
|
-
// app/serializers/application.js
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
Most requests in EmberData are made with respect to a particular `type` (or `modelName`)
|
|
78
|
-
(e.g., "get me the full collection of **books**" or "get me the **employee** whose id is 37"). We
|
|
79
|
-
refer to this as the **primary** resource `type`.
|
|
80
|
-
|
|
81
|
-
Typically `serializerFor` will be used to find a serializer with a name matching that of the primary
|
|
82
|
-
resource `type` for the request, falling back to the `application` serializer for those types that
|
|
83
|
-
do not have a defined serializer. This is often described as a `per-model` or `per-type` strategy
|
|
84
|
-
for defining serializers. However, because APIs rarely format payloads per-type but rather
|
|
85
|
-
per-API-version, this may not be a desired strategy.
|
|
86
|
-
|
|
87
|
-
It is recommended that applications define only a single `application` adapter and serializer
|
|
88
|
-
where possible.
|
|
89
|
-
|
|
90
|
-
If you have multiple API formats and the per-type strategy is not viable, one strategy is to
|
|
91
|
-
write an `application` adapter and serializer that make use of `options` to specify the desired
|
|
92
|
-
format when making a request.
|
|
93
|
-
|
|
94
|
-
### Using a Serializer
|
|
95
|
-
|
|
96
|
-
Any serializer in `app/serializers/` can be looked up by `name` using `store.serializerFor(name)`.
|
|
97
|
-
|
|
98
|
-
### Default Serializers
|
|
99
|
-
|
|
100
|
-
For applications whose APIs are *very close to* or *exactly* the **REST** format or **JSON:API**
|
|
101
|
-
format the `@ember-data/serializer` package contains implementations these applications can
|
|
102
|
-
extend. It also contains a simple `JSONSerializer` for serializing to/from very basic JSON objects.
|
|
103
|
-
|
|
104
|
-
Many applications will find writing their own serializer to be more performant and less
|
|
105
|
-
complex than extending these classes even when their API format is very close to that expected
|
|
106
|
-
by these serializers.
|
|
107
|
-
|
|
108
|
-
It is recommended that apps write their own serializer to best suit the needs of their API and
|
|
109
|
-
application.
|
|
110
|
-
|
|
111
|
-
@module
|
|
112
|
-
*/
|
|
113
6
|
export { Serializer as default } from "@warp-drive/legacy/serializer";
|
|
114
7
|
|
|
115
8
|
}
|