@warp-drive/core 5.6.0-alpha.5 → 5.6.0-beta.1
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/README.md +8 -7
- package/cjs-dist/addon-shim.cjs +1 -0
- package/cjs-dist/cjs-set-config.cjs +1 -0
- package/declarations/build-config/babel-macros.d.ts +1 -0
- package/declarations/build-config/canary-features.d.ts +1 -0
- package/declarations/build-config/debugging.d.ts +1 -0
- package/declarations/build-config/deprecations.d.ts +1 -0
- package/declarations/build-config/env.d.ts +1 -0
- package/declarations/build-config/macros.d.ts +1 -0
- package/declarations/build-config.d.ts +1 -0
- package/declarations/configure.d.ts +7 -0
- package/declarations/graph/-private/-diff.d.ts +32 -0
- package/declarations/graph/-private/-edge-definition.d.ts +148 -0
- package/declarations/graph/-private/-state.d.ts +96 -0
- package/declarations/graph/-private/-utils.d.ts +31 -0
- package/declarations/graph/-private/coerce-id.d.ts +10 -0
- package/declarations/graph/-private/debug/assert-polymorphic-type.d.ts +18 -0
- package/declarations/graph/-private/edges/collection.d.ts +39 -0
- package/declarations/graph/-private/edges/implicit.d.ts +43 -0
- package/declarations/graph/-private/edges/resource.d.ts +24 -0
- package/declarations/graph/-private/graph.d.ts +90 -0
- package/declarations/graph/-private/normalize-link.d.ts +8 -0
- package/declarations/graph/-private/operations/add-to-related-records.d.ts +4 -0
- package/declarations/graph/-private/operations/merge-identifier.d.ts +3 -0
- package/declarations/graph/-private/operations/remove-from-related-records.d.ts +4 -0
- package/declarations/graph/-private/operations/replace-related-record.d.ts +3 -0
- package/declarations/graph/-private/operations/replace-related-records.d.ts +62 -0
- package/declarations/graph/-private/operations/update-relationship.d.ts +13 -0
- package/declarations/graph/-private.d.ts +13 -0
- package/declarations/index.d.ts +14 -3
- package/declarations/reactive/-private/document.d.ts +146 -0
- package/declarations/reactive/-private/fields/compute.d.ts +43 -0
- package/declarations/reactive/-private/fields/extension.d.ts +8 -0
- package/declarations/reactive/-private/fields/managed-array.d.ts +22 -0
- package/declarations/reactive/-private/fields/managed-object.d.ts +21 -0
- package/declarations/reactive/-private/fields/many-array-manager.d.ts +19 -0
- package/declarations/reactive/-private/hooks.d.ts +5 -0
- package/declarations/reactive/-private/record.d.ts +64 -0
- package/declarations/reactive/-private/schema.d.ts +271 -0
- package/declarations/reactive/-private/symbols.d.ts +36 -0
- package/declarations/reactive/-private.d.ts +1 -0
- package/declarations/reactive.d.ts +4 -0
- package/declarations/request/-private/context.d.ts +34 -35
- package/declarations/request/-private/debug.d.ts +2 -3
- package/declarations/request/-private/fetch.d.ts +24 -36
- package/declarations/request/-private/future.d.ts +3 -4
- package/declarations/request/-private/manager.d.ts +159 -132
- package/declarations/request/-private/promise-cache.d.ts +21 -13
- package/declarations/request/-private/types.d.ts +124 -123
- package/declarations/request/-private/utils.d.ts +8 -9
- package/declarations/request.d.ts +5 -433
- package/declarations/store/-private/cache-handler/handler.d.ts +62 -0
- package/declarations/store/-private/cache-handler/types.d.ts +98 -0
- package/declarations/store/-private/cache-handler/utils.d.ts +32 -0
- package/declarations/store/-private/caches/cache-utils.d.ts +12 -0
- package/declarations/store/-private/caches/identifier-cache.d.ts +304 -0
- package/declarations/store/-private/caches/instance-cache.d.ts +65 -0
- package/declarations/store/-private/caches/resource-utils.d.ts +9 -0
- package/declarations/store/-private/debug/utils.d.ts +6 -0
- package/declarations/store/-private/default-cache-policy.d.ts +384 -0
- package/declarations/store/-private/legacy-model-support/record-reference.d.ts +159 -0
- package/declarations/store/-private/legacy-model-support/shim-model-class.d.ts +17 -0
- package/declarations/store/-private/managers/cache-capabilities-manager.d.ts +25 -0
- package/declarations/store/-private/managers/cache-manager.d.ts +443 -0
- package/declarations/store/-private/managers/notification-manager.d.ts +98 -0
- package/declarations/store/-private/managers/record-array-manager.d.ts +97 -0
- package/declarations/store/-private/network/request-cache.d.ts +107 -0
- package/declarations/store/-private/new-core-tmp/promise-state.d.ts +263 -0
- package/declarations/store/-private/new-core-tmp/reactivity/configure.d.ts +176 -0
- package/declarations/store/-private/new-core-tmp/reactivity/internal.d.ts +169 -0
- package/declarations/store/-private/new-core-tmp/reactivity/signal.d.ts +35 -0
- package/declarations/store/-private/new-core-tmp/request-state.d.ts +277 -0
- package/declarations/store/-private/new-core-tmp/request-subscription.d.ts +261 -0
- package/declarations/store/-private/record-arrays/identifier-array.d.ts +147 -0
- package/declarations/store/-private/record-arrays/many-array.d.ts +197 -0
- package/declarations/store/-private/record-arrays/native-proxy-type-fix.d.ts +126 -0
- package/declarations/store/-private/store-service.d.ts +1605 -0
- package/declarations/store/-private/utils/coerce-id.d.ts +10 -0
- package/declarations/store/-private/utils/construct-resource.d.ts +6 -0
- package/declarations/store/-private/utils/is-non-empty-string.d.ts +1 -0
- package/declarations/store/-private/utils/normalize-model-name.d.ts +1 -0
- package/declarations/store/-private/utils/uuid-polyfill.d.ts +1 -0
- package/declarations/store/-private.d.ts +31 -0
- package/declarations/store/-types/q/cache-capabilities-manager.d.ts +99 -0
- package/declarations/store/-types/q/ds-model.d.ts +21 -0
- package/declarations/store/-types/q/identifier.d.ts +20 -0
- package/declarations/store/-types/q/record-instance.d.ts +23 -0
- package/declarations/store/-types/q/schema-service.d.ts +354 -0
- package/declarations/store/-types/q/store.d.ts +32 -0
- package/declarations/store.d.ts +1 -0
- package/declarations/types/-private.d.ts +16 -8
- package/declarations/types/cache/aliases.d.ts +11 -1
- package/declarations/types/cache/change.d.ts +4 -5
- package/declarations/types/cache/mutations.d.ts +51 -28
- package/declarations/types/cache/operations.d.ts +60 -47
- package/declarations/types/cache/relationship.d.ts +11 -9
- package/declarations/types/cache.d.ts +495 -484
- package/declarations/types/graph.d.ts +31 -40
- package/declarations/types/identifier.d.ts +83 -82
- package/declarations/types/json/raw.d.ts +1 -2
- package/declarations/types/params.d.ts +4 -5
- package/declarations/types/record.d.ts +117 -76
- package/declarations/types/request.d.ts +289 -266
- package/declarations/types/runtime.d.ts +8 -9
- package/declarations/types/schema/concepts.d.ts +19 -13
- package/declarations/types/schema/fields.d.ts +1712 -1741
- package/declarations/types/schema/fields.type-test.d.ts +0 -1
- package/declarations/types/spec/document.d.ts +28 -22
- package/declarations/types/spec/error.d.ts +16 -17
- package/declarations/types/spec/json-api-raw.d.ts +102 -102
- package/declarations/types/symbols.d.ts +74 -75
- package/declarations/types/utils.d.ts +5 -5
- package/declarations/types.d.ts +10 -7
- package/declarations/utils/string.d.ts +48 -0
- package/dist/build-config/babel-macros.js +1 -0
- package/dist/build-config/canary-features.js +1 -0
- package/dist/build-config/debugging.js +1 -0
- package/dist/build-config/deprecations.js +1 -0
- package/dist/build-config/env.js +1 -0
- package/dist/build-config/macros.js +1 -0
- package/dist/build-config.js +1 -0
- package/dist/configure-B48bFHOl.js +181 -0
- package/dist/configure.js +1 -0
- package/dist/{context-DE5sFezZ.js → context-COmAnXUQ.js} +2 -2
- package/dist/graph/-private.js +3372 -0
- package/dist/handler-C2T-IyJK.js +339 -0
- package/dist/index.js +37 -104
- package/dist/reactive/-private.js +1 -0
- package/dist/reactive.js +1988 -0
- package/dist/request-state-CjLph1LP.js +8139 -0
- package/dist/request.js +1 -1
- package/dist/store/-private.js +3 -0
- package/dist/store.js +545 -0
- package/dist/symbols-SIstXMLI.js +44 -0
- package/dist/types/-private.js +3 -3
- package/dist/types/record.js +1 -1
- package/dist/types/request.js +2 -2
- package/dist/types/runtime.js +1 -1
- package/dist/types/schema/fields.js +8 -41
- package/dist/types/symbols.js +3 -3
- package/dist/utils/string.js +92 -0
- package/logos/NCC-1701-a-blue.svg +4 -0
- package/logos/NCC-1701-a-gold.svg +4 -0
- package/logos/NCC-1701-a-gold_100.svg +1 -0
- package/logos/NCC-1701-a-gold_base-64.txt +1 -0
- package/logos/NCC-1701-a.svg +4 -0
- package/logos/README.md +4 -0
- package/logos/docs-badge.svg +2 -0
- package/logos/ember-data-logo-dark.svg +12 -0
- package/logos/ember-data-logo-light.svg +12 -0
- package/logos/github-header.svg +444 -0
- package/logos/social1.png +0 -0
- package/logos/social2.png +0 -0
- package/logos/warp-drive-logo-dark.svg +4 -0
- package/logos/warp-drive-logo-gold.svg +4 -0
- package/package.json +11 -8
- package/declarations/index.d.ts.map +0 -1
- package/declarations/request/-private/context.d.ts.map +0 -1
- package/declarations/request/-private/debug.d.ts.map +0 -1
- package/declarations/request/-private/fetch.d.ts.map +0 -1
- package/declarations/request/-private/future.d.ts.map +0 -1
- package/declarations/request/-private/manager.d.ts.map +0 -1
- package/declarations/request/-private/promise-cache.d.ts.map +0 -1
- package/declarations/request/-private/types.d.ts.map +0 -1
- package/declarations/request/-private/utils.d.ts.map +0 -1
- package/declarations/request.d.ts.map +0 -1
- package/declarations/types/-private.d.ts.map +0 -1
- package/declarations/types/cache/aliases.d.ts.map +0 -1
- package/declarations/types/cache/change.d.ts.map +0 -1
- package/declarations/types/cache/mutations.d.ts.map +0 -1
- package/declarations/types/cache/operations.d.ts.map +0 -1
- package/declarations/types/cache/relationship.d.ts.map +0 -1
- package/declarations/types/cache.d.ts.map +0 -1
- package/declarations/types/graph.d.ts.map +0 -1
- package/declarations/types/identifier.d.ts.map +0 -1
- package/declarations/types/json/raw.d.ts.map +0 -1
- package/declarations/types/params.d.ts.map +0 -1
- package/declarations/types/record.d.ts.map +0 -1
- package/declarations/types/record.type-test.d.ts +0 -2
- package/declarations/types/record.type-test.d.ts.map +0 -1
- package/declarations/types/request.d.ts.map +0 -1
- package/declarations/types/request.type-test.d.ts +0 -2
- package/declarations/types/request.type-test.d.ts.map +0 -1
- package/declarations/types/runtime.d.ts.map +0 -1
- package/declarations/types/schema/concepts.d.ts.map +0 -1
- package/declarations/types/schema/fields.d.ts.map +0 -1
- package/declarations/types/schema/fields.type-test.d.ts.map +0 -1
- package/declarations/types/spec/document.d.ts.map +0 -1
- package/declarations/types/spec/error.d.ts.map +0 -1
- package/declarations/types/spec/json-api-raw.d.ts.map +0 -1
- package/declarations/types/symbols.d.ts.map +0 -1
- package/declarations/types/utils.d.ts.map +0 -1
- package/declarations/types.d.ts.map +0 -1
- package/dist/context-DE5sFezZ.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/request.js.map +0 -1
- package/dist/types/-private.js.map +0 -1
- package/dist/types/cache/aliases.js.map +0 -1
- package/dist/types/cache/change.js.map +0 -1
- package/dist/types/cache/mutations.js.map +0 -1
- package/dist/types/cache/operations.js.map +0 -1
- package/dist/types/cache/relationship.js.map +0 -1
- package/dist/types/cache.js.map +0 -1
- package/dist/types/graph.js.map +0 -1
- package/dist/types/identifier.js.map +0 -1
- package/dist/types/json/raw.js.map +0 -1
- package/dist/types/params.js.map +0 -1
- package/dist/types/record.js.map +0 -1
- package/dist/types/request.js.map +0 -1
- package/dist/types/runtime.js.map +0 -1
- package/dist/types/schema/concepts.js.map +0 -1
- package/dist/types/schema/fields.js.map +0 -1
- package/dist/types/schema/fields.type-test.js.map +0 -1
- package/dist/types/spec/document.js.map +0 -1
- package/dist/types/spec/error.js.map +0 -1
- package/dist/types/spec/json-api-raw.js.map +0 -1
- package/dist/types/symbols.js.map +0 -1
- package/dist/types/utils.js.map +0 -1
- package/dist/types.js.map +0 -1
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { J as ReactiveDocument } from "./request-state-CjLph1LP.js";
|
|
2
|
+
import { SkipCache, EnableHydration } from './types/request.js';
|
|
3
|
+
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
4
|
+
const MUTATION_OPS = new Set(['createRecord', 'updateRecord', 'deleteRecord']);
|
|
5
|
+
function calcShouldFetch(store, request, hasCachedValue, identifier) {
|
|
6
|
+
const {
|
|
7
|
+
cacheOptions
|
|
8
|
+
} = request;
|
|
9
|
+
return request.op && MUTATION_OPS.has(request.op) || cacheOptions?.reload || !hasCachedValue || (store.lifetimes && identifier ? store.lifetimes.isHardExpired(identifier, store) : false);
|
|
10
|
+
}
|
|
11
|
+
function calcShouldBackgroundFetch(store, request, willFetch, identifier) {
|
|
12
|
+
const {
|
|
13
|
+
cacheOptions
|
|
14
|
+
} = request;
|
|
15
|
+
return cacheOptions?.backgroundReload || (store.lifetimes && identifier ? store.lifetimes.isSoftExpired(identifier, store) : false);
|
|
16
|
+
}
|
|
17
|
+
function isMutation(request) {
|
|
18
|
+
return Boolean(request.op && MUTATION_OPS.has(request.op));
|
|
19
|
+
}
|
|
20
|
+
function isCacheAffecting(document) {
|
|
21
|
+
if (!isMutation(document.request)) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
// a mutation combined with a 204 has no cache impact when no known records were involved
|
|
25
|
+
// a createRecord with a 201 with an empty response and no known records should similarly
|
|
26
|
+
// have no cache impact
|
|
27
|
+
|
|
28
|
+
if (document.request.op === 'createRecord' && document.response?.status === 201) {
|
|
29
|
+
return document.content ? Object.keys(document.content).length > 0 : false;
|
|
30
|
+
}
|
|
31
|
+
return document.response?.status !== 204;
|
|
32
|
+
}
|
|
33
|
+
function isAggregateError(error) {
|
|
34
|
+
return error instanceof AggregateError || error.name === 'AggregateError' && Array.isArray(error.errors);
|
|
35
|
+
}
|
|
36
|
+
// TODO @runspired, consider if we should deep freeze errors (potentially only in debug) vs cloning them
|
|
37
|
+
function cloneError(error) {
|
|
38
|
+
const isAggregate = isAggregateError(error);
|
|
39
|
+
const cloned = isAggregate ? new AggregateError(structuredClone(error.errors), error.message) : new Error(error.message);
|
|
40
|
+
cloned.stack = error.stack;
|
|
41
|
+
cloned.error = error.error;
|
|
42
|
+
|
|
43
|
+
// copy over enumerable properties
|
|
44
|
+
Object.assign(cloned, error);
|
|
45
|
+
return cloned;
|
|
46
|
+
}
|
|
47
|
+
function getPriority(identifier, deduped, priority) {
|
|
48
|
+
if (identifier) {
|
|
49
|
+
const existing = deduped.get(identifier);
|
|
50
|
+
if (existing) {
|
|
51
|
+
return existing.priority;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return priority;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A CacheHandler that adds support for using an WarpDrive Cache with a RequestManager.
|
|
59
|
+
*
|
|
60
|
+
* This handler will only run when a request has supplied a `store` instance. Requests
|
|
61
|
+
* issued by the store via `store.request()` will automatically have the `store` instance
|
|
62
|
+
* attached to the request.
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* requestManager.request({
|
|
66
|
+
* store: store,
|
|
67
|
+
* url: '/api/posts',
|
|
68
|
+
* method: 'GET'
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* When this handler elects to handle a request, it will return the raw `StructuredDocument`
|
|
73
|
+
* unless the request has `[EnableHydration]` set to `true`. In this case, the handler will
|
|
74
|
+
* return a `Document` instance that will automatically update the UI when the cache is updated
|
|
75
|
+
* in the future and will hydrate any identifiers in the StructuredDocument into Record instances.
|
|
76
|
+
*
|
|
77
|
+
* When issuing a request via the store, [EnableHydration] is automatically set to `true`. This
|
|
78
|
+
* means that if desired you can issue requests that utilize the cache without needing to also
|
|
79
|
+
* utilize Record instances if desired.
|
|
80
|
+
*
|
|
81
|
+
* Said differently, you could elect to issue all requests via a RequestManager, without ever using
|
|
82
|
+
* the store directly, by setting [EnableHydration] to `true` and providing a store instance. Not
|
|
83
|
+
* necessarily the most useful thing, but the decoupled nature of the RequestManager and incremental-feature
|
|
84
|
+
* approach of WarpDrive allows for this flexibility.
|
|
85
|
+
*
|
|
86
|
+
* ```ts
|
|
87
|
+
* import { EnableHydration } from '@warp-drive/core/types/request';
|
|
88
|
+
*
|
|
89
|
+
* requestManager.request({
|
|
90
|
+
* store: store,
|
|
91
|
+
* url: '/api/posts',
|
|
92
|
+
* method: 'GET',
|
|
93
|
+
* [EnableHydration]: true
|
|
94
|
+
* });
|
|
95
|
+
*
|
|
96
|
+
*/
|
|
97
|
+
const CacheHandler = {
|
|
98
|
+
request(context, next) {
|
|
99
|
+
// if we have no cache or no cache-key skip cache handling
|
|
100
|
+
if (!context.request.store || context.request.cacheOptions?.[SkipCache]) {
|
|
101
|
+
return next(context.request);
|
|
102
|
+
}
|
|
103
|
+
const {
|
|
104
|
+
store
|
|
105
|
+
} = context.request;
|
|
106
|
+
const identifier = store.identifierCache.getOrCreateDocumentIdentifier(context.request);
|
|
107
|
+
if (identifier) {
|
|
108
|
+
context.setIdentifier(identifier);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// used to dedupe existing requests that match
|
|
112
|
+
const DEDUPE = store.requestManager._deduped;
|
|
113
|
+
const activeRequest = identifier && DEDUPE.get(identifier);
|
|
114
|
+
const peeked = identifier ? store.cache.peekRequest(identifier) : null;
|
|
115
|
+
|
|
116
|
+
// determine if we should skip cache
|
|
117
|
+
if (calcShouldFetch(store, context.request, !!peeked, identifier)) {
|
|
118
|
+
if (activeRequest) {
|
|
119
|
+
activeRequest.priority = {
|
|
120
|
+
blocking: true
|
|
121
|
+
};
|
|
122
|
+
return activeRequest.promise;
|
|
123
|
+
}
|
|
124
|
+
let promise = fetchContentAndHydrate(next, context, identifier, {
|
|
125
|
+
blocking: true
|
|
126
|
+
});
|
|
127
|
+
if (identifier) {
|
|
128
|
+
promise = promise.finally(() => {
|
|
129
|
+
DEDUPE.delete(identifier);
|
|
130
|
+
store.notifications.notify(identifier, 'state');
|
|
131
|
+
});
|
|
132
|
+
DEDUPE.set(identifier, {
|
|
133
|
+
priority: {
|
|
134
|
+
blocking: true
|
|
135
|
+
},
|
|
136
|
+
promise
|
|
137
|
+
});
|
|
138
|
+
store.notifications.notify(identifier, 'state');
|
|
139
|
+
}
|
|
140
|
+
return promise;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// if we have not skipped cache, determine if we should update behind the scenes
|
|
144
|
+
if (calcShouldBackgroundFetch(store, context.request, false, identifier)) {
|
|
145
|
+
let promise = activeRequest?.promise || fetchContentAndHydrate(next, context, identifier, {
|
|
146
|
+
blocking: false
|
|
147
|
+
});
|
|
148
|
+
if (identifier && !activeRequest) {
|
|
149
|
+
promise = promise.finally(() => {
|
|
150
|
+
DEDUPE.delete(identifier);
|
|
151
|
+
store.notifications.notify(identifier, 'state');
|
|
152
|
+
});
|
|
153
|
+
DEDUPE.set(identifier, {
|
|
154
|
+
priority: {
|
|
155
|
+
blocking: false
|
|
156
|
+
},
|
|
157
|
+
promise
|
|
158
|
+
});
|
|
159
|
+
store.notifications.notify(identifier, 'state');
|
|
160
|
+
}
|
|
161
|
+
store.requestManager._pending.set(context.id, promise);
|
|
162
|
+
}
|
|
163
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
164
|
+
if (!test) {
|
|
165
|
+
throw new Error(`Expected a peeked request to be present`);
|
|
166
|
+
}
|
|
167
|
+
})(peeked) : {};
|
|
168
|
+
const shouldHydrate = context.request[EnableHydration] || false;
|
|
169
|
+
context.setResponse(peeked.response);
|
|
170
|
+
if ('error' in peeked) {
|
|
171
|
+
const content = shouldHydrate ? maybeUpdateUiObjects(store, context.request, {
|
|
172
|
+
shouldHydrate,
|
|
173
|
+
identifier
|
|
174
|
+
}, peeked.content) : peeked.content;
|
|
175
|
+
const newError = cloneError(peeked);
|
|
176
|
+
newError.content = content;
|
|
177
|
+
throw newError;
|
|
178
|
+
}
|
|
179
|
+
const result = shouldHydrate ? maybeUpdateUiObjects(store, context.request, {
|
|
180
|
+
shouldHydrate,
|
|
181
|
+
identifier
|
|
182
|
+
}, peeked.content) : peeked.content;
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
function maybeUpdateUiObjects(store, request, options, document) {
|
|
187
|
+
const {
|
|
188
|
+
identifier
|
|
189
|
+
} = options;
|
|
190
|
+
if (!document || !options.shouldHydrate) {
|
|
191
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
192
|
+
if (!test) {
|
|
193
|
+
throw new Error(`The CacheHandler expected response content but none was found`);
|
|
194
|
+
}
|
|
195
|
+
})(!options.shouldHydrate) : {};
|
|
196
|
+
return document ?? null;
|
|
197
|
+
}
|
|
198
|
+
if (identifier) {
|
|
199
|
+
return store._instanceCache.getDocument(identifier);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// if we don't have an identifier, we give the document
|
|
203
|
+
// its own local cache
|
|
204
|
+
return new ReactiveDocument(store, null, {
|
|
205
|
+
request,
|
|
206
|
+
document
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
function updateCacheForSuccess(store, request, options, document) {
|
|
210
|
+
let response = null;
|
|
211
|
+
if (isMutation(request)) {
|
|
212
|
+
const record = request.data?.record || request.records?.[0];
|
|
213
|
+
if (record) {
|
|
214
|
+
response = store.cache.didCommit(record, document);
|
|
215
|
+
|
|
216
|
+
// a mutation combined with a 204 has no cache impact when no known records were involved
|
|
217
|
+
// a createRecord with a 201 with an empty response and no known records should similarly
|
|
218
|
+
// have no cache impact
|
|
219
|
+
} else if (isCacheAffecting(document)) {
|
|
220
|
+
response = store.cache.put(document);
|
|
221
|
+
}
|
|
222
|
+
} else {
|
|
223
|
+
response = store.cache.put(document);
|
|
224
|
+
}
|
|
225
|
+
return maybeUpdateUiObjects(store, request, options, response);
|
|
226
|
+
}
|
|
227
|
+
function handleFetchSuccess(store, context, options, document) {
|
|
228
|
+
const {
|
|
229
|
+
request
|
|
230
|
+
} = context;
|
|
231
|
+
store.requestManager._pending.delete(context.id);
|
|
232
|
+
store._enableAsyncFlush = true;
|
|
233
|
+
let response;
|
|
234
|
+
store._join(() => {
|
|
235
|
+
response = updateCacheForSuccess(store, request, options, document);
|
|
236
|
+
});
|
|
237
|
+
store._enableAsyncFlush = null;
|
|
238
|
+
if (store.lifetimes?.didRequest) {
|
|
239
|
+
store.lifetimes.didRequest(context.request, document.response, options.identifier, store);
|
|
240
|
+
}
|
|
241
|
+
const finalPriority = getPriority(options.identifier, store.requestManager._deduped, options.priority);
|
|
242
|
+
if (finalPriority.blocking) {
|
|
243
|
+
return response;
|
|
244
|
+
} else {
|
|
245
|
+
store.notifications._flush();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function updateCacheForError(store, context, options, error) {
|
|
249
|
+
let response;
|
|
250
|
+
if (isMutation(context.request)) {
|
|
251
|
+
// TODO similar to didCommit we should spec this to be similar to cache.put for handling full response
|
|
252
|
+
// currently we let the response remain undefiend.
|
|
253
|
+
const errors = error && error.content && typeof error.content === 'object' && 'errors' in error.content && Array.isArray(error.content.errors) ? error.content.errors : undefined;
|
|
254
|
+
const record = context.request.data?.record || context.request.records?.[0];
|
|
255
|
+
store.cache.commitWasRejected(record, errors);
|
|
256
|
+
} else {
|
|
257
|
+
response = store.cache.put(error);
|
|
258
|
+
return maybeUpdateUiObjects(store, context.request, options, response);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function handleFetchError(store, context, options, error) {
|
|
262
|
+
store.requestManager._pending.delete(context.id);
|
|
263
|
+
if (context.request.signal?.aborted) {
|
|
264
|
+
throw error;
|
|
265
|
+
}
|
|
266
|
+
store._enableAsyncFlush = true;
|
|
267
|
+
let response;
|
|
268
|
+
store._join(() => {
|
|
269
|
+
response = updateCacheForError(store, context, options, error);
|
|
270
|
+
});
|
|
271
|
+
store._enableAsyncFlush = null;
|
|
272
|
+
if (options.identifier && store.lifetimes?.didRequest) {
|
|
273
|
+
store.lifetimes.didRequest(context.request, error.response, options.identifier, store);
|
|
274
|
+
}
|
|
275
|
+
if (isMutation(context.request)) {
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
const finalPriority = getPriority(options.identifier, store.requestManager._deduped, options.priority);
|
|
279
|
+
if (finalPriority.blocking) {
|
|
280
|
+
const newError = cloneError(error);
|
|
281
|
+
newError.content = response;
|
|
282
|
+
throw newError;
|
|
283
|
+
} else {
|
|
284
|
+
store.notifications._flush();
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function fetchContentAndHydrate(next, context, identifier, priority) {
|
|
288
|
+
const {
|
|
289
|
+
store
|
|
290
|
+
} = context.request;
|
|
291
|
+
const shouldHydrate = context.request[EnableHydration] || false;
|
|
292
|
+
const options = {
|
|
293
|
+
shouldHydrate,
|
|
294
|
+
identifier,
|
|
295
|
+
priority
|
|
296
|
+
};
|
|
297
|
+
let isMut = false;
|
|
298
|
+
if (isMutation(context.request)) {
|
|
299
|
+
isMut = true;
|
|
300
|
+
// TODO should we handle multiple records in request.records by iteratively calling willCommit for each
|
|
301
|
+
const record = context.request.data?.record || context.request.records?.[0];
|
|
302
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
303
|
+
if (!test) {
|
|
304
|
+
throw new Error(`Expected to receive a list of records included in the ${context.request.op} request`);
|
|
305
|
+
}
|
|
306
|
+
})(record || !shouldHydrate) : {};
|
|
307
|
+
if (record) {
|
|
308
|
+
store.cache.willCommit(record, context);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (store.lifetimes?.willRequest) {
|
|
312
|
+
store.lifetimes.willRequest(context.request, identifier, store);
|
|
313
|
+
}
|
|
314
|
+
const promise = next(context.request).then(document => {
|
|
315
|
+
return handleFetchSuccess(store, context, options, document);
|
|
316
|
+
}, error => {
|
|
317
|
+
return handleFetchError(store, context, options, error);
|
|
318
|
+
});
|
|
319
|
+
if (!isMut) {
|
|
320
|
+
return promise;
|
|
321
|
+
}
|
|
322
|
+
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
323
|
+
if (!test) {
|
|
324
|
+
throw new Error(`Expected a mutation`);
|
|
325
|
+
}
|
|
326
|
+
})(isMutation(context.request)) : {};
|
|
327
|
+
|
|
328
|
+
// for mutations we need to enqueue the promise with the requestStateService
|
|
329
|
+
// TODO should we enque a request per record in records?
|
|
330
|
+
const record = context.request.data?.record || context.request.records?.[0];
|
|
331
|
+
return store._requestCache._enqueue(promise, {
|
|
332
|
+
data: [{
|
|
333
|
+
op: 'saveRecord',
|
|
334
|
+
recordIdentifier: record,
|
|
335
|
+
options: undefined
|
|
336
|
+
}]
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
export { CacheHandler as C };
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
|
-
import { setLogging, getRuntimeConfig } from
|
|
2
|
-
import { a as cloneResponseProperties, I as IS_CACHE_HANDLER, b as assertValidRequest, e as executeNextHandler, d as getRequestResult, u as upgradePromise, s as setPromiseResult, f as clearRequestResult } from "./context-
|
|
3
|
-
import { macroCondition, getGlobalConfig
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* ```ts
|
|
11
|
-
* import Fetch from '@ember-data/request/fetch';
|
|
12
|
-
*
|
|
13
|
-
* manager.use([Fetch]);
|
|
14
|
-
* ```
|
|
15
|
-
*
|
|
16
|
-
* @module
|
|
17
|
-
*/
|
|
1
|
+
import { setLogging, getRuntimeConfig } from './types/runtime.js';
|
|
2
|
+
import { a as cloneResponseProperties, I as IS_CACHE_HANDLER, b as assertValidRequest, e as executeNextHandler, d as getRequestResult, u as upgradePromise, s as setPromiseResult, f as clearRequestResult } from "./context-COmAnXUQ.js";
|
|
3
|
+
import { macroCondition, getGlobalConfig } from '@embroider/macros';
|
|
4
|
+
import { w as waitFor } from "./configure-B48bFHOl.js";
|
|
5
|
+
import { peekUniversalTransient, setUniversalTransient } from './types/-private.js';
|
|
6
|
+
export { S as Store, r as recordIdentifierFor, N as setIdentifierForgetMethod, K as setIdentifierGenerationMethod, O as setIdentifierResetMethod, L as setIdentifierUpdateMethod, P as setKeyInfoForResource, s as storeFor } from "./request-state-CjLph1LP.js";
|
|
7
|
+
export { C as CacheHandler } from "./handler-C2T-IyJK.js";
|
|
8
|
+
import '@ember/debug';
|
|
9
|
+
import './utils/string.js';
|
|
18
10
|
|
|
19
11
|
// Lazily close over fetch to avoid breaking Mirage
|
|
20
12
|
const _fetch = typeof fetch !== 'undefined' ? (...args) => fetch(...args) : typeof FastBoot !== 'undefined' ? (...args) => FastBoot.require('node-fetch')(...args) : () => {
|
|
@@ -35,16 +27,20 @@ const MUTATION_OPS = new Set(['updateRecord', 'createRecord', 'deleteRecord']);
|
|
|
35
27
|
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']]);
|
|
36
28
|
|
|
37
29
|
/**
|
|
38
|
-
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { Fetch } from '@warp-drive/core';
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* A basic Fetch Handler which converts a request into a
|
|
39
35
|
* `fetch` call presuming the response to be `json`.
|
|
40
36
|
*
|
|
41
37
|
* ```ts
|
|
42
|
-
* import Fetch from '@
|
|
38
|
+
* import { RequestManager, Fetch } from '@warp-drive/core';
|
|
43
39
|
*
|
|
44
|
-
* manager
|
|
40
|
+
* const manager = new RequestManager()
|
|
41
|
+
* .use([Fetch]);
|
|
45
42
|
* ```
|
|
46
43
|
*
|
|
47
|
-
* @class Fetch
|
|
48
44
|
* @public
|
|
49
45
|
*/
|
|
50
46
|
const Fetch = {
|
|
@@ -187,84 +183,9 @@ const Fetch = {
|
|
|
187
183
|
function isDict(v) {
|
|
188
184
|
return v !== null && typeof v === 'object';
|
|
189
185
|
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* ```js
|
|
193
|
-
* import RequestManager from '@ember-data/request';
|
|
194
|
-
* ```
|
|
195
|
-
*
|
|
196
|
-
* A RequestManager provides a request/response flow in which configured
|
|
197
|
-
* handlers are successively given the opportunity to handle, modify, or
|
|
198
|
-
* pass-along a request.
|
|
199
|
-
*
|
|
200
|
-
* ```ts
|
|
201
|
-
* interface RequestManager {
|
|
202
|
-
* request<T>(req: RequestInfo): Future<T>;
|
|
203
|
-
* }
|
|
204
|
-
* ```
|
|
205
|
-
*
|
|
206
|
-
* For example:
|
|
207
|
-
*
|
|
208
|
-
* ```ts
|
|
209
|
-
* import RequestManager from '@ember-data/request';
|
|
210
|
-
* import Fetch from '@ember-data/request/fetch';
|
|
211
|
-
* import Auth from 'ember-simple-auth/ember-data-handler';
|
|
212
|
-
* import Config from './config';
|
|
213
|
-
*
|
|
214
|
-
* const { apiUrl } = Config;
|
|
215
|
-
*
|
|
216
|
-
* // ... create manager
|
|
217
|
-
* const manager = new RequestManager().use([Auth, Fetch]);
|
|
218
|
-
*
|
|
219
|
-
* // ... execute a request
|
|
220
|
-
* const response = await manager.request({
|
|
221
|
-
* url: `${apiUrl}/users`
|
|
222
|
-
* });
|
|
223
|
-
* ```
|
|
224
|
-
*
|
|
225
|
-
* ### Futures
|
|
226
|
-
*
|
|
227
|
-
* The return value of `manager.request` is a `Future`, which allows
|
|
228
|
-
* access to limited information about the request while it is still
|
|
229
|
-
* pending and fulfills with the final state when the request completes.
|
|
230
|
-
*
|
|
231
|
-
* A `Future` is cancellable via `abort`.
|
|
232
|
-
*
|
|
233
|
-
* Handlers may optionally expose a `ReadableStream` to the `Future` for
|
|
234
|
-
* streaming data; however, when doing so the future should not resolve
|
|
235
|
-
* until the response stream is fully read.
|
|
236
|
-
*
|
|
237
|
-
* ```ts
|
|
238
|
-
* interface Future<T> extends Promise<StructuredDocument<T>> {
|
|
239
|
-
* abort(): void;
|
|
240
|
-
*
|
|
241
|
-
* async getStream(): ReadableStream | null;
|
|
242
|
-
* }
|
|
243
|
-
* ```
|
|
244
|
-
*
|
|
245
|
-
* ### StructuredDocuments
|
|
246
|
-
*
|
|
247
|
-
* A Future resolves with a `StructuredDataDocument` or rejects with a `StructuredErrorDocument`.
|
|
248
|
-
*
|
|
249
|
-
* ```ts
|
|
250
|
-
* interface StructuredDataDocument<T> {
|
|
251
|
-
* request: ImmutableRequestInfo;
|
|
252
|
-
* response: ImmutableResponseInfo;
|
|
253
|
-
* content: T;
|
|
254
|
-
* }
|
|
255
|
-
* interface StructuredErrorDocument extends Error {
|
|
256
|
-
* request: ImmutableRequestInfo;
|
|
257
|
-
* response: ImmutableResponseInfo;
|
|
258
|
-
* error: string | object;
|
|
259
|
-
* }
|
|
260
|
-
* type StructuredDocument<T> = StructuredDataDocument<T> | StructuredErrorDocument;
|
|
261
|
-
* ```
|
|
262
|
-
*
|
|
263
|
-
* @class RequestManager
|
|
264
|
-
* @public
|
|
265
|
-
*/
|
|
266
186
|
class RequestManager {
|
|
267
187
|
#handlers = [];
|
|
188
|
+
/** @internal */
|
|
268
189
|
|
|
269
190
|
/**
|
|
270
191
|
* A map of pending requests from request.id to their
|
|
@@ -275,6 +196,8 @@ class RequestManager {
|
|
|
275
196
|
* @internal
|
|
276
197
|
*/
|
|
277
198
|
|
|
199
|
+
/** @internal */
|
|
200
|
+
|
|
278
201
|
constructor(options) {
|
|
279
202
|
Object.assign(this, options);
|
|
280
203
|
this._pending = new Map();
|
|
@@ -289,8 +212,6 @@ class RequestManager {
|
|
|
289
212
|
* registers itself as a Cache handler.
|
|
290
213
|
*
|
|
291
214
|
* @public
|
|
292
|
-
* @param {Handler[]} cacheHandler
|
|
293
|
-
* @return {ThisType}
|
|
294
215
|
*/
|
|
295
216
|
useCache(cacheHandler) {
|
|
296
217
|
if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
|
|
@@ -328,7 +249,7 @@ class RequestManager {
|
|
|
328
249
|
throw new Error(`\`RequestManager.use(<Handler[]>)\` expects an array of handlers, but was called with \`${typeof newHandlers}\``);
|
|
329
250
|
}
|
|
330
251
|
newHandlers.forEach((handler, index) => {
|
|
331
|
-
if (!handler || typeof handler !== 'object' || typeof handler.request !== 'function') {
|
|
252
|
+
if (!handler || typeof handler !== 'function' && typeof handler !== 'object' || typeof handler.request !== 'function') {
|
|
332
253
|
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.`);
|
|
333
254
|
}
|
|
334
255
|
});
|
|
@@ -375,10 +296,7 @@ class RequestManager {
|
|
|
375
296
|
const cacheResult = getRequestResult(requestId);
|
|
376
297
|
if (macroCondition(getGlobalConfig().WarpDrive.env.TESTING)) {
|
|
377
298
|
if (!request.disableTestWaiter) {
|
|
378
|
-
const
|
|
379
|
-
waitForPromise
|
|
380
|
-
} = importSync('@ember/test-waiters');
|
|
381
|
-
const newPromise = waitForPromise(promise);
|
|
299
|
+
const newPromise = waitFor(promise);
|
|
382
300
|
const finalPromise = upgradePromise(newPromise.then(result => {
|
|
383
301
|
setPromiseResult(finalPromise, {
|
|
384
302
|
isError: false,
|
|
@@ -427,11 +345,26 @@ class RequestManager {
|
|
|
427
345
|
}
|
|
428
346
|
return finalPromise;
|
|
429
347
|
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* This method exists so that the RequestManager can be created
|
|
351
|
+
* can be created by container/factory systems that expect to
|
|
352
|
+
* call a static `create` method to instantiate the class.
|
|
353
|
+
*
|
|
354
|
+
* Using `new RequestManager()` directly is preferred.
|
|
355
|
+
*
|
|
356
|
+
* @private
|
|
357
|
+
*/
|
|
430
358
|
static create(options) {
|
|
431
359
|
return new this(options);
|
|
432
360
|
}
|
|
433
361
|
}
|
|
434
362
|
|
|
363
|
+
/**
|
|
364
|
+
* @module
|
|
365
|
+
* @mergeModuleWith <project>
|
|
366
|
+
*/
|
|
367
|
+
|
|
435
368
|
// @ts-expect-error adding to globalThis
|
|
436
369
|
globalThis.setWarpDriveLogging = setLogging;
|
|
437
370
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { E as Editable, L as Legacy } from "../symbols-SIstXMLI.js";
|