@khanacademy/wonder-blocks-data 10.0.5 → 10.1.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/CHANGELOG.md +38 -0
- package/dist/components/data.d.ts +52 -0
- package/dist/components/data.js.flow +63 -0
- package/dist/components/gql-router.d.ts +24 -0
- package/dist/components/gql-router.js.flow +33 -0
- package/dist/components/intercept-context.d.ts +10 -0
- package/dist/components/intercept-context.js.flow +19 -0
- package/dist/components/intercept-requests.d.ts +42 -0
- package/dist/components/intercept-requests.js.flow +51 -0
- package/dist/components/track-data.d.ts +11 -0
- package/dist/components/track-data.js.flow +18 -0
- package/dist/es/index.js +196 -214
- package/dist/hooks/use-cached-effect.d.ts +70 -0
- package/dist/hooks/use-cached-effect.js.flow +85 -0
- package/dist/hooks/use-gql-router-context.d.ts +5 -0
- package/dist/hooks/use-gql-router-context.js.flow +15 -0
- package/dist/hooks/use-gql.d.ts +12 -0
- package/dist/hooks/use-gql.js.flow +29 -0
- package/dist/hooks/use-hydratable-effect.d.ts +102 -0
- package/dist/hooks/use-hydratable-effect.js.flow +125 -0
- package/dist/hooks/use-request-interception.d.ts +14 -0
- package/dist/hooks/use-request-interception.js.flow +25 -0
- package/dist/hooks/use-server-effect.d.ts +39 -0
- package/dist/hooks/use-server-effect.js.flow +51 -0
- package/dist/hooks/use-shared-cache.d.ts +32 -0
- package/dist/hooks/use-shared-cache.js.flow +43 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +198 -219
- package/dist/index.js.flow +48 -2
- package/dist/util/data-error.d.ts +45 -0
- package/dist/util/data-error.js.flow +64 -0
- package/dist/util/get-gql-data-from-response.d.ts +4 -0
- package/dist/util/get-gql-data-from-response.js.flow +13 -0
- package/dist/util/get-gql-request-id.d.ts +5 -0
- package/dist/util/get-gql-request-id.js.flow +20 -0
- package/dist/util/gql-error.d.ts +28 -0
- package/dist/util/gql-error.js.flow +43 -0
- package/dist/util/gql-router-context.d.ts +3 -0
- package/dist/util/gql-router-context.js.flow +10 -0
- package/dist/util/gql-types.d.ts +34 -0
- package/dist/util/gql-types.js.flow +53 -0
- package/dist/util/graphql-document-node-parser.d.ts +18 -0
- package/dist/util/graphql-document-node-parser.js.flow +31 -0
- package/dist/util/graphql-types.d.ts +19 -0
- package/dist/util/graphql-types.js.flow +30 -0
- package/dist/util/hydration-cache-api.d.ts +17 -0
- package/dist/util/hydration-cache-api.js.flow +30 -0
- package/dist/util/merge-gql-context.d.ts +8 -0
- package/dist/util/merge-gql-context.js.flow +19 -0
- package/dist/util/purge-caches.d.ts +8 -0
- package/dist/util/purge-caches.js.flow +15 -0
- package/dist/util/request-api.d.ts +28 -0
- package/dist/util/request-api.js.flow +34 -0
- package/dist/util/request-fulfillment.d.ts +37 -0
- package/dist/util/request-fulfillment.js.flow +50 -0
- package/dist/util/request-tracking.d.ts +62 -0
- package/dist/util/request-tracking.js.flow +81 -0
- package/dist/util/result-from-cache-response.d.ts +5 -0
- package/dist/util/result-from-cache-response.js.flow +15 -0
- package/dist/util/scoped-in-memory-cache.d.ts +38 -0
- package/dist/util/scoped-in-memory-cache.js.flow +57 -0
- package/dist/util/serializable-in-memory-cache.d.ts +16 -0
- package/dist/util/serializable-in-memory-cache.js.flow +26 -0
- package/dist/util/ssr-cache.d.ts +51 -0
- package/dist/util/ssr-cache.js.flow +87 -0
- package/dist/util/status.d.ts +10 -0
- package/dist/util/status.js.flow +19 -0
- package/dist/util/to-gql-operation.d.ts +32 -0
- package/dist/util/to-gql-operation.js.flow +45 -0
- package/dist/util/types.d.ts +111 -0
- package/dist/util/types.js.flow +151 -0
- package/package.json +5 -6
- package/src/components/__tests__/{data.test.js → data.test.tsx} +50 -16
- package/src/components/__tests__/{gql-router.test.js → gql-router.test.tsx} +6 -7
- package/src/components/__tests__/{intercept-requests.test.js → intercept-requests.test.tsx} +4 -5
- package/src/components/__tests__/{track-data.test.js → track-data.test.tsx} +4 -5
- package/src/components/{data.js → data.ts} +13 -21
- package/src/components/{gql-router.js → gql-router.tsx} +14 -16
- package/src/components/{intercept-context.js → intercept-context.ts} +5 -4
- package/src/components/{intercept-requests.js → intercept-requests.tsx} +9 -10
- package/src/components/{track-data.js → track-data.tsx} +5 -6
- package/src/hooks/__tests__/{use-cached-effect.test.js → use-cached-effect.test.tsx} +65 -63
- package/src/hooks/__tests__/{use-gql-router-context.test.js → use-gql-router-context.test.tsx} +9 -9
- package/src/hooks/__tests__/{use-gql.test.js → use-gql.test.tsx} +23 -24
- package/src/hooks/__tests__/{use-hydratable-effect.test.js → use-hydratable-effect.test.ts} +52 -54
- package/src/hooks/__tests__/{use-request-interception.test.js → use-request-interception.test.tsx} +7 -5
- package/src/hooks/__tests__/{use-server-effect.test.js → use-server-effect.test.ts} +16 -10
- package/src/hooks/__tests__/{use-shared-cache.test.js → use-shared-cache.test.ts} +13 -13
- package/src/hooks/{use-cached-effect.js → use-cached-effect.ts} +34 -31
- package/src/hooks/{use-gql-router-context.js → use-gql-router-context.ts} +6 -7
- package/src/hooks/{use-gql.js → use-gql.ts} +9 -9
- package/src/hooks/{use-hydratable-effect.js → use-hydratable-effect.ts} +60 -67
- package/src/hooks/{use-request-interception.js → use-request-interception.ts} +6 -6
- package/src/hooks/{use-server-effect.js → use-server-effect.ts} +12 -14
- package/src/hooks/{use-shared-cache.js → use-shared-cache.ts} +16 -11
- package/src/index.ts +46 -0
- package/src/util/__tests__/{get-gql-data-from-response.test.js → get-gql-data-from-response.test.ts} +1 -2
- package/src/util/__tests__/{get-gql-request-id.test.js → get-gql-request-id.test.ts} +10 -12
- package/src/util/__tests__/{graphql-document-node-parser.test.js → graphql-document-node-parser.test.ts} +12 -13
- package/src/util/__tests__/{hydration-cache-api.test.js → hydration-cache-api.test.ts} +3 -4
- package/src/util/__tests__/{merge-gql-context.test.js → merge-gql-context.test.ts} +5 -6
- package/src/util/__tests__/{purge-caches.test.js → purge-caches.test.ts} +3 -4
- package/src/util/__tests__/{request-api.test.js → request-api.test.ts} +5 -5
- package/src/util/__tests__/{request-fulfillment.test.js → request-fulfillment.test.ts} +2 -3
- package/src/util/__tests__/{request-tracking.test.js → request-tracking.test.tsx} +15 -8
- package/src/util/__tests__/{result-from-cache-response.test.js → result-from-cache-response.test.ts} +3 -5
- package/src/util/__tests__/{scoped-in-memory-cache.test.js → scoped-in-memory-cache.test.ts} +5 -6
- package/src/util/__tests__/{serializable-in-memory-cache.test.js → serializable-in-memory-cache.test.ts} +8 -8
- package/src/util/__tests__/{ssr-cache.test.js → ssr-cache.test.ts} +5 -4
- package/src/util/__tests__/{to-gql-operation.test.js → to-gql-operation.test.ts} +5 -4
- package/src/util/{data-error.js → data-error.ts} +3 -4
- package/src/util/{get-gql-data-from-response.js → get-gql-data-from-response.ts} +3 -8
- package/src/util/{get-gql-request-id.js → get-gql-request-id.ts} +13 -17
- package/src/util/{gql-error.js → gql-error.ts} +3 -4
- package/src/util/gql-router-context.ts +6 -0
- package/src/util/{gql-types.js → gql-types.ts} +27 -23
- package/src/util/{graphql-document-node-parser.js → graphql-document-node-parser.ts} +8 -9
- package/src/util/graphql-types.ts +27 -0
- package/src/util/{hydration-cache-api.js → hydration-cache-api.ts} +6 -4
- package/src/util/{merge-gql-context.js → merge-gql-context.ts} +3 -3
- package/src/util/{purge-caches.js → purge-caches.ts} +2 -3
- package/src/util/{request-api.js → request-api.ts} +4 -5
- package/src/util/{request-fulfillment.js → request-fulfillment.ts} +15 -14
- package/src/util/{request-tracking.js → request-tracking.ts} +15 -16
- package/src/util/{result-from-cache-response.js → result-from-cache-response.ts} +6 -7
- package/src/util/{scoped-in-memory-cache.js → scoped-in-memory-cache.ts} +3 -4
- package/src/util/{serializable-in-memory-cache.js → serializable-in-memory-cache.ts} +5 -6
- package/src/util/{ssr-cache.js → ssr-cache.ts} +21 -20
- package/src/util/{status.js → status.ts} +5 -6
- package/src/util/{to-gql-operation.js → to-gql-operation.ts} +4 -5
- package/src/util/{types.js → types.ts} +41 -49
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/src/__docs__/_overview_.stories.mdx +0 -18
- package/src/__docs__/_overview_graphql.stories.mdx +0 -35
- package/src/__docs__/_overview_ssr_.stories.mdx +0 -185
- package/src/__docs__/_overview_testing_.stories.mdx +0 -123
- package/src/__docs__/exports.abort-inflight-requests.stories.mdx +0 -20
- package/src/__docs__/exports.data-error.stories.mdx +0 -23
- package/src/__docs__/exports.data-errors.stories.mdx +0 -23
- package/src/__docs__/exports.data.stories.mdx +0 -146
- package/src/__docs__/exports.fetch-tracked-requests.stories.mdx +0 -24
- package/src/__docs__/exports.get-gql-request-id.stories.mdx +0 -24
- package/src/__docs__/exports.gql-error.stories.mdx +0 -23
- package/src/__docs__/exports.gql-errors.stories.mdx +0 -20
- package/src/__docs__/exports.gql-router.stories.mdx +0 -29
- package/src/__docs__/exports.has-tracked-requests-to-be-fetched.stories.mdx +0 -20
- package/src/__docs__/exports.intercept-requests.stories.mdx +0 -69
- package/src/__docs__/exports.intialize-hydration-cache.stories.mdx +0 -29
- package/src/__docs__/exports.purge-caches.stories.mdx +0 -23
- package/src/__docs__/exports.purge-hydration-cache.stories.mdx +0 -24
- package/src/__docs__/exports.scoped-in-memory-cache.stories.mdx +0 -92
- package/src/__docs__/exports.serializable-in-memory-cache.stories.mdx +0 -112
- package/src/__docs__/exports.shared-cache.stories.mdx +0 -16
- package/src/__docs__/exports.status.stories.mdx +0 -31
- package/src/__docs__/exports.track-data.stories.mdx +0 -209
- package/src/__docs__/exports.use-cached-effect.stories.mdx +0 -44
- package/src/__docs__/exports.use-gql.stories.mdx +0 -41
- package/src/__docs__/exports.use-hydratable-effect.stories.mdx +0 -43
- package/src/__docs__/exports.use-server-effect.stories.mdx +0 -50
- package/src/__docs__/exports.use-shared-cache.stories.mdx +0 -30
- package/src/__docs__/exports.when-client-side.stories.mdx +0 -33
- package/src/__docs__/types.cached-response.stories.mdx +0 -29
- package/src/__docs__/types.error-options.stories.mdx +0 -21
- package/src/__docs__/types.fetch-policy.stories.mdx +0 -44
- package/src/__docs__/types.gql-context.stories.mdx +0 -20
- package/src/__docs__/types.gql-fetch-fn.stories.mdx +0 -24
- package/src/__docs__/types.gql-fetch-options.stories.mdx +0 -24
- package/src/__docs__/types.gql-operation-type.stories.mdx +0 -24
- package/src/__docs__/types.gql-operation.stories.mdx +0 -67
- package/src/__docs__/types.raw-scoped-cache.stories.mdx +0 -27
- package/src/__docs__/types.response-cache.stories.mdx +0 -33
- package/src/__docs__/types.result.stories.mdx +0 -39
- package/src/__docs__/types.scoped-cache.stories.mdx +0 -114
- package/src/__docs__/types.valid-cache-data.stories.mdx +0 -23
- package/src/index.js +0 -55
- package/src/util/gql-router-context.js +0 -6
- package/src/util/graphql-types.js +0 -30
- /package/src/hooks/__tests__/__snapshots__/{use-shared-cache.test.js.snap → use-shared-cache.test.ts.snap} +0 -0
- /package/src/util/__tests__/__snapshots__/{scoped-in-memory-cache.test.js.snap → scoped-in-memory-cache.test.ts.snap} +0 -0
- /package/src/util/__tests__/__snapshots__/{serializable-in-memory-cache.test.js.snap → serializable-in-memory-cache.test.ts.snap} +0 -0
package/dist/es/index.js
CHANGED
|
@@ -1,10 +1,72 @@
|
|
|
1
1
|
import { Server, useForceUpdate } from '@khanacademy/wonder-blocks-core';
|
|
2
2
|
import { KindError, clone, entries } from '@khanacademy/wonder-stuff-core';
|
|
3
|
-
import _extends from '@babel/runtime/helpers/extends';
|
|
4
3
|
import * as React from 'react';
|
|
5
4
|
import { useContext, useRef, useMemo, useCallback } from 'react';
|
|
6
5
|
|
|
7
|
-
const FetchPolicy =
|
|
6
|
+
const FetchPolicy = {
|
|
7
|
+
CacheBeforeNetwork: "CacheBeforeNetwork",
|
|
8
|
+
CacheAndNetwork: "CacheAndNetwork",
|
|
9
|
+
CacheOnly: "CacheOnly",
|
|
10
|
+
NetworkOnly: "NetworkOnly"
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
function _typeof(obj) {
|
|
14
|
+
"@babel/helpers - typeof";
|
|
15
|
+
|
|
16
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
|
|
17
|
+
return typeof obj;
|
|
18
|
+
} : function (obj) {
|
|
19
|
+
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
|
20
|
+
}, _typeof(obj);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function _toPrimitive(input, hint) {
|
|
24
|
+
if (_typeof(input) !== "object" || input === null) return input;
|
|
25
|
+
var prim = input[Symbol.toPrimitive];
|
|
26
|
+
if (prim !== undefined) {
|
|
27
|
+
var res = prim.call(input, hint || "default");
|
|
28
|
+
if (_typeof(res) !== "object") return res;
|
|
29
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
30
|
+
}
|
|
31
|
+
return (hint === "string" ? String : Number)(input);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function _toPropertyKey(arg) {
|
|
35
|
+
var key = _toPrimitive(arg, "string");
|
|
36
|
+
return _typeof(key) === "symbol" ? key : String(key);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function _defineProperties(target, props) {
|
|
40
|
+
for (var i = 0; i < props.length; i++) {
|
|
41
|
+
var descriptor = props[i];
|
|
42
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
43
|
+
descriptor.configurable = true;
|
|
44
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
45
|
+
Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
49
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
50
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
51
|
+
Object.defineProperty(Constructor, "prototype", {
|
|
52
|
+
writable: false
|
|
53
|
+
});
|
|
54
|
+
return Constructor;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function _setPrototypeOf(o, p) {
|
|
58
|
+
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
|
|
59
|
+
o.__proto__ = p;
|
|
60
|
+
return o;
|
|
61
|
+
};
|
|
62
|
+
return _setPrototypeOf(o, p);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function _inheritsLoose(subClass, superClass) {
|
|
66
|
+
subClass.prototype = Object.create(superClass.prototype);
|
|
67
|
+
subClass.prototype.constructor = subClass;
|
|
68
|
+
_setPrototypeOf(subClass, superClass);
|
|
69
|
+
}
|
|
8
70
|
|
|
9
71
|
const DataErrors = Object.freeze({
|
|
10
72
|
Unknown: "Unknown",
|
|
@@ -15,116 +77,106 @@ const DataErrors = Object.freeze({
|
|
|
15
77
|
Parse: "Parse",
|
|
16
78
|
Hydrated: "Hydrated"
|
|
17
79
|
});
|
|
18
|
-
|
|
19
|
-
|
|
80
|
+
let DataError = function (_KindError) {
|
|
81
|
+
_inheritsLoose(DataError, _KindError);
|
|
82
|
+
function DataError(message, kind, {
|
|
20
83
|
metadata,
|
|
21
84
|
cause
|
|
22
85
|
} = {}) {
|
|
23
|
-
|
|
86
|
+
return _KindError.call(this, message, kind, {
|
|
24
87
|
metadata,
|
|
25
88
|
cause,
|
|
26
89
|
name: "Data"
|
|
27
|
-
});
|
|
90
|
+
}) || this;
|
|
28
91
|
}
|
|
92
|
+
return DataError;
|
|
93
|
+
}(KindError);
|
|
29
94
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
constructor(initialCache = {}) {
|
|
95
|
+
let ScopedInMemoryCache = function () {
|
|
96
|
+
function ScopedInMemoryCache(initialCache = {}) {
|
|
97
|
+
this._cache = void 0;
|
|
34
98
|
this._cache = initialCache;
|
|
35
99
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return Object.keys(this._cache).length > 0;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
set(scope, id, value) {
|
|
100
|
+
var _proto = ScopedInMemoryCache.prototype;
|
|
101
|
+
_proto.set = function set(scope, id, value) {
|
|
42
102
|
var _this$_cache$scope;
|
|
43
|
-
|
|
44
103
|
if (!id || typeof id !== "string") {
|
|
45
104
|
throw new DataError("id must be non-empty string", DataErrors.InvalidInput);
|
|
46
105
|
}
|
|
47
|
-
|
|
48
106
|
if (!scope || typeof scope !== "string") {
|
|
49
107
|
throw new DataError("scope must be non-empty string", DataErrors.InvalidInput);
|
|
50
108
|
}
|
|
51
|
-
|
|
52
109
|
if (typeof value === "function") {
|
|
53
110
|
throw new DataError("value must be a non-function value", DataErrors.InvalidInput);
|
|
54
111
|
}
|
|
55
|
-
|
|
56
112
|
this._cache[scope] = (_this$_cache$scope = this._cache[scope]) != null ? _this$_cache$scope : {};
|
|
57
113
|
this._cache[scope][id] = value;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
get(scope, id) {
|
|
114
|
+
};
|
|
115
|
+
_proto.get = function get(scope, id) {
|
|
61
116
|
var _this$_cache$scope$id, _this$_cache$scope2;
|
|
62
|
-
|
|
63
117
|
return (_this$_cache$scope$id = (_this$_cache$scope2 = this._cache[scope]) == null ? void 0 : _this$_cache$scope2[id]) != null ? _this$_cache$scope$id : null;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
purge(scope, id) {
|
|
118
|
+
};
|
|
119
|
+
_proto.purge = function purge(scope, id) {
|
|
67
120
|
var _this$_cache$scope3;
|
|
68
|
-
|
|
69
121
|
if (!((_this$_cache$scope3 = this._cache[scope]) != null && _this$_cache$scope3[id])) {
|
|
70
122
|
return;
|
|
71
123
|
}
|
|
72
|
-
|
|
73
124
|
delete this._cache[scope][id];
|
|
74
|
-
|
|
75
125
|
if (Object.keys(this._cache[scope]).length === 0) {
|
|
76
126
|
delete this._cache[scope];
|
|
77
127
|
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
purgeScope(scope, predicate) {
|
|
128
|
+
};
|
|
129
|
+
_proto.purgeScope = function purgeScope(scope, predicate) {
|
|
81
130
|
if (!this._cache[scope]) {
|
|
82
131
|
return;
|
|
83
132
|
}
|
|
84
|
-
|
|
85
133
|
if (predicate == null) {
|
|
86
134
|
delete this._cache[scope];
|
|
87
135
|
return;
|
|
88
136
|
}
|
|
89
|
-
|
|
90
137
|
for (const key of Object.keys(this._cache[scope])) {
|
|
91
138
|
if (predicate(key, this._cache[scope][key])) {
|
|
92
139
|
delete this._cache[scope][key];
|
|
93
140
|
}
|
|
94
141
|
}
|
|
95
|
-
|
|
96
142
|
if (Object.keys(this._cache[scope]).length === 0) {
|
|
97
143
|
delete this._cache[scope];
|
|
98
144
|
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
purgeAll(predicate) {
|
|
145
|
+
};
|
|
146
|
+
_proto.purgeAll = function purgeAll(predicate) {
|
|
102
147
|
if (predicate == null) {
|
|
103
148
|
this._cache = {};
|
|
104
149
|
return;
|
|
105
150
|
}
|
|
106
|
-
|
|
107
151
|
for (const scope of Object.keys(this._cache)) {
|
|
108
152
|
this.purgeScope(scope, (id, value) => predicate(scope, id, value));
|
|
109
153
|
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
154
|
+
};
|
|
155
|
+
_createClass(ScopedInMemoryCache, [{
|
|
156
|
+
key: "inUse",
|
|
157
|
+
get: function () {
|
|
158
|
+
return Object.keys(this._cache).length > 0;
|
|
159
|
+
}
|
|
160
|
+
}]);
|
|
161
|
+
return ScopedInMemoryCache;
|
|
162
|
+
}();
|
|
163
|
+
|
|
164
|
+
let SerializableInMemoryCache = function (_ScopedInMemoryCache) {
|
|
165
|
+
_inheritsLoose(SerializableInMemoryCache, _ScopedInMemoryCache);
|
|
166
|
+
function SerializableInMemoryCache(initialCache = {}) {
|
|
167
|
+
var _this;
|
|
116
168
|
try {
|
|
117
|
-
|
|
169
|
+
_this = _ScopedInMemoryCache.call(this, clone(initialCache)) || this;
|
|
118
170
|
} catch (e) {
|
|
119
171
|
throw new DataError(`An error occurred trying to initialize from a response cache snapshot: ${e}`, DataErrors.InvalidInput);
|
|
120
172
|
}
|
|
173
|
+
return _this;
|
|
121
174
|
}
|
|
122
|
-
|
|
123
|
-
set(scope, id, value) {
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
clone() {
|
|
175
|
+
var _proto = SerializableInMemoryCache.prototype;
|
|
176
|
+
_proto.set = function set(scope, id, value) {
|
|
177
|
+
_ScopedInMemoryCache.prototype.set.call(this, scope, id, Object.freeze(clone(value)));
|
|
178
|
+
};
|
|
179
|
+
_proto.clone = function clone$1() {
|
|
128
180
|
try {
|
|
129
181
|
return clone(this._cache);
|
|
130
182
|
} catch (e) {
|
|
@@ -132,79 +184,57 @@ class SerializableInMemoryCache extends ScopedInMemoryCache {
|
|
|
132
184
|
cause: e
|
|
133
185
|
});
|
|
134
186
|
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
}
|
|
187
|
+
};
|
|
188
|
+
return SerializableInMemoryCache;
|
|
189
|
+
}(ScopedInMemoryCache);
|
|
138
190
|
|
|
139
191
|
const DefaultScope$2 = "default";
|
|
140
|
-
|
|
141
192
|
let _default$2;
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
_default$2 = new SsrCache();
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return _default$2;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
constructor(hydrationCache = null, ssrOnlyCache = null) {
|
|
193
|
+
let SsrCache = function () {
|
|
194
|
+
function SsrCache(hydrationCache = null, ssrOnlyCache = null) {
|
|
195
|
+
this._hydrationCache = void 0;
|
|
196
|
+
this._ssrOnlyCache = void 0;
|
|
153
197
|
this.initialize = source => {
|
|
154
198
|
if (this._hydrationCache.inUse) {
|
|
155
199
|
throw new Error("Cannot initialize data response cache more than once");
|
|
156
200
|
}
|
|
157
|
-
|
|
158
201
|
this._hydrationCache = new SerializableInMemoryCache({
|
|
159
202
|
[DefaultScope$2]: source
|
|
160
203
|
});
|
|
161
204
|
};
|
|
162
|
-
|
|
163
205
|
this.cacheData = (id, data, hydrate) => this._setCachedResponse(id, {
|
|
164
206
|
data
|
|
165
207
|
}, hydrate);
|
|
166
|
-
|
|
167
208
|
this.cacheError = (id, error, hydrate) => {
|
|
168
209
|
const errorMessage = typeof error === "string" ? error : error.message;
|
|
169
210
|
return this._setCachedResponse(id, {
|
|
170
211
|
error: errorMessage
|
|
171
212
|
}, hydrate);
|
|
172
213
|
};
|
|
173
|
-
|
|
174
214
|
this.getEntry = id => {
|
|
175
215
|
const ssrEntry = Server.isServerSide() ? this._ssrOnlyCache.get(DefaultScope$2, id) : null;
|
|
176
216
|
const internalEntry = ssrEntry != null ? ssrEntry : this._hydrationCache.get(DefaultScope$2, id);
|
|
177
|
-
|
|
178
217
|
if (!Server.isServerSide() && internalEntry != null) {
|
|
179
218
|
this._hydrationCache.purge(DefaultScope$2, id);
|
|
180
219
|
}
|
|
181
|
-
|
|
182
220
|
return internalEntry;
|
|
183
221
|
};
|
|
184
|
-
|
|
185
222
|
this.purgeData = predicate => {
|
|
186
223
|
const realPredicate = predicate ? (_, key, cachedEntry) => predicate(key, cachedEntry) : undefined;
|
|
187
|
-
|
|
188
224
|
this._hydrationCache.purgeAll(realPredicate);
|
|
189
|
-
|
|
190
225
|
this._ssrOnlyCache.purgeAll(realPredicate);
|
|
191
226
|
};
|
|
192
|
-
|
|
193
227
|
this.cloneHydratableData = () => {
|
|
194
228
|
var _cache$DefaultScope;
|
|
195
|
-
|
|
196
229
|
const cache = this._hydrationCache.clone();
|
|
197
|
-
|
|
198
230
|
return (_cache$DefaultScope = cache[DefaultScope$2]) != null ? _cache$DefaultScope : {};
|
|
199
231
|
};
|
|
200
|
-
|
|
201
232
|
this._ssrOnlyCache = ssrOnlyCache || new SerializableInMemoryCache();
|
|
202
233
|
this._hydrationCache = hydrationCache || new SerializableInMemoryCache();
|
|
203
234
|
}
|
|
204
|
-
|
|
205
|
-
_setCachedResponse(id, entry, hydrate) {
|
|
235
|
+
var _proto = SsrCache.prototype;
|
|
236
|
+
_proto._setCachedResponse = function _setCachedResponse(id, entry, hydrate) {
|
|
206
237
|
const frozenEntry = Object.freeze(entry);
|
|
207
|
-
|
|
208
238
|
if (Server.isServerSide()) {
|
|
209
239
|
if (hydrate) {
|
|
210
240
|
this._hydrationCache.set(DefaultScope$2, id, frozenEntry);
|
|
@@ -212,31 +242,50 @@ class SsrCache {
|
|
|
212
242
|
this._ssrOnlyCache.set(DefaultScope$2, id, frozenEntry);
|
|
213
243
|
}
|
|
214
244
|
}
|
|
215
|
-
|
|
216
245
|
return frozenEntry;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
|
|
246
|
+
};
|
|
247
|
+
_createClass(SsrCache, null, [{
|
|
248
|
+
key: "Default",
|
|
249
|
+
get: function () {
|
|
250
|
+
if (!_default$2) {
|
|
251
|
+
_default$2 = new SsrCache();
|
|
252
|
+
}
|
|
253
|
+
return _default$2;
|
|
254
|
+
}
|
|
255
|
+
}]);
|
|
256
|
+
return SsrCache;
|
|
257
|
+
}();
|
|
220
258
|
|
|
221
259
|
const initializeHydrationCache = source => SsrCache.Default.initialize(source);
|
|
222
260
|
const purgeHydrationCache = predicate => SsrCache.Default.purgeData(predicate);
|
|
223
261
|
|
|
224
|
-
|
|
262
|
+
function _extends() {
|
|
263
|
+
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
264
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
265
|
+
var source = arguments[i];
|
|
266
|
+
for (var key in source) {
|
|
267
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
268
|
+
target[key] = source[key];
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return target;
|
|
273
|
+
};
|
|
274
|
+
return _extends.apply(this, arguments);
|
|
275
|
+
}
|
|
225
276
|
|
|
226
|
-
|
|
227
|
-
|
|
277
|
+
let _default$1;
|
|
278
|
+
let RequestFulfillment = function () {
|
|
279
|
+
function RequestFulfillment() {
|
|
228
280
|
this._requests = {};
|
|
229
|
-
|
|
230
281
|
this.fulfill = (id, {
|
|
231
282
|
handler,
|
|
232
283
|
hydrate: _hydrate = true
|
|
233
284
|
}) => {
|
|
234
285
|
const inflight = this._requests[id];
|
|
235
|
-
|
|
236
286
|
if (inflight) {
|
|
237
287
|
return inflight;
|
|
238
288
|
}
|
|
239
|
-
|
|
240
289
|
const request = handler().then(data => ({
|
|
241
290
|
status: "success",
|
|
242
291
|
data
|
|
@@ -246,13 +295,11 @@ class RequestFulfillment {
|
|
|
246
295
|
unexpectedError: error
|
|
247
296
|
}
|
|
248
297
|
}) : error;
|
|
249
|
-
|
|
250
298
|
if (actualError.name === "AbortError") {
|
|
251
299
|
return {
|
|
252
300
|
status: "aborted"
|
|
253
301
|
};
|
|
254
302
|
}
|
|
255
|
-
|
|
256
303
|
return {
|
|
257
304
|
status: "error",
|
|
258
305
|
error: actualError
|
|
@@ -263,42 +310,32 @@ class RequestFulfillment {
|
|
|
263
310
|
this._requests[id] = request;
|
|
264
311
|
return request;
|
|
265
312
|
};
|
|
266
|
-
|
|
267
313
|
this.abort = id => {
|
|
268
314
|
delete this._requests[id];
|
|
269
315
|
};
|
|
270
|
-
|
|
271
316
|
this.abortAll = () => {
|
|
272
317
|
Object.keys(this._requests).forEach(id => this.abort(id));
|
|
273
318
|
};
|
|
274
319
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
_default$1
|
|
320
|
+
_createClass(RequestFulfillment, null, [{
|
|
321
|
+
key: "Default",
|
|
322
|
+
get: function () {
|
|
323
|
+
if (!_default$1) {
|
|
324
|
+
_default$1 = new RequestFulfillment();
|
|
325
|
+
}
|
|
326
|
+
return _default$1;
|
|
279
327
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
}
|
|
328
|
+
}]);
|
|
329
|
+
return RequestFulfillment;
|
|
330
|
+
}();
|
|
285
331
|
|
|
286
332
|
const TrackerContext = React.createContext(null);
|
|
287
|
-
|
|
288
333
|
let _default;
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
static get Default() {
|
|
292
|
-
if (!_default) {
|
|
293
|
-
_default = new RequestTracker();
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
return _default;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
constructor(responseCache = undefined) {
|
|
334
|
+
let RequestTracker = function () {
|
|
335
|
+
function RequestTracker(responseCache = undefined) {
|
|
300
336
|
this._trackedRequests = {};
|
|
301
|
-
|
|
337
|
+
this._responseCache = void 0;
|
|
338
|
+
this._requestFulfillment = void 0;
|
|
302
339
|
this.trackDataRequest = (id, handler, hydrate) => {
|
|
303
340
|
if (this._trackedRequests[id] == null) {
|
|
304
341
|
this._trackedRequests[id] = {
|
|
@@ -307,82 +344,78 @@ class RequestTracker {
|
|
|
307
344
|
};
|
|
308
345
|
}
|
|
309
346
|
};
|
|
310
|
-
|
|
311
347
|
this.reset = () => {
|
|
312
348
|
this._trackedRequests = {};
|
|
313
349
|
};
|
|
314
|
-
|
|
315
350
|
this.fulfillTrackedRequests = () => {
|
|
316
351
|
const promises = [];
|
|
317
352
|
const {
|
|
318
353
|
cacheData,
|
|
319
354
|
cacheError
|
|
320
355
|
} = this._responseCache;
|
|
321
|
-
|
|
322
356
|
for (const requestKey of Object.keys(this._trackedRequests)) {
|
|
323
357
|
const options = this._trackedRequests[requestKey];
|
|
324
|
-
|
|
325
358
|
try {
|
|
326
359
|
promises.push(this._requestFulfillment.fulfill(requestKey, _extends({}, options)).then(result => {
|
|
327
360
|
switch (result.status) {
|
|
328
361
|
case "success":
|
|
329
362
|
cacheData(requestKey, result.data, options.hydrate);
|
|
330
363
|
break;
|
|
331
|
-
|
|
332
364
|
case "error":
|
|
333
365
|
cacheError(requestKey, result.error, options.hydrate);
|
|
334
366
|
break;
|
|
335
367
|
}
|
|
336
|
-
|
|
337
368
|
return;
|
|
338
369
|
}));
|
|
339
370
|
} catch (e) {
|
|
340
371
|
promises.push(Promise.resolve(cacheError(requestKey, e, options.hydrate)));
|
|
341
372
|
}
|
|
342
373
|
}
|
|
343
|
-
|
|
344
374
|
this.reset();
|
|
345
375
|
return Promise.all(promises).then(() => this._responseCache.cloneHydratableData());
|
|
346
376
|
};
|
|
347
|
-
|
|
348
377
|
this._responseCache = responseCache || SsrCache.Default;
|
|
349
378
|
this._requestFulfillment = new RequestFulfillment();
|
|
350
379
|
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
}
|
|
380
|
+
_createClass(RequestTracker, [{
|
|
381
|
+
key: "hasUnfulfilledRequests",
|
|
382
|
+
get: function () {
|
|
383
|
+
return Object.keys(this._trackedRequests).length > 0;
|
|
384
|
+
}
|
|
385
|
+
}], [{
|
|
386
|
+
key: "Default",
|
|
387
|
+
get: function () {
|
|
388
|
+
if (!_default) {
|
|
389
|
+
_default = new RequestTracker();
|
|
390
|
+
}
|
|
391
|
+
return _default;
|
|
392
|
+
}
|
|
393
|
+
}]);
|
|
394
|
+
return RequestTracker;
|
|
395
|
+
}();
|
|
357
396
|
|
|
358
397
|
const SSRCheck = () => {
|
|
359
398
|
if (Server.isServerSide()) {
|
|
360
399
|
return null;
|
|
361
400
|
}
|
|
362
|
-
|
|
363
401
|
if (process.env.NODE_ENV === "production") {
|
|
364
402
|
return new DataError("No CSR tracking", DataErrors.NotAllowed);
|
|
365
403
|
} else {
|
|
366
404
|
return new DataError("Data requests are not tracked for fulfillment when when client-side", DataErrors.NotAllowed);
|
|
367
405
|
}
|
|
368
406
|
};
|
|
369
|
-
|
|
370
407
|
const fetchTrackedRequests = () => {
|
|
371
408
|
const ssrCheck = SSRCheck();
|
|
372
|
-
|
|
373
409
|
if (ssrCheck != null) {
|
|
374
410
|
return Promise.reject(ssrCheck);
|
|
375
411
|
}
|
|
376
|
-
|
|
377
412
|
return RequestTracker.Default.fulfillTrackedRequests();
|
|
378
413
|
};
|
|
379
414
|
const hasTrackedRequestsToBeFetched = () => {
|
|
380
415
|
const ssrCheck = SSRCheck();
|
|
381
|
-
|
|
382
416
|
if (ssrCheck != null) {
|
|
383
417
|
throw ssrCheck;
|
|
384
418
|
}
|
|
385
|
-
|
|
386
419
|
return RequestTracker.Default.hasUnfulfilledRequests;
|
|
387
420
|
};
|
|
388
421
|
const abortInflightRequests = () => {
|
|
@@ -395,23 +428,18 @@ const useSharedCache = (id, scope, initialValue) => {
|
|
|
395
428
|
if (!id || typeof id !== "string") {
|
|
396
429
|
throw new DataError("id must be a non-empty string", DataErrors.InvalidInput);
|
|
397
430
|
}
|
|
398
|
-
|
|
399
431
|
if (!scope || typeof scope !== "string") {
|
|
400
432
|
throw new DataError("scope must be a non-empty string", DataErrors.InvalidInput);
|
|
401
433
|
}
|
|
402
|
-
|
|
403
434
|
const cacheValue = React.useCallback(value => value == null ? cache$1.purge(scope, id) : cache$1.set(scope, id, value), [id, scope]);
|
|
404
435
|
let currentValue = cache$1.get(scope, id);
|
|
405
|
-
|
|
406
436
|
if (currentValue == null && initialValue !== undefined) {
|
|
407
437
|
const value = typeof initialValue === "function" ? initialValue() : initialValue;
|
|
408
|
-
|
|
409
438
|
if (value != null) {
|
|
410
439
|
cacheValue(value);
|
|
411
440
|
currentValue = value;
|
|
412
441
|
}
|
|
413
442
|
}
|
|
414
|
-
|
|
415
443
|
return [currentValue, cacheValue];
|
|
416
444
|
};
|
|
417
445
|
|
|
@@ -420,18 +448,22 @@ const purgeCaches = () => {
|
|
|
420
448
|
purgeHydrationCache();
|
|
421
449
|
};
|
|
422
450
|
|
|
423
|
-
|
|
424
|
-
|
|
451
|
+
let TrackData = function (_React$Component) {
|
|
452
|
+
_inheritsLoose(TrackData, _React$Component);
|
|
453
|
+
function TrackData() {
|
|
454
|
+
return _React$Component.apply(this, arguments) || this;
|
|
455
|
+
}
|
|
456
|
+
var _proto = TrackData.prototype;
|
|
457
|
+
_proto.render = function render() {
|
|
425
458
|
if (!Server.isServerSide()) {
|
|
426
459
|
throw new Error("This component is not for use during client-side rendering");
|
|
427
460
|
}
|
|
428
|
-
|
|
429
461
|
return React.createElement(TrackerContext.Provider, {
|
|
430
462
|
value: RequestTracker.Default.trackDataRequest
|
|
431
463
|
}, this.props.children);
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
}
|
|
464
|
+
};
|
|
465
|
+
return TrackData;
|
|
466
|
+
}(React.Component);
|
|
435
467
|
|
|
436
468
|
const loadingStatus = Object.freeze({
|
|
437
469
|
status: "loading"
|
|
@@ -456,20 +488,16 @@ const resultFromCachedResponse = cacheEntry => {
|
|
|
456
488
|
if (cacheEntry == null) {
|
|
457
489
|
return null;
|
|
458
490
|
}
|
|
459
|
-
|
|
460
491
|
const {
|
|
461
492
|
data,
|
|
462
493
|
error
|
|
463
494
|
} = cacheEntry;
|
|
464
|
-
|
|
465
495
|
if (error != null) {
|
|
466
496
|
return Status.error(new DataError(error, DataErrors.Hydrated));
|
|
467
497
|
}
|
|
468
|
-
|
|
469
498
|
if (data != null) {
|
|
470
499
|
return Status.success(data);
|
|
471
500
|
}
|
|
472
|
-
|
|
473
501
|
return Status.aborted();
|
|
474
502
|
};
|
|
475
503
|
|
|
@@ -482,7 +510,6 @@ const useRequestInterception = (requestId, handler) => {
|
|
|
482
510
|
if (prev != null) {
|
|
483
511
|
return prev;
|
|
484
512
|
}
|
|
485
|
-
|
|
486
513
|
return interceptor(requestId);
|
|
487
514
|
}, null);
|
|
488
515
|
return interceptResponse != null ? interceptResponse : handler();
|
|
@@ -498,18 +525,15 @@ const useServerEffect = (requestId, handler, options = {}) => {
|
|
|
498
525
|
const interceptedHandler = useRequestInterception(requestId, handler);
|
|
499
526
|
const cachedResult = SsrCache.Default.getEntry(requestId);
|
|
500
527
|
const maybeTrack = useContext(TrackerContext);
|
|
501
|
-
|
|
502
528
|
if (!skip && cachedResult == null && Server.isServerSide()) {
|
|
503
529
|
maybeTrack == null ? void 0 : maybeTrack(requestId, interceptedHandler, hydrate);
|
|
504
530
|
}
|
|
505
|
-
|
|
506
531
|
return cachedResult == null ? null : resultFromCachedResponse(cachedResult);
|
|
507
532
|
};
|
|
508
533
|
|
|
509
534
|
const DefaultScope$1 = "useCachedEffect";
|
|
510
535
|
const useCachedEffect = (requestId, handler, options = {}) => {
|
|
511
536
|
var _ref;
|
|
512
|
-
|
|
513
537
|
const {
|
|
514
538
|
fetchPolicy = FetchPolicy.CacheBeforeNetwork,
|
|
515
539
|
skip: hardSkip = false,
|
|
@@ -524,73 +548,57 @@ const useCachedEffect = (requestId, handler, options = {}) => {
|
|
|
524
548
|
const currentRequestRef = React.useRef();
|
|
525
549
|
const fetchRequest = React.useMemo(() => {
|
|
526
550
|
var _currentRequestRef$cu;
|
|
527
|
-
|
|
528
551
|
(_currentRequestRef$cu = currentRequestRef.current) == null ? void 0 : _currentRequestRef$cu.cancel();
|
|
529
552
|
currentRequestRef.current = null;
|
|
530
553
|
networkResultRef.current = null;
|
|
531
|
-
|
|
532
554
|
const fetchFn = () => {
|
|
533
555
|
var _currentRequestRef$cu2, _currentRequestRef$cu3;
|
|
534
|
-
|
|
535
556
|
if (fetchPolicy === FetchPolicy.CacheOnly) {
|
|
536
557
|
throw new DataError("Cannot fetch with CacheOnly policy", DataErrors.NotAllowed);
|
|
537
558
|
}
|
|
538
|
-
|
|
539
559
|
const request = RequestFulfillment.Default.fulfill(`${requestId}|${scope}`, {
|
|
540
560
|
handler: interceptedHandler
|
|
541
561
|
});
|
|
542
|
-
|
|
543
562
|
if (request === ((_currentRequestRef$cu2 = currentRequestRef.current) == null ? void 0 : _currentRequestRef$cu2.request)) {
|
|
544
563
|
return;
|
|
545
564
|
}
|
|
546
|
-
|
|
547
565
|
networkResultRef.current = null;
|
|
548
566
|
(_currentRequestRef$cu3 = currentRequestRef.current) == null ? void 0 : _currentRequestRef$cu3.cancel();
|
|
549
567
|
let cancel = false;
|
|
550
568
|
request.then(result => {
|
|
551
569
|
currentRequestRef.current = null;
|
|
552
|
-
|
|
553
570
|
if (cancel) {
|
|
554
571
|
return;
|
|
555
572
|
}
|
|
556
|
-
|
|
557
573
|
setMostRecentResult(result);
|
|
558
574
|
networkResultRef.current = result;
|
|
559
|
-
|
|
560
575
|
if (onResultChanged != null) {
|
|
561
576
|
onResultChanged(result);
|
|
562
577
|
} else {
|
|
563
578
|
forceUpdate();
|
|
564
579
|
}
|
|
565
|
-
|
|
566
580
|
return;
|
|
567
581
|
});
|
|
568
582
|
currentRequestRef.current = {
|
|
569
583
|
requestId,
|
|
570
584
|
request,
|
|
571
|
-
|
|
572
585
|
cancel() {
|
|
573
586
|
cancel = true;
|
|
574
587
|
RequestFulfillment.Default.abort(requestId);
|
|
575
588
|
}
|
|
576
|
-
|
|
577
589
|
};
|
|
578
590
|
};
|
|
579
|
-
|
|
580
591
|
return fetchFn;
|
|
581
592
|
}, [requestId, onResultChanged, forceUpdate, setMostRecentResult, fetchPolicy]);
|
|
582
593
|
const shouldFetch = React.useMemo(() => {
|
|
583
594
|
if (hardSkip) {
|
|
584
595
|
return false;
|
|
585
596
|
}
|
|
586
|
-
|
|
587
597
|
switch (fetchPolicy) {
|
|
588
598
|
case FetchPolicy.CacheOnly:
|
|
589
599
|
return false;
|
|
590
|
-
|
|
591
600
|
case FetchPolicy.CacheBeforeNetwork:
|
|
592
601
|
return mostRecentResult == null;
|
|
593
|
-
|
|
594
602
|
case FetchPolicy.CacheAndNetwork:
|
|
595
603
|
case FetchPolicy.NetworkOnly:
|
|
596
604
|
return networkResultRef.current == null;
|
|
@@ -600,11 +608,9 @@ const useCachedEffect = (requestId, handler, options = {}) => {
|
|
|
600
608
|
if (!shouldFetch) {
|
|
601
609
|
return;
|
|
602
610
|
}
|
|
603
|
-
|
|
604
611
|
fetchRequest();
|
|
605
612
|
return () => {
|
|
606
613
|
var _currentRequestRef$cu4;
|
|
607
|
-
|
|
608
614
|
(_currentRequestRef$cu4 = currentRequestRef.current) == null ? void 0 : _currentRequestRef$cu4.cancel();
|
|
609
615
|
currentRequestRef.current = null;
|
|
610
616
|
};
|
|
@@ -616,7 +622,12 @@ const useCachedEffect = (requestId, handler, options = {}) => {
|
|
|
616
622
|
return [result, fetchRequest];
|
|
617
623
|
};
|
|
618
624
|
|
|
619
|
-
const WhenClientSide =
|
|
625
|
+
const WhenClientSide = {
|
|
626
|
+
DoNotHydrate: "DoNotHydrate",
|
|
627
|
+
ExecuteWhenNoResult: "ExecuteWhenNoResult",
|
|
628
|
+
ExecuteWhenNoSuccessResult: "ExecuteWhenNoSuccessResult",
|
|
629
|
+
AlwaysExecute: "AlwaysExecute"
|
|
630
|
+
};
|
|
620
631
|
const DefaultScope = "useHydratableEffect";
|
|
621
632
|
const useHydratableEffect = (requestId, handler, options = {}) => {
|
|
622
633
|
const {
|
|
@@ -635,15 +646,12 @@ const useHydratableEffect = (requestId, handler, options = {}) => {
|
|
|
635
646
|
case WhenClientSide.DoNotHydrate:
|
|
636
647
|
case WhenClientSide.AlwaysExecute:
|
|
637
648
|
return null;
|
|
638
|
-
|
|
639
649
|
case WhenClientSide.ExecuteWhenNoResult:
|
|
640
650
|
return serverResult;
|
|
641
|
-
|
|
642
651
|
case WhenClientSide.ExecuteWhenNoSuccessResult:
|
|
643
652
|
if ((serverResult == null ? void 0 : serverResult.status) === "success") {
|
|
644
653
|
return serverResult;
|
|
645
654
|
}
|
|
646
|
-
|
|
647
655
|
return null;
|
|
648
656
|
}
|
|
649
657
|
}, [serverResult]);
|
|
@@ -685,11 +693,9 @@ const InterceptRequests = ({
|
|
|
685
693
|
|
|
686
694
|
const toString = value => {
|
|
687
695
|
var _JSON$stringify;
|
|
688
|
-
|
|
689
696
|
if (typeof value === "string") {
|
|
690
697
|
return value;
|
|
691
698
|
}
|
|
692
|
-
|
|
693
699
|
if (typeof value === "object" && value != null) {
|
|
694
700
|
if (value instanceof Date) {
|
|
695
701
|
return value.toISOString();
|
|
@@ -697,14 +703,11 @@ const toString = value => {
|
|
|
697
703
|
return value.toString();
|
|
698
704
|
}
|
|
699
705
|
}
|
|
700
|
-
|
|
701
706
|
return (_JSON$stringify = JSON.stringify(value)) != null ? _JSON$stringify : "";
|
|
702
707
|
};
|
|
703
|
-
|
|
704
708
|
const toStringifiedVariables = (acc, key, value) => {
|
|
705
709
|
if (typeof value === "object" && value !== null) {
|
|
706
710
|
const subValues = entries(value);
|
|
707
|
-
|
|
708
711
|
if (subValues.length !== 0) {
|
|
709
712
|
return subValues.reduce((innerAcc, [i, v]) => {
|
|
710
713
|
const subKey = `${key}.${i}`;
|
|
@@ -712,18 +715,15 @@ const toStringifiedVariables = (acc, key, value) => {
|
|
|
712
715
|
}, acc);
|
|
713
716
|
}
|
|
714
717
|
}
|
|
715
|
-
|
|
716
718
|
acc[key] = toString(value);
|
|
717
719
|
return acc;
|
|
718
720
|
};
|
|
719
|
-
|
|
720
721
|
const getGqlRequestId = (operation, variables, context) => {
|
|
721
722
|
const parts = [];
|
|
722
723
|
const sortableContext = new URLSearchParams(context);
|
|
723
724
|
sortableContext.sort();
|
|
724
725
|
parts.push(sortableContext.toString());
|
|
725
726
|
parts.push(operation.id);
|
|
726
|
-
|
|
727
727
|
if (variables != null) {
|
|
728
728
|
const stringifiedVariables = Object.keys(variables).reduce((acc, key) => {
|
|
729
729
|
const value = variables[key];
|
|
@@ -733,7 +733,6 @@ const getGqlRequestId = (operation, variables, context) => {
|
|
|
733
733
|
sortableVariables.sort();
|
|
734
734
|
parts.push(sortableVariables.toString());
|
|
735
735
|
}
|
|
736
|
-
|
|
737
736
|
return parts.join("|");
|
|
738
737
|
};
|
|
739
738
|
|
|
@@ -742,24 +741,24 @@ const GqlErrors = Object.freeze({
|
|
|
742
741
|
BadResponse: "BadResponse",
|
|
743
742
|
ErrorResult: "ErrorResult"
|
|
744
743
|
});
|
|
745
|
-
|
|
746
|
-
|
|
744
|
+
let GqlError = function (_KindError) {
|
|
745
|
+
_inheritsLoose(GqlError, _KindError);
|
|
746
|
+
function GqlError(message, kind, {
|
|
747
747
|
metadata,
|
|
748
748
|
cause
|
|
749
749
|
} = {}) {
|
|
750
|
-
|
|
750
|
+
return _KindError.call(this, message, kind, {
|
|
751
751
|
metadata,
|
|
752
752
|
cause,
|
|
753
753
|
name: "Gql"
|
|
754
|
-
});
|
|
754
|
+
}) || this;
|
|
755
755
|
}
|
|
756
|
-
|
|
757
|
-
}
|
|
756
|
+
return GqlError;
|
|
757
|
+
}(KindError);
|
|
758
758
|
|
|
759
759
|
const getGqlDataFromResponse = async response => {
|
|
760
760
|
const bodyText = await response.text();
|
|
761
761
|
let result;
|
|
762
|
-
|
|
763
762
|
try {
|
|
764
763
|
result = JSON.parse(bodyText);
|
|
765
764
|
} catch (e) {
|
|
@@ -771,7 +770,6 @@ const getGqlDataFromResponse = async response => {
|
|
|
771
770
|
cause: e
|
|
772
771
|
});
|
|
773
772
|
}
|
|
774
|
-
|
|
775
773
|
if (response.status >= 300) {
|
|
776
774
|
throw new DataError("Response unsuccessful", DataErrors.Network, {
|
|
777
775
|
metadata: {
|
|
@@ -780,7 +778,6 @@ const getGqlDataFromResponse = async response => {
|
|
|
780
778
|
}
|
|
781
779
|
});
|
|
782
780
|
}
|
|
783
|
-
|
|
784
781
|
if (!Object.prototype.hasOwnProperty.call(result, "data") && !Object.prototype.hasOwnProperty.call(result, "errors")) {
|
|
785
782
|
throw new GqlError("Server response missing", GqlErrors.BadResponse, {
|
|
786
783
|
metadata: {
|
|
@@ -789,7 +786,6 @@ const getGqlDataFromResponse = async response => {
|
|
|
789
786
|
}
|
|
790
787
|
});
|
|
791
788
|
}
|
|
792
|
-
|
|
793
789
|
if (result.errors != null && Array.isArray(result.errors) && result.errors.length > 0) {
|
|
794
790
|
throw new GqlError("GraphQL errors", GqlErrors.ErrorResult, {
|
|
795
791
|
metadata: {
|
|
@@ -798,7 +794,6 @@ const getGqlDataFromResponse = async response => {
|
|
|
798
794
|
}
|
|
799
795
|
});
|
|
800
796
|
}
|
|
801
|
-
|
|
802
797
|
return result.data;
|
|
803
798
|
};
|
|
804
799
|
|
|
@@ -809,13 +804,10 @@ const DocumentTypes = Object.freeze({
|
|
|
809
804
|
const cache = new Map();
|
|
810
805
|
function graphQLDocumentNodeParser(document) {
|
|
811
806
|
var _definition$name;
|
|
812
|
-
|
|
813
807
|
const cached = cache.get(document);
|
|
814
|
-
|
|
815
808
|
if (cached) {
|
|
816
809
|
return cached;
|
|
817
810
|
}
|
|
818
|
-
|
|
819
811
|
if (!(document != null && document.kind)) {
|
|
820
812
|
if (process.env.NODE_ENV === "production") {
|
|
821
813
|
throw new DataError("Bad DocumentNode", DataErrors.InvalidInput);
|
|
@@ -823,12 +815,10 @@ function graphQLDocumentNodeParser(document) {
|
|
|
823
815
|
throw new DataError(`Argument of ${JSON.stringify(document)} passed to parser was not a valid GraphQL ` + `DocumentNode. You may need to use 'graphql-tag' or another method ` + `to convert your operation into a document`, DataErrors.InvalidInput);
|
|
824
816
|
}
|
|
825
817
|
}
|
|
826
|
-
|
|
827
818
|
const fragments = document.definitions.filter(x => x.kind === "FragmentDefinition");
|
|
828
819
|
const queries = document.definitions.filter(x => x.kind === "OperationDefinition" && x.operation === "query");
|
|
829
820
|
const mutations = document.definitions.filter(x => x.kind === "OperationDefinition" && x.operation === "mutation");
|
|
830
821
|
const subscriptions = document.definitions.filter(x => x.kind === "OperationDefinition" && x.operation === "subscription");
|
|
831
|
-
|
|
832
822
|
if (fragments.length && !queries.length && !mutations.length) {
|
|
833
823
|
if (process.env.NODE_ENV === "production") {
|
|
834
824
|
throw new DataError("Fragment only", DataErrors.InvalidInput);
|
|
@@ -836,7 +826,6 @@ function graphQLDocumentNodeParser(document) {
|
|
|
836
826
|
throw new DataError(`Passing only a fragment to 'graphql' is not supported. ` + `You must include a query or mutation as well`, DataErrors.InvalidInput);
|
|
837
827
|
}
|
|
838
828
|
}
|
|
839
|
-
|
|
840
829
|
if (subscriptions.length) {
|
|
841
830
|
if (process.env.NODE_ENV === "production") {
|
|
842
831
|
throw new DataError("No subscriptions", DataErrors.InvalidInput);
|
|
@@ -844,7 +833,6 @@ function graphQLDocumentNodeParser(document) {
|
|
|
844
833
|
throw new DataError(`We do not support subscriptions. ` + `${JSON.stringify(document)} had ${subscriptions.length} subscriptions`, DataErrors.InvalidInput);
|
|
845
834
|
}
|
|
846
835
|
}
|
|
847
|
-
|
|
848
836
|
if (queries.length + mutations.length > 1) {
|
|
849
837
|
if (process.env.NODE_ENV === "production") {
|
|
850
838
|
throw new DataError("Too many ops", DataErrors.InvalidInput);
|
|
@@ -852,7 +840,6 @@ function graphQLDocumentNodeParser(document) {
|
|
|
852
840
|
throw new DataError(`We only support one query or mutation per component. ` + `${JSON.stringify(document)} had ${queries.length} queries and ` + `${mutations.length} mutations. `, DataErrors.InvalidInput);
|
|
853
841
|
}
|
|
854
842
|
}
|
|
855
|
-
|
|
856
843
|
const type = queries.length ? DocumentTypes.query : DocumentTypes.mutation;
|
|
857
844
|
const definitions = queries.length ? queries : mutations;
|
|
858
845
|
const definition = definitions[0];
|
|
@@ -901,18 +888,15 @@ const mergeGqlContext = (defaultContext, overrides) => {
|
|
|
901
888
|
acc[key] = overrides[key];
|
|
902
889
|
}
|
|
903
890
|
}
|
|
904
|
-
|
|
905
891
|
return acc;
|
|
906
892
|
}, _extends({}, defaultContext));
|
|
907
893
|
};
|
|
908
894
|
|
|
909
895
|
const useGqlRouterContext = (contextOverrides = {}) => {
|
|
910
896
|
const gqlRouterContext = useContext(GqlRouterContext);
|
|
911
|
-
|
|
912
897
|
if (gqlRouterContext == null) {
|
|
913
898
|
throw new GqlError("No GqlRouter", GqlErrors.Internal);
|
|
914
899
|
}
|
|
915
|
-
|
|
916
900
|
const {
|
|
917
901
|
fetch,
|
|
918
902
|
defaultContext
|
|
@@ -922,11 +906,9 @@ const useGqlRouterContext = (contextOverrides = {}) => {
|
|
|
922
906
|
const refKeys = Object.keys(contextRef.current);
|
|
923
907
|
const mergedKeys = Object.keys(mergedContext);
|
|
924
908
|
const shouldWeUpdateRef = refKeys.length !== mergedKeys.length || mergedKeys.every(key => contextRef.current[key] !== mergedContext[key]);
|
|
925
|
-
|
|
926
909
|
if (shouldWeUpdateRef) {
|
|
927
910
|
contextRef.current = mergedContext;
|
|
928
911
|
}
|
|
929
|
-
|
|
930
912
|
const finalContext = contextRef.current;
|
|
931
913
|
const finalRouterContext = useMemo(() => ({
|
|
932
914
|
fetch,
|