@warp-drive/legacy 5.6.0-alpha.11

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 (211) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/LICENSE.md +23 -0
  3. package/README.md +54 -0
  4. package/addon-main.cjs +5 -0
  5. package/declarations/adapter/-private/build-url-mixin.d.ts +34 -0
  6. package/declarations/adapter/-private/build-url-mixin.d.ts.map +1 -0
  7. package/declarations/adapter/-private/fastboot-interface.d.ts +9 -0
  8. package/declarations/adapter/-private/fastboot-interface.d.ts.map +1 -0
  9. package/declarations/adapter/-private/utils/continue-on-reject.d.ts +8 -0
  10. package/declarations/adapter/-private/utils/continue-on-reject.d.ts.map +1 -0
  11. package/declarations/adapter/-private/utils/determine-body-promise.d.ts +5 -0
  12. package/declarations/adapter/-private/utils/determine-body-promise.d.ts.map +1 -0
  13. package/declarations/adapter/-private/utils/fetch.d.ts +9 -0
  14. package/declarations/adapter/-private/utils/fetch.d.ts.map +1 -0
  15. package/declarations/adapter/-private/utils/parse-response-headers.d.ts +2 -0
  16. package/declarations/adapter/-private/utils/parse-response-headers.d.ts.map +1 -0
  17. package/declarations/adapter/-private/utils/serialize-into-hash.d.ts +7 -0
  18. package/declarations/adapter/-private/utils/serialize-into-hash.d.ts.map +1 -0
  19. package/declarations/adapter/-private/utils/serialize-query-params.d.ts +6 -0
  20. package/declarations/adapter/-private/utils/serialize-query-params.d.ts.map +1 -0
  21. package/declarations/adapter/-private.d.ts +6 -0
  22. package/declarations/adapter/-private.d.ts.map +1 -0
  23. package/declarations/adapter/error.d.ts +188 -0
  24. package/declarations/adapter/error.d.ts.map +1 -0
  25. package/declarations/adapter/json-api.d.ts +234 -0
  26. package/declarations/adapter/json-api.d.ts.map +1 -0
  27. package/declarations/adapter/rest.d.ts +823 -0
  28. package/declarations/adapter/rest.d.ts.map +1 -0
  29. package/declarations/adapter.d.ts +801 -0
  30. package/declarations/adapter.d.ts.map +1 -0
  31. package/declarations/compat/-private.d.ts +14 -0
  32. package/declarations/compat/-private.d.ts.map +1 -0
  33. package/declarations/compat/builders/find-all.d.ts +33 -0
  34. package/declarations/compat/builders/find-all.d.ts.map +1 -0
  35. package/declarations/compat/builders/find-record.d.ts +54 -0
  36. package/declarations/compat/builders/find-record.d.ts.map +1 -0
  37. package/declarations/compat/builders/query.d.ts +61 -0
  38. package/declarations/compat/builders/query.d.ts.map +1 -0
  39. package/declarations/compat/builders/save-record.d.ts +32 -0
  40. package/declarations/compat/builders/save-record.d.ts.map +1 -0
  41. package/declarations/compat/builders/utils.d.ts +4 -0
  42. package/declarations/compat/builders/utils.d.ts.map +1 -0
  43. package/declarations/compat/builders.d.ts +15 -0
  44. package/declarations/compat/builders.d.ts.map +1 -0
  45. package/declarations/compat/legacy-network-handler/fetch-manager.d.ts +47 -0
  46. package/declarations/compat/legacy-network-handler/fetch-manager.d.ts.map +1 -0
  47. package/declarations/compat/legacy-network-handler/identifier-has-id.d.ts +3 -0
  48. package/declarations/compat/legacy-network-handler/identifier-has-id.d.ts.map +1 -0
  49. package/declarations/compat/legacy-network-handler/legacy-data-fetch.d.ts +12 -0
  50. package/declarations/compat/legacy-network-handler/legacy-data-fetch.d.ts.map +1 -0
  51. package/declarations/compat/legacy-network-handler/legacy-data-utils.d.ts +6 -0
  52. package/declarations/compat/legacy-network-handler/legacy-data-utils.d.ts.map +1 -0
  53. package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts +3 -0
  54. package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts.map +1 -0
  55. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +527 -0
  56. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts.map +1 -0
  57. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +224 -0
  58. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts.map +1 -0
  59. package/declarations/compat/legacy-network-handler/serializer-response.d.ts +7 -0
  60. package/declarations/compat/legacy-network-handler/serializer-response.d.ts.map +1 -0
  61. package/declarations/compat/legacy-network-handler/snapshot-record-array.d.ts +89 -0
  62. package/declarations/compat/legacy-network-handler/snapshot-record-array.d.ts.map +1 -0
  63. package/declarations/compat/legacy-network-handler/snapshot.d.ts +237 -0
  64. package/declarations/compat/legacy-network-handler/snapshot.d.ts.map +1 -0
  65. package/declarations/compat/utils.d.ts +138 -0
  66. package/declarations/compat/utils.d.ts.map +1 -0
  67. package/declarations/compat.d.ts +134 -0
  68. package/declarations/compat.d.ts.map +1 -0
  69. package/declarations/model/-private/attr.d.ts +165 -0
  70. package/declarations/model/-private/attr.d.ts.map +1 -0
  71. package/declarations/model/-private/attr.type-test.d.ts +2 -0
  72. package/declarations/model/-private/attr.type-test.d.ts.map +1 -0
  73. package/declarations/model/-private/belongs-to.d.ts +173 -0
  74. package/declarations/model/-private/belongs-to.d.ts.map +1 -0
  75. package/declarations/model/-private/belongs-to.type-test.d.ts +2 -0
  76. package/declarations/model/-private/belongs-to.type-test.d.ts.map +1 -0
  77. package/declarations/model/-private/debug/assert-polymorphic-type.d.ts +6 -0
  78. package/declarations/model/-private/debug/assert-polymorphic-type.d.ts.map +1 -0
  79. package/declarations/model/-private/errors.d.ts +290 -0
  80. package/declarations/model/-private/errors.d.ts.map +1 -0
  81. package/declarations/model/-private/has-many.d.ts +164 -0
  82. package/declarations/model/-private/has-many.d.ts.map +1 -0
  83. package/declarations/model/-private/has-many.type-test.d.ts +2 -0
  84. package/declarations/model/-private/has-many.type-test.d.ts.map +1 -0
  85. package/declarations/model/-private/hooks.d.ts +11 -0
  86. package/declarations/model/-private/hooks.d.ts.map +1 -0
  87. package/declarations/model/-private/legacy-relationships-support.d.ts +57 -0
  88. package/declarations/model/-private/legacy-relationships-support.d.ts.map +1 -0
  89. package/declarations/model/-private/model-for-mixin.d.ts +4 -0
  90. package/declarations/model/-private/model-for-mixin.d.ts.map +1 -0
  91. package/declarations/model/-private/model-methods.d.ts +35 -0
  92. package/declarations/model/-private/model-methods.d.ts.map +1 -0
  93. package/declarations/model/-private/model.d.ts +1270 -0
  94. package/declarations/model/-private/model.d.ts.map +1 -0
  95. package/declarations/model/-private/model.type-test.d.ts +2 -0
  96. package/declarations/model/-private/model.type-test.d.ts.map +1 -0
  97. package/declarations/model/-private/notify-changes.d.ts +5 -0
  98. package/declarations/model/-private/notify-changes.d.ts.map +1 -0
  99. package/declarations/model/-private/promise-belongs-to.d.ts +40 -0
  100. package/declarations/model/-private/promise-belongs-to.d.ts.map +1 -0
  101. package/declarations/model/-private/promise-many-array.d.ts +126 -0
  102. package/declarations/model/-private/promise-many-array.d.ts.map +1 -0
  103. package/declarations/model/-private/promise-proxy-base.d.ts +3 -0
  104. package/declarations/model/-private/promise-proxy-base.d.ts.map +1 -0
  105. package/declarations/model/-private/record-state.d.ts +78 -0
  106. package/declarations/model/-private/record-state.d.ts.map +1 -0
  107. package/declarations/model/-private/references/belongs-to.d.ts +495 -0
  108. package/declarations/model/-private/references/belongs-to.d.ts.map +1 -0
  109. package/declarations/model/-private/references/has-many.d.ts +504 -0
  110. package/declarations/model/-private/references/has-many.d.ts.map +1 -0
  111. package/declarations/model/-private/schema-provider.d.ts +63 -0
  112. package/declarations/model/-private/schema-provider.d.ts.map +1 -0
  113. package/declarations/model/-private/type-utils.d.ts +57 -0
  114. package/declarations/model/-private/type-utils.d.ts.map +1 -0
  115. package/declarations/model/-private/util.d.ts +6 -0
  116. package/declarations/model/-private/util.d.ts.map +1 -0
  117. package/declarations/model/-private.d.ts +8 -0
  118. package/declarations/model/-private.d.ts.map +1 -0
  119. package/declarations/model/migration-support.d.ts +287 -0
  120. package/declarations/model/migration-support.d.ts.map +1 -0
  121. package/declarations/model/migration-support.type-test.d.ts +2 -0
  122. package/declarations/model/migration-support.type-test.d.ts.map +1 -0
  123. package/declarations/model.d.ts +50 -0
  124. package/declarations/model.d.ts.map +1 -0
  125. package/declarations/serializer/-private/embedded-records-mixin.d.ts +97 -0
  126. package/declarations/serializer/-private/embedded-records-mixin.d.ts.map +1 -0
  127. package/declarations/serializer/-private/transforms/boolean.d.ts +47 -0
  128. package/declarations/serializer/-private/transforms/boolean.d.ts.map +1 -0
  129. package/declarations/serializer/-private/transforms/boolean.type-test.d.ts +2 -0
  130. package/declarations/serializer/-private/transforms/boolean.type-test.d.ts.map +1 -0
  131. package/declarations/serializer/-private/transforms/date.d.ts +28 -0
  132. package/declarations/serializer/-private/transforms/date.d.ts.map +1 -0
  133. package/declarations/serializer/-private/transforms/number.d.ts +29 -0
  134. package/declarations/serializer/-private/transforms/number.d.ts.map +1 -0
  135. package/declarations/serializer/-private/transforms/string.d.ts +29 -0
  136. package/declarations/serializer/-private/transforms/string.d.ts.map +1 -0
  137. package/declarations/serializer/-private/transforms/transform.d.ts +119 -0
  138. package/declarations/serializer/-private/transforms/transform.d.ts.map +1 -0
  139. package/declarations/serializer/-private/utils.d.ts +4 -0
  140. package/declarations/serializer/-private/utils.d.ts.map +1 -0
  141. package/declarations/serializer/json-api.d.ts +496 -0
  142. package/declarations/serializer/json-api.d.ts.map +1 -0
  143. package/declarations/serializer/json.d.ts +1047 -0
  144. package/declarations/serializer/json.d.ts.map +1 -0
  145. package/declarations/serializer/rest.d.ts +554 -0
  146. package/declarations/serializer/rest.d.ts.map +1 -0
  147. package/declarations/serializer/transform.d.ts +6 -0
  148. package/declarations/serializer/transform.d.ts.map +1 -0
  149. package/declarations/serializer.d.ts +258 -0
  150. package/declarations/serializer.d.ts.map +1 -0
  151. package/dist/-private-DFfBszo5.js +1182 -0
  152. package/dist/-private-DFfBszo5.js.map +1 -0
  153. package/dist/adapter/-private.js +1 -0
  154. package/dist/adapter/-private.js.map +1 -0
  155. package/dist/adapter/error.js +254 -0
  156. package/dist/adapter/error.js.map +1 -0
  157. package/dist/adapter/json-api.js +129 -0
  158. package/dist/adapter/json-api.js.map +1 -0
  159. package/dist/adapter/rest.js +1262 -0
  160. package/dist/adapter/rest.js.map +1 -0
  161. package/dist/adapter.js +1284 -0
  162. package/dist/adapter.js.map +1 -0
  163. package/dist/compat/-private.js +1 -0
  164. package/dist/compat/-private.js.map +1 -0
  165. package/dist/compat/builders.js +292 -0
  166. package/dist/compat/builders.js.map +1 -0
  167. package/dist/compat/utils.js +225 -0
  168. package/dist/compat/utils.js.map +1 -0
  169. package/dist/compat.js +999 -0
  170. package/dist/compat.js.map +1 -0
  171. package/dist/errors-D74uk36r.js +2541 -0
  172. package/dist/errors-D74uk36r.js.map +1 -0
  173. package/dist/json-BCH3fil7.js +1349 -0
  174. package/dist/json-BCH3fil7.js.map +1 -0
  175. package/dist/model/-private.js +2 -0
  176. package/dist/model/-private.js.map +1 -0
  177. package/dist/model/migration-support.js +453 -0
  178. package/dist/model/migration-support.js.map +1 -0
  179. package/dist/model.js +736 -0
  180. package/dist/model.js.map +1 -0
  181. package/dist/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  182. package/dist/runtime-BPCpkOf1-BKOwiRJp.js.map +1 -0
  183. package/dist/schema-provider-CXFLTMjg.js +2228 -0
  184. package/dist/schema-provider-CXFLTMjg.js.map +1 -0
  185. package/dist/serialize-into-hash-BxfqWC8u.js +260 -0
  186. package/dist/serialize-into-hash-BxfqWC8u.js.map +1 -0
  187. package/dist/serializer/json-api.js +514 -0
  188. package/dist/serializer/json-api.js.map +1 -0
  189. package/dist/serializer/json.js +6 -0
  190. package/dist/serializer/json.js.map +1 -0
  191. package/dist/serializer/rest.js +1245 -0
  192. package/dist/serializer/rest.js.map +1 -0
  193. package/dist/serializer/transform.js +313 -0
  194. package/dist/serializer/transform.js.map +1 -0
  195. package/dist/serializer.js +252 -0
  196. package/dist/serializer.js.map +1 -0
  197. package/logos/NCC-1701-a-blue.svg +4 -0
  198. package/logos/NCC-1701-a-gold.svg +4 -0
  199. package/logos/NCC-1701-a-gold_100.svg +1 -0
  200. package/logos/NCC-1701-a-gold_base-64.txt +1 -0
  201. package/logos/NCC-1701-a.svg +4 -0
  202. package/logos/README.md +4 -0
  203. package/logos/docs-badge.svg +2 -0
  204. package/logos/ember-data-logo-dark.svg +12 -0
  205. package/logos/ember-data-logo-light.svg +12 -0
  206. package/logos/github-header.svg +444 -0
  207. package/logos/social1.png +0 -0
  208. package/logos/social2.png +0 -0
  209. package/logos/warp-drive-logo-dark.svg +4 -0
  210. package/logos/warp-drive-logo-gold.svg +4 -0
  211. package/package.json +72 -0
package/dist/compat.js ADDED
@@ -0,0 +1,999 @@
1
+ import { getOwner } from '@ember/application';
2
+ import { recordIdentifierFor } from '@warp-drive/core';
3
+ import { waitFor, _deprecatingNormalize } from '@warp-drive/core/store/-private';
4
+ import { p as payloadIsNotBlank, n as normalizeResponseHelper, i as iterateData, F as FetchManager, S as SaveOp, a as assertIdentifierHasId, b as SnapshotRecordArray } from "./-private-DFfBszo5.js";
5
+ import { macroCondition, getGlobalConfig } from '@embroider/macros';
6
+ function _findHasMany(adapter, store, identifier, link, relationship, options) {
7
+ const promise = Promise.resolve().then(() => {
8
+ const snapshot = store._fetchManager.createSnapshot(identifier, options);
9
+ const useLink = !link || typeof link === 'string';
10
+ const relatedLink = useLink ? link : link.href;
11
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
12
+ if (!test) {
13
+ throw new Error(`Attempted to load a hasMany relationship from a specified 'link' in the original payload, but the specified link is empty. You must provide a valid 'link' in the original payload to use 'findHasMany'`);
14
+ }
15
+ })(relatedLink) : {};
16
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
17
+ if (!test) {
18
+ throw new Error(`Expected the adapter to implement 'findHasMany' but it does not`);
19
+ }
20
+ })(typeof adapter.findHasMany === 'function') : {};
21
+ return adapter.findHasMany(store, snapshot, relatedLink, relationship);
22
+ });
23
+ return promise.then(adapterPayload => {
24
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
25
+ if (!test) {
26
+ throw new Error(`You made a 'findHasMany' request for a ${identifier.type}'s '${relationship.name}' relationship, using link '${JSON.stringify(link)}' , but the adapter's response did not have any data`);
27
+ }
28
+ })(payloadIsNotBlank(adapterPayload)) : {};
29
+ const modelClass = store.modelFor(relationship.type);
30
+ const serializer = store.serializerFor(relationship.type);
31
+ let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findHasMany');
32
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
33
+ if (!test) {
34
+ throw new Error(`fetched the hasMany relationship '${relationship.name}' for ${identifier.type}:${identifier.id} with link '${JSON.stringify(link)}', but no data member is present in the response. If no data exists, the response should set { data: [] }`);
35
+ }
36
+ })('data' in payload && Array.isArray(payload.data)) : {};
37
+ payload = syncRelationshipDataFromLink(store, payload, identifier, relationship);
38
+ return store._push(payload, true);
39
+ }, null);
40
+ }
41
+ function _findBelongsTo(store, identifier, link, relationship, options) {
42
+ const promise = Promise.resolve().then(() => {
43
+ const adapter = store.adapterFor(identifier.type);
44
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
45
+ if (!test) {
46
+ throw new Error(`You tried to load a belongsTo relationship but you have no adapter (for ${identifier.type})`);
47
+ }
48
+ })(adapter) : {};
49
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
50
+ if (!test) {
51
+ throw new Error(`You tried to load a belongsTo relationship from a specified 'link' in the original payload but your adapter does not implement 'findBelongsTo'`);
52
+ }
53
+ })(typeof adapter.findBelongsTo === 'function') : {};
54
+ const snapshot = store._fetchManager.createSnapshot(identifier, options);
55
+ const useLink = !link || typeof link === 'string';
56
+ const relatedLink = useLink ? link : link.href;
57
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
58
+ if (!test) {
59
+ throw new Error(`Attempted to load a belongsTo relationship from a specified 'link' in the original payload, but the specified link is empty. You must provide a valid 'link' in the original payload to use 'findBelongsTo'`);
60
+ }
61
+ })(relatedLink) : {};
62
+ return adapter.findBelongsTo(store, snapshot, relatedLink, relationship);
63
+ });
64
+ return promise.then(adapterPayload => {
65
+ const modelClass = store.modelFor(relationship.type);
66
+ const serializer = store.serializerFor(relationship.type);
67
+ let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findBelongsTo');
68
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
69
+ if (!test) {
70
+ throw new Error(`fetched the belongsTo relationship '${relationship.name}' for ${identifier.type}:${identifier.id} with link '${JSON.stringify(link)}', but no data member is present in the response. If no data exists, the response should set { data: null }`);
71
+ }
72
+ })('data' in payload && (payload.data === null || typeof payload.data === 'object' && !Array.isArray(payload.data))) : {};
73
+ if (!payload.data && !payload.links && !payload.meta) {
74
+ return null;
75
+ }
76
+ payload = syncRelationshipDataFromLink(store, payload, identifier, relationship);
77
+ return store._push(payload, true);
78
+ }, null);
79
+ }
80
+
81
+ // sync
82
+ // iterate over records in payload.data
83
+ // for each record
84
+ // assert that record.relationships[inverse] is either undefined (so we can fix it)
85
+ // or provide a data: {id, type} that matches the record that requested it
86
+ // return the relationship data for the parent
87
+ function syncRelationshipDataFromLink(store, payload, parentIdentifier, relationship) {
88
+ // ensure the right hand side (incoming payload) points to the parent record that
89
+ // requested this relationship
90
+ const relationshipData = payload.data ? iterateData(payload.data, (data, index) => {
91
+ const {
92
+ id,
93
+ type
94
+ } = data;
95
+ ensureRelationshipIsSetToParent(data, parentIdentifier, store, relationship, index);
96
+ return {
97
+ id,
98
+ type
99
+ };
100
+ }) : null;
101
+ const relatedDataHash = {};
102
+ if ('meta' in payload) {
103
+ relatedDataHash.meta = payload.meta;
104
+ }
105
+ if ('links' in payload) {
106
+ relatedDataHash.links = payload.links;
107
+ }
108
+ if ('data' in payload) {
109
+ relatedDataHash.data = relationshipData;
110
+ }
111
+
112
+ // now, push the left hand side (the parent record) to ensure things are in sync, since
113
+ // the payload will be pushed with store._push
114
+ const parentPayload = {
115
+ id: parentIdentifier.id,
116
+ type: parentIdentifier.type,
117
+ relationships: {
118
+ [relationship.name]: relatedDataHash
119
+ }
120
+ };
121
+ if (!Array.isArray(payload.included)) {
122
+ payload.included = [];
123
+ }
124
+ payload.included.push(parentPayload);
125
+ return payload;
126
+ }
127
+ function ensureRelationshipIsSetToParent(payload, parentIdentifier, store, parentRelationship, index) {
128
+ const {
129
+ id,
130
+ type
131
+ } = payload;
132
+ if (!payload.relationships) {
133
+ payload.relationships = {};
134
+ }
135
+ const {
136
+ relationships
137
+ } = payload;
138
+ const inverse = getInverse(store, parentIdentifier, parentRelationship, type);
139
+ if (inverse) {
140
+ const {
141
+ inverseKey,
142
+ kind
143
+ } = inverse;
144
+ const relationshipData = relationships[inverseKey]?.data;
145
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
146
+ if (typeof relationshipData !== 'undefined' && !relationshipDataPointsToParent(relationshipData, parentIdentifier)) {
147
+ const inspect = function inspect(thing) {
148
+ return `'${JSON.stringify(thing)}'`;
149
+ };
150
+ const quotedType = inspect(type);
151
+ const quotedInverse = inspect(inverseKey);
152
+ const expected = inspect({
153
+ id: parentIdentifier.id,
154
+ type: parentIdentifier.type
155
+ });
156
+ const expectedModel = `${parentIdentifier.type}:${parentIdentifier.id}`;
157
+ const got = inspect(relationshipData);
158
+ const prefix = typeof index === 'number' ? `data[${index}]` : `data`;
159
+ const path = `${prefix}.relationships.${inverseKey}.data`;
160
+ const data = Array.isArray(relationshipData) ? relationshipData[0] : relationshipData;
161
+ const other = data ? `<${data.type}:${data.id}>` : null;
162
+ const relationshipFetched = `${expectedModel}.${parentRelationship.kind}("${parentRelationship.name}")`;
163
+ const includedRecord = `<${type}:${id}>`;
164
+ const message = [`Encountered mismatched relationship: Ember Data expected ${path} in the payload from ${relationshipFetched} to include ${expected} but got ${got} instead.\n`, `The ${includedRecord} record loaded at ${prefix} in the payload specified ${other} as its ${quotedInverse}, but should have specified ${expectedModel} (the record the relationship is being loaded from) as its ${quotedInverse} instead.`, `This could mean that the response for ${relationshipFetched} may have accidentally returned ${quotedType} records that aren't related to ${expectedModel} and could be related to a different ${parentIdentifier.type} record instead.`, `Ember Data has corrected the ${includedRecord} record's ${quotedInverse} relationship to ${expectedModel} so that ${relationshipFetched} will include ${includedRecord}.`, `Please update the response from the server or change your serializer to either ensure that the response for only includes ${quotedType} records that specify ${expectedModel} as their ${quotedInverse}, or omit the ${quotedInverse} relationship from the response.`].join('\n');
165
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
166
+ {
167
+ throw new Error(message);
168
+ }
169
+ })() : {};
170
+ }
171
+ }
172
+ if (kind !== 'hasMany' || typeof relationshipData !== 'undefined') {
173
+ relationships[inverseKey] = relationships[inverseKey] || {};
174
+ relationships[inverseKey].data = fixRelationshipData(relationshipData ?? null, kind, parentIdentifier);
175
+ }
176
+ }
177
+ }
178
+ function inverseForRelationship(store, identifier, key) {
179
+ const definition = store.schema.fields(identifier).get(key);
180
+ if (!definition) {
181
+ return null;
182
+ }
183
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
184
+ if (!test) {
185
+ throw new Error(`Expected the field definition to be a relationship`);
186
+ }
187
+ })(definition.kind === 'hasMany' || definition.kind === 'belongsTo') : {};
188
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
189
+ if (!test) {
190
+ throw new Error(`Expected the relationship defintion to specify the inverse type or null.`);
191
+ }
192
+ })(definition.options?.inverse === null || typeof definition.options?.inverse === 'string' && definition.options.inverse.length > 0) : {};
193
+ return definition.options.inverse;
194
+ }
195
+ function getInverse(store, parentIdentifier, parentRelationship, type) {
196
+ const {
197
+ name: lhs_relationshipName
198
+ } = parentRelationship;
199
+ const {
200
+ type: parentType
201
+ } = parentIdentifier;
202
+ const inverseKey = inverseForRelationship(store, {
203
+ type: parentType
204
+ }, lhs_relationshipName);
205
+ if (inverseKey) {
206
+ const definition = store.schema.fields({
207
+ type
208
+ }).get(inverseKey);
209
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
210
+ if (!test) {
211
+ throw new Error(`Expected the field definition to be a relationship`);
212
+ }
213
+ })(definition && (definition.kind === 'hasMany' || definition.kind === 'belongsTo')) : {};
214
+ return {
215
+ inverseKey,
216
+ kind: definition.kind
217
+ };
218
+ }
219
+ }
220
+ function relationshipDataPointsToParent(relationshipData, identifier) {
221
+ if (relationshipData === null) {
222
+ return false;
223
+ }
224
+ if (Array.isArray(relationshipData)) {
225
+ if (relationshipData.length === 0) {
226
+ return false;
227
+ }
228
+ for (let i = 0; i < relationshipData.length; i++) {
229
+ const entry = relationshipData[i];
230
+ if (validateRelationshipEntry(entry, identifier)) {
231
+ return true;
232
+ }
233
+ }
234
+ } else {
235
+ return validateRelationshipEntry(relationshipData, identifier);
236
+ }
237
+ return false;
238
+ }
239
+ function fixRelationshipData(relationshipData, relationshipKind, {
240
+ id,
241
+ type
242
+ }) {
243
+ const parentRelationshipData = {
244
+ id,
245
+ type
246
+ };
247
+ let payload = null;
248
+ if (relationshipKind === 'hasMany') {
249
+ const relData = relationshipData || [];
250
+ if (relationshipData) {
251
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
252
+ if (!test) {
253
+ throw new Error('expected the relationship data to be an array');
254
+ }
255
+ })(Array.isArray(relationshipData)) : {};
256
+ // these arrays could be massive so this is better than filter
257
+ // Note: this is potentially problematic if type/id are not in the
258
+ // same state of normalization.
259
+ const found = relationshipData.find(v => {
260
+ return v.type === parentRelationshipData.type && v.id === parentRelationshipData.id;
261
+ });
262
+ if (!found) {
263
+ relData.push(parentRelationshipData);
264
+ }
265
+ } else {
266
+ relData.push(parentRelationshipData);
267
+ }
268
+ payload = relData;
269
+ } else {
270
+ const relData = relationshipData || {};
271
+ Object.assign(relData, parentRelationshipData);
272
+ payload = relData;
273
+ }
274
+ return payload;
275
+ }
276
+ function validateRelationshipEntry({
277
+ id
278
+ }, {
279
+ id: parentModelID
280
+ }) {
281
+ return !!id && id.toString() === parentModelID;
282
+ }
283
+ const PotentialLegacyOperations = new Set(['findRecord', 'findAll', 'query', 'queryRecord', 'findBelongsTo', 'findHasMany', 'updateRecord', 'createRecord', 'deleteRecord']);
284
+ const LegacyNetworkHandler = {
285
+ request(context, next) {
286
+ // if we are not a legacy request, move on
287
+ if (context.request.url || !context.request.op || !PotentialLegacyOperations.has(context.request.op)) {
288
+ return next(context.request);
289
+ }
290
+ const {
291
+ store
292
+ } = context.request;
293
+ if (!store._fetchManager) {
294
+ store._fetchManager = new FetchManager(store);
295
+ }
296
+ switch (context.request.op) {
297
+ case 'findRecord':
298
+ return findRecord(context);
299
+ case 'findAll':
300
+ return findAll(context);
301
+ case 'query':
302
+ return query(context);
303
+ case 'queryRecord':
304
+ return queryRecord(context);
305
+ case 'findBelongsTo':
306
+ return findBelongsTo(context);
307
+ case 'findHasMany':
308
+ return findHasMany(context);
309
+ case 'updateRecord':
310
+ return saveRecord(context);
311
+ case 'createRecord':
312
+ return saveRecord(context);
313
+ case 'deleteRecord':
314
+ return saveRecord(context);
315
+ default:
316
+ return next(context.request);
317
+ }
318
+ }
319
+ };
320
+ function findBelongsTo(context) {
321
+ const {
322
+ store,
323
+ data,
324
+ records: identifiers
325
+ } = context.request;
326
+ const {
327
+ options,
328
+ record,
329
+ links,
330
+ useLink,
331
+ field
332
+ } = data;
333
+ const identifier = identifiers?.[0];
334
+
335
+ // short circuit if we are already loading
336
+ const pendingRequest = identifier && store._fetchManager.getPendingFetch(identifier, options);
337
+ if (pendingRequest) {
338
+ return pendingRequest;
339
+ }
340
+ if (useLink) {
341
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
342
+ if (!test) {
343
+ throw new Error(`Expected a related link when calling store.findBelongsTo, found ${String(links)}`);
344
+ }
345
+ })(links && links.related) : {};
346
+ return _findBelongsTo(store, record, links.related, field, options);
347
+ }
348
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
349
+ if (!test) {
350
+ throw new Error(`Expected an identifier`);
351
+ }
352
+ })(Array.isArray(identifiers) && identifiers.length === 1) : {};
353
+ const manager = store._fetchManager;
354
+ assertIdentifierHasId(identifier);
355
+ return options.reload ? manager.scheduleFetch(identifier, options, context.request) : manager.fetchDataIfNeededForIdentifier(identifier, options, context.request);
356
+ }
357
+ function findHasMany(context) {
358
+ const {
359
+ store,
360
+ data,
361
+ records: identifiers
362
+ } = context.request;
363
+ const {
364
+ options,
365
+ record,
366
+ links,
367
+ useLink,
368
+ field
369
+ } = data;
370
+
371
+ // link case
372
+ if (useLink) {
373
+ const adapter = store.adapterFor(record.type);
374
+ /*
375
+ If a relationship was originally populated by the adapter as a link
376
+ (as opposed to a list of IDs), this method is called when the
377
+ relationship is fetched.
378
+ The link (which is usually a URL) is passed through unchanged, so the
379
+ adapter can make whatever request it wants.
380
+ The usual use-case is for the server to register a URL as a link, and
381
+ then use that URL in the future to make a request for the relationship.
382
+ */
383
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
384
+ if (!test) {
385
+ throw new Error(`You tried to load a hasMany relationship but you have no adapter (for ${record.type})`);
386
+ }
387
+ })(adapter) : {};
388
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
389
+ if (!test) {
390
+ throw new Error(`You tried to load a hasMany relationship from a specified 'link' in the original payload but your adapter does not implement 'findHasMany'`);
391
+ }
392
+ })(typeof adapter.findHasMany === 'function') : {};
393
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
394
+ if (!test) {
395
+ throw new Error(`Expected a related link when calling store.findHasMany, found ${String(links)}`);
396
+ }
397
+ })(links && links.related) : {};
398
+ return _findHasMany(adapter, store, record, links.related, field, options);
399
+ }
400
+
401
+ // identifiers case
402
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
403
+ if (!test) {
404
+ throw new Error(`Expected an array of identifiers to fetch`);
405
+ }
406
+ })(Array.isArray(identifiers)) : {};
407
+ const fetches = new Array(identifiers.length);
408
+ const manager = store._fetchManager;
409
+ for (let i = 0; i < identifiers.length; i++) {
410
+ const identifier = identifiers[i];
411
+ // TODO we probably can be lenient here and return from cache for the isNew case
412
+ assertIdentifierHasId(identifier);
413
+ fetches[i] = options.reload ? manager.scheduleFetch(identifier, options, context.request) : manager.fetchDataIfNeededForIdentifier(identifier, options, context.request);
414
+ }
415
+ return Promise.all(fetches);
416
+ }
417
+ function saveRecord(context) {
418
+ const {
419
+ store,
420
+ data,
421
+ op: operation
422
+ } = context.request;
423
+ const {
424
+ options,
425
+ record: identifier
426
+ } = data;
427
+ store.cache.willCommit(identifier, context);
428
+ const saveOptions = Object.assign({
429
+ [SaveOp]: operation
430
+ }, options);
431
+ const fetchManagerPromise = store._fetchManager.scheduleSave(identifier, saveOptions);
432
+ return fetchManagerPromise.then(payload => {
433
+ let result;
434
+ store._join(() => {
435
+ // @ts-expect-error we don't have access to a response in legacy
436
+ result = store.cache.didCommit(identifier, {
437
+ request: context.request,
438
+ content: payload
439
+ });
440
+ });
441
+
442
+ // blatantly lie if we were a createRecord request
443
+ // to give some semblance of cache-control to the
444
+ // CachePolicy while legacy is still around
445
+ if (store.lifetimes?.didRequest && operation === 'createRecord') {
446
+ store.lifetimes.didRequest(context.request, {
447
+ status: 201
448
+ }, null, store);
449
+ }
450
+ return store.peekRecord(result.data);
451
+ }).catch(e => {
452
+ let err = e;
453
+ if (!e) {
454
+ err = new Error(`Unknown Error Occurred During Request`);
455
+ } else if (typeof e === 'string') {
456
+ err = new Error(e);
457
+ }
458
+ adapterDidInvalidate(store, identifier, err);
459
+ throw err;
460
+ });
461
+ }
462
+ function adapterDidInvalidate(store, identifier, error) {
463
+ if (error && error.isAdapterError === true && error.code === 'InvalidError') {
464
+ const serializer = store.serializerFor(identifier.type);
465
+
466
+ // TODO @deprecate extractErrors being called
467
+ // TODO remove extractErrors from the default serializers.
468
+ if (serializer && typeof serializer.extractErrors === 'function') {
469
+ const errorsHash = serializer.extractErrors(store, store.modelFor(identifier.type), error, identifier.id);
470
+ error.errors = errorsHashToArray(errorsHash);
471
+ }
472
+ }
473
+ const cache = store.cache;
474
+ if (error.errors) {
475
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
476
+ if (!test) {
477
+ throw new Error(`Expected the cache in use by resource ${String(identifier)} to have a getErrors(identifier) method for retrieving errors.`);
478
+ }
479
+ })(typeof cache.getErrors === 'function') : {};
480
+ let jsonApiErrors = error.errors;
481
+ if (jsonApiErrors.length === 0) {
482
+ jsonApiErrors = [{
483
+ title: 'Invalid Error',
484
+ detail: '',
485
+ source: {
486
+ pointer: '/data'
487
+ }
488
+ }];
489
+ }
490
+ cache.commitWasRejected(identifier, jsonApiErrors);
491
+ } else {
492
+ cache.commitWasRejected(identifier);
493
+ }
494
+ }
495
+ function makeArray(value) {
496
+ return Array.isArray(value) ? value : [value];
497
+ }
498
+ const PRIMARY_ATTRIBUTE_KEY = 'base';
499
+ function errorsHashToArray(errors) {
500
+ const out = [];
501
+ if (errors) {
502
+ Object.keys(errors).forEach(key => {
503
+ const messages = makeArray(errors[key]);
504
+ for (let i = 0; i < messages.length; i++) {
505
+ let title = 'Invalid Attribute';
506
+ let pointer = `/data/attributes/${key}`;
507
+ if (key === PRIMARY_ATTRIBUTE_KEY) {
508
+ title = 'Invalid Document';
509
+ pointer = `/data`;
510
+ }
511
+ out.push({
512
+ title: title,
513
+ detail: messages[i],
514
+ source: {
515
+ pointer: pointer
516
+ }
517
+ });
518
+ }
519
+ });
520
+ }
521
+ return out;
522
+ }
523
+ function findRecord(context) {
524
+ const {
525
+ store,
526
+ data
527
+ } = context.request;
528
+ const {
529
+ record: identifier,
530
+ options
531
+ } = data;
532
+ let promise;
533
+
534
+ // if not loaded start loading
535
+ if (!store._instanceCache.recordIsLoaded(identifier)) {
536
+ promise = store._fetchManager.fetchDataIfNeededForIdentifier(identifier, options, context.request);
537
+
538
+ // Refetch if the reload option is passed
539
+ } else if (options.reload) {
540
+ assertIdentifierHasId(identifier);
541
+ promise = store._fetchManager.scheduleFetch(identifier, options, context.request);
542
+ } else {
543
+ let snapshot = null;
544
+ const adapter = store.adapterFor(identifier.type);
545
+
546
+ // Refetch the record if the adapter thinks the record is stale
547
+ if (typeof options.reload === 'undefined' && adapter.shouldReloadRecord && adapter.shouldReloadRecord(store, snapshot = store._fetchManager.createSnapshot(identifier, options))) {
548
+ assertIdentifierHasId(identifier);
549
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
550
+ promise = store._fetchManager.scheduleFetch(identifier, Object.assign({}, options, {
551
+ reload: true
552
+ }), context.request);
553
+ } else {
554
+ options.reload = true;
555
+ promise = store._fetchManager.scheduleFetch(identifier, options, context.request);
556
+ }
557
+ } else {
558
+ // Trigger the background refetch if backgroundReload option is passed
559
+ if (options.backgroundReload !== false && (options.backgroundReload || !adapter.shouldBackgroundReloadRecord || adapter.shouldBackgroundReloadRecord(store, snapshot = snapshot || store._fetchManager.createSnapshot(identifier, options)))) {
560
+ assertIdentifierHasId(identifier);
561
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
562
+ void store._fetchManager.scheduleFetch(identifier, Object.assign({}, options, {
563
+ backgroundReload: true
564
+ }), context.request);
565
+ } else {
566
+ options.backgroundReload = true;
567
+ void store._fetchManager.scheduleFetch(identifier, options, context.request);
568
+ }
569
+ }
570
+
571
+ // Return the cached record
572
+ promise = Promise.resolve(identifier);
573
+ }
574
+ }
575
+ return promise.then(i => store.peekRecord(i));
576
+ }
577
+ function findAll(context) {
578
+ const {
579
+ store,
580
+ data
581
+ } = context.request;
582
+ const {
583
+ type,
584
+ options
585
+ } = data;
586
+ const adapter = store.adapterFor(type);
587
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
588
+ if (!test) {
589
+ throw new Error(`You tried to load all records but you have no adapter (for ${type})`);
590
+ }
591
+ })(adapter) : {};
592
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
593
+ if (!test) {
594
+ throw new Error(`You tried to load all records but your adapter does not implement 'findAll'`);
595
+ }
596
+ })(typeof adapter.findAll === 'function') : {};
597
+
598
+ // avoid initializing the liveArray just to set `isUpdating`
599
+ const maybeRecordArray = store.recordArrayManager._live.get(type);
600
+ const snapshotArray = new SnapshotRecordArray(store, type, options);
601
+ const shouldReload = options.reload || options.reload !== false && (adapter.shouldReloadAll && adapter.shouldReloadAll(store, snapshotArray) || !adapter.shouldReloadAll && snapshotArray.length === 0);
602
+ let fetch;
603
+ if (shouldReload) {
604
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
605
+ maybeRecordArray && (maybeRecordArray.isUpdating = true);
606
+ fetch = _findAll(adapter, store, type, snapshotArray, context.request, true);
607
+ } else {
608
+ fetch = Promise.resolve(store.peekAll(type));
609
+ if (options.backgroundReload || options.backgroundReload !== false && (!adapter.shouldBackgroundReloadAll || adapter.shouldBackgroundReloadAll(store, snapshotArray))) {
610
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
611
+ maybeRecordArray && (maybeRecordArray.isUpdating = true);
612
+ void _findAll(adapter, store, type, snapshotArray, context.request, false);
613
+ }
614
+ }
615
+ return fetch;
616
+ }
617
+ function _findAll(adapter, store, type, snapshotArray, request, isAsyncFlush) {
618
+ const schema = store.modelFor(type);
619
+ let promise = Promise.resolve().then(() => adapter.findAll(store, schema, null, snapshotArray));
620
+ promise = promise.then(adapterPayload => {
621
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
622
+ if (!test) {
623
+ throw new Error(`You made a 'findAll' request for '${type}' records, but the adapter's response did not have any data`);
624
+ }
625
+ })(payloadIsNotBlank(adapterPayload)) : {};
626
+ const serializer = store.serializerFor(type);
627
+ const payload = normalizeResponseHelper(serializer, store, schema, adapterPayload, null, 'findAll');
628
+ store._push(payload, isAsyncFlush);
629
+ snapshotArray._recordArray.isUpdating = false;
630
+ if (macroCondition(getGlobalConfig().WarpDrive.activeLogging.LOG_REQUESTS)) {
631
+ if (getGlobalConfig().WarpDrive.debug.LOG_REQUESTS || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REQUESTS) {
632
+ // eslint-disable-next-line no-console
633
+ console.log(`request: findAll<${type}> background reload complete`);
634
+ }
635
+ }
636
+ return snapshotArray._recordArray;
637
+ });
638
+ if (macroCondition(getGlobalConfig().WarpDrive.env.TESTING)) {
639
+ if (!request.disableTestWaiter) {
640
+ promise = waitFor(promise);
641
+ }
642
+ }
643
+ return promise;
644
+ }
645
+ function query(context) {
646
+ const {
647
+ store,
648
+ data
649
+ } = context.request;
650
+ let {
651
+ options
652
+ } = data;
653
+ // eslint-disable-next-line @typescript-eslint/no-shadow
654
+ const {
655
+ type,
656
+ query
657
+ } = data;
658
+ const adapter = store.adapterFor(type);
659
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
660
+ if (!test) {
661
+ throw new Error(`You tried to make a query but you have no adapter (for ${type})`);
662
+ }
663
+ })(adapter) : {};
664
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
665
+ if (!test) {
666
+ throw new Error(`You tried to make a query but your adapter does not implement 'query'`);
667
+ }
668
+ })(typeof adapter.query === 'function') : {};
669
+ const recordArray = options._recordArray || store.recordArrayManager.getCollection({
670
+ type,
671
+ query
672
+ });
673
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
674
+ options = Object.assign({}, options);
675
+ delete options._recordArray;
676
+ } else {
677
+ delete options._recordArray;
678
+ }
679
+ const schema = store.modelFor(type);
680
+ const promise = Promise.resolve().then(() => adapter.query(store, schema, query, recordArray, options));
681
+ return promise.then(adapterPayload => {
682
+ const serializer = store.serializerFor(type);
683
+ const payload = normalizeResponseHelper(serializer, store, schema, adapterPayload, null, 'query');
684
+ const identifiers = store._push(payload, true);
685
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
686
+ if (!test) {
687
+ throw new Error('The response to store.query is expected to be an array but it was a single record. Please wrap your response in an array or use `store.queryRecord` to query for a single record.');
688
+ }
689
+ })(Array.isArray(identifiers)) : {};
690
+ store.recordArrayManager.populateManagedArray(recordArray, identifiers, payload);
691
+ return recordArray;
692
+ });
693
+ }
694
+ function assertSingleResourceDocument(payload) {
695
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
696
+ if (!test) {
697
+ throw new Error(`Expected the primary data returned by the serializer for a 'queryRecord' response to be a single object or null but instead it was an array.`);
698
+ }
699
+ })(!Array.isArray(payload.data)) : {};
700
+ }
701
+ function queryRecord(context) {
702
+ const {
703
+ store,
704
+ data
705
+ } = context.request;
706
+ // eslint-disable-next-line @typescript-eslint/no-shadow
707
+ const {
708
+ type,
709
+ query,
710
+ options
711
+ } = data;
712
+ const adapter = store.adapterFor(type);
713
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
714
+ if (!test) {
715
+ throw new Error(`You tried to make a query but you have no adapter (for ${type})`);
716
+ }
717
+ })(adapter) : {};
718
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
719
+ if (!test) {
720
+ throw new Error(`You tried to make a query but your adapter does not implement 'queryRecord'`);
721
+ }
722
+ })(typeof adapter.queryRecord === 'function') : {};
723
+ const schema = store.modelFor(type);
724
+ const promise = Promise.resolve().then(() => adapter.queryRecord(store, schema, query, options));
725
+ return promise.then(adapterPayload => {
726
+ const serializer = store.serializerFor(type);
727
+ const payload = normalizeResponseHelper(serializer, store, schema, adapterPayload, null, 'queryRecord');
728
+ assertSingleResourceDocument(payload);
729
+ const identifier = store._push(payload, true);
730
+ return identifier ? store.peekRecord(identifier) : null;
731
+ });
732
+ }
733
+
734
+ /**
735
+ Returns an instance of the adapter for a given type. For
736
+ example, `adapterFor('person')` will return an instance of
737
+ the adapter located at `app/adapters/person.js`
738
+
739
+ If no `person` adapter is found, this method will look
740
+ for an `application` adapter (the default adapter for
741
+ your entire application).
742
+
743
+ @public
744
+ @param {String} modelName
745
+ @return {Adapter}
746
+ */
747
+
748
+ function adapterFor(modelName, _allowMissing) {
749
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
750
+ if (!test) {
751
+ throw new Error(`Attempted to call store.adapterFor(), but the store instance has already been destroyed.`);
752
+ }
753
+ })(!(this.isDestroying || this.isDestroyed)) : {};
754
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
755
+ if (!test) {
756
+ throw new Error(`You need to pass a model name to the store's adapterFor method`);
757
+ }
758
+ })(modelName) : {};
759
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
760
+ if (!test) {
761
+ throw new Error(`Passing classes to store.adapterFor has been removed. Please pass a dasherized string instead of ${modelName}`);
762
+ }
763
+ })(typeof modelName === 'string') : {};
764
+ this._adapterCache = this._adapterCache || Object.create(null);
765
+ const normalizedModelName = _deprecatingNormalize(modelName);
766
+ const {
767
+ _adapterCache
768
+ } = this;
769
+ let adapter = _adapterCache[normalizedModelName];
770
+ if (adapter) {
771
+ return adapter;
772
+ }
773
+ const owner = getOwner(this);
774
+
775
+ // name specific adapter
776
+ adapter = owner.lookup(`adapter:${normalizedModelName}`);
777
+ if (adapter !== undefined) {
778
+ _adapterCache[normalizedModelName] = adapter;
779
+ return adapter;
780
+ }
781
+
782
+ // no adapter found for the specific name, fallback and check for application adapter
783
+ adapter = _adapterCache.application || owner.lookup('adapter:application');
784
+ if (adapter !== undefined) {
785
+ _adapterCache[normalizedModelName] = adapter;
786
+ _adapterCache.application = adapter;
787
+ return adapter;
788
+ }
789
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
790
+ if (!test) {
791
+ throw new Error(`No adapter was found for '${modelName}' and no 'application' adapter was found as a fallback.`);
792
+ }
793
+ })(_allowMissing) : {};
794
+ }
795
+
796
+ /**
797
+ Returns an instance of the serializer for a given type. For
798
+ example, `serializerFor('person')` will return an instance of
799
+ `App.PersonSerializer`.
800
+
801
+ If no `App.PersonSerializer` is found, this method will look
802
+ for an `App.ApplicationSerializer` (the default serializer for
803
+ your entire application).
804
+
805
+ If a serializer cannot be found on the adapter, it will fall back
806
+ to an instance of `JSONSerializer`.
807
+
808
+ @public
809
+ @param {String} modelName the record to serialize
810
+ @return {Serializer}
811
+ */
812
+ function serializerFor(modelName) {
813
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
814
+ if (!test) {
815
+ throw new Error(`Attempted to call store.serializerFor(), but the store instance has already been destroyed.`);
816
+ }
817
+ })(!(this.isDestroying || this.isDestroyed)) : {};
818
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
819
+ if (!test) {
820
+ throw new Error(`You need to pass a model name to the store's serializerFor method`);
821
+ }
822
+ })(modelName) : {};
823
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
824
+ if (!test) {
825
+ throw new Error(`Passing classes to store.serializerFor has been removed. Please pass a dasherized string instead of ${modelName}`);
826
+ }
827
+ })(typeof modelName === 'string') : {};
828
+ this._serializerCache = this._serializerCache || Object.create(null);
829
+ const normalizedModelName = _deprecatingNormalize(modelName);
830
+ const {
831
+ _serializerCache
832
+ } = this;
833
+ let serializer = _serializerCache[normalizedModelName];
834
+ if (serializer) {
835
+ return serializer;
836
+ }
837
+
838
+ // by name
839
+ const owner = getOwner(this);
840
+ serializer = owner.lookup(`serializer:${normalizedModelName}`);
841
+ if (serializer !== undefined) {
842
+ _serializerCache[normalizedModelName] = serializer;
843
+ return serializer;
844
+ }
845
+
846
+ // no serializer found for the specific model, fallback and check for application serializer
847
+ serializer = _serializerCache.application || owner.lookup('serializer:application');
848
+ if (serializer !== undefined) {
849
+ _serializerCache[normalizedModelName] = serializer;
850
+ _serializerCache.application = serializer;
851
+ return serializer;
852
+ }
853
+ return null;
854
+ }
855
+
856
+ /**
857
+ `normalize` converts a json payload into the normalized form that
858
+ [push](../methods/push?anchor=push) expects.
859
+
860
+ Example
861
+
862
+ ```js
863
+ socket.on('message', function(message) {
864
+ let modelName = message.model;
865
+ let data = message.data;
866
+ store.push(store.normalize(modelName, data));
867
+ });
868
+ ```
869
+
870
+ @public
871
+ @param {String} modelName The name of the model type for this payload
872
+ @param {Object} payload
873
+ @return {Object} The normalized payload
874
+ */
875
+ // TODO @runspired @deprecate users should call normalize on the associated serializer directly
876
+ function normalize(modelName, payload) {
877
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
878
+ if (!test) {
879
+ throw new Error(`Attempted to call store.normalize(), but the store instance has already been destroyed.`);
880
+ }
881
+ })(!(this.isDestroying || this.isDestroyed)) : {};
882
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
883
+ if (!test) {
884
+ throw new Error(`You need to pass a model name to the store's normalize method`);
885
+ }
886
+ })(modelName) : {};
887
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
888
+ if (!test) {
889
+ throw new Error(`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${typeof modelName}`);
890
+ }
891
+ })(typeof modelName === 'string') : {};
892
+ const normalizedModelName = _deprecatingNormalize(modelName);
893
+ const serializer = this.serializerFor(normalizedModelName);
894
+ const schema = this.modelFor(normalizedModelName);
895
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
896
+ if (!test) {
897
+ throw new Error(`You must define a normalize method in your serializer in order to call store.normalize`);
898
+ }
899
+ })(typeof serializer?.normalize === 'function') : {};
900
+ return serializer.normalize(schema, payload);
901
+ }
902
+
903
+ /**
904
+ Push some raw data into the store.
905
+
906
+ This method can be used both to push in brand new
907
+ records, as well as to update existing records. You
908
+ can push in more than one type of object at once.
909
+ All objects should be in the format expected by the
910
+ serializer.
911
+
912
+ ```js [app/serializers/application.js]
913
+ import RESTSerializer from '@ember-data/serializer/rest';
914
+
915
+ export default class ApplicationSerializer extends RESTSerializer;
916
+ ```
917
+
918
+ ```js
919
+ let pushData = {
920
+ posts: [
921
+ { id: 1, postTitle: "Great post", commentIds: [2] }
922
+ ],
923
+ comments: [
924
+ { id: 2, commentBody: "Insightful comment" }
925
+ ]
926
+ }
927
+
928
+ store.pushPayload(pushData);
929
+ ```
930
+
931
+ By default, the data will be deserialized using a default
932
+ serializer (the application serializer if it exists).
933
+
934
+ Alternatively, `pushPayload` will accept a model type which
935
+ will determine which serializer will process the payload.
936
+
937
+ ```js [app/serializers/application.js]
938
+ import RESTSerializer from '@ember-data/serializer/rest';
939
+
940
+ export default class ApplicationSerializer extends RESTSerializer;
941
+ ```
942
+
943
+ ```js [app/serializers/post.js]
944
+ import JSONSerializer from '@ember-data/serializer/json';
945
+
946
+ export default JSONSerializer;
947
+ ```
948
+
949
+ ```js
950
+ store.pushPayload(pushData); // Will use the application serializer
951
+ store.pushPayload('post', pushData); // Will use the post serializer
952
+ ```
953
+
954
+ @public
955
+ @param {String} modelName Optionally, a model type used to determine which serializer will be used
956
+ @param {Object} inputPayload
957
+ */
958
+ // TODO @runspired @deprecate pushPayload in favor of looking up the serializer
959
+ function pushPayload(modelName, inputPayload) {
960
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
961
+ if (!test) {
962
+ throw new Error(`Attempted to call store.pushPayload(), but the store instance has already been destroyed.`);
963
+ }
964
+ })(!(this.isDestroying || this.isDestroyed)) : {};
965
+ const payload = inputPayload || modelName;
966
+ const normalizedModelName = inputPayload ? _deprecatingNormalize(modelName) : 'application';
967
+ const serializer = this.serializerFor(normalizedModelName);
968
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
969
+ if (!test) {
970
+ throw new Error(`You cannot use 'store.pushPayload(<type>, <payload>)' unless the serializer for '${normalizedModelName}' defines 'pushPayload'`);
971
+ }
972
+ })(serializer && typeof serializer.pushPayload === 'function') : {};
973
+ serializer.pushPayload(this, payload);
974
+ }
975
+
976
+ // TODO @runspired @deprecate records should implement their own serialization if desired
977
+ function serializeRecord(record, options) {
978
+ // TODO we used to check if the record was destroyed here
979
+ if (!this._fetchManager) {
980
+ this._fetchManager = new FetchManager(this);
981
+ }
982
+ return this._fetchManager.createSnapshot(recordIdentifierFor(record)).serialize(options);
983
+ }
984
+ function cleanup() {
985
+ // enqueue destruction of any adapters/serializers we have created
986
+ for (const adapterName in this._adapterCache) {
987
+ const adapter = this._adapterCache[adapterName];
988
+ if (typeof adapter.destroy === 'function') {
989
+ adapter.destroy();
990
+ }
991
+ }
992
+ for (const serializerName in this._serializerCache) {
993
+ const serializer = this._serializerCache[serializerName];
994
+ if (typeof serializer.destroy === 'function') {
995
+ serializer.destroy();
996
+ }
997
+ }
998
+ }
999
+ export { LegacyNetworkHandler, adapterFor, cleanup, normalize, pushPayload, serializeRecord, serializerFor };