@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,648 @@
1
+ import { dasherize, pluralize, singularize } from '@warp-drive/utilities/string';
2
+ import { J as JSONSerializer } from "../json-CCU-ZQ4b.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
+ // @ts-expect-error untyped
174
+ (test => {
175
+ if (!test) {
176
+ throw new Error(this.warnMessageForUndefinedType());
177
+ }
178
+ })(resourceHash.type);
179
+ const type = this.modelNameFromPayloadKey(resourceHash.type);
180
+
181
+ // @ts-expect-error store is dynamically added
182
+ if (!this.store.schema.hasResource({
183
+ type
184
+ })) {
185
+ {
186
+ // @ts-expect-error untyped
187
+ console.warn(this.warnMessageNoModelForType(type, resourceHash.type, 'modelNameFromPayloadKey'), false, {
188
+ id: 'ds.serializer.model-for-type-missing'
189
+ });
190
+ }
191
+ return null;
192
+ }
193
+
194
+ // @ts-expect-error store is dynamically added
195
+ const modelClass = this.store.modelFor(type);
196
+ // @ts-expect-error store is dynamically added
197
+ const serializer = this.store.serializerFor(type);
198
+ const {
199
+ data
200
+ } = serializer.normalize(modelClass, resourceHash);
201
+ return data;
202
+ },
203
+ /**
204
+ Normalize some data and push it into the store.
205
+ @public
206
+ */
207
+ pushPayload(store, payload) {
208
+ const normalizedPayload = this._normalizeDocumentHelper(payload);
209
+ store.push(normalizedPayload);
210
+ },
211
+ /**
212
+ @return A {json:api} Document
213
+ @private
214
+ */
215
+ _normalizeResponse(store, primaryModelClass, payload, id, requestType, isSingle) {
216
+ const normalizedPayload = this._normalizeDocumentHelper(payload);
217
+ return normalizedPayload;
218
+ },
219
+ normalizeQueryRecordResponse() {
220
+ // @ts-expect-error untyped
221
+ const normalized = this._super(...arguments);
222
+ (test => {
223
+ if (!test) {
224
+ throw new Error('Expected the primary data returned by the serializer for a `queryRecord` response to be a single object but instead it was an array.');
225
+ }
226
+ })(!Array.isArray(normalized.data));
227
+ return normalized;
228
+ },
229
+ extractAttributes(modelClass, resourceHash) {
230
+ const attributes = {};
231
+ if (resourceHash.attributes) {
232
+ modelClass.eachAttribute(key => {
233
+ const attributeKey = this.keyForAttribute(key, 'deserialize');
234
+ if (resourceHash.attributes[attributeKey] !== undefined) {
235
+ attributes[key] = resourceHash.attributes[attributeKey];
236
+ }
237
+ {
238
+ if (resourceHash.attributes[attributeKey] === undefined && resourceHash.attributes[key] !== undefined) {
239
+ (test => {
240
+ {
241
+ throw new Error(`Your payload for '${modelClass.modelName}' contains '${key}', but your serializer is setup to look for '${attributeKey}'. This is most likely because Ember Data's JSON API serializer dasherizes attribute keys by default. You should subclass JSONAPISerializer and implement 'keyForAttribute(key) { return key; }' to prevent Ember Data from customizing your attribute keys.`);
242
+ }
243
+ })();
244
+ }
245
+ }
246
+ });
247
+ }
248
+ return attributes;
249
+ },
250
+ /**
251
+ Returns a relationship formatted as a JSON-API "relationship object".
252
+ http://jsonapi.org/format/#document-resource-object-relationships
253
+ @public
254
+ */
255
+ extractRelationship(relationshipHash) {
256
+ if (Array.isArray(relationshipHash.data)) {
257
+ const ret = new Array(relationshipHash.data.length);
258
+ for (let i = 0; i < relationshipHash.data.length; i++) {
259
+ const data = relationshipHash.data[i];
260
+ ret[i] = this._normalizeRelationshipDataHelper(data);
261
+ }
262
+ relationshipHash.data = ret;
263
+ } else if (relationshipHash.data && typeof relationshipHash.data === 'object') {
264
+ relationshipHash.data = this._normalizeRelationshipDataHelper(relationshipHash.data);
265
+ }
266
+ return relationshipHash;
267
+ },
268
+ /**
269
+ Returns the resource's relationships formatted as a JSON-API "relationships object".
270
+ http://jsonapi.org/format/#document-resource-object-relationships
271
+ @public
272
+ */
273
+ extractRelationships(modelClass, resourceHash) {
274
+ const relationships = {};
275
+ if (resourceHash.relationships) {
276
+ modelClass.eachRelationship((key, relationshipMeta) => {
277
+ const relationshipKey = this.keyForRelationship(key, relationshipMeta.kind, 'deserialize');
278
+ if (resourceHash.relationships[relationshipKey] !== undefined) {
279
+ const relationshipHash = resourceHash.relationships[relationshipKey];
280
+ relationships[key] = this.extractRelationship(relationshipHash);
281
+ }
282
+ {
283
+ if (resourceHash.relationships[relationshipKey] === undefined && resourceHash.relationships[key] !== undefined) {
284
+ (test => {
285
+ {
286
+ throw new Error(`Your payload for '${modelClass.modelName}' contains '${key}', but your serializer is setup to look for '${relationshipKey}'. This is most likely because Ember Data's JSON API serializer dasherizes relationship keys by default. You should subclass JSONAPISerializer and implement 'keyForRelationship(key) { return key; }' to prevent Ember Data from customizing your relationship keys.`);
287
+ }
288
+ })();
289
+ }
290
+ }
291
+ });
292
+ }
293
+ return relationships;
294
+ },
295
+ /**
296
+ @private
297
+ */
298
+ _extractType(modelClass, resourceHash) {
299
+ return this.modelNameFromPayloadKey(resourceHash.type);
300
+ },
301
+ /**
302
+ Dasherizes and singularizes the model name in the payload to match
303
+ the format Ember Data uses internally for the model name.
304
+ For example the key `posts` would be converted to `post` and the
305
+ key `studentAssesments` would be converted to `student-assesment`.
306
+ @public
307
+ @return the model's modelName
308
+ */
309
+ modelNameFromPayloadKey(key) {
310
+ return dasherize(singularize(key));
311
+ },
312
+ /**
313
+ Converts the model name to a pluralized version of the model name.
314
+ For example `post` would be converted to `posts` and
315
+ `student-assesment` would be converted to `student-assesments`.
316
+ @public
317
+ */
318
+ payloadKeyFromModelName(modelName) {
319
+ return pluralize(modelName);
320
+ },
321
+ normalize(modelClass, resourceHash) {
322
+ if (resourceHash.attributes) {
323
+ // @ts-expect-error untyped
324
+ this.normalizeUsingDeclaredMapping(modelClass, resourceHash.attributes);
325
+ }
326
+ if (resourceHash.relationships) {
327
+ // @ts-expect-error untyped
328
+ this.normalizeUsingDeclaredMapping(modelClass, resourceHash.relationships);
329
+ }
330
+ const data = {
331
+ // @ts-expect-error untyped
332
+ id: this.extractId(modelClass, resourceHash),
333
+ type: this._extractType(modelClass, resourceHash),
334
+ attributes: this.extractAttributes(modelClass, resourceHash),
335
+ relationships: this.extractRelationships(modelClass, resourceHash)
336
+ };
337
+ if (resourceHash.lid) {
338
+ // @ts-expect-error untyped
339
+ data.lid = resourceHash.lid;
340
+ }
341
+
342
+ // @ts-expect-error untyped
343
+ this.applyTransforms(modelClass, data.attributes);
344
+ return {
345
+ data
346
+ };
347
+ },
348
+ /**
349
+ `keyForAttribute` can be used to define rules for how to convert an
350
+ attribute name in your model to a key in your JSON.
351
+ By default `JSONAPISerializer` follows the format used on the examples of
352
+ http://jsonapi.org/format and uses dashes as the word separator in the JSON
353
+ attribute keys.
354
+ This behaviour can be easily customized by extending this method.
355
+ Example
356
+ ```js [app/serializers/application.js]
357
+ import JSONAPISerializer from '@warp-drive/legacy/serializer/json-api';
358
+ import { dasherize } from '<app-name>/utils/string-utils';
359
+ export default class ApplicationSerializer extends JSONAPISerializer {
360
+ keyForAttribute(attr, method) {
361
+ return dasherize(attr).toUpperCase();
362
+ }
363
+ }
364
+ ```
365
+ @public
366
+ @return normalized key
367
+ */
368
+ keyForAttribute(key, method) {
369
+ return dasherize(key);
370
+ },
371
+ /**
372
+ `keyForRelationship` can be used to define a custom key when
373
+ serializing and deserializing relationship properties.
374
+ By default `JSONAPISerializer` follows the format used on the examples of
375
+ http://jsonapi.org/format and uses dashes as word separators in
376
+ relationship properties.
377
+ This behaviour can be easily customized by extending this method.
378
+ Example
379
+ ```js [app/serializers/post.js]
380
+ import JSONAPISerializer from '@warp-drive/legacy/serializer/json-api';
381
+ import { underscore } from '<app-name>/utils/string-utils';
382
+ export default class ApplicationSerializer extends JSONAPISerializer {
383
+ keyForRelationship(key, relationship, method) {
384
+ return underscore(key);
385
+ }
386
+ }
387
+ ```
388
+ @public
389
+ @return the normalized key
390
+ */
391
+ keyForRelationship(key, typeClass, method) {
392
+ return dasherize(key);
393
+ },
394
+ /**
395
+ Called when a record is saved in order to convert the
396
+ record into JSON.
397
+ For example, consider this model:
398
+ ```js [app/models/comment.js]
399
+ import Model, { attr, belongsTo } from '@warp-drive/legacy/model';
400
+ export default class CommentModel extends Model {
401
+ @attr title;
402
+ @attr body;
403
+ @belongsTo('user', { async: false, inverse: null })
404
+ author;
405
+ }
406
+ ```
407
+ The default serialization would create a JSON-API resource object like:
408
+ ```javascript
409
+ {
410
+ "data": {
411
+ "type": "comments",
412
+ "attributes": {
413
+ "title": "Rails is unagi",
414
+ "body": "Rails? Omakase? O_O",
415
+ },
416
+ "relationships": {
417
+ "author": {
418
+ "data": {
419
+ "id": "12",
420
+ "type": "users"
421
+ }
422
+ }
423
+ }
424
+ }
425
+ }
426
+ ```
427
+ By default, attributes are passed through as-is, unless
428
+ you specified an attribute type (`attr('date')`). If
429
+ you specify a transform, the JavaScript value will be
430
+ serialized when inserted into the attributes hash.
431
+ Belongs-to relationships are converted into JSON-API
432
+ resource identifier objects.
433
+ ## IDs
434
+ `serialize` takes an options hash with a single option:
435
+ `includeId`. If this option is `true`, `serialize` will,
436
+ by default include the ID in the JSON object it builds.
437
+ The JSONAPIAdapter passes in `includeId: true` when serializing a record
438
+ for `createRecord` or `updateRecord`.
439
+ ## Customization
440
+ Your server may expect data in a different format than the
441
+ built-in serialization format.
442
+ In that case, you can implement `serialize` yourself and
443
+ return data formatted to match your API's expectations, or override
444
+ the invoked adapter method and do the serialization in the adapter directly
445
+ by using the provided snapshot.
446
+ If your API's format differs greatly from the JSON:API spec, you should
447
+ consider authoring your own adapter and serializer instead of extending
448
+ this class.
449
+ ```js [app/serializers/post.js]
450
+ import JSONAPISerializer from '@warp-drive/legacy/serializer/json-api';
451
+ export default class PostSerializer extends JSONAPISerializer {
452
+ serialize(snapshot, options) {
453
+ let json = {
454
+ POST_TTL: snapshot.attr('title'),
455
+ POST_BDY: snapshot.attr('body'),
456
+ POST_CMS: snapshot.hasMany('comments', { ids: true })
457
+ };
458
+ if (options.includeId) {
459
+ json.POST_ID_ = snapshot.id;
460
+ }
461
+ return json;
462
+ }
463
+ }
464
+ ```
465
+ ## Customizing an App-Wide Serializer
466
+ If you want to define a serializer for your entire
467
+ application, you'll probably want to use `eachAttribute`
468
+ and `eachRelationship` on the record.
469
+ ```js [app/serializers/application.js]
470
+ import JSONAPISerializer from '@warp-drive/legacy/serializer/json-api';
471
+ import { underscore, singularize } from '<app-name>/utils/string-utils';
472
+ export default class ApplicationSerializer extends JSONAPISerializer {
473
+ serialize(snapshot, options) {
474
+ let json = {};
475
+ snapshot.eachAttribute((name) => {
476
+ json[serverAttributeName(name)] = snapshot.attr(name);
477
+ });
478
+ snapshot.eachRelationship((name, relationship) => {
479
+ if (relationship.kind === 'hasMany') {
480
+ json[serverHasManyName(name)] = snapshot.hasMany(name, { ids: true });
481
+ }
482
+ });
483
+ if (options.includeId) {
484
+ json.ID_ = snapshot.id;
485
+ }
486
+ return json;
487
+ }
488
+ }
489
+ function serverAttributeName(attribute) {
490
+ return underscore(attribute).toUpperCase();
491
+ }
492
+ function serverHasManyName(name) {
493
+ return serverAttributeName(singularize(name)) + '_IDS';
494
+ }
495
+ ```
496
+ This serializer will generate JSON that looks like this:
497
+ ```javascript
498
+ {
499
+ "TITLE": "Rails is omakase",
500
+ "BODY": "Yep. Omakase.",
501
+ "COMMENT_IDS": [ "1", "2", "3" ]
502
+ }
503
+ ```
504
+ ## Tweaking the Default Formatting
505
+ If you just want to do some small tweaks on the default JSON:API formatted response,
506
+ you can call `super.serialize` first and make the tweaks
507
+ on the returned object.
508
+ ```js [app/serializers/post.js]
509
+ import JSONAPISerializer from '@warp-drive/legacy/serializer/json-api';
510
+ export default class PostSerializer extends JSONAPISerializer {
511
+ serialize(snapshot, options) {
512
+ let json = super.serialize(...arguments);
513
+ json.data.attributes.subject = json.data.attributes.title;
514
+ delete json.data.attributes.title;
515
+ return json;
516
+ }
517
+ }
518
+ ```
519
+ @public
520
+ */
521
+ serialize(snapshot, options) {
522
+ // @ts-expect-error untyped
523
+ const data = this._super(...arguments);
524
+ data.type = this.payloadKeyFromModelName(snapshot.modelName);
525
+ return {
526
+ data
527
+ };
528
+ },
529
+ serializeAttribute(snapshot, json, key, attribute) {
530
+ const type = attribute.type;
531
+
532
+ // @ts-expect-error untyped
533
+ if (this._canSerialize(key)) {
534
+ json.attributes = json.attributes || {};
535
+ let value = snapshot.attr(key);
536
+ if (type) {
537
+ // @ts-expect-error untyped
538
+ const transform = this.transformFor(type);
539
+ value = transform.serialize(value, attribute.options);
540
+ }
541
+
542
+ // @ts-expect-error store is dynamically added
543
+ const schema = this.store.modelFor(snapshot.modelName);
544
+ // @ts-expect-error untyped
545
+ let payloadKey = this._getMappedKey(key, schema);
546
+ if (payloadKey === key) {
547
+ payloadKey = this.keyForAttribute(key, 'serialize');
548
+ }
549
+ json.attributes[payloadKey] = value;
550
+ }
551
+ },
552
+ serializeBelongsTo(snapshot, json, relationship) {
553
+ const name = relationship.name;
554
+
555
+ // @ts-expect-error untyped
556
+ if (this._canSerialize(name)) {
557
+ const belongsTo = snapshot.belongsTo(name);
558
+ // @ts-expect-error not narrowed
559
+ const belongsToIsNotNew = belongsTo && !belongsTo.isNew;
560
+ if (belongsTo === null || belongsToIsNotNew) {
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) {
568
+ payloadKey = this.keyForRelationship(name, 'belongsTo', 'serialize');
569
+ }
570
+ let data = null;
571
+ if (belongsTo) {
572
+ // @ts-expect-error not narrowed
573
+ const payloadType = this.payloadKeyFromModelName(belongsTo.modelName);
574
+
575
+ // @ts-expect-error untyped
576
+ data = {
577
+ type: payloadType,
578
+ // @ts-expect-error not narrowed
579
+ id: belongsTo.id
580
+ };
581
+ }
582
+ json.relationships[payloadKey] = {
583
+ data
584
+ };
585
+ }
586
+ }
587
+ },
588
+ serializeHasMany(snapshot, json, relationship) {
589
+ const name = relationship.name;
590
+
591
+ // @ts-expect-error untyped
592
+ if (this.shouldSerializeHasMany(snapshot, name, relationship)) {
593
+ const hasMany = snapshot.hasMany(name);
594
+ if (hasMany !== undefined) {
595
+ json.relationships = json.relationships || {};
596
+
597
+ // @ts-expect-error store is dynamically added
598
+ const schema = this.store.modelFor(snapshot.modelName);
599
+ // @ts-expect-error untyped
600
+ let payloadKey = this._getMappedKey(name, schema);
601
+ if (payloadKey === name && this.keyForRelationship) {
602
+ payloadKey = this.keyForRelationship(name, 'hasMany', 'serialize');
603
+ }
604
+
605
+ // only serialize has many relationships that are not new
606
+ // @ts-expect-error not narrowed
607
+ const nonNewHasMany = hasMany.filter(item => !item.isNew);
608
+ const data = new Array(nonNewHasMany.length);
609
+ for (let i = 0; i < nonNewHasMany.length; i++) {
610
+ const item = hasMany[i];
611
+ // @ts-expect-error not narrowed
612
+ const payloadType = this.payloadKeyFromModelName(item.modelName);
613
+ data[i] = {
614
+ type: payloadType,
615
+ // @ts-expect-error not narrowed
616
+ id: item.id
617
+ };
618
+ }
619
+ json.relationships[payloadKey] = {
620
+ data
621
+ };
622
+ }
623
+ }
624
+ }
625
+ });
626
+ {
627
+ JSONAPISerializer.reopen({
628
+ init(...args) {
629
+ this._super(...args);
630
+ (test => {
631
+ if (!test) {
632
+ throw new Error(`You've used the EmbeddedRecordsMixin in ${this.toString()} which is not fully compatible with the JSON:API specification. Please confirm that this works for your specific API and add \`this.isEmbeddedRecordsMixinCompatible = true\` to your serializer.`);
633
+ }
634
+ })(!this.isEmbeddedRecordsMixin || this.isEmbeddedRecordsMixinCompatible === true);
635
+ const constructor = this.constructor;
636
+ console.warn(`You've defined 'extractMeta' in ${constructor.toString()} which is not used for serializers extending JSONAPISerializer. Read more at https://api.emberjs.com/ember-data/release/classes/JSONAPISerializer on how to customize meta when using JSON API.`, this.extractMeta === JSONSerializer.prototype.extractMeta, {
637
+ id: 'ds.serializer.json-api.extractMeta'
638
+ });
639
+ },
640
+ warnMessageForUndefinedType() {
641
+ return 'Encountered a resource object with an undefined type (resolved resource using ' + this.constructor.toString() + ')';
642
+ },
643
+ warnMessageNoModelForType(modelName, originalType, usedLookup) {
644
+ return `Encountered a resource object with type "${originalType}", but no model was found for model name "${modelName}" (resolved model name using '${this.constructor.toString()}.${usedLookup}("${originalType}")').`;
645
+ }
646
+ });
647
+ }
648
+ 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-CCU-ZQ4b.js";