@warp-drive/core 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 (248) hide show
  1. package/README.md +22 -38
  2. package/declarations/build-config.d.ts +18 -1
  3. package/declarations/graph/-private/-edge-definition.d.ts +12 -2
  4. package/declarations/index.d.ts +90 -8
  5. package/declarations/reactive/-private/document.d.ts +58 -46
  6. package/declarations/reactive/-private/record.d.ts +10 -1
  7. package/declarations/reactive/-private/schema.d.ts +77 -4
  8. package/declarations/reactive/-private.d.ts +1 -0
  9. package/declarations/reactive.d.ts +13 -7
  10. package/declarations/request/-private/types.d.ts +1 -1
  11. package/declarations/request.d.ts +47 -0
  12. package/declarations/store/-private/caches/instance-cache.d.ts +5 -6
  13. package/declarations/store/-private/default-cache-policy.d.ts +147 -129
  14. package/declarations/store/-private/managers/cache-capabilities-manager.d.ts +1 -1
  15. package/declarations/store/-private/managers/cache-key-manager.d.ts +26 -8
  16. package/declarations/store/-private/managers/cache-manager.d.ts +6 -4
  17. package/declarations/store/-private/managers/notification-manager.d.ts +1 -1
  18. package/declarations/store/-private/new-core-tmp/promise-state.d.ts +1 -0
  19. package/declarations/store/-private/new-core-tmp/request-state.d.ts +1 -1
  20. package/declarations/store/-private/store-service.d.ts +43 -64
  21. package/declarations/store/-private.d.ts +0 -1
  22. package/declarations/store/-types/q/cache-capabilities-manager.d.ts +1 -1
  23. package/declarations/store/deprecated/-private.d.ts +1 -1
  24. package/declarations/store/deprecated/store.d.ts +33 -32
  25. package/declarations/store.d.ts +1 -0
  26. package/declarations/types/cache.d.ts +8 -6
  27. package/declarations/types/record.d.ts +132 -0
  28. package/declarations/types/request.d.ts +26 -14
  29. package/declarations/types/schema/fields.d.ts +33 -9
  30. package/declarations/{store/-types/q → types/schema}/schema-service.d.ts +15 -13
  31. package/declarations/types/spec/document.d.ts +34 -0
  32. package/declarations/types/symbols.d.ts +2 -2
  33. package/declarations/types.d.ts +1 -1
  34. package/dist/build-config.js +1 -1
  35. package/dist/default-cache-policy-D7_u4YRH.js +572 -0
  36. package/dist/graph/-private.js +13 -4
  37. package/dist/{request-state-CUuZzgvE.js → index-BKcD4JZK.js} +10018 -8847
  38. package/dist/index.js +6 -382
  39. package/dist/reactive.js +4 -778
  40. package/dist/{context-C_7OLieY.js → request-oqoLC9rz.js} +219 -172
  41. package/dist/request.js +1 -1
  42. package/dist/store/-private.js +1 -1
  43. package/dist/store.js +1 -533
  44. package/dist/types/-private.js +1 -1
  45. package/dist/types/record.js +127 -0
  46. package/dist/types/request.js +14 -12
  47. package/dist/types/schema/fields.js +14 -0
  48. package/dist/types/schema/schema-service.js +0 -0
  49. package/dist/types/symbols.js +2 -2
  50. package/dist/unpkg/dev/-private-3C1OkYtZ.js +39 -0
  51. package/dist/unpkg/dev/build-config/babel-macros.js +1 -0
  52. package/dist/unpkg/dev/build-config/canary-features.js +1 -0
  53. package/dist/unpkg/dev/build-config/debugging.js +1 -0
  54. package/dist/unpkg/dev/build-config/deprecations.js +1 -0
  55. package/dist/unpkg/dev/build-config/env.js +1 -0
  56. package/dist/unpkg/dev/build-config/macros.js +1 -0
  57. package/dist/unpkg/dev/build-config.js +1 -0
  58. package/dist/unpkg/dev/configure-BC66sfNO.js +183 -0
  59. package/dist/unpkg/dev/configure.js +1 -0
  60. package/dist/unpkg/dev/graph/-private.js +3131 -0
  61. package/dist/unpkg/dev/index-DqhXrNZ_.js +11160 -0
  62. package/dist/unpkg/dev/index.js +6 -0
  63. package/dist/unpkg/dev/reactive/-private.js +1 -0
  64. package/dist/unpkg/dev/reactive.js +127 -0
  65. package/dist/unpkg/dev/request-CA9K0gXq.js +719 -0
  66. package/dist/unpkg/dev/request.js +1 -0
  67. package/dist/unpkg/dev/runtime-DGG4CvlW.js +135 -0
  68. package/dist/unpkg/dev/store/-private.js +56 -0
  69. package/dist/unpkg/dev/store.js +558 -0
  70. package/dist/unpkg/dev/types/-private.js +69 -0
  71. package/dist/unpkg/dev/types/cache/aliases.js +0 -0
  72. package/dist/unpkg/dev/types/cache/change.js +0 -0
  73. package/dist/unpkg/dev/types/cache/mutations.js +0 -0
  74. package/dist/unpkg/dev/types/cache/operations.js +0 -0
  75. package/dist/unpkg/dev/types/cache/relationship.js +0 -0
  76. package/dist/unpkg/dev/types/cache.js +0 -0
  77. package/dist/unpkg/dev/types/graph.js +0 -0
  78. package/dist/unpkg/dev/types/identifier.js +61 -0
  79. package/dist/unpkg/dev/types/json/raw.js +0 -0
  80. package/dist/unpkg/dev/types/params.js +0 -0
  81. package/dist/unpkg/dev/types/record.js +191 -0
  82. package/dist/unpkg/dev/types/request.js +77 -0
  83. package/dist/unpkg/dev/types/runtime.js +34 -0
  84. package/dist/unpkg/dev/types/schema/concepts.js +0 -0
  85. package/dist/unpkg/dev/types/schema/fields.js +505 -0
  86. package/dist/unpkg/dev/types/schema/fields.type-test.js +0 -0
  87. package/dist/unpkg/dev/types/schema/schema-service.js +0 -0
  88. package/dist/unpkg/dev/types/spec/document.js +0 -0
  89. package/dist/unpkg/dev/types/spec/error.js +0 -0
  90. package/dist/unpkg/dev/types/spec/json-api-raw.js +0 -0
  91. package/dist/unpkg/dev/types/symbols.js +84 -0
  92. package/dist/unpkg/dev/types/utils.js +0 -0
  93. package/dist/unpkg/dev/types.js +0 -0
  94. package/dist/unpkg/dev/utils/string.js +91 -0
  95. package/dist/unpkg/dev-deprecated/-private-3C1OkYtZ.js +39 -0
  96. package/dist/unpkg/dev-deprecated/build-config/babel-macros.js +1 -0
  97. package/dist/unpkg/dev-deprecated/build-config/canary-features.js +1 -0
  98. package/dist/unpkg/dev-deprecated/build-config/debugging.js +1 -0
  99. package/dist/unpkg/dev-deprecated/build-config/deprecations.js +1 -0
  100. package/dist/unpkg/dev-deprecated/build-config/env.js +1 -0
  101. package/dist/unpkg/dev-deprecated/build-config/macros.js +1 -0
  102. package/dist/unpkg/dev-deprecated/build-config.js +1 -0
  103. package/dist/unpkg/dev-deprecated/configure-BC66sfNO.js +183 -0
  104. package/dist/unpkg/dev-deprecated/configure.js +1 -0
  105. package/dist/unpkg/dev-deprecated/graph/-private.js +3326 -0
  106. package/dist/unpkg/dev-deprecated/index-BBlq5is_.js +11775 -0
  107. package/dist/unpkg/dev-deprecated/index.js +5 -0
  108. package/dist/unpkg/dev-deprecated/reactive/-private.js +1 -0
  109. package/dist/unpkg/dev-deprecated/reactive.js +127 -0
  110. package/dist/unpkg/dev-deprecated/request-CA9K0gXq.js +719 -0
  111. package/dist/unpkg/dev-deprecated/request.js +1 -0
  112. package/dist/unpkg/dev-deprecated/runtime-DfhJzpZH.js +135 -0
  113. package/dist/unpkg/dev-deprecated/store/-private.js +2 -0
  114. package/dist/unpkg/dev-deprecated/store.js +558 -0
  115. package/dist/unpkg/dev-deprecated/types/-private.js +69 -0
  116. package/dist/unpkg/dev-deprecated/types/cache/aliases.js +0 -0
  117. package/dist/unpkg/dev-deprecated/types/cache/change.js +0 -0
  118. package/dist/unpkg/dev-deprecated/types/cache/mutations.js +0 -0
  119. package/dist/unpkg/dev-deprecated/types/cache/operations.js +0 -0
  120. package/dist/unpkg/dev-deprecated/types/cache/relationship.js +0 -0
  121. package/dist/unpkg/dev-deprecated/types/cache.js +0 -0
  122. package/dist/unpkg/dev-deprecated/types/graph.js +0 -0
  123. package/dist/unpkg/dev-deprecated/types/identifier.js +61 -0
  124. package/dist/unpkg/dev-deprecated/types/json/raw.js +0 -0
  125. package/dist/unpkg/dev-deprecated/types/params.js +0 -0
  126. package/dist/unpkg/dev-deprecated/types/record.js +191 -0
  127. package/dist/unpkg/dev-deprecated/types/request.js +77 -0
  128. package/dist/unpkg/dev-deprecated/types/runtime.js +34 -0
  129. package/dist/unpkg/dev-deprecated/types/schema/concepts.js +0 -0
  130. package/dist/unpkg/dev-deprecated/types/schema/fields.js +505 -0
  131. package/dist/unpkg/dev-deprecated/types/schema/fields.type-test.js +0 -0
  132. package/dist/unpkg/dev-deprecated/types/schema/schema-service.js +0 -0
  133. package/dist/unpkg/dev-deprecated/types/spec/document.js +0 -0
  134. package/dist/unpkg/dev-deprecated/types/spec/error.js +0 -0
  135. package/dist/unpkg/dev-deprecated/types/spec/json-api-raw.js +0 -0
  136. package/dist/unpkg/dev-deprecated/types/symbols.js +84 -0
  137. package/dist/unpkg/dev-deprecated/types/utils.js +0 -0
  138. package/dist/unpkg/dev-deprecated/types.js +0 -0
  139. package/dist/unpkg/dev-deprecated/utils/string.js +91 -0
  140. package/dist/unpkg/prod/-private-3C1OkYtZ.js +39 -0
  141. package/dist/unpkg/prod/build-config/babel-macros.js +1 -0
  142. package/dist/unpkg/prod/build-config/canary-features.js +1 -0
  143. package/dist/unpkg/prod/build-config/debugging.js +1 -0
  144. package/dist/unpkg/prod/build-config/deprecations.js +1 -0
  145. package/dist/unpkg/prod/build-config/env.js +1 -0
  146. package/dist/unpkg/prod/build-config/macros.js +1 -0
  147. package/dist/unpkg/prod/build-config.js +1 -0
  148. package/dist/unpkg/prod/configure-C0C1LpG6.js +158 -0
  149. package/dist/unpkg/prod/configure.js +1 -0
  150. package/dist/unpkg/prod/graph/-private.js +2234 -0
  151. package/dist/unpkg/prod/handler-LAyD1Y5l.js +1619 -0
  152. package/dist/unpkg/prod/hooks-BfiqDg3O.js +26 -0
  153. package/dist/unpkg/prod/index.js +481 -0
  154. package/dist/unpkg/prod/promise-state-ipG60SdD.js +6738 -0
  155. package/dist/unpkg/prod/reactive/-private.js +1 -0
  156. package/dist/unpkg/prod/reactive.js +127 -0
  157. package/dist/unpkg/prod/request-CN2LxbYX.js +437 -0
  158. package/dist/unpkg/prod/request.js +1 -0
  159. package/dist/unpkg/prod/store/-private.js +127 -0
  160. package/dist/unpkg/prod/store.js +437 -0
  161. package/dist/unpkg/prod/types/-private.js +49 -0
  162. package/dist/unpkg/prod/types/cache/aliases.js +0 -0
  163. package/dist/unpkg/prod/types/cache/change.js +0 -0
  164. package/dist/unpkg/prod/types/cache/mutations.js +0 -0
  165. package/dist/unpkg/prod/types/cache/operations.js +0 -0
  166. package/dist/unpkg/prod/types/cache/relationship.js +0 -0
  167. package/dist/unpkg/prod/types/cache.js +0 -0
  168. package/dist/unpkg/prod/types/graph.js +0 -0
  169. package/dist/unpkg/prod/types/identifier.js +61 -0
  170. package/dist/unpkg/prod/types/json/raw.js +0 -0
  171. package/dist/unpkg/prod/types/params.js +0 -0
  172. package/dist/unpkg/prod/types/record.js +191 -0
  173. package/dist/unpkg/prod/types/request.js +77 -0
  174. package/dist/unpkg/prod/types/runtime.js +34 -0
  175. package/dist/unpkg/prod/types/schema/concepts.js +0 -0
  176. package/dist/unpkg/prod/types/schema/fields.js +505 -0
  177. package/dist/unpkg/prod/types/schema/fields.type-test.js +0 -0
  178. package/dist/unpkg/prod/types/schema/schema-service.js +0 -0
  179. package/dist/unpkg/prod/types/spec/document.js +0 -0
  180. package/dist/unpkg/prod/types/spec/error.js +0 -0
  181. package/dist/unpkg/prod/types/spec/json-api-raw.js +0 -0
  182. package/dist/unpkg/prod/types/symbols.js +84 -0
  183. package/dist/unpkg/prod/types/utils.js +0 -0
  184. package/dist/unpkg/prod/types.js +0 -0
  185. package/dist/unpkg/prod/utils/string.js +72 -0
  186. package/dist/unpkg/prod-deprecated/-private-3C1OkYtZ.js +39 -0
  187. package/dist/unpkg/prod-deprecated/build-config/babel-macros.js +1 -0
  188. package/dist/unpkg/prod-deprecated/build-config/canary-features.js +1 -0
  189. package/dist/unpkg/prod-deprecated/build-config/debugging.js +1 -0
  190. package/dist/unpkg/prod-deprecated/build-config/deprecations.js +1 -0
  191. package/dist/unpkg/prod-deprecated/build-config/env.js +1 -0
  192. package/dist/unpkg/prod-deprecated/build-config/macros.js +1 -0
  193. package/dist/unpkg/prod-deprecated/build-config.js +1 -0
  194. package/dist/unpkg/prod-deprecated/configure-BQ8CpIcW.js +158 -0
  195. package/dist/unpkg/prod-deprecated/configure.js +1 -0
  196. package/dist/unpkg/prod-deprecated/graph/-private.js +2407 -0
  197. package/dist/unpkg/prod-deprecated/handler-D639oFvl.js +334 -0
  198. package/dist/unpkg/prod-deprecated/hooks-DGvi9teJ.js +26 -0
  199. package/dist/unpkg/prod-deprecated/index.js +481 -0
  200. package/dist/unpkg/prod-deprecated/promise-state-CYvoIPna.js +8458 -0
  201. package/dist/unpkg/prod-deprecated/reactive/-private.js +1 -0
  202. package/dist/unpkg/prod-deprecated/reactive.js +126 -0
  203. package/dist/unpkg/prod-deprecated/request-CN2LxbYX.js +437 -0
  204. package/dist/unpkg/prod-deprecated/request.js +1 -0
  205. package/dist/unpkg/prod-deprecated/store/-private.js +89 -0
  206. package/dist/unpkg/prod-deprecated/store.js +437 -0
  207. package/dist/unpkg/prod-deprecated/types/-private.js +49 -0
  208. package/dist/unpkg/prod-deprecated/types/cache/aliases.js +0 -0
  209. package/dist/unpkg/prod-deprecated/types/cache/change.js +0 -0
  210. package/dist/unpkg/prod-deprecated/types/cache/mutations.js +0 -0
  211. package/dist/unpkg/prod-deprecated/types/cache/operations.js +0 -0
  212. package/dist/unpkg/prod-deprecated/types/cache/relationship.js +0 -0
  213. package/dist/unpkg/prod-deprecated/types/cache.js +0 -0
  214. package/dist/unpkg/prod-deprecated/types/graph.js +0 -0
  215. package/dist/unpkg/prod-deprecated/types/identifier.js +61 -0
  216. package/dist/unpkg/prod-deprecated/types/json/raw.js +0 -0
  217. package/dist/unpkg/prod-deprecated/types/params.js +0 -0
  218. package/dist/unpkg/prod-deprecated/types/record.js +191 -0
  219. package/dist/unpkg/prod-deprecated/types/request.js +77 -0
  220. package/dist/unpkg/prod-deprecated/types/runtime.js +34 -0
  221. package/dist/unpkg/prod-deprecated/types/schema/concepts.js +0 -0
  222. package/dist/unpkg/prod-deprecated/types/schema/fields.js +505 -0
  223. package/dist/unpkg/prod-deprecated/types/schema/fields.type-test.js +0 -0
  224. package/dist/unpkg/prod-deprecated/types/schema/schema-service.js +0 -0
  225. package/dist/unpkg/prod-deprecated/types/spec/document.js +0 -0
  226. package/dist/unpkg/prod-deprecated/types/spec/error.js +0 -0
  227. package/dist/unpkg/prod-deprecated/types/spec/json-api-raw.js +0 -0
  228. package/dist/unpkg/prod-deprecated/types/symbols.js +84 -0
  229. package/dist/unpkg/prod-deprecated/types/utils.js +0 -0
  230. package/dist/unpkg/prod-deprecated/types.js +0 -0
  231. package/dist/unpkg/prod-deprecated/utils/string.js +72 -0
  232. package/logos/README.md +2 -2
  233. package/logos/logo-yellow-slab.svg +1 -0
  234. package/logos/word-mark-black.svg +1 -0
  235. package/logos/word-mark-white.svg +1 -0
  236. package/package.json +11 -3
  237. package/logos/NCC-1701-a-blue.svg +0 -4
  238. package/logos/NCC-1701-a-gold.svg +0 -4
  239. package/logos/NCC-1701-a-gold_100.svg +0 -1
  240. package/logos/NCC-1701-a-gold_base-64.txt +0 -1
  241. package/logos/NCC-1701-a.svg +0 -4
  242. package/logos/docs-badge.svg +0 -2
  243. package/logos/ember-data-logo-dark.svg +0 -12
  244. package/logos/ember-data-logo-light.svg +0 -12
  245. package/logos/social1.png +0 -0
  246. package/logos/social2.png +0 -0
  247. package/logos/warp-drive-logo-dark.svg +0 -4
  248. package/logos/warp-drive-logo-gold.svg +0 -4
@@ -0,0 +1,26 @@
1
+ import { R as ReactiveResource } from "./promise-state-ipG60SdD.js";
2
+ import { D as Destroy } from "./-private-3C1OkYtZ.js";
3
+ function instantiateRecord(store, identifier, createArgs) {
4
+ const schema = store.schema;
5
+ const resourceSchema = schema.resource(identifier);
6
+ const legacy = resourceSchema?.legacy ?? false;
7
+ const editable = legacy;
8
+ const record = new ReactiveResource({
9
+ store,
10
+ resourceKey: identifier,
11
+ modeName: legacy ? 'legacy' : 'polaris',
12
+ legacy: legacy,
13
+ editable: editable,
14
+ path: null,
15
+ field: null,
16
+ value: null
17
+ });
18
+ if (createArgs && editable) {
19
+ Object.assign(record, createArgs);
20
+ }
21
+ return record;
22
+ }
23
+ function teardownRecord(record) {
24
+ record[Destroy]();
25
+ }
26
+ export { instantiateRecord as i, teardownRecord as t };
@@ -0,0 +1,481 @@
1
+ import { r as registerDerivations, S as SchemaService } from "./promise-state-ipG60SdD.js";
2
+ export { e as cacheKeyFor, e as recordIdentifierFor, j as setIdentifierForgetMethod, h as setIdentifierGenerationMethod, k as setIdentifierResetMethod, i as setIdentifierUpdateMethod, l as setKeyInfoForResource, s as storeFor } from "./promise-state-ipG60SdD.js";
3
+ import { i as instantiateRecord, t as teardownRecord } from "./hooks-BfiqDg3O.js";
4
+ import { peekUniversalTransient, setUniversalTransient } from './types/-private.js';
5
+ import { S as Store, C as CacheHandler } from "./handler-LAyD1Y5l.js";
6
+ import { c as cloneResponseProperties, I as IS_CACHE_HANDLER, e as executeNextHandler, g as getRequestResult, u as upgradePromise, s as setPromiseResult, a as clearRequestResult } from "./request-CN2LxbYX.js";
7
+ import { EnableHydration } from './types/request.js';
8
+ import { DefaultCachePolicy } from './store.js';
9
+ import './types/runtime.js';
10
+
11
+ // Lazily close over fetch to avoid breaking Mirage
12
+ const _fetch = typeof fetch !== 'undefined' ? (...args) => fetch(...args) : typeof FastBoot !== 'undefined' ? (...args) => FastBoot.require('node-fetch')(...args) : () => {
13
+ throw new Error('No Fetch Implementation Found');
14
+ };
15
+
16
+ // clones a response in a way that should still
17
+ // allow it to stream
18
+ function cloneResponse(response, overrides) {
19
+ const props = cloneResponseProperties(response);
20
+ return new Response(response.body, Object.assign(props, overrides));
21
+ }
22
+ const MUTATION_OPS = new Set(['updateRecord', 'createRecord', 'deleteRecord']);
23
+ const ERROR_STATUS_CODE_FOR = new Map([[400, 'Bad Request'], [401, 'Unauthorized'], [402, 'Payment Required'], [403, 'Forbidden'], [404, 'Not Found'], [405, 'Method Not Allowed'], [406, 'Not Acceptable'], [407, 'Proxy Authentication Required'], [408, 'Request Timeout'], [409, 'Conflict'], [410, 'Gone'], [411, 'Length Required'], [412, 'Precondition Failed'], [413, 'Payload Too Large'], [414, 'URI Too Long'], [415, 'Unsupported Media Type'], [416, 'Range Not Satisfiable'], [417, 'Expectation Failed'], [419, 'Page Expired'], [420, 'Enhance Your Calm'], [421, 'Misdirected Request'], [422, 'Unprocessable Entity'], [423, 'Locked'], [424, 'Failed Dependency'], [425, 'Too Early'], [426, 'Upgrade Required'], [428, 'Precondition Required'], [429, 'Too Many Requests'], [430, 'Request Header Fields Too Large'], [431, 'Request Header Fields Too Large'], [450, 'Blocked By Windows Parental Controls'], [451, 'Unavailable For Legal Reasons'], [500, 'Internal Server Error'], [501, 'Not Implemented'], [502, 'Bad Gateway'], [503, 'Service Unavailable'], [504, 'Gateway Timeout'], [505, 'HTTP Version Not Supported'], [506, 'Variant Also Negotiates'], [507, 'Insufficient Storage'], [508, 'Loop Detected'], [509, 'Bandwidth Limit Exceeded'], [510, 'Not Extended'], [511, 'Network Authentication Required']]);
24
+
25
+ /**
26
+ * ```ts
27
+ * import { Fetch } from '@warp-drive/core';
28
+ * ```
29
+ *
30
+ * A basic Fetch Handler which converts a request into a
31
+ * `fetch` call presuming the response to be `json`.
32
+ *
33
+ * ```ts
34
+ * import { RequestManager, Fetch } from '@warp-drive/core';
35
+ *
36
+ * const manager = new RequestManager()
37
+ * .use([Fetch]);
38
+ * ```
39
+ *
40
+ * @public
41
+ */
42
+ const Fetch = {
43
+ async request(context) {
44
+ let response;
45
+ try {
46
+ ({});
47
+ response = await _fetch(context.request.url, context.request);
48
+ } catch (e) {
49
+ if (e instanceof DOMException && e.name === 'AbortError') {
50
+ e.statusText = 'Aborted';
51
+ e.status = 20;
52
+ e.isRequestError = true;
53
+ } else {
54
+ e.statusText = 'Unknown Network Error';
55
+ e.status = 0;
56
+ if (!(e instanceof DOMException)) {
57
+ e.code = 0;
58
+ }
59
+ e.isRequestError = true;
60
+ }
61
+ throw e;
62
+ }
63
+ const isError = !response.ok || response.status >= 400;
64
+ const op = context.request.op;
65
+ const isMutationOp = Boolean(op && MUTATION_OPS.has(op));
66
+ if (!isError && !isMutationOp && response.status !== 204 && !response.headers.has('date')) {
67
+ {
68
+ const headers = new Headers(response.headers);
69
+ headers.set('date', new Date().toUTCString());
70
+ response = cloneResponse(response, {
71
+ headers
72
+ });
73
+ }
74
+ }
75
+ context.setResponse(response);
76
+ if (response.status === 204) {
77
+ return null;
78
+ }
79
+ let text = '';
80
+ // if we are in a mirage context, we cannot support streaming
81
+ {
82
+ const reader = response.body.getReader();
83
+ const decoder = new TextDecoder();
84
+ let isStreaming = context.hasRequestedStream;
85
+ let stream = isStreaming ? new TransformStream() : null;
86
+ let writer = stream?.writable.getWriter();
87
+ if (isStreaming) {
88
+ // Listen for the abort event on the AbortSignal
89
+ context.request.signal?.addEventListener('abort', () => {
90
+ if (!isStreaming) {
91
+ return;
92
+ }
93
+ void stream.writable.abort('Request Aborted');
94
+ void stream.readable.cancel('Request Aborted');
95
+ });
96
+ context.setStream(stream.readable);
97
+ }
98
+ while (true) {
99
+ // we manually read the stream instead of using `response.json()`
100
+ // or `response.text()` because if we need to stream the body
101
+ // we need to be able to pass the stream along efficiently.
102
+ const {
103
+ done,
104
+ value
105
+ } = await reader.read();
106
+ if (done) {
107
+ if (isStreaming) {
108
+ isStreaming = false;
109
+ await writer.ready;
110
+ await writer.close();
111
+ }
112
+ break;
113
+ }
114
+ text += decoder.decode(value, {
115
+ stream: true
116
+ });
117
+
118
+ // if we are streaming, we want to pass the stream along
119
+ if (isStreaming) {
120
+ await writer.ready;
121
+ await writer.write(value);
122
+ } else if (context.hasRequestedStream) {
123
+ const encode = new TextEncoder();
124
+ isStreaming = true;
125
+ stream = new TransformStream();
126
+ // Listen for the abort event on the AbortSignal
127
+ // eslint-disable-next-line @typescript-eslint/no-loop-func
128
+ context.request.signal?.addEventListener('abort', () => {
129
+ if (!isStreaming) {
130
+ return;
131
+ }
132
+ void stream.writable.abort('Request Aborted');
133
+ void stream.readable.cancel('Request Aborted');
134
+ });
135
+ context.setStream(stream.readable);
136
+ writer = stream.writable.getWriter();
137
+ await writer.ready;
138
+ await writer.write(encode.encode(text));
139
+ await writer.ready;
140
+ await writer.write(value);
141
+ }
142
+ }
143
+ if (isStreaming) {
144
+ isStreaming = false;
145
+ await writer.ready;
146
+ await writer.close();
147
+ }
148
+ }
149
+ // if we are an error, we will want to throw
150
+ if (isError) {
151
+ let errorPayload;
152
+ try {
153
+ errorPayload = JSON.parse(text);
154
+ } catch {
155
+ // void;
156
+ }
157
+ // attempt errors discovery
158
+ const errors = Array.isArray(errorPayload) ? errorPayload : isDict(errorPayload) && Array.isArray(errorPayload.errors) ? errorPayload.errors : null;
159
+ const statusText = response.statusText || ERROR_STATUS_CODE_FOR.get(response.status) || 'Unknown Request Error';
160
+ const msg = `[${response.status} ${statusText}] ${context.request.method ?? 'GET'} (${response.type}) - ${response.url}`;
161
+ const error = errors ? new AggregateError(errors, msg) : new Error(msg);
162
+ error.status = response.status;
163
+ error.statusText = statusText;
164
+ error.isRequestError = true;
165
+ error.code = error.status;
166
+ error.name = error.statusText.replaceAll(' ', '') + 'Error';
167
+ error.content = errorPayload;
168
+ throw error;
169
+ } else {
170
+ return JSON.parse(text);
171
+ }
172
+ }
173
+ };
174
+ function isDict(v) {
175
+ return v !== null && typeof v === 'object';
176
+ }
177
+
178
+ /**
179
+ * ## Import
180
+ *
181
+ * ```js
182
+ * import { RequestManager } from '@warp-drive/core';
183
+ * ```
184
+ *
185
+ * For complete usage guide see the [RequestManager Documentation](/guides/).
186
+ *
187
+ * ## How It Works
188
+ *
189
+ * ```ts
190
+ * interface RequestManager {
191
+ * request<T>(req: RequestInfo): Future<T>;
192
+ * }
193
+ * ```
194
+ *
195
+ * A RequestManager provides a request/response flow in which configured
196
+ * handlers are successively given the opportunity to handle, modify, or
197
+ * pass-along a request.
198
+ *
199
+ * <img src="/images/handlers-all-labeled.gif" alt="RequestManager Flow Animation" width="100%" />
200
+ *
201
+ * For example:
202
+ *
203
+ * ::: code-group
204
+ *
205
+ * ```ts [Setup.ts]
206
+ * import { RequestManager, Fetch } from '@warp-drive/core';
207
+ * import { AutoCompress } from '@warp-drive/utilities/handlers';
208
+ * import Auth from 'ember-simple-auth/handler';
209
+ *
210
+ * // ... create manager
211
+ * const manager = new RequestManager()
212
+ * .use([Auth, new AutoCompress(), Fetch]); // [!code focus]
213
+ * ```
214
+ *
215
+ * ```ts [Usage.ts]
216
+ * import Config from './config';
217
+ *
218
+ * const { apiUrl } = Config;
219
+ *
220
+ * // ... execute a request
221
+ * const response = await manager.request({
222
+ * url: `${apiUrl}/users`
223
+ * });
224
+ * ```
225
+ *
226
+ * :::
227
+ *
228
+ * ### Futures
229
+ *
230
+ * The return value of `manager.request` is a `Future`, which allows
231
+ * access to limited information about the request while it is still
232
+ * pending and fulfills with the final state when the request completes.
233
+ *
234
+ * A `Future` is cancellable via `abort`.
235
+ *
236
+ * Handlers may optionally expose a `ReadableStream` to the `Future` for
237
+ * streaming data; however, when doing so the future should not resolve
238
+ * until the response stream is fully read.
239
+ *
240
+ * ```ts
241
+ * interface Future<T> extends Promise<StructuredDocument<T>> {
242
+ * abort(): void;
243
+ *
244
+ * async getStream(): ReadableStream | null;
245
+ * }
246
+ * ```
247
+ *
248
+ * ### StructuredDocuments
249
+ *
250
+ * A Future resolves with a `StructuredDataDocument` or rejects with a `StructuredErrorDocument`.
251
+ *
252
+ * ```ts
253
+ * interface StructuredDataDocument<T> {
254
+ * request: ImmutableRequestInfo;
255
+ * response: ImmutableResponseInfo;
256
+ * content: T;
257
+ * }
258
+ * interface StructuredErrorDocument extends Error {
259
+ * request: ImmutableRequestInfo;
260
+ * response: ImmutableResponseInfo;
261
+ * error: string | object;
262
+ * }
263
+ * type StructuredDocument<T> = StructuredDataDocument<T> | StructuredErrorDocument;
264
+ * ```
265
+ *
266
+ * @public
267
+ */
268
+ class RequestManager {
269
+ /** @internal */
270
+
271
+ /** @internal */
272
+
273
+ /**
274
+ * A map of pending requests from request.id to their
275
+ * associated CacheHandler promise.
276
+ *
277
+ * This queue is managed by the CacheHandler
278
+ *
279
+ * @internal
280
+ */
281
+
282
+ /** @internal */
283
+
284
+ constructor(options) {
285
+ Object.assign(this, options);
286
+ this._pending = new Map();
287
+ this._deduped = new Map();
288
+ this._handlers = [];
289
+ }
290
+
291
+ /**
292
+ * Register a handler to use for primary cache intercept.
293
+ *
294
+ * Only one such handler may exist. If using the same
295
+ * RequestManager as the Store instance the Store
296
+ * registers itself as a Cache handler.
297
+ *
298
+ * @public
299
+ */
300
+ useCache(cacheHandler) {
301
+ cacheHandler[IS_CACHE_HANDLER] = true;
302
+ this._handlers.unshift(cacheHandler);
303
+ return this;
304
+ }
305
+
306
+ /**
307
+ * Register handler(s) to use when a request is issued.
308
+ *
309
+ * Handlers will be invoked in the order they are registered.
310
+ * Each Handler is given the opportunity to handle the request,
311
+ * curry the request, or pass along a modified request.
312
+ *
313
+ * @public
314
+ */
315
+ use(newHandlers) {
316
+ const handlers = this._handlers;
317
+ handlers.push(...newHandlers);
318
+ return this;
319
+ }
320
+
321
+ /**
322
+ * Issue a Request.
323
+ *
324
+ * Returns a Future that fulfills with a StructuredDocument
325
+ *
326
+ * @public
327
+ */
328
+ request(request) {
329
+ const handlers = this._handlers;
330
+ const controller = request.controller || new AbortController();
331
+ if (request.controller) {
332
+ delete request.controller;
333
+ }
334
+ const requestId = peekUniversalTransient('REQ_ID') ?? 0;
335
+ setUniversalTransient('REQ_ID', requestId + 1);
336
+ const context = {
337
+ controller,
338
+ response: null,
339
+ stream: null,
340
+ hasRequestedStream: false,
341
+ id: requestId,
342
+ identifier: null,
343
+ requester: request[EnableHydration] && request.store ? request.store : this
344
+ };
345
+ const promise = executeNextHandler(handlers, request, 0, context);
346
+
347
+ // the cache handler will set the result of the request synchronously
348
+ // if it is able to fulfill the request from the cache
349
+ const cacheResult = getRequestResult(requestId);
350
+ // const promise1 = store.request(myRequest);
351
+ // const promise2 = store.request(myRequest);
352
+ // promise1 === promise2; // false
353
+ // either we need to make promise1 === promise2, or we need to make sure that
354
+ // we need to have a way to key from request to result
355
+ // such that we can lookup the result here and return it if it exists
356
+ const finalPromise = upgradePromise(promise.then(result => {
357
+ setPromiseResult(finalPromise, {
358
+ isError: false,
359
+ result
360
+ });
361
+ clearRequestResult(requestId);
362
+ return result;
363
+ }, error => {
364
+ setPromiseResult(finalPromise, {
365
+ isError: true,
366
+ result: error
367
+ });
368
+ clearRequestResult(requestId);
369
+ throw error;
370
+ }), promise);
371
+ if (cacheResult) {
372
+ setPromiseResult(finalPromise, cacheResult);
373
+ }
374
+ return finalPromise;
375
+ }
376
+
377
+ /**
378
+ * This method exists so that the RequestManager can be created
379
+ * can be created by container/factory systems that expect to
380
+ * call a static `create` method to instantiate the class.
381
+ *
382
+ * Using `new RequestManager()` directly is preferred.
383
+ *
384
+ * @private
385
+ */
386
+ static create(options) {
387
+ return new this(options);
388
+ }
389
+ }
390
+
391
+ /**
392
+ * This type exists for internal use only for
393
+ * where intimate contracts still exist either for
394
+ * the Test Suite or for Legacy code.
395
+ *
396
+ * @private
397
+ */
398
+
399
+ /**
400
+ * @module
401
+ * @mergeModuleWith <project>
402
+ */
403
+
404
+ /**
405
+ * Options for setting up a Store instance with `useRecommendedStore`.
406
+ */
407
+
408
+ /**
409
+ * Creates a configured Store class with recommended defaults
410
+ * for schema handling, reactivity, caching, and request management.
411
+ *
412
+ * ```ts
413
+ * import { useRecommendedStore } from '@warp-drive/core';
414
+ * import { JSONAPICache } from '@warp-drive/json-api';
415
+ *
416
+ * export const Store = useRecommendedStore({
417
+ * cache: JSONAPICache,
418
+ * schemas: [],
419
+ * });
420
+ * ```
421
+ */
422
+ function useRecommendedStore(options, StoreKlass = Store) {
423
+ return class AppStore extends StoreKlass {
424
+ requestManager = new RequestManager().use([...(options.handlers ?? []), Fetch]).useCache(CacheHandler);
425
+ lifetimes = options.policy ?? new DefaultCachePolicy({
426
+ apiCacheHardExpires: 15 * 60 * 1000,
427
+ // 15 minutes
428
+ apiCacheSoftExpires: 1 * 30 * 1000,
429
+ // 30 seconds
430
+ constraints: {
431
+ headers: {
432
+ 'X-WarpDrive-Expires': true,
433
+ 'Cache-Control': true,
434
+ Expires: true
435
+ }
436
+ }
437
+ });
438
+ createSchemaService() {
439
+ const schema = new SchemaService();
440
+ registerDerivations(schema);
441
+ if (options.schemas) schema.registerResources(options.schemas);
442
+ if (options.traits) {
443
+ for (const trait of options.traits) {
444
+ schema.registerTrait(trait);
445
+ }
446
+ }
447
+ if (options.derivations) {
448
+ for (const derivation of options.derivations) {
449
+ schema.registerDerivation(derivation);
450
+ }
451
+ }
452
+ if (options.transformations) {
453
+ for (const transformation of options.transformations) {
454
+ schema.registerTransformation(transformation);
455
+ }
456
+ }
457
+ if (options.hashFns) {
458
+ for (const hashFn of options.hashFns) {
459
+ schema.registerHashFn(hashFn);
460
+ }
461
+ }
462
+ if (options.CAUTION_MEGA_DANGER_ZONE_extensions) {
463
+ for (const extension of options.CAUTION_MEGA_DANGER_ZONE_extensions) {
464
+ schema.CAUTION_MEGA_DANGER_ZONE_registerExtension(extension);
465
+ }
466
+ }
467
+ return schema;
468
+ }
469
+ createCache(capabilities) {
470
+ // eslint-disable-next-line new-cap
471
+ return new options.cache(capabilities);
472
+ }
473
+ instantiateRecord(key, createArgs) {
474
+ return instantiateRecord(this, key, createArgs);
475
+ }
476
+ teardownRecord(record) {
477
+ return teardownRecord(record);
478
+ }
479
+ };
480
+ }
481
+ export { CacheHandler, Fetch, RequestManager, Store, useRecommendedStore };