@warp-drive/legacy 5.8.0-alpha.4 → 5.8.0-alpha.40
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 +14 -27
- package/declarations/adapter/error.d.ts +5 -5
- package/declarations/adapter/json-api.d.ts +6 -8
- package/declarations/adapter/rest.d.ts +5 -8
- package/declarations/adapter.d.ts +4 -6
- package/declarations/compat/-private.d.ts +1 -1
- package/declarations/compat/builders/find-all.d.ts +6 -6
- package/declarations/compat/builders/find-record.d.ts +8 -8
- package/declarations/compat/builders/query.d.ts +12 -12
- package/declarations/compat/extensions.d.ts +1 -1
- package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +7 -9
- package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +20 -30
- package/declarations/compat/utils.d.ts +17 -17
- package/declarations/compat.d.ts +35 -11
- package/declarations/index.d.ts +70 -0
- package/declarations/model/-private/attr.d.ts +5 -6
- package/declarations/model/-private/belongs-to.d.ts +4 -5
- package/declarations/model/-private/has-many.d.ts +4 -5
- package/declarations/model/-private/hooks.d.ts +1 -1
- package/declarations/model/-private/legacy-relationships-support.d.ts +2 -2
- package/declarations/model/-private/model.d.ts +8 -8
- package/declarations/model/-private/record-state.d.ts +1 -1
- package/declarations/model/-private/references/belongs-to.d.ts +5 -5
- package/declarations/model/-private/references/has-many.d.ts +3 -3
- package/declarations/model/migration-support.d.ts +46 -21
- package/declarations/model-fragments/extensions/fragment-array.d.ts +16 -0
- package/declarations/model-fragments/extensions/fragment.d.ts +15 -0
- package/declarations/model-fragments/hooks/model-for.d.ts +20 -0
- package/declarations/model-fragments/index.d.ts +5 -0
- package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +9 -0
- package/declarations/model-fragments/utilities/with-array-defaults.d.ts +15 -0
- package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +20 -0
- package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +19 -0
- package/declarations/model-fragments/utilities/with-legacy.d.ts +3 -0
- package/declarations/model-fragments.d.ts +9 -0
- package/declarations/model.d.ts +2 -2
- package/declarations/serializer/-private/embedded-records-mixin.d.ts +1 -6
- package/declarations/serializer/-private/transforms/boolean.d.ts +2 -2
- package/declarations/serializer/-private/transforms/date.d.ts +2 -2
- package/declarations/serializer/-private/transforms/number.d.ts +1 -1
- package/declarations/serializer/-private/transforms/string.d.ts +1 -1
- package/declarations/serializer/json-api.d.ts +7 -9
- package/declarations/serializer/json.d.ts +7 -9
- package/declarations/serializer/rest.d.ts +4 -6
- package/declarations/serializer.d.ts +9 -12
- package/dist/{-private-8UmnAf9J.js → -private-B1pSSN52.js} +1 -1
- package/dist/adapter/-private.js +1 -1
- package/dist/adapter/error.js +12 -13
- package/dist/adapter/json-api.js +4 -1
- package/dist/adapter/rest.js +6 -9
- package/dist/adapter.js +4 -6
- package/dist/compat/-private.js +1 -1
- package/dist/compat/builders.js +26 -26
- package/dist/compat/utils.js +17 -18
- package/dist/compat.js +58 -41
- package/dist/{errors-8kD2mSe_.js → errors-CIGPcDvd.js} +87 -65
- package/dist/hooks-QqRnX108.js +74 -0
- package/dist/index.js +195 -0
- package/dist/{json-DziiodPf.js → json-BNrV8EYG.js} +12 -16
- package/dist/model/-private.js +1 -1
- package/dist/model/migration-support.js +54 -24
- package/dist/model-for-CqXsIKws.js +221 -0
- package/dist/model-fragments.js +76 -0
- package/dist/model.js +18 -90
- package/dist/{schema-provider-DQu4Rjco.js → schema-provider-g5MfTj8n.js} +18 -20
- package/dist/{serialize-into-hash-CS0MIv4F.js → serialize-into-hash-BnYvPex3.js} +1 -1
- package/dist/serializer/json-api.js +17 -44
- package/dist/serializer/json.js +1 -1
- package/dist/serializer/rest.js +14 -21
- package/dist/serializer/transform.js +15 -6
- package/dist/serializer.js +9 -13
- package/dist/store.js +3 -0
- package/dist/unpkg/dev/-private-DbaSCSym.js +1205 -0
- package/dist/unpkg/dev/adapter/-private.js +1 -0
- package/dist/unpkg/dev/adapter/error.js +335 -0
- package/dist/unpkg/dev/adapter/json-api.js +271 -0
- package/dist/unpkg/dev/adapter/rest.js +1255 -0
- package/dist/unpkg/dev/adapter.js +1252 -0
- package/dist/unpkg/dev/compat/-private.js +1 -0
- package/dist/unpkg/dev/compat/builders.js +275 -0
- package/dist/unpkg/dev/compat/extensions.js +242 -0
- package/dist/unpkg/dev/compat/utils.js +223 -0
- package/dist/unpkg/dev/compat.js +1146 -0
- package/dist/unpkg/dev/errors-DD96TBEs.js +2591 -0
- package/dist/unpkg/dev/hooks-CqWjNWeL.js +73 -0
- package/dist/unpkg/dev/index.js +197 -0
- package/dist/unpkg/dev/json-CCU-ZQ4b.js +1269 -0
- package/dist/unpkg/dev/model/-private.js +1 -0
- package/dist/unpkg/dev/model/migration-support.js +551 -0
- package/dist/unpkg/dev/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/dev/model-fragments.js +76 -0
- package/dist/unpkg/dev/model.js +678 -0
- package/dist/unpkg/dev/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/dev/schema-provider-B8jiJOYC.js +2229 -0
- package/dist/unpkg/dev/serialize-into-hash-CPAZXrQU.js +259 -0
- package/dist/unpkg/dev/serializer/json-api.js +648 -0
- package/dist/unpkg/dev/serializer/json.js +4 -0
- package/dist/unpkg/dev/serializer/rest.js +1242 -0
- package/dist/unpkg/dev/serializer/transform.js +278 -0
- package/dist/unpkg/dev/serializer.js +248 -0
- package/dist/unpkg/dev/store.js +636 -0
- package/dist/unpkg/dev/util-DvanW33H.js +20 -0
- package/dist/unpkg/dev/utils-BhvS1iTS.js +8 -0
- package/dist/unpkg/dev-deprecated/-private-DbaSCSym.js +1205 -0
- package/dist/unpkg/dev-deprecated/adapter/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/adapter/error.js +335 -0
- package/dist/unpkg/dev-deprecated/adapter/json-api.js +271 -0
- package/dist/unpkg/dev-deprecated/adapter/rest.js +1255 -0
- package/dist/unpkg/dev-deprecated/adapter.js +1252 -0
- package/dist/unpkg/dev-deprecated/compat/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/compat/builders.js +275 -0
- package/dist/unpkg/dev-deprecated/compat/extensions.js +242 -0
- package/dist/unpkg/dev-deprecated/compat/utils.js +223 -0
- package/dist/unpkg/dev-deprecated/compat.js +1146 -0
- package/dist/unpkg/dev-deprecated/errors-DEnabIZj.js +2594 -0
- package/dist/unpkg/dev-deprecated/hooks-CAll-Ets.js +73 -0
- package/dist/unpkg/dev-deprecated/index.js +196 -0
- package/dist/unpkg/dev-deprecated/json-CCU-ZQ4b.js +1269 -0
- package/dist/unpkg/dev-deprecated/model/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/model/migration-support.js +568 -0
- package/dist/unpkg/dev-deprecated/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/dev-deprecated/model-fragments.js +76 -0
- package/dist/unpkg/dev-deprecated/model.js +682 -0
- package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/dev-deprecated/schema-provider-k2qXQTBg.js +2254 -0
- package/dist/unpkg/dev-deprecated/serialize-into-hash-CPAZXrQU.js +259 -0
- package/dist/unpkg/dev-deprecated/serializer/json-api.js +648 -0
- package/dist/unpkg/dev-deprecated/serializer/json.js +4 -0
- package/dist/unpkg/dev-deprecated/serializer/rest.js +1242 -0
- package/dist/unpkg/dev-deprecated/serializer/transform.js +278 -0
- package/dist/unpkg/dev-deprecated/serializer.js +248 -0
- package/dist/unpkg/dev-deprecated/store.js +636 -0
- package/dist/unpkg/dev-deprecated/util-CWr5WQOT.js +24 -0
- package/dist/unpkg/dev-deprecated/utils-C9PJehtL.js +12 -0
- package/dist/unpkg/prod/-private-Cvf_97EG.js +970 -0
- package/dist/unpkg/prod/adapter/-private.js +1 -0
- package/dist/unpkg/prod/adapter/error.js +330 -0
- package/dist/unpkg/prod/adapter/json-api.js +266 -0
- package/dist/unpkg/prod/adapter/rest.js +1218 -0
- package/dist/unpkg/prod/adapter.js +1219 -0
- package/dist/unpkg/prod/compat/-private.js +1 -0
- package/dist/unpkg/prod/compat/builders.js +210 -0
- package/dist/unpkg/prod/compat/extensions.js +232 -0
- package/dist/unpkg/prod/compat/utils.js +218 -0
- package/dist/unpkg/prod/compat.js +726 -0
- package/dist/unpkg/prod/errors-CXnfnBfQ.js +2343 -0
- package/dist/unpkg/prod/hooks-DvyWhLNg.js +41 -0
- package/dist/unpkg/prod/index.js +151 -0
- package/dist/unpkg/prod/json-BYrUP8ao.js +1256 -0
- package/dist/unpkg/prod/model/-private.js +1 -0
- package/dist/unpkg/prod/model/migration-support.js +544 -0
- package/dist/unpkg/prod/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/prod/model-fragments.js +76 -0
- package/dist/unpkg/prod/model.js +593 -0
- package/dist/unpkg/prod/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/prod/schema-provider-CHujJvA9.js +1904 -0
- package/dist/unpkg/prod/serialize-into-hash-DYU2egXl.js +215 -0
- package/dist/unpkg/prod/serializer/json-api.js +591 -0
- package/dist/unpkg/prod/serializer/json.js +4 -0
- package/dist/unpkg/prod/serializer/rest.js +1210 -0
- package/dist/unpkg/prod/serializer/transform.js +278 -0
- package/dist/unpkg/prod/serializer.js +248 -0
- package/dist/unpkg/prod/store.js +504 -0
- package/dist/unpkg/prod/util-DvanW33H.js +20 -0
- package/dist/unpkg/prod/utils-BhvS1iTS.js +8 -0
- package/dist/unpkg/prod-deprecated/-private-Cvf_97EG.js +970 -0
- package/dist/unpkg/prod-deprecated/adapter/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/adapter/error.js +330 -0
- package/dist/unpkg/prod-deprecated/adapter/json-api.js +266 -0
- package/dist/unpkg/prod-deprecated/adapter/rest.js +1218 -0
- package/dist/unpkg/prod-deprecated/adapter.js +1219 -0
- package/dist/unpkg/prod-deprecated/compat/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/compat/builders.js +210 -0
- package/dist/unpkg/prod-deprecated/compat/extensions.js +232 -0
- package/dist/unpkg/prod-deprecated/compat/utils.js +218 -0
- package/dist/unpkg/prod-deprecated/compat.js +726 -0
- package/dist/unpkg/prod-deprecated/errors-CG1SPYVg.js +2346 -0
- package/dist/unpkg/prod-deprecated/hooks-BIUBiNGR.js +41 -0
- package/dist/unpkg/prod-deprecated/index.js +150 -0
- package/dist/unpkg/prod-deprecated/json-BYrUP8ao.js +1256 -0
- package/dist/unpkg/prod-deprecated/model/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/model/migration-support.js +561 -0
- package/dist/unpkg/prod-deprecated/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/prod-deprecated/model-fragments.js +76 -0
- package/dist/unpkg/prod-deprecated/model.js +596 -0
- package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/prod-deprecated/schema-provider-BJ4TWnZf.js +1947 -0
- package/dist/unpkg/prod-deprecated/serialize-into-hash-DYU2egXl.js +215 -0
- package/dist/unpkg/prod-deprecated/serializer/json-api.js +591 -0
- package/dist/unpkg/prod-deprecated/serializer/json.js +4 -0
- package/dist/unpkg/prod-deprecated/serializer/rest.js +1210 -0
- package/dist/unpkg/prod-deprecated/serializer/transform.js +278 -0
- package/dist/unpkg/prod-deprecated/serializer.js +248 -0
- package/dist/unpkg/prod-deprecated/store.js +504 -0
- package/dist/unpkg/prod-deprecated/util-B6cn-i93.js +23 -0
- package/dist/unpkg/prod-deprecated/utils-BUWwQwCh.js +11 -0
- package/logos/README.md +2 -2
- package/logos/logo-yellow-slab.svg +1 -0
- package/logos/word-mark-black.svg +1 -0
- package/logos/word-mark-white.svg +1 -0
- package/package.json +14 -6
- package/logos/NCC-1701-a-blue.svg +0 -4
- package/logos/NCC-1701-a-gold.svg +0 -4
- package/logos/NCC-1701-a-gold_100.svg +0 -1
- package/logos/NCC-1701-a-gold_base-64.txt +0 -1
- package/logos/NCC-1701-a.svg +0 -4
- package/logos/docs-badge.svg +0 -2
- package/logos/ember-data-logo-dark.svg +0 -12
- package/logos/ember-data-logo-light.svg +0 -12
- package/logos/social1.png +0 -0
- package/logos/social2.png +0 -0
- package/logos/warp-drive-logo-dark.svg +0 -4
- package/logos/warp-drive-logo-gold.svg +0 -4
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import EmberObject from '@ember/object';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
The `Transform` class is used to serialize and deserialize model
|
|
5
|
+
attributes when they are saved or loaded from an
|
|
6
|
+
adapter. Subclassing `Transform` is useful for creating custom
|
|
7
|
+
attributes. All subclasses of `Transform` must implement a
|
|
8
|
+
`serialize` and a `deserialize` method.
|
|
9
|
+
|
|
10
|
+
Example
|
|
11
|
+
|
|
12
|
+
```js [app/transforms/temperature.js]
|
|
13
|
+
|
|
14
|
+
// Converts centigrade in the JSON to fahrenheit in the app
|
|
15
|
+
export default class TemperatureTransform {
|
|
16
|
+
deserialize(serialized, options) {
|
|
17
|
+
return (serialized * 1.8) + 32;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
serialize(deserialized, options) {
|
|
21
|
+
return (deserialized - 32) / 1.8;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static create() {
|
|
25
|
+
return new this();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Usage
|
|
31
|
+
|
|
32
|
+
```js [app/models/requirement.js]
|
|
33
|
+
import { Model, attr } from '@warp-drive/legacy/model';
|
|
34
|
+
|
|
35
|
+
export default class RequirementModel extends Model {
|
|
36
|
+
@attr('string') name;
|
|
37
|
+
@attr('temperature') temperature;
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The options passed into the `attr` function when the attribute is
|
|
42
|
+
declared on the model is also available in the transform.
|
|
43
|
+
|
|
44
|
+
```js [app/models/post.js]
|
|
45
|
+
import { Model, attr } from '@warp-drive/legacy/model';
|
|
46
|
+
|
|
47
|
+
export default class PostModel extends Model {
|
|
48
|
+
@attr('string') title;
|
|
49
|
+
@attr('markdown', {
|
|
50
|
+
markdown: {
|
|
51
|
+
gfm: false,
|
|
52
|
+
sanitize: true
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
markdown;
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```js [app/transforms/markdown.js]
|
|
60
|
+
export default class MarkdownTransform {
|
|
61
|
+
serialize(deserialized, options) {
|
|
62
|
+
return deserialized.raw;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
deserialize(serialized, options) {
|
|
66
|
+
let markdownOptions = options.markdown || {};
|
|
67
|
+
|
|
68
|
+
return marked(serialized, markdownOptions);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static create() {
|
|
72
|
+
return new this();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
@class Transform
|
|
78
|
+
@public
|
|
79
|
+
*/
|
|
80
|
+
const Transform = EmberObject;
|
|
81
|
+
|
|
82
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
The `BooleanTransform` class is used to serialize and deserialize
|
|
86
|
+
boolean attributes on Ember Data record objects. This transform is
|
|
87
|
+
used when `'boolean'` is passed as the type parameter to the
|
|
88
|
+
{@link attr}function.
|
|
89
|
+
|
|
90
|
+
Usage
|
|
91
|
+
|
|
92
|
+
```js [app/models/user.js]
|
|
93
|
+
import { Model, attr } from '@warp-drive/legacy/model';
|
|
94
|
+
|
|
95
|
+
export default class UserModel extends Model {
|
|
96
|
+
@attr('boolean') isAdmin;
|
|
97
|
+
@attr('string') name;
|
|
98
|
+
@attr('string') email;
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
By default, the boolean transform only allows for values of `true` or
|
|
103
|
+
`false`. You can opt into allowing `null` values for
|
|
104
|
+
boolean attributes via `attr('boolean', { allowNull: true })`
|
|
105
|
+
|
|
106
|
+
```js [app/models/user.js]
|
|
107
|
+
import { Model, attr } from '@warp-drive/legacy/model';
|
|
108
|
+
|
|
109
|
+
export default class UserModel extends Model {
|
|
110
|
+
@attr('string') email;
|
|
111
|
+
@attr('string') username;
|
|
112
|
+
@attr('boolean', { allowNull: true }) wantsWeeklyEmail;
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
@public
|
|
117
|
+
*/
|
|
118
|
+
class BooleanTransform {
|
|
119
|
+
deserialize(serialized, options) {
|
|
120
|
+
if ((serialized === null || serialized === undefined) && options?.allowNull === true) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
if (typeof serialized === 'boolean') {
|
|
124
|
+
return serialized;
|
|
125
|
+
} else if (typeof serialized === 'string') {
|
|
126
|
+
return /^(true|t|1)$/i.test(serialized);
|
|
127
|
+
} else if (typeof serialized === 'number') {
|
|
128
|
+
return serialized === 1;
|
|
129
|
+
} else {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
serialize(deserialized, options) {
|
|
134
|
+
if ((deserialized === null || deserialized === undefined) && options?.allowNull === true) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
return Boolean(deserialized);
|
|
138
|
+
}
|
|
139
|
+
static create() {
|
|
140
|
+
return new this();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
The `DateTransform` class is used to serialize and deserialize
|
|
148
|
+
date attributes on Ember Data record objects. This transform is used
|
|
149
|
+
when `'date'` is passed as the type parameter to the
|
|
150
|
+
{@link attr} function. It uses the [`ISO 8601`](https://en.wikipedia.org/wiki/ISO_8601)
|
|
151
|
+
standard.
|
|
152
|
+
|
|
153
|
+
```js [app/models/score.js]
|
|
154
|
+
import Model, { attr, belongsTo } from '@ember-data/model';
|
|
155
|
+
|
|
156
|
+
export default class ScoreModel extends Model {
|
|
157
|
+
@attr('number') value;
|
|
158
|
+
@belongsTo('player') player;
|
|
159
|
+
@attr('date') date;
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
@public
|
|
164
|
+
*/
|
|
165
|
+
|
|
166
|
+
class DateTransform {
|
|
167
|
+
deserialize(serialized, _options) {
|
|
168
|
+
if (typeof serialized === 'string') {
|
|
169
|
+
let offset = serialized.indexOf('+');
|
|
170
|
+
if (offset !== -1 && serialized.length - 5 === offset) {
|
|
171
|
+
offset += 3;
|
|
172
|
+
return new Date(serialized.slice(0, offset) + ':' + serialized.slice(offset));
|
|
173
|
+
}
|
|
174
|
+
return new Date(serialized);
|
|
175
|
+
} else if (typeof serialized === 'number') {
|
|
176
|
+
return new Date(serialized);
|
|
177
|
+
} else if (serialized === null || serialized === undefined) {
|
|
178
|
+
// if the value is null return null
|
|
179
|
+
// if the value is not present in the data return undefined
|
|
180
|
+
return serialized;
|
|
181
|
+
} else {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
serialize(date, _options) {
|
|
186
|
+
// @ts-expect-error isNaN accepts date as it is coercible
|
|
187
|
+
if (date instanceof Date && !isNaN(date)) {
|
|
188
|
+
return date.toISOString();
|
|
189
|
+
} else {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
static create() {
|
|
194
|
+
return new this();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
199
|
+
|
|
200
|
+
function isNumber(value) {
|
|
201
|
+
return value === value && value !== Infinity && value !== -Infinity;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
The `NumberTransform` class is used to serialize and deserialize
|
|
205
|
+
numeric attributes on Ember Data record objects. This transform is
|
|
206
|
+
used when `number` is passed as the type parameter to the
|
|
207
|
+
{@link attr} function.
|
|
208
|
+
|
|
209
|
+
Usage
|
|
210
|
+
|
|
211
|
+
```js [app/models/score.js]
|
|
212
|
+
import Model, { attr, belongsTo } from '@ember-data/model';
|
|
213
|
+
|
|
214
|
+
export default class ScoreModel extends Model {
|
|
215
|
+
@attr('number') value;
|
|
216
|
+
@belongsTo('player') player;
|
|
217
|
+
@attr('date') date;
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
@public
|
|
222
|
+
*/
|
|
223
|
+
class NumberTransform {
|
|
224
|
+
deserialize(serialized, _options) {
|
|
225
|
+
if (serialized === '' || serialized === null || serialized === undefined) {
|
|
226
|
+
return null;
|
|
227
|
+
} else {
|
|
228
|
+
const transformed = Number(serialized);
|
|
229
|
+
return isNumber(transformed) ? transformed : null;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
serialize(deserialized, _options) {
|
|
233
|
+
if (deserialized === '' || deserialized === null || deserialized === undefined) {
|
|
234
|
+
return null;
|
|
235
|
+
} else {
|
|
236
|
+
const transformed = Number(deserialized);
|
|
237
|
+
return isNumber(transformed) ? transformed : null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
static create() {
|
|
241
|
+
return new this();
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
The `StringTransform` class is used to serialize and deserialize
|
|
249
|
+
string attributes on Ember Data record objects. This transform is
|
|
250
|
+
used when `string` is passed as the type parameter to the
|
|
251
|
+
{@link attr} function.
|
|
252
|
+
|
|
253
|
+
Usage
|
|
254
|
+
|
|
255
|
+
```js [app/models/user.js]
|
|
256
|
+
import Model, { attr, belongsTo } from '@ember-data/model';
|
|
257
|
+
|
|
258
|
+
export default class UserModel extends Model {
|
|
259
|
+
@attr('boolean') isAdmin;
|
|
260
|
+
@attr('string') name;
|
|
261
|
+
@attr('string') email;
|
|
262
|
+
}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
@public
|
|
266
|
+
*/
|
|
267
|
+
class StringTransform {
|
|
268
|
+
deserialize(serialized, _options) {
|
|
269
|
+
return !serialized && serialized !== '' ? null : String(serialized);
|
|
270
|
+
}
|
|
271
|
+
serialize(deserialized, _options) {
|
|
272
|
+
return !deserialized && deserialized !== '' ? null : String(deserialized);
|
|
273
|
+
}
|
|
274
|
+
static create() {
|
|
275
|
+
return new this();
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
export { BooleanTransform, DateTransform, NumberTransform, StringTransform, Transform };
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import EmberObject from '@ember/object';
|
|
2
|
+
import * as s from '@ember/service';
|
|
3
|
+
import { a as decorateFieldV2, i as initializeDeferredDecorator } from "./runtime-BPCpkOf1-BKOwiRJp.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
## Overview
|
|
7
|
+
|
|
8
|
+
:::danger
|
|
9
|
+
⚠️ **This is LEGACY documentation** for a feature that is no longer encouraged to be used.
|
|
10
|
+
If starting a new app or thinking of implementing a new serializer, consider writing a
|
|
11
|
+
{@link Handler} instead to be used with the {@link RequestManager}
|
|
12
|
+
:::
|
|
13
|
+
|
|
14
|
+
In order to properly manage and present your data, WarpDrive
|
|
15
|
+
needs to understand the structure of data it receives.
|
|
16
|
+
|
|
17
|
+
`Serializers` convert data between the server's API format and
|
|
18
|
+
the format WarpDrive understands.
|
|
19
|
+
|
|
20
|
+
Data received from an API response is **normalized** into
|
|
21
|
+
[{json:api}](https://jsonapi.org/) (the format used internally
|
|
22
|
+
by WarpDrive), while data sent to an API is **serialized**
|
|
23
|
+
into the format the API expects.
|
|
24
|
+
|
|
25
|
+
### Implementing a Serializer
|
|
26
|
+
|
|
27
|
+
There are only two required serializer methods, one for
|
|
28
|
+
normalizing data from the server API format into {json:api}, and
|
|
29
|
+
another for serializing records via `Snapshots` into the expected
|
|
30
|
+
server API format.
|
|
31
|
+
|
|
32
|
+
To implement a serializer, export a class that conforms to the structure
|
|
33
|
+
described by {@link MinimumSerializerInterface}
|
|
34
|
+
from the `app/serializers/` directory. An example is below.
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import EmberObject from '@ember/object';
|
|
38
|
+
|
|
39
|
+
export default class ApplicationSerializer extends EmberObject {
|
|
40
|
+
normalizeResponse(store, schema, rawPayload) {
|
|
41
|
+
return rawPayload;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
serialize(snapshot, options) {
|
|
45
|
+
const serializedResource = {
|
|
46
|
+
id: snapshot.id,
|
|
47
|
+
type: snapshot.modelName,
|
|
48
|
+
attributes: snapshot.attributes()
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return serializedResource;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
### Serializer Resolution
|
|
58
|
+
|
|
59
|
+
`store.serializerFor(name)` will lookup serializers defined in
|
|
60
|
+
`app/serializers/` and return an instance. If no serializer is found, an
|
|
61
|
+
error will be thrown.
|
|
62
|
+
|
|
63
|
+
`serializerFor` first attempts to find a serializer with an exact match on `name`,
|
|
64
|
+
then falls back to checking for the presence of a serializer named `application`.
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
store.serializerFor('author');
|
|
68
|
+
|
|
69
|
+
// lookup paths (in order) =>
|
|
70
|
+
// app/serializers/author.js
|
|
71
|
+
// app/serializers/application.js
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Most requests in `@warp-drive/legacy` are made with respect to a particular `type` (or `modelName`)
|
|
75
|
+
(e.g., "get me the full collection of **books**" or "get me the **employee** whose id is 37"). We
|
|
76
|
+
refer to this as the **primary** resource `type`.
|
|
77
|
+
|
|
78
|
+
Typically `serializerFor` will be used to find a serializer with a name matching that of the primary
|
|
79
|
+
resource `type` for the request, falling back to the `application` serializer for those types that
|
|
80
|
+
do not have a defined serializer. This is often described as a `per-model` or `per-type` strategy
|
|
81
|
+
for defining serializers. However, because APIs rarely format payloads per-type but rather
|
|
82
|
+
per-API-version, this may not be a desired strategy.
|
|
83
|
+
|
|
84
|
+
It is recommended that applications define only a single `application` adapter and serializer
|
|
85
|
+
where possible.
|
|
86
|
+
|
|
87
|
+
If you have multiple API formats and the per-type strategy is not viable, one strategy is to
|
|
88
|
+
write an `application` adapter and serializer that make use of `options` to specify the desired
|
|
89
|
+
format when making a request.
|
|
90
|
+
|
|
91
|
+
### Using a Serializer
|
|
92
|
+
|
|
93
|
+
Any serializer in `app/serializers/` can be looked up by `name` using `store.serializerFor(name)`.
|
|
94
|
+
|
|
95
|
+
### Default Serializers
|
|
96
|
+
|
|
97
|
+
For applications whose APIs are *very close to* or *exactly* the **REST** format or **JSON:API**
|
|
98
|
+
format the `@warp-drive/legacy/serializer` package contains implementations these applications can
|
|
99
|
+
extend. It also contains a simple `JSONSerializer` for serializing to/from very basic JSON objects.
|
|
100
|
+
|
|
101
|
+
Many applications will find writing their own serializer to be more performant and less
|
|
102
|
+
complex than extending these classes even when their API format is very close to that expected
|
|
103
|
+
by these serializers.
|
|
104
|
+
|
|
105
|
+
It is recommended that apps write their own serializer to best suit the needs of their API and
|
|
106
|
+
application.
|
|
107
|
+
|
|
108
|
+
@module
|
|
109
|
+
*/
|
|
110
|
+
const service = s.service ?? s.inject;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
> ⚠️ CAUTION you likely want the docs for {@link MinimumSerializerInterface}
|
|
114
|
+
> as extending this abstract class is unnecessary.
|
|
115
|
+
|
|
116
|
+
`Serializer` is an abstract base class that you may override in your
|
|
117
|
+
application to customize it for your backend. The minimum set of methods
|
|
118
|
+
that you should implement is:
|
|
119
|
+
|
|
120
|
+
* `normalizeResponse()`
|
|
121
|
+
* `serialize()`
|
|
122
|
+
|
|
123
|
+
And you can optionally override the following methods:
|
|
124
|
+
|
|
125
|
+
* `normalize()`
|
|
126
|
+
|
|
127
|
+
For an example implementation, see the included {@link JSONSerializer}.
|
|
128
|
+
|
|
129
|
+
@class Serializer
|
|
130
|
+
@public
|
|
131
|
+
*/
|
|
132
|
+
|
|
133
|
+
class Serializer extends EmberObject {
|
|
134
|
+
static {
|
|
135
|
+
decorateFieldV2(this.prototype, "store", [service]);
|
|
136
|
+
}
|
|
137
|
+
#store = (initializeDeferredDecorator(this, "store"), void 0);
|
|
138
|
+
/**
|
|
139
|
+
The `store` property is the application's `store` that contains
|
|
140
|
+
all records. It can be used to look up serializers for other model
|
|
141
|
+
types that may be nested inside the payload response.
|
|
142
|
+
Example:
|
|
143
|
+
```js
|
|
144
|
+
Serializer.extend({
|
|
145
|
+
extractRelationship(relationshipModelName, relationshipHash) {
|
|
146
|
+
let modelClass = this.store.modelFor(relationshipModelName);
|
|
147
|
+
let relationshipSerializer = this.store.serializerFor(relationshipModelName);
|
|
148
|
+
return relationshipSerializer.normalize(modelClass, relationshipHash);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
@property store
|
|
153
|
+
@type {Store}
|
|
154
|
+
@public
|
|
155
|
+
*/
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
The `normalizeResponse` method is used to normalize a payload from the
|
|
159
|
+
server to a JSON-API Document.
|
|
160
|
+
http://jsonapi.org/format/#document-structure
|
|
161
|
+
Example:
|
|
162
|
+
```js
|
|
163
|
+
Serializer.extend({
|
|
164
|
+
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
|
|
165
|
+
if (requestType === 'findRecord') {
|
|
166
|
+
return this.normalize(primaryModelClass, payload);
|
|
167
|
+
} else {
|
|
168
|
+
return payload.reduce(function(documentHash, item) {
|
|
169
|
+
let { data, included } = this.normalize(primaryModelClass, item);
|
|
170
|
+
documentHash.included.push(...included);
|
|
171
|
+
documentHash.data.push(data);
|
|
172
|
+
return documentHash;
|
|
173
|
+
}, { data: [], included: [] })
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
```
|
|
178
|
+
@since 1.13.0
|
|
179
|
+
@public
|
|
180
|
+
@param {Store} store
|
|
181
|
+
@param {Model} primaryModelClass
|
|
182
|
+
@param {Object} payload
|
|
183
|
+
@param {String|Number} id
|
|
184
|
+
@param {String} requestType
|
|
185
|
+
@return {Object} JSON-API Document
|
|
186
|
+
*/
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
The `serialize` method is used when a record is saved in order to convert
|
|
190
|
+
the record into the form that your external data source expects.
|
|
191
|
+
`serialize` takes an optional `options` hash with a single option:
|
|
192
|
+
- `includeId`: If this is `true`, `serialize` should include the ID
|
|
193
|
+
in the serialized object it builds.
|
|
194
|
+
Example:
|
|
195
|
+
```js
|
|
196
|
+
Serializer.extend({
|
|
197
|
+
serialize(snapshot, options) {
|
|
198
|
+
let json = {
|
|
199
|
+
id: snapshot.id
|
|
200
|
+
};
|
|
201
|
+
snapshot.eachAttribute((key, attribute) => {
|
|
202
|
+
json[key] = snapshot.attr(key);
|
|
203
|
+
});
|
|
204
|
+
snapshot.eachRelationship((key, relationship) => {
|
|
205
|
+
if (relationship.kind === 'belongsTo') {
|
|
206
|
+
json[key] = snapshot.belongsTo(key, { id: true });
|
|
207
|
+
} else if (relationship.kind === 'hasMany') {
|
|
208
|
+
json[key] = snapshot.hasMany(key, { ids: true });
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
return json;
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
```
|
|
215
|
+
@public
|
|
216
|
+
@param {Snapshot} snapshot
|
|
217
|
+
@param {Object} [options]
|
|
218
|
+
@return {Object}
|
|
219
|
+
*/
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
The `normalize` method is used to convert a payload received from your
|
|
223
|
+
external data source into the normalized form `store.push()` expects. You
|
|
224
|
+
should override this method, munge the hash and return the normalized
|
|
225
|
+
payload.
|
|
226
|
+
Example:
|
|
227
|
+
```js
|
|
228
|
+
Serializer.extend({
|
|
229
|
+
normalize(modelClass, resourceHash) {
|
|
230
|
+
let data = {
|
|
231
|
+
id: resourceHash.id,
|
|
232
|
+
type: modelClass.modelName,
|
|
233
|
+
attributes: resourceHash
|
|
234
|
+
};
|
|
235
|
+
return { data: data };
|
|
236
|
+
}
|
|
237
|
+
})
|
|
238
|
+
```
|
|
239
|
+
@public
|
|
240
|
+
@param {Model} typeClass
|
|
241
|
+
@param {Object} hash
|
|
242
|
+
@return {Object}
|
|
243
|
+
*/
|
|
244
|
+
normalize(_typeClass, hash) {
|
|
245
|
+
return hash;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
export { Serializer };
|