@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
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
import {render} from "@testing-library/react";
|
|
4
3
|
|
|
5
|
-
import InterceptContext from "../intercept-context
|
|
6
|
-
import InterceptRequests from "../intercept-requests
|
|
4
|
+
import InterceptContext from "../intercept-context";
|
|
5
|
+
import InterceptRequests from "../intercept-requests";
|
|
7
6
|
|
|
8
7
|
describe("InterceptRequests", () => {
|
|
9
8
|
afterEach(() => {
|
|
@@ -12,11 +11,11 @@ describe("InterceptRequests", () => {
|
|
|
12
11
|
|
|
13
12
|
it("should update context with fulfillRequest method", () => {
|
|
14
13
|
// Arrange
|
|
15
|
-
const fakeHandler = (requestId): Promise<string> =>
|
|
14
|
+
const fakeHandler = (requestId: any): Promise<string> =>
|
|
16
15
|
Promise.resolve("data");
|
|
17
16
|
const props = {
|
|
18
17
|
interceptor: fakeHandler,
|
|
19
|
-
};
|
|
18
|
+
} as const;
|
|
20
19
|
const captureContextFn = jest.fn();
|
|
21
20
|
|
|
22
21
|
// Act
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
import {Server} from "@khanacademy/wonder-blocks-core";
|
|
4
3
|
import {render, screen} from "@testing-library/react";
|
|
5
4
|
|
|
6
|
-
import TrackData from "../track-data
|
|
7
|
-
import {RequestTracker, TrackerContext} from "../../util/request-tracking
|
|
5
|
+
import TrackData from "../track-data";
|
|
6
|
+
import {RequestTracker, TrackerContext} from "../../util/request-tracking";
|
|
8
7
|
|
|
9
8
|
describe("TrackData", () => {
|
|
10
9
|
afterEach(() => {
|
|
@@ -41,11 +40,11 @@ describe("TrackData", () => {
|
|
|
41
40
|
jest.spyOn(Server, "isServerSide").mockReturnValue(true);
|
|
42
41
|
|
|
43
42
|
// Act
|
|
44
|
-
const result = await new Promise((resolve, reject) => {
|
|
43
|
+
const result = await new Promise((resolve: any, reject: any) => {
|
|
45
44
|
render(
|
|
46
45
|
<TrackData>
|
|
47
46
|
<TrackerContext.Consumer>
|
|
48
|
-
{(fn) => resolve(fn)}
|
|
47
|
+
{(fn: any) => resolve(fn)}
|
|
49
48
|
</TrackerContext.Consumer>
|
|
50
49
|
</TrackData>,
|
|
51
50
|
);
|
|
@@ -1,30 +1,24 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
|
|
4
3
|
import {
|
|
5
4
|
useHydratableEffect,
|
|
6
|
-
// TODO(somewhatabstract, FEI-4174): Update eslint-plugin-import when they
|
|
7
|
-
// have fixed:
|
|
8
|
-
// https://github.com/import-js/eslint-plugin-import/issues/2073
|
|
9
|
-
// eslint-disable-next-line import/named
|
|
10
5
|
WhenClientSide,
|
|
11
|
-
} from "../hooks/use-hydratable-effect
|
|
6
|
+
} from "../hooks/use-hydratable-effect";
|
|
12
7
|
|
|
13
|
-
import type {Result, ValidCacheData} from "../util/types
|
|
8
|
+
import type {Result, ValidCacheData} from "../util/types";
|
|
14
9
|
|
|
15
10
|
type Props<
|
|
16
11
|
/**
|
|
17
12
|
* The type of data resolved by the handler's fulfillRequest method.
|
|
18
13
|
*/
|
|
19
|
-
TData
|
|
20
|
-
> = {
|
|
14
|
+
TData extends ValidCacheData,
|
|
15
|
+
> = {
|
|
21
16
|
/**
|
|
22
17
|
* A unique identifier for the request.
|
|
23
18
|
*
|
|
24
19
|
* This should not be shared by other uses of this component.
|
|
25
20
|
*/
|
|
26
|
-
requestId: string
|
|
27
|
-
|
|
21
|
+
requestId: string;
|
|
28
22
|
/**
|
|
29
23
|
* This defines how the request is fulfilled.
|
|
30
24
|
*
|
|
@@ -32,8 +26,7 @@ type Props<
|
|
|
32
26
|
* old handler result may be given. This is not a supported mode of
|
|
33
27
|
* operation.
|
|
34
28
|
*/
|
|
35
|
-
handler: () => Promise<TData
|
|
36
|
-
|
|
29
|
+
handler: () => Promise<TData>;
|
|
37
30
|
/**
|
|
38
31
|
* How the hook should behave when rendering client-side for the first time.
|
|
39
32
|
*
|
|
@@ -41,8 +34,7 @@ type Props<
|
|
|
41
34
|
*
|
|
42
35
|
* Default is `OnClientRender.ExecuteWhenNoSuccessResult`.
|
|
43
36
|
*/
|
|
44
|
-
clientBehavior?: WhenClientSide
|
|
45
|
-
|
|
37
|
+
clientBehavior?: typeof WhenClientSide[keyof typeof WhenClientSide];
|
|
46
38
|
/**
|
|
47
39
|
* When true, the children will be rendered with the existing result
|
|
48
40
|
* until the pending load is completed. Otherwise, the children will be
|
|
@@ -50,28 +42,28 @@ type Props<
|
|
|
50
42
|
*
|
|
51
43
|
* Defaults to false.
|
|
52
44
|
*/
|
|
53
|
-
retainResultOnChange?: boolean
|
|
54
|
-
|
|
45
|
+
retainResultOnChange?: boolean;
|
|
55
46
|
/**
|
|
56
47
|
* A function that will render the content of this component using the
|
|
57
48
|
* loading state and data or error that gets retrieved from cache or loaded
|
|
58
49
|
* via the request if no cached value is available.
|
|
59
50
|
*/
|
|
60
|
-
children: (result: Result<TData>) => React.
|
|
61
|
-
|
|
51
|
+
children: (result: Result<TData>) => React.ReactElement;
|
|
52
|
+
};
|
|
62
53
|
|
|
63
54
|
/**
|
|
64
55
|
* This component is the main component of Wonder Blocks Data. With this, data
|
|
65
56
|
* requirements can be placed in a React application in a manner that will
|
|
66
57
|
* support server-side rendering and efficient caching.
|
|
67
58
|
*/
|
|
68
|
-
|
|
59
|
+
// TODO(FEI-5000): Update this support generic props correctly
|
|
60
|
+
const Data: React.FC<Props<any>> = <TData extends ValidCacheData>({
|
|
69
61
|
requestId,
|
|
70
62
|
handler,
|
|
71
63
|
children,
|
|
72
64
|
retainResultOnChange = false,
|
|
73
65
|
clientBehavior = WhenClientSide.ExecuteWhenNoSuccessResult,
|
|
74
|
-
}: Props<TData>): React.
|
|
66
|
+
}: Props<TData>): React.ReactElement => {
|
|
75
67
|
const result = useHydratableEffect(requestId, handler, {
|
|
76
68
|
retainResultOnChange,
|
|
77
69
|
clientBehavior,
|
|
@@ -1,30 +1,27 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
|
|
4
|
-
import {GqlRouterContext} from "../util/gql-router-context
|
|
3
|
+
import {GqlRouterContext} from "../util/gql-router-context";
|
|
5
4
|
|
|
6
5
|
import type {
|
|
7
6
|
GqlContext,
|
|
8
7
|
GqlFetchFn,
|
|
9
8
|
GqlRouterConfiguration,
|
|
10
|
-
} from "../util/gql-types
|
|
9
|
+
} from "../util/gql-types";
|
|
11
10
|
|
|
12
|
-
type Props<TContext
|
|
11
|
+
type Props<TContext extends GqlContext> = {
|
|
13
12
|
/**
|
|
14
13
|
* The default context to be used by operations when no context is provided.
|
|
15
14
|
*/
|
|
16
|
-
defaultContext: TContext
|
|
17
|
-
|
|
15
|
+
defaultContext: TContext;
|
|
18
16
|
/**
|
|
19
17
|
* The function to use when fetching requests.
|
|
20
18
|
*/
|
|
21
|
-
fetch: GqlFetchFn<any, any, TContext
|
|
22
|
-
|
|
19
|
+
fetch: GqlFetchFn<any, any, TContext>;
|
|
23
20
|
/**
|
|
24
21
|
* The children to be rendered inside the router.
|
|
25
22
|
*/
|
|
26
|
-
children: React.
|
|
27
|
-
|
|
23
|
+
children: React.ReactNode;
|
|
24
|
+
};
|
|
28
25
|
|
|
29
26
|
/**
|
|
30
27
|
* Configure GraphQL routing for GraphQL hooks and components.
|
|
@@ -32,17 +29,18 @@ type Props<TContext: GqlContext> = {|
|
|
|
32
29
|
* These can be nested. Components and hooks relying on the GraphQL routing
|
|
33
30
|
* will use the configuration from their closest ancestral GqlRouter.
|
|
34
31
|
*/
|
|
35
|
-
|
|
32
|
+
// TODO(FEI-5000): Update this support generic props correctly
|
|
33
|
+
export const GqlRouter: React.FC<Props<any>> = <TContext extends GqlContext>({
|
|
36
34
|
defaultContext: thisDefaultContext,
|
|
37
35
|
fetch: thisFetch,
|
|
38
36
|
children,
|
|
39
|
-
}: Props<TContext>): React.
|
|
37
|
+
}: Props<TContext>): React.ReactElement => {
|
|
40
38
|
// We don't care if we're nested. We always force our callers to define
|
|
41
39
|
// everything. It makes for a clearer API and requires less error checking
|
|
42
|
-
// code (assuming our
|
|
43
|
-
// to anything - our callers can tell us what function to use quite
|
|
44
|
-
// If code that consumes this wants more nuanced nesting, it can
|
|
45
|
-
// it within its own GqlRouter than then defers to this one.
|
|
40
|
+
// code (assuming our TypeScript types are correct). We also don't default
|
|
41
|
+
// fetch to anything - our callers can tell us what function to use quite
|
|
42
|
+
// easily. If code that consumes this wants more nuanced nesting, it can
|
|
43
|
+
// implement it within its own GqlRouter than then defers to this one.
|
|
46
44
|
|
|
47
45
|
// We want to always use the same object if things haven't changed to avoid
|
|
48
46
|
// over-rendering consumers of our context, let's memoize the configuration.
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
|
-
import type {ValidCacheData} from "../util/types
|
|
2
|
+
import type {ValidCacheData} from "../util/types";
|
|
4
3
|
|
|
5
|
-
type InterceptContextData =
|
|
6
|
-
(
|
|
4
|
+
type InterceptContextData = ReadonlyArray<
|
|
5
|
+
(
|
|
6
|
+
requestId: string,
|
|
7
|
+
) => Promise<ValidCacheData | null | undefined> | null | undefined
|
|
7
8
|
>;
|
|
8
9
|
|
|
9
10
|
/**
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
|
|
4
|
-
import InterceptContext from "./intercept-context
|
|
3
|
+
import InterceptContext from "./intercept-context";
|
|
5
4
|
|
|
6
|
-
import type {ValidCacheData} from "../util/types
|
|
5
|
+
import type {ValidCacheData} from "../util/types";
|
|
7
6
|
|
|
8
|
-
type Props<TData
|
|
7
|
+
type Props<TData extends ValidCacheData> = {
|
|
9
8
|
/**
|
|
10
9
|
* Called to intercept and possibly handle the request.
|
|
11
10
|
* If this returns null, the request will be handled by ancestor
|
|
@@ -20,16 +19,15 @@ type Props<TData: ValidCacheData> = {|
|
|
|
20
19
|
* so make sure to only intercept requests that you recognize from the
|
|
21
20
|
* identifier.
|
|
22
21
|
*/
|
|
23
|
-
interceptor: (requestId: string) =>
|
|
24
|
-
|
|
22
|
+
interceptor: (requestId: string) => Promise<TData> | null | undefined;
|
|
25
23
|
/**
|
|
26
24
|
* The children to render within this component. Any requests by `Data`
|
|
27
25
|
* components that use same ID as this component will be intercepted.
|
|
28
26
|
* If `InterceptRequests` is used within `children`, that interception will
|
|
29
27
|
* be given a chance to intercept first.
|
|
30
28
|
*/
|
|
31
|
-
children: React.
|
|
32
|
-
|
|
29
|
+
children: React.ReactNode;
|
|
30
|
+
};
|
|
33
31
|
|
|
34
32
|
/**
|
|
35
33
|
* This component provides a mechanism to intercept data requests.
|
|
@@ -45,10 +43,11 @@ type Props<TData: ValidCacheData> = {|
|
|
|
45
43
|
* interceptor closest to the intercepted request is called first, and the
|
|
46
44
|
* furthest interceptor is called last.
|
|
47
45
|
*/
|
|
48
|
-
|
|
46
|
+
// TODO(FEI-5000): Update this support generic props correctly
|
|
47
|
+
const InterceptRequests: React.FC<Props<any>> = <TData extends ValidCacheData>({
|
|
49
48
|
interceptor,
|
|
50
49
|
children,
|
|
51
|
-
}: Props<TData>): React.
|
|
50
|
+
}: Props<TData>): React.ReactElement => {
|
|
52
51
|
const interceptors = React.useContext(InterceptContext);
|
|
53
52
|
|
|
54
53
|
const updatedInterceptors = React.useMemo(
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
// @flow
|
|
2
1
|
import * as React from "react";
|
|
3
2
|
import {Server} from "@khanacademy/wonder-blocks-core";
|
|
4
3
|
|
|
5
|
-
import {RequestTracker, TrackerContext} from "../util/request-tracking
|
|
4
|
+
import {RequestTracker, TrackerContext} from "../util/request-tracking";
|
|
6
5
|
|
|
7
|
-
type TrackDataProps = {
|
|
8
|
-
children: React.
|
|
9
|
-
|
|
6
|
+
type TrackDataProps = {
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
};
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
11
|
* Component to enable data request tracking when server-side rendering.
|
|
13
12
|
*/
|
|
14
13
|
export default class TrackData extends React.Component<TrackDataProps> {
|
|
15
|
-
render(): React.
|
|
14
|
+
render(): React.ReactElement {
|
|
16
15
|
if (!Server.isServerSide()) {
|
|
17
16
|
throw new Error(
|
|
18
17
|
"This component is not for use during client-side rendering",
|