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