@ember-data-mirror/request 5.4.0-alpha.64 → 5.4.0-alpha.71

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.
package/addon/index.js DELETED
@@ -1,240 +0,0 @@
1
- import { _ as _classPrivateFieldBase, I as IS_CACHE_HANDLER, a as assertValidRequest, e as executeNextHandler, g as getRequestResult, u as upgradePromise, s as setPromiseResult, b as clearRequestResult, d as _classPrivateFieldKey } from "./context-DMaVGooY";
2
- export { f as createDeferred, h as getPromiseResult } from "./context-DMaVGooY";
3
- import { macroCondition, getOwnConfig, importSync } from '@embroider/macros';
4
- let REQ_ID = 0;
5
- /**
6
- * ```js
7
- * import RequestManager from '@ember-data-mirror/request';
8
- * ```
9
- *
10
- * A RequestManager provides a request/response flow in which configured
11
- * handlers are successively given the opportunity to handle, modify, or
12
- * pass-along a request.
13
- *
14
- * ```ts
15
- * interface RequestManager {
16
- * request<T>(req: RequestInfo): Future<T>;
17
- * }
18
- * ```
19
- *
20
- * For example:
21
- *
22
- * ```ts
23
- * import RequestManager from '@ember-data-mirror/request';
24
- * import Fetch from '@ember-data-mirror/request/fetch';
25
- * import Auth from 'ember-simple-auth/ember-data-handler';
26
- * import Config from './config';
27
- *
28
- * const { apiUrl } = Config;
29
- *
30
- * // ... create manager
31
- * const manager = new RequestManager();
32
- * manager.use([Auth, Fetch]);
33
- *
34
- * // ... execute a request
35
- * const response = await manager.request({
36
- * url: `${apiUrl}/users`
37
- * });
38
- * ```
39
- *
40
- * ### Futures
41
- *
42
- * The return value of `manager.request` is a `Future`, which allows
43
- * access to limited information about the request while it is still
44
- * pending and fulfills with the final state when the request completes.
45
- *
46
- * A `Future` is cancellable via `abort`.
47
- *
48
- * Handlers may optionally expose a `ReadableStream` to the `Future` for
49
- * streaming data; however, when doing so the future should not resolve
50
- * until the response stream is fully read.
51
- *
52
- * ```ts
53
- * interface Future<T> extends Promise<StructuredDocument<T>> {
54
- * abort(): void;
55
- *
56
- * async getStream(): ReadableStream | null;
57
- * }
58
- * ```
59
- *
60
- * ### StructuredDocuments
61
- *
62
- * A Future resolves with a `StructuredDataDocument` or rejects with a `StructuredErrorDocument`.
63
- *
64
- * ```ts
65
- * interface StructuredDataDocument<T> {
66
- * request: ImmutableRequestInfo;
67
- * response: ImmutableResponseInfo;
68
- * content: T;
69
- * }
70
- * interface StructuredErrorDocument extends Error {
71
- * request: ImmutableRequestInfo;
72
- * response: ImmutableResponseInfo;
73
- * error: string | object;
74
- * }
75
- * type StructuredDocument<T> = StructuredDataDocument<T> | StructuredErrorDocument;
76
- * ```
77
- *
78
- * @class RequestManager
79
- * @public
80
- */
81
- var _handlers = /*#__PURE__*/_classPrivateFieldKey("handlers");
82
- class RequestManager {
83
- constructor(options) {
84
- Object.defineProperty(this, _handlers, {
85
- writable: true,
86
- value: []
87
- });
88
- Object.assign(this, options);
89
- this._pending = new Map();
90
- }
91
-
92
- /**
93
- * Register a handler to use for primary cache intercept.
94
- *
95
- * Only one such handler may exist. If using the same
96
- * RequestManager as the Store instance the Store
97
- * registers itself as a Cache handler.
98
- *
99
- * @method useCache
100
- * @public
101
- * @param {Handler[]} cacheHandler
102
- * @return {void}
103
- */
104
- useCache(cacheHandler) {
105
- if (macroCondition(getOwnConfig().env.DEBUG)) {
106
- if (this._hasCacheHandler) {
107
- throw new Error(`\`RequestManager.useCache(<handler>)\` May only be invoked once.`);
108
- }
109
- if (Object.isFrozen(_classPrivateFieldBase(this, _handlers)[_handlers])) {
110
- throw new Error(`\`RequestManager.useCache(<handler>)\` May only be invoked prior to any request having been made.`);
111
- }
112
- this._hasCacheHandler = true;
113
- }
114
- cacheHandler[IS_CACHE_HANDLER] = true;
115
- _classPrivateFieldBase(this, _handlers)[_handlers].unshift(cacheHandler);
116
- }
117
-
118
- /**
119
- * Register handler(s) to use when a request is issued.
120
- *
121
- * Handlers will be invoked in the order they are registered.
122
- * Each Handler is given the opportunity to handle the request,
123
- * curry the request, or pass along a modified request.
124
- *
125
- * @method use
126
- * @public
127
- * @param {Handler[]} newHandlers
128
- * @return {void}
129
- */
130
- use(newHandlers) {
131
- const handlers = _classPrivateFieldBase(this, _handlers)[_handlers];
132
- if (macroCondition(getOwnConfig().env.DEBUG)) {
133
- if (Object.isFrozen(handlers)) {
134
- throw new Error(`Cannot add a Handler to a RequestManager after a request has been made`);
135
- }
136
- if (!Array.isArray(newHandlers)) {
137
- throw new Error(`\`RequestManager.use(<Handler[]>)\` expects an array of handlers, but was called with \`${typeof newHandlers}\``);
138
- }
139
- newHandlers.forEach((handler, index) => {
140
- if (!handler || typeof handler !== 'object' || typeof handler.request !== 'function') {
141
- throw new Error(`\`RequestManager.use(<Handler[]>)\` expected to receive an array of handler objects with request methods, by the handler at index ${index} does not conform.`);
142
- }
143
- });
144
- }
145
- handlers.push(...newHandlers);
146
- }
147
-
148
- /**
149
- * Issue a Request.
150
- *
151
- * Returns a Future that fulfills with a StructuredDocument
152
- *
153
- * @method request
154
- * @public
155
- * @param {RequestInfo} request
156
- * @return {Future}
157
- */
158
- request(request) {
159
- const handlers = _classPrivateFieldBase(this, _handlers)[_handlers];
160
- if (macroCondition(getOwnConfig().env.DEBUG)) {
161
- if (!Object.isFrozen(handlers)) {
162
- Object.freeze(handlers);
163
- }
164
- assertValidRequest(request, true);
165
- }
166
- const controller = request.controller || new AbortController();
167
- if (request.controller) {
168
- delete request.controller;
169
- }
170
- const requestId = REQ_ID++;
171
- const promise = executeNextHandler(handlers, request, 0, {
172
- controller,
173
- response: null,
174
- stream: null,
175
- hasRequestedStream: false,
176
- id: requestId
177
- });
178
-
179
- // the cache handler will set the result of the request synchronously
180
- // if it is able to fulfill the request from the cache
181
- const cacheResult = getRequestResult(requestId);
182
- if (macroCondition(getOwnConfig().env.TESTING)) {
183
- if (!request.disableTestWaiter) {
184
- const {
185
- waitForPromise
186
- } = importSync('@ember/test-waiters');
187
- const newPromise = waitForPromise(promise);
188
- const finalPromise = upgradePromise(newPromise.then(result => {
189
- setPromiseResult(finalPromise, {
190
- isError: false,
191
- result
192
- });
193
- clearRequestResult(requestId);
194
- return result;
195
- }, error => {
196
- setPromiseResult(finalPromise, {
197
- isError: true,
198
- result: error
199
- });
200
- clearRequestResult(requestId);
201
- throw error;
202
- }), promise);
203
- if (cacheResult) {
204
- setPromiseResult(finalPromise, cacheResult);
205
- }
206
- return finalPromise;
207
- }
208
- }
209
-
210
- // const promise1 = store.request(myRequest);
211
- // const promise2 = store.request(myRequest);
212
- // promise1 === promise2; // false
213
- // either we need to make promise1 === promise2, or we need to make sure that
214
- // we need to have a way to key from request to result
215
- // such that we can lookup the result here and return it if it exists
216
- const finalPromise = upgradePromise(promise.then(result => {
217
- setPromiseResult(finalPromise, {
218
- isError: false,
219
- result
220
- });
221
- clearRequestResult(requestId);
222
- return result;
223
- }, error => {
224
- setPromiseResult(finalPromise, {
225
- isError: true,
226
- result: error
227
- });
228
- clearRequestResult(requestId);
229
- throw error;
230
- }), promise);
231
- if (cacheResult) {
232
- setPromiseResult(finalPromise, cacheResult);
233
- }
234
- return finalPromise;
235
- }
236
- static create(options) {
237
- return new this(options);
238
- }
239
- }
240
- export { RequestManager as default, setPromiseResult };
package/addon-main.js DELETED
@@ -1,94 +0,0 @@
1
- const requireModule = require('@ember-data-mirror/private-build-infra/src/utilities/require-module');
2
- const getEnv = require('@ember-data-mirror/private-build-infra/src/utilities/get-env');
3
- const detectModule = require('@ember-data-mirror/private-build-infra/src/utilities/detect-module');
4
-
5
- const pkg = require('./package.json');
6
-
7
- module.exports = {
8
- name: pkg.name,
9
-
10
- options: {
11
- '@embroider/macros': {
12
- setOwnConfig: {},
13
- },
14
- },
15
-
16
- _emberDataConfig: null,
17
- configureEmberData() {
18
- if (this._emberDataConfig) {
19
- return this._emberDataConfig;
20
- }
21
- const app = this._findHost();
22
- const isProd = /production/.test(process.env.EMBER_ENV);
23
- const hostOptions = app.options?.emberData || {};
24
- const debugOptions = Object.assign(
25
- {
26
- LOG_PAYLOADS: false,
27
- LOG_OPERATIONS: false,
28
- LOG_MUTATIONS: false,
29
- LOG_NOTIFICATIONS: false,
30
- LOG_REQUESTS: false,
31
- LOG_REQUEST_STATUS: false,
32
- LOG_IDENTIFIERS: false,
33
- LOG_GRAPH: false,
34
- LOG_INSTANCE_CACHE: false,
35
- },
36
- hostOptions.debug || {}
37
- );
38
-
39
- const HAS_DEBUG_PACKAGE = detectModule(require, '@ember-data-mirror/debug', __dirname, pkg);
40
- const HAS_META_PACKAGE = detectModule(require, 'ember-data-mirror', __dirname, pkg);
41
-
42
- const includeDataAdapterInProduction =
43
- typeof hostOptions.includeDataAdapterInProduction === 'boolean'
44
- ? hostOptions.includeDataAdapterInProduction
45
- : HAS_META_PACKAGE;
46
-
47
- const includeDataAdapter = HAS_DEBUG_PACKAGE ? (isProd ? includeDataAdapterInProduction : true) : false;
48
- const DEPRECATIONS = require('@ember-data-mirror/private-build-infra/src/deprecations')(hostOptions.compatWith || null);
49
- const FEATURES = require('@ember-data-mirror/private-build-infra/src/features')(isProd);
50
-
51
- const ALL_PACKAGES = requireModule('@ember-data-mirror/private-build-infra/virtual-packages/packages.js');
52
- const MACRO_PACKAGE_FLAGS = Object.assign({}, ALL_PACKAGES.default);
53
- delete MACRO_PACKAGE_FLAGS['HAS_DEBUG_PACKAGE'];
54
-
55
- Object.keys(MACRO_PACKAGE_FLAGS).forEach((key) => {
56
- MACRO_PACKAGE_FLAGS[key] = detectModule(require, MACRO_PACKAGE_FLAGS[key], __dirname, pkg);
57
- });
58
-
59
- // copy configs forward
60
- const ownConfig = this.options['@embroider/macros'].setOwnConfig;
61
- ownConfig.polyfillUUID = hostOptions.polyfillUUID ?? false;
62
- ownConfig.compatWith = hostOptions.compatWith || null;
63
- ownConfig.debug = debugOptions;
64
- ownConfig.deprecations = Object.assign(DEPRECATIONS, ownConfig.deprecations || {}, hostOptions.deprecations || {});
65
- ownConfig.features = Object.assign({}, FEATURES);
66
- ownConfig.includeDataAdapter = includeDataAdapter;
67
- ownConfig.packages = MACRO_PACKAGE_FLAGS;
68
- ownConfig.env = getEnv(ownConfig);
69
-
70
- this._emberDataConfig = ownConfig;
71
- return ownConfig;
72
- },
73
-
74
- included() {
75
- this.configureEmberData();
76
- return this._super.included.call(this, ...arguments);
77
- },
78
-
79
- treeForVendor() {
80
- return;
81
- },
82
- treeForPublic() {
83
- return;
84
- },
85
- treeForStyles() {
86
- return;
87
- },
88
- treeForAddonStyles() {
89
- return;
90
- },
91
- treeForApp() {
92
- return;
93
- },
94
- };