@warp-drive/legacy 5.8.0-beta.0 → 5.8.0-beta.2
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 +16 -26
- package/declarations/adapter/error.d.ts +7 -7
- package/declarations/adapter/json-api.d.ts +6 -8
- package/declarations/adapter/rest.d.ts +26 -112
- package/declarations/adapter.d.ts +6 -8
- 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 +102 -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 +18 -59
- package/declarations/model/-private/promise-many-array.d.ts +0 -18
- package/declarations/model/-private/record-state.d.ts +1 -1
- package/declarations/model/-private/references/belongs-to.d.ts +19 -29
- package/declarations/model/-private/references/has-many.d.ts +14 -16
- 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 +11 -12
- package/declarations/serializer/json.d.ts +9 -11
- package/declarations/serializer/rest.d.ts +4 -6
- package/declarations/serializer.d.ts +9 -12
- package/dist/{-private-8UmnAf9J.js → -private-BG3bMiKp.js} +3 -2
- package/dist/adapter/-private.js +1 -1
- package/dist/adapter/error.js +14 -15
- package/dist/adapter/json-api.js +4 -1
- package/dist/adapter/rest.js +38 -125
- package/dist/adapter.js +6 -8
- 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 +61 -43
- package/dist/{errors-8kD2mSe_.js → errors-Cz5KrzBk.js} +115 -122
- package/dist/hooks-D6diaM34.js +74 -0
- package/dist/index.js +195 -0
- package/dist/{json-Et4mt_LM.js → json-ChdEfB0X.js} +18 -35
- package/dist/model/-private.js +1 -1
- package/dist/model/migration-support.js +59 -27
- 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-DJCV_6AF.js} +50 -95
- package/dist/{serialize-into-hash-CS0MIv4F.js → serialize-into-hash-DPZYoF-i.js} +1 -1
- package/dist/serializer/json-api.js +18 -45
- 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 +5 -1
- package/dist/unpkg/dev/-private-DtjBbEgy.js +1206 -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 +1171 -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 +1147 -0
- package/dist/unpkg/dev/errors-DmGGJr3T.js +2562 -0
- package/dist/unpkg/dev/hooks-CkYiE6Ud.js +73 -0
- package/dist/unpkg/dev/index.js +197 -0
- package/dist/unpkg/dev/json-Cu1LNgmQ.js +1256 -0
- package/dist/unpkg/dev/model/-private.js +1 -0
- package/dist/unpkg/dev/model/migration-support.js +553 -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-DDVYxmUV.js +2186 -0
- package/dist/unpkg/dev/serialize-into-hash-B2xDbuo5.js +259 -0
- package/dist/unpkg/dev/serializer/json-api.js +649 -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 +637 -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-DtjBbEgy.js +1206 -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 +1171 -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 +1147 -0
- package/dist/unpkg/dev-deprecated/errors-Spt6ubMd.js +2565 -0
- package/dist/unpkg/dev-deprecated/hooks-DOXegvhL.js +73 -0
- package/dist/unpkg/dev-deprecated/index.js +196 -0
- package/dist/unpkg/dev-deprecated/json-Cu1LNgmQ.js +1256 -0
- package/dist/unpkg/dev-deprecated/model/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/model/migration-support.js +570 -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-BP6_8N-V.js +2211 -0
- package/dist/unpkg/dev-deprecated/serialize-into-hash-B2xDbuo5.js +259 -0
- package/dist/unpkg/dev-deprecated/serializer/json-api.js +649 -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 +637 -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-BdyZaGEh.js +971 -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 +1134 -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 +727 -0
- package/dist/unpkg/prod/errors-BGVFCBmi.js +2314 -0
- package/dist/unpkg/prod/hooks-BztVA_x0.js +41 -0
- package/dist/unpkg/prod/index.js +151 -0
- package/dist/unpkg/prod/json-BWrZ5546.js +1243 -0
- package/dist/unpkg/prod/model/-private.js +1 -0
- package/dist/unpkg/prod/model/migration-support.js +546 -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-DJtD_8jZ.js +1861 -0
- package/dist/unpkg/prod/serialize-into-hash-DGlzQteF.js +215 -0
- package/dist/unpkg/prod/serializer/json-api.js +592 -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 +505 -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-BdyZaGEh.js +971 -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 +1134 -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 +727 -0
- package/dist/unpkg/prod-deprecated/errors-CdDaK81x.js +2317 -0
- package/dist/unpkg/prod-deprecated/hooks-yId87yyG.js +41 -0
- package/dist/unpkg/prod-deprecated/index.js +150 -0
- package/dist/unpkg/prod-deprecated/json-BWrZ5546.js +1243 -0
- package/dist/unpkg/prod-deprecated/model/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/model/migration-support.js +563 -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-CjX55uSY.js +1904 -0
- package/dist/unpkg/prod-deprecated/serialize-into-hash-DGlzQteF.js +215 -0
- package/dist/unpkg/prod-deprecated/serializer/json-api.js +592 -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 +505 -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 +15 -7
- 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,592 @@
|
|
|
1
|
+
import { dasherize, pluralize, singularize } from '@warp-drive/utilities/string';
|
|
2
|
+
import { J as JSONSerializer } from "../json-BWrZ5546.js";
|
|
3
|
+
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* :::danger
|
|
12
|
+
⚠️ <strong>This is LEGACY documentation</strong> for a feature that is no longer encouraged to be used.
|
|
13
|
+
If starting a new app or thinking of implementing a new adapter, consider writing a
|
|
14
|
+
{@link Handler} instead to be used with the {@link RequestManager}
|
|
15
|
+
:::
|
|
16
|
+
|
|
17
|
+
In WarpDrive a Serializer is used to serialize and deserialize
|
|
18
|
+
records when they are transferred in and out of an external source.
|
|
19
|
+
This process involves normalizing property names, transforming
|
|
20
|
+
attribute values and serializing relationships.
|
|
21
|
+
|
|
22
|
+
`JSONAPISerializer` supports the http://jsonapi.org/ spec, though
|
|
23
|
+
even compliant {json:api} servers may find writing an application
|
|
24
|
+
specific serializer better suited to their needs and more performant.
|
|
25
|
+
|
|
26
|
+
This serializer normalizes a JSON API payload that looks like:
|
|
27
|
+
|
|
28
|
+
```js [app/models/player.js]
|
|
29
|
+
import Model, { attr, belongsTo } from '@warp-drive/legacy/model';
|
|
30
|
+
|
|
31
|
+
export default class Player extends Model {
|
|
32
|
+
@attr('string') name;
|
|
33
|
+
@attr('string') skill;
|
|
34
|
+
@attr('number') gamesPlayed;
|
|
35
|
+
@belongsTo('club') club;
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```js [app/models/club.js]
|
|
40
|
+
import Model, { attr, hasMany } from '@warp-drive/legacy/model';
|
|
41
|
+
|
|
42
|
+
export default class Club extends Model {
|
|
43
|
+
@attr('string') name;
|
|
44
|
+
@attr('string') location;
|
|
45
|
+
@hasMany('player') players;
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
{
|
|
51
|
+
"data": [
|
|
52
|
+
{
|
|
53
|
+
"attributes": {
|
|
54
|
+
"name": "Benfica",
|
|
55
|
+
"location": "Portugal"
|
|
56
|
+
},
|
|
57
|
+
"id": "1",
|
|
58
|
+
"relationships": {
|
|
59
|
+
"players": {
|
|
60
|
+
"data": [
|
|
61
|
+
{
|
|
62
|
+
"id": "3",
|
|
63
|
+
"type": "players"
|
|
64
|
+
}
|
|
65
|
+
]
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"type": "clubs"
|
|
69
|
+
}
|
|
70
|
+
],
|
|
71
|
+
"included": [
|
|
72
|
+
{
|
|
73
|
+
"attributes": {
|
|
74
|
+
"name": "Eusebio Silva Ferreira",
|
|
75
|
+
"skill": "Rocket shot",
|
|
76
|
+
"games-played": 431
|
|
77
|
+
},
|
|
78
|
+
"id": "3",
|
|
79
|
+
"relationships": {
|
|
80
|
+
"club": {
|
|
81
|
+
"data": {
|
|
82
|
+
"id": "1",
|
|
83
|
+
"type": "clubs"
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
"type": "players"
|
|
88
|
+
}
|
|
89
|
+
]
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
to the format that the JSONAPICache expects.
|
|
94
|
+
|
|
95
|
+
### Customizing meta
|
|
96
|
+
|
|
97
|
+
Since a JSON API Document can have meta defined in multiple locations you can
|
|
98
|
+
use the specific serializer hooks if you need to customize the meta.
|
|
99
|
+
|
|
100
|
+
One scenario would be to camelCase the meta keys of your payload. The example
|
|
101
|
+
below shows how this could be done using `normalizeArrayResponse` and
|
|
102
|
+
`extractRelationship`.
|
|
103
|
+
|
|
104
|
+
```js [app/serializers/application.js]
|
|
105
|
+
import JSONAPISerializer from '@warp-drive/legacy/serializer/json-api';
|
|
106
|
+
|
|
107
|
+
export default class ApplicationSerializer extends JSONAPISerializer {
|
|
108
|
+
normalizeArrayResponse(store, primaryModelClass, payload, id, requestType) {
|
|
109
|
+
let normalizedDocument = super.normalizeArrayResponse(...arguments);
|
|
110
|
+
|
|
111
|
+
// Customize document meta
|
|
112
|
+
normalizedDocument.meta = camelCaseKeys(normalizedDocument.meta);
|
|
113
|
+
|
|
114
|
+
return normalizedDocument;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
extractRelationship(relationshipHash) {
|
|
118
|
+
let normalizedRelationship = super.extractRelationship(...arguments);
|
|
119
|
+
|
|
120
|
+
// Customize relationship meta
|
|
121
|
+
normalizedRelationship.meta = camelCaseKeys(normalizedRelationship.meta);
|
|
122
|
+
|
|
123
|
+
return normalizedRelationship;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
@since 1.13.0
|
|
129
|
+
@class JSONAPISerializer
|
|
130
|
+
@public
|
|
131
|
+
*/
|
|
132
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
133
|
+
const JSONAPISerializer = JSONSerializer.extend({
|
|
134
|
+
/**
|
|
135
|
+
@private
|
|
136
|
+
*/
|
|
137
|
+
_normalizeDocumentHelper(documentHash) {
|
|
138
|
+
if (Array.isArray(documentHash.data)) {
|
|
139
|
+
const ret = new Array(documentHash.data.length);
|
|
140
|
+
for (let i = 0; i < documentHash.data.length; i++) {
|
|
141
|
+
const data = documentHash.data[i];
|
|
142
|
+
ret[i] = this._normalizeResourceHelper(data);
|
|
143
|
+
}
|
|
144
|
+
documentHash.data = ret;
|
|
145
|
+
} else if (documentHash.data && typeof documentHash.data === 'object') {
|
|
146
|
+
documentHash.data = this._normalizeResourceHelper(documentHash.data);
|
|
147
|
+
}
|
|
148
|
+
if (Array.isArray(documentHash.included)) {
|
|
149
|
+
const ret = [];
|
|
150
|
+
for (let i = 0; i < documentHash.included.length; i++) {
|
|
151
|
+
const included = documentHash.included[i];
|
|
152
|
+
const normalized = this._normalizeResourceHelper(included);
|
|
153
|
+
if (normalized !== null) {
|
|
154
|
+
// @ts-expect-error untyped
|
|
155
|
+
// can be null when unknown type is encountered
|
|
156
|
+
ret.push(normalized);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
documentHash.included = ret;
|
|
160
|
+
}
|
|
161
|
+
return documentHash;
|
|
162
|
+
},
|
|
163
|
+
/**
|
|
164
|
+
@private
|
|
165
|
+
*/
|
|
166
|
+
_normalizeRelationshipDataHelper(relationshipDataHash) {
|
|
167
|
+
relationshipDataHash.type = this.modelNameFromPayloadKey(relationshipDataHash.type);
|
|
168
|
+
return relationshipDataHash;
|
|
169
|
+
},
|
|
170
|
+
/**
|
|
171
|
+
@private
|
|
172
|
+
*/
|
|
173
|
+
_normalizeResourceHelper(resourceHash) {
|
|
174
|
+
const type = this.modelNameFromPayloadKey(resourceHash.type);
|
|
175
|
+
|
|
176
|
+
// @ts-expect-error store is dynamically added
|
|
177
|
+
if (!this.store.schema.hasResource({
|
|
178
|
+
type
|
|
179
|
+
})) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// @ts-expect-error store is dynamically added
|
|
184
|
+
const modelClass = this.store.modelFor(type);
|
|
185
|
+
// @ts-expect-error store is dynamically added
|
|
186
|
+
const serializer = this.store.serializerFor(type);
|
|
187
|
+
const {
|
|
188
|
+
data
|
|
189
|
+
} = serializer.normalize(modelClass, resourceHash);
|
|
190
|
+
return data;
|
|
191
|
+
},
|
|
192
|
+
/**
|
|
193
|
+
Normalize some data and push it into the store.
|
|
194
|
+
@public
|
|
195
|
+
*/
|
|
196
|
+
pushPayload(store, payload) {
|
|
197
|
+
const normalizedPayload = this._normalizeDocumentHelper(payload);
|
|
198
|
+
store.push(normalizedPayload);
|
|
199
|
+
},
|
|
200
|
+
/**
|
|
201
|
+
@return A {json:api} Document
|
|
202
|
+
@private
|
|
203
|
+
*/
|
|
204
|
+
_normalizeResponse(store, primaryModelClass, payload, id, requestType, isSingle) {
|
|
205
|
+
const normalizedPayload = this._normalizeDocumentHelper(payload);
|
|
206
|
+
return normalizedPayload;
|
|
207
|
+
},
|
|
208
|
+
normalizeQueryRecordResponse() {
|
|
209
|
+
// @ts-expect-error untyped
|
|
210
|
+
const normalized = this._super(...arguments);
|
|
211
|
+
return normalized;
|
|
212
|
+
},
|
|
213
|
+
extractAttributes(modelClass, resourceHash) {
|
|
214
|
+
const attributes = {};
|
|
215
|
+
if (resourceHash.attributes) {
|
|
216
|
+
modelClass.eachAttribute(key => {
|
|
217
|
+
const attributeKey = this.keyForAttribute(key, 'deserialize');
|
|
218
|
+
if (resourceHash.attributes[attributeKey] !== undefined) {
|
|
219
|
+
attributes[key] = resourceHash.attributes[attributeKey];
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
return attributes;
|
|
224
|
+
},
|
|
225
|
+
/**
|
|
226
|
+
Returns a relationship formatted as a JSON-API "relationship object".
|
|
227
|
+
http://jsonapi.org/format/#document-resource-object-relationships
|
|
228
|
+
@public
|
|
229
|
+
*/
|
|
230
|
+
extractRelationship(relationshipHash) {
|
|
231
|
+
if (Array.isArray(relationshipHash.data)) {
|
|
232
|
+
const ret = new Array(relationshipHash.data.length);
|
|
233
|
+
for (let i = 0; i < relationshipHash.data.length; i++) {
|
|
234
|
+
const data = relationshipHash.data[i];
|
|
235
|
+
ret[i] = this._normalizeRelationshipDataHelper(data);
|
|
236
|
+
}
|
|
237
|
+
relationshipHash.data = ret;
|
|
238
|
+
} else if (relationshipHash.data && typeof relationshipHash.data === 'object') {
|
|
239
|
+
relationshipHash.data = this._normalizeRelationshipDataHelper(relationshipHash.data);
|
|
240
|
+
}
|
|
241
|
+
return relationshipHash;
|
|
242
|
+
},
|
|
243
|
+
/**
|
|
244
|
+
Returns the resource's relationships formatted as a JSON-API "relationships object".
|
|
245
|
+
http://jsonapi.org/format/#document-resource-object-relationships
|
|
246
|
+
@public
|
|
247
|
+
*/
|
|
248
|
+
extractRelationships(modelClass, resourceHash) {
|
|
249
|
+
const relationships = {};
|
|
250
|
+
if (resourceHash.relationships) {
|
|
251
|
+
modelClass.eachRelationship((key, relationshipMeta) => {
|
|
252
|
+
const relationshipKey = this.keyForRelationship(key, relationshipMeta.kind, 'deserialize');
|
|
253
|
+
if (resourceHash.relationships[relationshipKey] !== undefined) {
|
|
254
|
+
const relationshipHash = resourceHash.relationships[relationshipKey];
|
|
255
|
+
relationships[key] = this.extractRelationship(relationshipHash);
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
return relationships;
|
|
260
|
+
},
|
|
261
|
+
/**
|
|
262
|
+
@private
|
|
263
|
+
*/
|
|
264
|
+
_extractType(modelClass, resourceHash) {
|
|
265
|
+
return this.modelNameFromPayloadKey(resourceHash.type);
|
|
266
|
+
},
|
|
267
|
+
/**
|
|
268
|
+
Dasherizes and singularizes the model name in the payload to match
|
|
269
|
+
the common convention for model names.
|
|
270
|
+
For example the key `posts` would be converted to `post` and the
|
|
271
|
+
key `studentAssesments` would be converted to `student-assesment`.
|
|
272
|
+
@public
|
|
273
|
+
@return the model's modelName
|
|
274
|
+
*/
|
|
275
|
+
modelNameFromPayloadKey(key) {
|
|
276
|
+
return dasherize(singularize(key));
|
|
277
|
+
},
|
|
278
|
+
/**
|
|
279
|
+
Converts the model name to a pluralized version of the model name.
|
|
280
|
+
For example `post` would be converted to `posts` and
|
|
281
|
+
`student-assesment` would be converted to `student-assesments`.
|
|
282
|
+
@public
|
|
283
|
+
*/
|
|
284
|
+
payloadKeyFromModelName(modelName) {
|
|
285
|
+
return pluralize(modelName);
|
|
286
|
+
},
|
|
287
|
+
normalize(modelClass, resourceHash) {
|
|
288
|
+
if (resourceHash.attributes) {
|
|
289
|
+
// @ts-expect-error untyped
|
|
290
|
+
this.normalizeUsingDeclaredMapping(modelClass, resourceHash.attributes);
|
|
291
|
+
}
|
|
292
|
+
if (resourceHash.relationships) {
|
|
293
|
+
// @ts-expect-error untyped
|
|
294
|
+
this.normalizeUsingDeclaredMapping(modelClass, resourceHash.relationships);
|
|
295
|
+
}
|
|
296
|
+
const data = {
|
|
297
|
+
// @ts-expect-error untyped
|
|
298
|
+
id: this.extractId(modelClass, resourceHash),
|
|
299
|
+
type: this._extractType(modelClass, resourceHash),
|
|
300
|
+
attributes: this.extractAttributes(modelClass, resourceHash),
|
|
301
|
+
relationships: this.extractRelationships(modelClass, resourceHash)
|
|
302
|
+
};
|
|
303
|
+
if (resourceHash.lid) {
|
|
304
|
+
// @ts-expect-error untyped
|
|
305
|
+
data.lid = resourceHash.lid;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// @ts-expect-error untyped
|
|
309
|
+
this.applyTransforms(modelClass, data.attributes);
|
|
310
|
+
return {
|
|
311
|
+
data
|
|
312
|
+
};
|
|
313
|
+
},
|
|
314
|
+
/**
|
|
315
|
+
`keyForAttribute` can be used to define rules for how to convert an
|
|
316
|
+
attribute name in your model to a key in your JSON.
|
|
317
|
+
By default `JSONAPISerializer` follows the format used on the examples of
|
|
318
|
+
http://jsonapi.org/format and uses dashes as the word separator in the JSON
|
|
319
|
+
attribute keys.
|
|
320
|
+
This behaviour can be easily customized by extending this method.
|
|
321
|
+
Example
|
|
322
|
+
```js [app/serializers/application.js]
|
|
323
|
+
import JSONAPISerializer from '@warp-drive/legacy/serializer/json-api';
|
|
324
|
+
import { dasherize } from '<app-name>/utils/string-utils';
|
|
325
|
+
export default class ApplicationSerializer extends JSONAPISerializer {
|
|
326
|
+
keyForAttribute(attr, method) {
|
|
327
|
+
return dasherize(attr).toUpperCase();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
@public
|
|
332
|
+
@return normalized key
|
|
333
|
+
*/
|
|
334
|
+
keyForAttribute(key, method) {
|
|
335
|
+
return dasherize(key);
|
|
336
|
+
},
|
|
337
|
+
/**
|
|
338
|
+
`keyForRelationship` can be used to define a custom key when
|
|
339
|
+
serializing and deserializing relationship properties.
|
|
340
|
+
By default `JSONAPISerializer` follows the format used on the examples of
|
|
341
|
+
http://jsonapi.org/format and uses dashes as word separators in
|
|
342
|
+
relationship properties.
|
|
343
|
+
This behaviour can be easily customized by extending this method.
|
|
344
|
+
Example
|
|
345
|
+
```js [app/serializers/post.js]
|
|
346
|
+
import JSONAPISerializer from '@warp-drive/legacy/serializer/json-api';
|
|
347
|
+
import { underscore } from '<app-name>/utils/string-utils';
|
|
348
|
+
export default class ApplicationSerializer extends JSONAPISerializer {
|
|
349
|
+
keyForRelationship(key, relationship, method) {
|
|
350
|
+
return underscore(key);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
```
|
|
354
|
+
@public
|
|
355
|
+
@return the normalized key
|
|
356
|
+
*/
|
|
357
|
+
keyForRelationship(key, typeClass, method) {
|
|
358
|
+
return dasherize(key);
|
|
359
|
+
},
|
|
360
|
+
/**
|
|
361
|
+
Called when a record is saved in order to convert the
|
|
362
|
+
record into JSON.
|
|
363
|
+
For example, consider this model:
|
|
364
|
+
```js [app/models/comment.js]
|
|
365
|
+
import Model, { attr, belongsTo } from '@warp-drive/legacy/model';
|
|
366
|
+
export default class CommentModel extends Model {
|
|
367
|
+
@attr title;
|
|
368
|
+
@attr body;
|
|
369
|
+
@belongsTo('user', { async: false, inverse: null })
|
|
370
|
+
author;
|
|
371
|
+
}
|
|
372
|
+
```
|
|
373
|
+
The default serialization would create a JSON-API resource object like:
|
|
374
|
+
```javascript
|
|
375
|
+
{
|
|
376
|
+
"data": {
|
|
377
|
+
"type": "comments",
|
|
378
|
+
"attributes": {
|
|
379
|
+
"title": "Rails is unagi",
|
|
380
|
+
"body": "Rails? Omakase? O_O",
|
|
381
|
+
},
|
|
382
|
+
"relationships": {
|
|
383
|
+
"author": {
|
|
384
|
+
"data": {
|
|
385
|
+
"id": "12",
|
|
386
|
+
"type": "users"
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
```
|
|
393
|
+
By default, attributes are passed through as-is, unless
|
|
394
|
+
you specified an attribute type (`attr('date')`). If
|
|
395
|
+
you specify a transform, the JavaScript value will be
|
|
396
|
+
serialized when inserted into the attributes hash.
|
|
397
|
+
Belongs-to relationships are converted into JSON-API
|
|
398
|
+
resource identifier objects.
|
|
399
|
+
## IDs
|
|
400
|
+
`serialize` takes an options hash with a single option:
|
|
401
|
+
`includeId`. If this option is `true`, `serialize` will,
|
|
402
|
+
by default include the ID in the JSON object it builds.
|
|
403
|
+
The JSONAPIAdapter passes in `includeId: true` when serializing a record
|
|
404
|
+
for `createRecord` or `updateRecord`.
|
|
405
|
+
## Customization
|
|
406
|
+
Your server may expect data in a different format than the
|
|
407
|
+
built-in serialization format.
|
|
408
|
+
In that case, you can implement `serialize` yourself and
|
|
409
|
+
return data formatted to match your API's expectations, or override
|
|
410
|
+
the invoked adapter method and do the serialization in the adapter directly
|
|
411
|
+
by using the provided snapshot.
|
|
412
|
+
If your API's format differs greatly from the JSON:API spec, you should
|
|
413
|
+
consider authoring your own adapter and serializer instead of extending
|
|
414
|
+
this class.
|
|
415
|
+
```js [app/serializers/post.js]
|
|
416
|
+
import JSONAPISerializer from '@warp-drive/legacy/serializer/json-api';
|
|
417
|
+
export default class PostSerializer extends JSONAPISerializer {
|
|
418
|
+
serialize(snapshot, options) {
|
|
419
|
+
let json = {
|
|
420
|
+
POST_TTL: snapshot.attr('title'),
|
|
421
|
+
POST_BDY: snapshot.attr('body'),
|
|
422
|
+
POST_CMS: snapshot.hasMany('comments', { ids: true })
|
|
423
|
+
};
|
|
424
|
+
if (options.includeId) {
|
|
425
|
+
json.POST_ID_ = snapshot.id;
|
|
426
|
+
}
|
|
427
|
+
return json;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
```
|
|
431
|
+
## Customizing an App-Wide Serializer
|
|
432
|
+
If you want to define a serializer for your entire
|
|
433
|
+
application, you'll probably want to use `eachAttribute`
|
|
434
|
+
and `eachRelationship` on the record.
|
|
435
|
+
```js [app/serializers/application.js]
|
|
436
|
+
import JSONAPISerializer from '@warp-drive/legacy/serializer/json-api';
|
|
437
|
+
import { underscore, singularize } from '<app-name>/utils/string-utils';
|
|
438
|
+
export default class ApplicationSerializer extends JSONAPISerializer {
|
|
439
|
+
serialize(snapshot, options) {
|
|
440
|
+
let json = {};
|
|
441
|
+
snapshot.eachAttribute((name) => {
|
|
442
|
+
json[serverAttributeName(name)] = snapshot.attr(name);
|
|
443
|
+
});
|
|
444
|
+
snapshot.eachRelationship((name, relationship) => {
|
|
445
|
+
if (relationship.kind === 'hasMany') {
|
|
446
|
+
json[serverHasManyName(name)] = snapshot.hasMany(name, { ids: true });
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
if (options.includeId) {
|
|
450
|
+
json.ID_ = snapshot.id;
|
|
451
|
+
}
|
|
452
|
+
return json;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
function serverAttributeName(attribute) {
|
|
456
|
+
return underscore(attribute).toUpperCase();
|
|
457
|
+
}
|
|
458
|
+
function serverHasManyName(name) {
|
|
459
|
+
return serverAttributeName(singularize(name)) + '_IDS';
|
|
460
|
+
}
|
|
461
|
+
```
|
|
462
|
+
This serializer will generate JSON that looks like this:
|
|
463
|
+
```javascript
|
|
464
|
+
{
|
|
465
|
+
"TITLE": "Rails is omakase",
|
|
466
|
+
"BODY": "Yep. Omakase.",
|
|
467
|
+
"COMMENT_IDS": [ "1", "2", "3" ]
|
|
468
|
+
}
|
|
469
|
+
```
|
|
470
|
+
## Tweaking the Default Formatting
|
|
471
|
+
If you just want to do some small tweaks on the default JSON:API formatted response,
|
|
472
|
+
you can call `super.serialize` first and make the tweaks
|
|
473
|
+
on the returned object.
|
|
474
|
+
```js [app/serializers/post.js]
|
|
475
|
+
import JSONAPISerializer from '@warp-drive/legacy/serializer/json-api';
|
|
476
|
+
export default class PostSerializer extends JSONAPISerializer {
|
|
477
|
+
serialize(snapshot, options) {
|
|
478
|
+
let json = super.serialize(...arguments);
|
|
479
|
+
json.data.attributes.subject = json.data.attributes.title;
|
|
480
|
+
delete json.data.attributes.title;
|
|
481
|
+
return json;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
```
|
|
485
|
+
@public
|
|
486
|
+
*/
|
|
487
|
+
serialize(snapshot, options) {
|
|
488
|
+
// @ts-expect-error untyped
|
|
489
|
+
const data = this._super(...arguments);
|
|
490
|
+
data.type = this.payloadKeyFromModelName(snapshot.modelName);
|
|
491
|
+
return {
|
|
492
|
+
data
|
|
493
|
+
};
|
|
494
|
+
},
|
|
495
|
+
serializeAttribute(snapshot, json, key, attribute) {
|
|
496
|
+
const type = attribute.type;
|
|
497
|
+
|
|
498
|
+
// @ts-expect-error untyped
|
|
499
|
+
if (this._canSerialize(key)) {
|
|
500
|
+
json.attributes = json.attributes || {};
|
|
501
|
+
let value = snapshot.attr(key);
|
|
502
|
+
if (type) {
|
|
503
|
+
// @ts-expect-error untyped
|
|
504
|
+
const transform = this.transformFor(type);
|
|
505
|
+
value = transform.serialize(value, attribute.options);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// @ts-expect-error store is dynamically added
|
|
509
|
+
const schema = this.store.modelFor(snapshot.modelName);
|
|
510
|
+
// @ts-expect-error untyped
|
|
511
|
+
let payloadKey = this._getMappedKey(key, schema);
|
|
512
|
+
if (payloadKey === key) {
|
|
513
|
+
payloadKey = this.keyForAttribute(key, 'serialize');
|
|
514
|
+
}
|
|
515
|
+
json.attributes[payloadKey] = value;
|
|
516
|
+
}
|
|
517
|
+
},
|
|
518
|
+
serializeBelongsTo(snapshot, json, relationship) {
|
|
519
|
+
const name = relationship.name;
|
|
520
|
+
|
|
521
|
+
// @ts-expect-error untyped
|
|
522
|
+
if (this._canSerialize(name)) {
|
|
523
|
+
const belongsTo = snapshot.belongsTo(name);
|
|
524
|
+
// @ts-expect-error not narrowed
|
|
525
|
+
const belongsToIsNotNew = belongsTo && !belongsTo.isNew;
|
|
526
|
+
if (belongsTo === null || belongsToIsNotNew) {
|
|
527
|
+
json.relationships = json.relationships || {};
|
|
528
|
+
|
|
529
|
+
// @ts-expect-error store is dynamically added
|
|
530
|
+
const schema = this.store.modelFor(snapshot.modelName);
|
|
531
|
+
// @ts-expect-error untyped
|
|
532
|
+
let payloadKey = this._getMappedKey(name, schema);
|
|
533
|
+
if (payloadKey === name) {
|
|
534
|
+
payloadKey = this.keyForRelationship(name, 'belongsTo', 'serialize');
|
|
535
|
+
}
|
|
536
|
+
let data = null;
|
|
537
|
+
if (belongsTo) {
|
|
538
|
+
// @ts-expect-error not narrowed
|
|
539
|
+
const payloadType = this.payloadKeyFromModelName(belongsTo.modelName);
|
|
540
|
+
|
|
541
|
+
// @ts-expect-error untyped
|
|
542
|
+
data = {
|
|
543
|
+
type: payloadType,
|
|
544
|
+
// @ts-expect-error not narrowed
|
|
545
|
+
id: belongsTo.id
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
json.relationships[payloadKey] = {
|
|
549
|
+
data
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
},
|
|
554
|
+
serializeHasMany(snapshot, json, relationship) {
|
|
555
|
+
const name = relationship.name;
|
|
556
|
+
|
|
557
|
+
// @ts-expect-error untyped
|
|
558
|
+
if (this.shouldSerializeHasMany(snapshot, name, relationship)) {
|
|
559
|
+
const hasMany = snapshot.hasMany(name);
|
|
560
|
+
if (hasMany !== undefined) {
|
|
561
|
+
json.relationships = json.relationships || {};
|
|
562
|
+
|
|
563
|
+
// @ts-expect-error store is dynamically added
|
|
564
|
+
const schema = this.store.modelFor(snapshot.modelName);
|
|
565
|
+
// @ts-expect-error untyped
|
|
566
|
+
let payloadKey = this._getMappedKey(name, schema);
|
|
567
|
+
if (payloadKey === name && this.keyForRelationship) {
|
|
568
|
+
payloadKey = this.keyForRelationship(name, 'hasMany', 'serialize');
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// only serialize has many relationships that are not new
|
|
572
|
+
// @ts-expect-error not narrowed
|
|
573
|
+
const nonNewHasMany = hasMany.filter(item => !item.isNew);
|
|
574
|
+
const data = new Array(nonNewHasMany.length);
|
|
575
|
+
for (let i = 0; i < nonNewHasMany.length; i++) {
|
|
576
|
+
const item = hasMany[i];
|
|
577
|
+
// @ts-expect-error not narrowed
|
|
578
|
+
const payloadType = this.payloadKeyFromModelName(item.modelName);
|
|
579
|
+
data[i] = {
|
|
580
|
+
type: payloadType,
|
|
581
|
+
// @ts-expect-error not narrowed
|
|
582
|
+
id: item.id
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
json.relationships[payloadKey] = {
|
|
586
|
+
data
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
export { JSONAPISerializer };
|