@warp-drive/legacy 5.9.0-alpha.2 → 5.9.0-alpha.21

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 (162) hide show
  1. package/README.md +6 -0
  2. package/declarations/adapter/-private/build-url-mixin.d.ts +156 -0
  3. package/declarations/adapter/error.d.ts +385 -169
  4. package/declarations/adapter/json-api.d.ts +12 -1
  5. package/declarations/adapter/rest.d.ts +74 -0
  6. package/declarations/adapter.d.ts +6 -0
  7. package/declarations/compat/builders/find-all.d.ts +0 -1
  8. package/declarations/compat/extensions.d.ts +40 -0
  9. package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts +7 -0
  10. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +4 -0
  11. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +7 -0
  12. package/declarations/compat/utils.d.ts +11 -0
  13. package/declarations/compat.d.ts +32 -0
  14. package/declarations/index.d.ts +142 -0
  15. package/declarations/model/-private/attr.d.ts +21 -0
  16. package/declarations/model/-private/belongs-to.d.ts +21 -0
  17. package/declarations/model/-private/hooks.d.ts +26 -0
  18. package/declarations/model/-private/model.d.ts +55 -4
  19. package/declarations/model/-private/promise-many-array.d.ts +25 -1
  20. package/declarations/model/-private/schema-provider.d.ts +11 -1
  21. package/declarations/model/migration-support.d.ts +31 -5
  22. package/declarations/model-fragments/extensions/fragment-array.d.ts +38 -0
  23. package/declarations/model-fragments/extensions/fragment.d.ts +35 -0
  24. package/declarations/model-fragments/hooks/model-for.d.ts +7 -0
  25. package/declarations/model-fragments/index.d.ts +8 -0
  26. package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +6 -0
  27. package/declarations/model-fragments/utilities/with-array-defaults.d.ts +30 -0
  28. package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +18 -0
  29. package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +15 -0
  30. package/declarations/model-fragments/utilities/with-legacy.d.ts +8 -0
  31. package/declarations/serializer/-private/transforms/boolean.d.ts +13 -0
  32. package/declarations/serializer/-private/transforms/date.d.ts +15 -1
  33. package/declarations/serializer/-private/transforms/number.d.ts +14 -1
  34. package/declarations/serializer/-private/transforms/string.d.ts +14 -1
  35. package/declarations/serializer/-private/utils.d.ts +24 -0
  36. package/declarations/serializer/json-api.d.ts +1 -1
  37. package/declarations/serializer.d.ts +4 -6
  38. package/declarations/store/-private.d.ts +2 -2
  39. package/declarations/store.d.ts +7 -0
  40. package/dist/adapter/error.js +386 -241
  41. package/dist/adapter/json-api.js +12 -0
  42. package/dist/adapter/rest.js +39 -3
  43. package/dist/adapter.js +23 -26
  44. package/dist/compat/builders.js +0 -1
  45. package/dist/compat/extensions.js +80 -3
  46. package/dist/compat/utils.js +14 -0
  47. package/dist/compat.js +17 -0
  48. package/dist/{errors-Cz5KrzBk.js → errors-DZKA206g.js} +24 -1
  49. package/dist/{hooks-D6diaM34.js → hooks-BvOqJBei.js} +33 -2
  50. package/dist/index.js +168 -13
  51. package/dist/{json-ChdEfB0X.js → json-DU8m1pGf.js} +83 -6
  52. package/dist/model/-private.js +1 -1
  53. package/dist/model/migration-support.js +15 -2
  54. package/dist/{unpkg/dev-deprecated/model-for-CqXsIKws.js → model-for-CXkJw59V.js} +77 -1
  55. package/dist/model-fragments.js +18 -2
  56. package/dist/model.js +12 -8
  57. package/dist/{schema-provider-DJCV_6AF.js → schema-provider-DtPOp0d7.js} +93 -6
  58. package/dist/serializer/json-api.js +8 -3
  59. package/dist/serializer/json.js +1 -1
  60. package/dist/serializer/rest.js +3 -4
  61. package/dist/serializer/transform.js +51 -3
  62. package/dist/serializer.js +4 -7
  63. package/dist/store.js +13 -7
  64. package/dist/unpkg/dev/adapter/error.js +386 -241
  65. package/dist/unpkg/dev/adapter/json-api.js +13 -1
  66. package/dist/unpkg/dev/adapter/rest.js +39 -3
  67. package/dist/unpkg/dev/adapter.js +23 -26
  68. package/dist/unpkg/dev/compat/builders.js +0 -1
  69. package/dist/unpkg/dev/compat/extensions.js +80 -3
  70. package/dist/unpkg/dev/compat/utils.js +14 -0
  71. package/dist/unpkg/dev/compat.js +34 -1
  72. package/dist/unpkg/dev/{errors-DmGGJr3T.js → errors-DngRU-If.js} +24 -1
  73. package/dist/unpkg/dev/{hooks-CkYiE6Ud.js → hooks-UuiLxWxS.js} +33 -2
  74. package/dist/unpkg/dev/index.js +171 -13
  75. package/dist/unpkg/dev/json-K2Y9z6MN.js +1333 -0
  76. package/dist/unpkg/dev/model/-private.js +1 -1
  77. package/dist/unpkg/dev/model/migration-support.js +16 -2
  78. package/dist/unpkg/{prod-deprecated/model-for-CqXsIKws.js → dev/model-for-CXkJw59V.js} +77 -1
  79. package/dist/unpkg/dev/model-fragments.js +18 -2
  80. package/dist/unpkg/dev/model.js +12 -8
  81. package/dist/unpkg/dev/{schema-provider-DDVYxmUV.js → schema-provider-0D7f-QOn.js} +92 -6
  82. package/dist/unpkg/dev/serializer/json-api.js +9 -4
  83. package/dist/unpkg/dev/serializer/json.js +1 -1
  84. package/dist/unpkg/dev/serializer/rest.js +3 -4
  85. package/dist/unpkg/dev/serializer/transform.js +51 -3
  86. package/dist/unpkg/dev/serializer.js +4 -7
  87. package/dist/unpkg/dev/store.js +21 -7
  88. package/dist/unpkg/dev-deprecated/adapter/error.js +386 -241
  89. package/dist/unpkg/dev-deprecated/adapter/json-api.js +13 -1
  90. package/dist/unpkg/dev-deprecated/adapter/rest.js +39 -3
  91. package/dist/unpkg/dev-deprecated/adapter.js +23 -26
  92. package/dist/unpkg/dev-deprecated/compat/builders.js +0 -1
  93. package/dist/unpkg/dev-deprecated/compat/extensions.js +80 -3
  94. package/dist/unpkg/dev-deprecated/compat/utils.js +14 -0
  95. package/dist/unpkg/dev-deprecated/compat.js +34 -1
  96. package/dist/unpkg/dev-deprecated/{errors-Spt6ubMd.js → errors-Bzidntpi.js} +24 -1
  97. package/dist/unpkg/dev-deprecated/{hooks-DOXegvhL.js → hooks-DVXhMW14.js} +33 -2
  98. package/dist/unpkg/dev-deprecated/index.js +171 -13
  99. package/dist/unpkg/{prod-deprecated/json-BWrZ5546.js → dev-deprecated/json-K2Y9z6MN.js} +96 -6
  100. package/dist/unpkg/dev-deprecated/model/-private.js +1 -1
  101. package/dist/unpkg/dev-deprecated/model/migration-support.js +16 -2
  102. package/dist/unpkg/{dev/model-for-CqXsIKws.js → dev-deprecated/model-for-CXkJw59V.js} +77 -1
  103. package/dist/unpkg/dev-deprecated/model-fragments.js +18 -2
  104. package/dist/unpkg/dev-deprecated/model.js +12 -8
  105. package/dist/unpkg/dev-deprecated/{schema-provider-BP6_8N-V.js → schema-provider-ZwO5_fZw.js} +84 -3
  106. package/dist/unpkg/dev-deprecated/serializer/json-api.js +9 -4
  107. package/dist/unpkg/dev-deprecated/serializer/json.js +1 -1
  108. package/dist/unpkg/dev-deprecated/serializer/rest.js +3 -4
  109. package/dist/unpkg/dev-deprecated/serializer/transform.js +51 -3
  110. package/dist/unpkg/dev-deprecated/serializer.js +4 -7
  111. package/dist/unpkg/dev-deprecated/store.js +21 -7
  112. package/dist/unpkg/prod/adapter/error.js +386 -241
  113. package/dist/unpkg/prod/adapter/json-api.js +13 -1
  114. package/dist/unpkg/prod/adapter/rest.js +39 -3
  115. package/dist/unpkg/prod/adapter.js +23 -26
  116. package/dist/unpkg/prod/compat/builders.js +0 -1
  117. package/dist/unpkg/prod/compat/extensions.js +70 -3
  118. package/dist/unpkg/prod/compat/utils.js +14 -0
  119. package/dist/unpkg/prod/compat.js +17 -0
  120. package/dist/unpkg/prod/{errors-BGVFCBmi.js → errors-B-PWeYlW.js} +24 -1
  121. package/dist/unpkg/prod/{hooks-BztVA_x0.js → hooks-Bx-zBziU.js} +32 -1
  122. package/dist/unpkg/prod/index.js +171 -5
  123. package/dist/unpkg/{dev/json-Cu1LNgmQ.js → prod/json-Qewu-UN9.js} +63 -19
  124. package/dist/unpkg/prod/model/-private.js +1 -1
  125. package/dist/unpkg/prod/model/migration-support.js +16 -2
  126. package/dist/unpkg/prod/model-for-CXkJw59V.js +297 -0
  127. package/dist/unpkg/prod/model-fragments.js +18 -2
  128. package/dist/unpkg/prod/model.js +12 -8
  129. package/dist/unpkg/prod/{schema-provider-DJtD_8jZ.js → schema-provider-BgRqk8i9.js} +96 -6
  130. package/dist/unpkg/prod/serializer/json-api.js +9 -4
  131. package/dist/unpkg/prod/serializer/json.js +1 -1
  132. package/dist/unpkg/prod/serializer/rest.js +3 -4
  133. package/dist/unpkg/prod/serializer/transform.js +51 -3
  134. package/dist/unpkg/prod/serializer.js +4 -7
  135. package/dist/unpkg/prod/store.js +21 -7
  136. package/dist/unpkg/prod-deprecated/adapter/error.js +386 -241
  137. package/dist/unpkg/prod-deprecated/adapter/json-api.js +13 -1
  138. package/dist/unpkg/prod-deprecated/adapter/rest.js +39 -3
  139. package/dist/unpkg/prod-deprecated/adapter.js +23 -26
  140. package/dist/unpkg/prod-deprecated/compat/builders.js +0 -1
  141. package/dist/unpkg/prod-deprecated/compat/extensions.js +70 -3
  142. package/dist/unpkg/prod-deprecated/compat/utils.js +14 -0
  143. package/dist/unpkg/prod-deprecated/compat.js +17 -0
  144. package/dist/unpkg/prod-deprecated/{errors-CdDaK81x.js → errors-Dl2J9d1D.js} +24 -1
  145. package/dist/unpkg/prod-deprecated/{hooks-yId87yyG.js → hooks-Cm60wWTf.js} +32 -1
  146. package/dist/unpkg/prod-deprecated/index.js +171 -5
  147. package/dist/unpkg/{dev-deprecated/json-Cu1LNgmQ.js → prod-deprecated/json-Qewu-UN9.js} +63 -19
  148. package/dist/unpkg/prod-deprecated/model/-private.js +1 -1
  149. package/dist/unpkg/prod-deprecated/model/migration-support.js +16 -2
  150. package/dist/{model-for-CqXsIKws.js → unpkg/prod-deprecated/model-for-CXkJw59V.js} +77 -1
  151. package/dist/unpkg/prod-deprecated/model-fragments.js +18 -2
  152. package/dist/unpkg/prod-deprecated/model.js +12 -8
  153. package/dist/unpkg/prod-deprecated/{schema-provider-CjX55uSY.js → schema-provider-Dh6tRqVI.js} +79 -3
  154. package/dist/unpkg/prod-deprecated/serializer/json-api.js +9 -4
  155. package/dist/unpkg/prod-deprecated/serializer/json.js +1 -1
  156. package/dist/unpkg/prod-deprecated/serializer/rest.js +3 -4
  157. package/dist/unpkg/prod-deprecated/serializer/transform.js +51 -3
  158. package/dist/unpkg/prod-deprecated/serializer.js +4 -7
  159. package/dist/unpkg/prod-deprecated/store.js +21 -7
  160. package/package.json +8 -8
  161. package/dist/unpkg/prod/json-BWrZ5546.js +0 -1243
  162. package/dist/unpkg/prod/model-for-CqXsIKws.js +0 -221
@@ -7,12 +7,12 @@ import '@ember/object';
7
7
  import '@warp-drive/core/types/symbols';
8
8
  import '@warp-drive/utilities/string';
9
9
  import '@warp-drive/core/store/-private';
10
- import "./errors-DmGGJr3T.js";
11
- import "./schema-provider-DDVYxmUV.js";
12
- import { i as instantiateRecord, t as teardownRecord, m as modelFor } from "./hooks-CkYiE6Ud.js";
10
+ import "./errors-DngRU-If.js";
11
+ import "./schema-provider-0D7f-QOn.js";
12
+ import { i as instantiateRecord, t as teardownRecord, m as modelFor } from "./hooks-UuiLxWxS.js";
13
13
  import '@ember/application';
14
14
  import { registerDerivations as registerDerivations$1, DelegatingSchemaService } from './model/migration-support.js';
15
- import { F as FragmentExtension, a as FragmentArrayExtension, f as fragmentsModelFor } from "./model-for-CqXsIKws.js";
15
+ import { F as FragmentExtension, a as FragmentArrayExtension, f as fragmentsModelFor } from "./model-for-CXkJw59V.js";
16
16
  import { restoreDeprecatedStoreBehaviors } from './store.js';
17
17
 
18
18
  /**
@@ -20,10 +20,153 @@ import { restoreDeprecatedStoreBehaviors } from './store.js';
20
20
  * @mergeModuleWith <project>
21
21
  */
22
22
 
23
+ /**
24
+ * Setup options for a legacy store configured to use `Model` with `linksMode`
25
+ * enabled, meaning no legacy adapter/serializer request infrastructure is required.
26
+ *
27
+ * @public
28
+ */
29
+
30
+ /**
31
+ * Setup options for a legacy store configured to use `Model` along with the
32
+ * legacy adapter/serializer network layer, but without the deprecated
33
+ * `store.findRecord`/`findAll`/`query`/etc. request methods.
34
+ *
35
+ * @public
36
+ */
37
+
38
+ /**
39
+ * Setup options for a legacy store configured to use `Model` along with the
40
+ * legacy adapter/serializer network layer and the deprecated
41
+ * `store.findRecord`/`findAll`/`query`/etc. request methods.
42
+ *
43
+ * @public
44
+ */
45
+
23
46
  //export type ConfiguredStore<T = unknown> = typeof Store;
24
47
 
48
+ /**
49
+ * The available options when setting up the legacy store,
50
+ * one of:
51
+ *
52
+ * - {@link LegacyModelStoreSetupOptions}
53
+ * - {@link LegacyModelAndNetworkStoreSetupOptions}
54
+ * - {@link LegacyModelAndNetworkAndRequestStoreSetupOptions}
55
+ */
56
+
25
57
  /**
26
58
  * Use the legacy store with the given options.
59
+ *
60
+ * See {@link LegacyStoreSetupOptions} for details on the available options.
61
+ *
62
+ * ```ts
63
+ * import { useLegacyStore } from '@warp-drive/legacy';
64
+ * import { JSONAPICache } from '@warp-drive/json-api';
65
+ *
66
+ * export default useLegacyStore({
67
+ * linksMode: false,
68
+ * legacyRequests: true,
69
+ * cache: JSONAPICache,
70
+ * schemas: [],
71
+ * });
72
+ * ```
73
+ *
74
+ * ### Adding Stateful Handlers
75
+ *
76
+ * A request {@link Handler} is sometimes more than a plain object or class
77
+ * with a `request` method — it may need access to a stateful dependency such
78
+ * as an Ember service (an auth token, a feature-flags service, an i18n
79
+ * helper, etc.).
80
+ *
81
+ * A plain class handler that only relies on Ember's `@service` decorator will
82
+ * not work here on its own: the handler is never instantiated *through*
83
+ * Ember's container (it's just `new`'d up), so it has no owner and its
84
+ * `@service` injections would fail to resolve.
85
+ *
86
+ * Instead, give `handlers` a function. It receives the {@link Store} instance
87
+ * being configured, which by the time the function runs already has an owner
88
+ * assigned. Use `getOwner`/`setOwner` from `@ember/owner` to transfer that
89
+ * owner onto your handler instance before returning it, exactly as you would
90
+ * when constructing any other DI-aware object outside of the container:
91
+ *
92
+ * ```ts
93
+ * import { getOwner, setOwner } from '@ember/owner';
94
+ * import { service } from '@ember/service';
95
+ * import { useLegacyStore } from '@warp-drive/legacy';
96
+ * import type { NextFn } from '@warp-drive/core/request';
97
+ * import type { RequestContext } from '@warp-drive/core/types/request';
98
+ * import { JSONAPICache } from '@warp-drive/json-api';
99
+ *
100
+ * class AuthHandler {
101
+ * @service session;
102
+ *
103
+ * request<T>(context: RequestContext, next: NextFn<T>) {
104
+ * const headers = new Headers(context.request.headers);
105
+ * headers.append('Authorization', `Bearer ${this.session.accessToken}`);
106
+ * return next(Object.assign({}, context.request, { headers }));
107
+ * }
108
+ * }
109
+ *
110
+ * export default useLegacyStore({
111
+ * linksMode: false,
112
+ * legacyRequests: true,
113
+ * cache: JSONAPICache,
114
+ * handlers: (store) => {
115
+ * const authHandler = new AuthHandler();
116
+ * setOwner(authHandler, getOwner(store)!);
117
+ * return [authHandler];
118
+ * },
119
+ * });
120
+ * ```
121
+ *
122
+ * The `handlers` function is invoked lazily and only once per store instance,
123
+ * the first time `store.requestManager` is accessed, so it is safe to do
124
+ * owner-dependent setup like this inside of it.
125
+ *
126
+ * ### Accessing the Store from a Handler's Context
127
+ *
128
+ * If a handler only needs to read something *off of the store itself*
129
+ * (its cache, or a property/service you've attached to a custom store
130
+ * subclass) rather than an unrelated Ember service, there is a second,
131
+ * simpler option that requires no DI/`setOwner` wiring at all.
132
+ *
133
+ * Every request issued via {@link Store.request | store.request(...)}
134
+ * automatically carries the originating store along as
135
+ * {@link RequestInfo.store | context.request.store}. Any handler — a plain
136
+ * object, a function-built handler, or a class — can read it directly,
137
+ * without needing the `handlers` callback form shown above:
138
+ *
139
+ * ```ts
140
+ * import { useLegacyStore } from '@warp-drive/legacy';
141
+ * import type { NextFn } from '@warp-drive/core/request';
142
+ * import type { RequestContext } from '@warp-drive/core/types/request';
143
+ * import { JSONAPICache } from '@warp-drive/json-api';
144
+ *
145
+ * const LoggingHandler = {
146
+ * request<T>(context: RequestContext, next: NextFn<T>) {
147
+ * // only present when the request was made via `store.request(...)`
148
+ * const store = context.request.store;
149
+ * if (store) {
150
+ * console.log(`[${store.constructor.name}] ${context.request.url ?? ''}`);
151
+ * }
152
+ * return next(context.request);
153
+ * },
154
+ * };
155
+ *
156
+ * export default useLegacyStore({
157
+ * linksMode: false,
158
+ * legacyRequests: true,
159
+ * cache: JSONAPICache,
160
+ * handlers: [LoggingHandler],
161
+ * });
162
+ * ```
163
+ *
164
+ * The trade-off versus the `getOwner`/`setOwner` pattern above is that
165
+ * `context.request.store` is only populated for requests issued via
166
+ * `store.request(...)`; a request made directly against a
167
+ * {@link RequestManager} won't have it set unless the caller supplies it
168
+ * explicitly, so a handler relying on it should treat it as optional (as
169
+ * `LoggingHandler` does above).
27
170
  */
28
171
 
29
172
  function useLegacyStore(options, StoreKlass = Store) {
@@ -35,7 +178,30 @@ function useLegacyStore(options, StoreKlass = Store) {
35
178
  // we extend the store to ensure we don't leak our prototype overrides to other stores below.
36
179
  class BaseKlass extends StoreKlass {}
37
180
  class LegacyConfiguredStore extends BaseKlass {
38
- requestManager = new RequestManager().use([options.linksMode ? null : LegacyNetworkHandler, ...(options.handlers ?? []), Fetch].filter(Boolean)).useCache(CacheHandler);
181
+ constructor(createArgs) {
182
+ super(createArgs);
183
+ // installed via defineProperty (rather than a class field/accessor) so that
184
+ // this lazy override of the inherited `requestManager` field does not
185
+ // conflict with the documented pattern of assigning it directly on
186
+ // consumer-authored Store subclasses. The setter preserves the ability
187
+ // to replace `requestManager` outright after construction.
188
+ let requestManager;
189
+ Object.defineProperty(this, 'requestManager', {
190
+ configurable: true,
191
+ enumerable: true,
192
+ get: () => {
193
+ if (!requestManager) {
194
+ const handlersOption = options.handlers;
195
+ const handlers = typeof handlersOption === 'function' ? handlersOption(this) : handlersOption ?? [];
196
+ requestManager = new RequestManager().use([options.linksMode ? null : LegacyNetworkHandler, ...handlers, Fetch].filter(Boolean)).useCache(CacheHandler);
197
+ }
198
+ return requestManager;
199
+ },
200
+ set: value => {
201
+ requestManager = value;
202
+ }
203
+ });
204
+ }
39
205
  lifetimes = options.policy ?? new DefaultCachePolicy({
40
206
  apiCacheHardExpires: 15 * 60 * 1000,
41
207
  // 15 minutes
@@ -115,14 +281,6 @@ function useLegacyStore(options, StoreKlass = Store) {
115
281
  }
116
282
  modelFor(type) {
117
283
  assertType(this.schema, type);
118
- // TODO I'm not sure this is right
119
- (test => {
120
- if (!test) {
121
- throw new Error(`modelFor should only be used to lookup legacy models when in linksMode: false`);
122
- }
123
- })(!options.linksMode || !this.schema.isDelegated({
124
- type
125
- }));
126
284
  const klass =
127
285
  // prefer real models if present
128
286
  modelFor.call(this, type) || (