@prismicio/react 2.5.1 → 2.6.0-alpha.0
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/dist/PrismicImage.cjs +7 -11
- package/dist/PrismicImage.cjs.map +1 -1
- package/dist/PrismicImage.d.ts +162 -163
- package/dist/PrismicImage.js +6 -10
- package/dist/PrismicImage.js.map +1 -1
- package/dist/PrismicLink.cjs +4 -66
- package/dist/PrismicLink.cjs.map +1 -1
- package/dist/PrismicLink.d.ts +6 -105
- package/dist/PrismicLink.js +4 -65
- package/dist/PrismicLink.js.map +1 -1
- package/dist/PrismicProvider.cjs.map +1 -1
- package/dist/PrismicProvider.d.ts +83 -84
- package/dist/PrismicProvider.js.map +1 -1
- package/dist/PrismicRichText.cjs +4 -71
- package/dist/PrismicRichText.cjs.map +1 -1
- package/dist/PrismicRichText.d.ts +4 -116
- package/dist/PrismicRichText.js +5 -70
- package/dist/PrismicRichText.js.map +1 -1
- package/dist/PrismicText.cjs +4 -4
- package/dist/PrismicText.cjs.map +1 -1
- package/dist/PrismicText.d.ts +39 -39
- package/dist/PrismicText.js +3 -3
- package/dist/PrismicText.js.map +1 -1
- package/dist/PrismicToolbar.cjs +6 -4
- package/dist/PrismicToolbar.cjs.map +1 -1
- package/dist/PrismicToolbar.d.ts +23 -21
- package/dist/PrismicToolbar.js +6 -4
- package/dist/PrismicToolbar.js.map +1 -1
- package/dist/SliceZone.cjs +4 -17
- package/dist/SliceZone.cjs.map +1 -1
- package/dist/SliceZone.d.ts +127 -188
- package/dist/SliceZone.js +4 -17
- package/dist/SliceZone.js.map +1 -1
- package/dist/clientHooks.d.ts +357 -357
- package/dist/index.cjs +0 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +21 -46
- package/dist/index.js +1 -4
- package/dist/index.js.map +1 -1
- package/dist/lib/__PRODUCTION__.d.ts +7 -7
- package/dist/lib/devMsg.d.ts +16 -16
- package/dist/lib/invariant.d.ts +24 -24
- package/dist/lib/isInternalURL.d.ts +8 -8
- package/dist/lib/pascalCase.d.ts +26 -26
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/react-server/PrismicLink.cjs +59 -0
- package/dist/react-server/PrismicLink.cjs.map +1 -0
- package/dist/react-server/PrismicLink.d.ts +72 -0
- package/dist/react-server/PrismicLink.js +42 -0
- package/dist/react-server/PrismicLink.js.map +1 -0
- package/dist/react-server/PrismicRichText.cjs +92 -0
- package/dist/react-server/PrismicRichText.cjs.map +1 -0
- package/dist/react-server/PrismicRichText.d.ts +104 -0
- package/dist/react-server/PrismicRichText.js +73 -0
- package/dist/react-server/PrismicRichText.js.map +1 -0
- package/dist/react-server/index.d.ts +4 -0
- package/dist/react-server/unsupported.cjs +97 -0
- package/dist/react-server/unsupported.cjs.map +1 -0
- package/dist/react-server/unsupported.d.ts +22 -0
- package/dist/react-server/unsupported.js +97 -0
- package/dist/react-server/unsupported.js.map +1 -0
- package/dist/react-server.cjs +44 -0
- package/dist/react-server.cjs.map +1 -0
- package/dist/react-server.js +41 -0
- package/dist/react-server.js.map +1 -0
- package/dist/types.d.ts +19 -19
- package/dist/usePrismicClient.d.ts +11 -11
- package/dist/usePrismicContext.d.ts +8 -8
- package/dist/usePrismicPreviewResolver.d.ts +45 -45
- package/dist/useStatefulPrismicClientMethod.cjs +6 -0
- package/dist/useStatefulPrismicClientMethod.cjs.map +1 -1
- package/dist/useStatefulPrismicClientMethod.d.ts +44 -44
- package/dist/useStatefulPrismicClientMethod.js +6 -0
- package/dist/useStatefulPrismicClientMethod.js.map +1 -1
- package/package.json +42 -47
- package/src/PrismicImage.tsx +30 -37
- package/src/PrismicLink.tsx +29 -259
- package/src/PrismicProvider.tsx +9 -8
- package/src/PrismicRichText.tsx +28 -275
- package/src/PrismicText.tsx +4 -5
- package/src/PrismicToolbar.tsx +7 -6
- package/src/SliceZone.tsx +8 -129
- package/src/index.ts +1 -9
- package/src/react-server/PrismicLink.tsx +179 -0
- package/src/react-server/PrismicRichText.tsx +267 -0
- package/src/react-server/index.ts +38 -0
- package/src/react-server/unsupported.ts +98 -0
- package/dist/lib/pascalCase.cjs +0 -10
- package/dist/lib/pascalCase.cjs.map +0 -1
- package/dist/lib/pascalCase.js +0 -10
- package/dist/lib/pascalCase.js.map +0 -1
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type * as prismic from "@prismicio/client";
|
|
2
|
-
/**
|
|
3
|
-
* Retrieve the `@prismicio/client` instance provided to `<PrismicProvider>`
|
|
4
|
-
* higher in the React tree.
|
|
5
|
-
*
|
|
6
|
-
* @param explicitClient - An optional `@prismicio/client` instance to override
|
|
7
|
-
* the Client provided to `<PrismicProvider>`.
|
|
8
|
-
*
|
|
9
|
-
* @returns The `@prismicio/client` instance provided to `<PrismicProvider>`.
|
|
10
|
-
*/
|
|
11
|
-
export declare const usePrismicClient: (explicitClient?: prismic.Client) => prismic.Client;
|
|
1
|
+
import type * as prismic from "@prismicio/client";
|
|
2
|
+
/**
|
|
3
|
+
* Retrieve the `@prismicio/client` instance provided to `<PrismicProvider>`
|
|
4
|
+
* higher in the React tree.
|
|
5
|
+
*
|
|
6
|
+
* @param explicitClient - An optional `@prismicio/client` instance to override
|
|
7
|
+
* the Client provided to `<PrismicProvider>`.
|
|
8
|
+
*
|
|
9
|
+
* @returns The `@prismicio/client` instance provided to `<PrismicProvider>`.
|
|
10
|
+
*/
|
|
11
|
+
export declare const usePrismicClient: (explicitClient?: prismic.Client) => prismic.Client;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { PrismicContextValue } from "./PrismicProvider";
|
|
2
|
-
/**
|
|
3
|
-
* React hook used to read shared configuration for `@prismicio/react`
|
|
4
|
-
* components and hooks.
|
|
5
|
-
*
|
|
6
|
-
* @returns The closest `<PrismicProvider>` context value.
|
|
7
|
-
*/
|
|
8
|
-
export declare const usePrismicContext: () => PrismicContextValue;
|
|
1
|
+
import { PrismicContextValue } from "./PrismicProvider";
|
|
2
|
+
/**
|
|
3
|
+
* React hook used to read shared configuration for `@prismicio/react`
|
|
4
|
+
* components and hooks.
|
|
5
|
+
*
|
|
6
|
+
* @returns The closest `<PrismicProvider>` context value.
|
|
7
|
+
*/
|
|
8
|
+
export declare const usePrismicContext: () => PrismicContextValue;
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import type * as prismic from "@prismicio/client";
|
|
2
|
-
import { ClientHookReturnType } from "./useStatefulPrismicClientMethod";
|
|
3
|
-
export type UsePrismicPreviewResolverArgs = {
|
|
4
|
-
/**
|
|
5
|
-
* An optional `@prismicio/client` instance to override the Client provided to
|
|
6
|
-
* `<PrismicProvider>`
|
|
7
|
-
*/
|
|
8
|
-
client?: prismic.Client;
|
|
9
|
-
/**
|
|
10
|
-
* A function that maps a Prismic document to a URL within your app.
|
|
11
|
-
*/
|
|
12
|
-
linkResolver?: Parameters<prismic.Client["resolvePreviewURL"]>[0]["linkResolver"];
|
|
13
|
-
/**
|
|
14
|
-
* A fallback URL if the Link Resolver does not return a value.
|
|
15
|
-
*/
|
|
16
|
-
defaultURL?: Parameters<prismic.Client["resolvePreviewURL"]>[0]["defaultURL"];
|
|
17
|
-
/**
|
|
18
|
-
* The preview token (also known as a ref) that will be used to query preview
|
|
19
|
-
* content from the Prismic repository.
|
|
20
|
-
*/
|
|
21
|
-
previewToken?: Parameters<prismic.Client["resolvePreviewURL"]>[0]["previewToken"];
|
|
22
|
-
/**
|
|
23
|
-
* The previewed document that will be used to determine the destination URL.
|
|
24
|
-
*/
|
|
25
|
-
documentID?: Parameters<prismic.Client["resolvePreviewURL"]>[0]["documentID"];
|
|
26
|
-
/**
|
|
27
|
-
* A function to automatically navigate to the resolved URL. If a function is
|
|
28
|
-
* not provded, `usePreviewResolver` will not navigate to the URL.
|
|
29
|
-
*
|
|
30
|
-
* @param url - The resolved preview URL.
|
|
31
|
-
*/
|
|
32
|
-
navigate?: (url: string) => unknown;
|
|
33
|
-
};
|
|
34
|
-
/**
|
|
35
|
-
* Resolve a preview session's URL. The resolved URL can be used to redirect to
|
|
36
|
-
* the previewed document.
|
|
37
|
-
*
|
|
38
|
-
* If a `navigate` function is provided, the hook will automatically navigate to
|
|
39
|
-
* the previewed document's URL.
|
|
40
|
-
*
|
|
41
|
-
* @param args - Arguments to configure how a URL is resolved.
|
|
42
|
-
*
|
|
43
|
-
* @returns A tuple containing the resolved URL and the hook's state.
|
|
44
|
-
*/
|
|
45
|
-
export declare const usePrismicPreviewResolver: (args?: UsePrismicPreviewResolverArgs) => ClientHookReturnType<string>;
|
|
1
|
+
import type * as prismic from "@prismicio/client";
|
|
2
|
+
import { ClientHookReturnType } from "./useStatefulPrismicClientMethod";
|
|
3
|
+
export type UsePrismicPreviewResolverArgs = {
|
|
4
|
+
/**
|
|
5
|
+
* An optional `@prismicio/client` instance to override the Client provided to
|
|
6
|
+
* `<PrismicProvider>`
|
|
7
|
+
*/
|
|
8
|
+
client?: prismic.Client;
|
|
9
|
+
/**
|
|
10
|
+
* A function that maps a Prismic document to a URL within your app.
|
|
11
|
+
*/
|
|
12
|
+
linkResolver?: Parameters<prismic.Client["resolvePreviewURL"]>[0]["linkResolver"];
|
|
13
|
+
/**
|
|
14
|
+
* A fallback URL if the Link Resolver does not return a value.
|
|
15
|
+
*/
|
|
16
|
+
defaultURL?: Parameters<prismic.Client["resolvePreviewURL"]>[0]["defaultURL"];
|
|
17
|
+
/**
|
|
18
|
+
* The preview token (also known as a ref) that will be used to query preview
|
|
19
|
+
* content from the Prismic repository.
|
|
20
|
+
*/
|
|
21
|
+
previewToken?: Parameters<prismic.Client["resolvePreviewURL"]>[0]["previewToken"];
|
|
22
|
+
/**
|
|
23
|
+
* The previewed document that will be used to determine the destination URL.
|
|
24
|
+
*/
|
|
25
|
+
documentID?: Parameters<prismic.Client["resolvePreviewURL"]>[0]["documentID"];
|
|
26
|
+
/**
|
|
27
|
+
* A function to automatically navigate to the resolved URL. If a function is
|
|
28
|
+
* not provded, `usePreviewResolver` will not navigate to the URL.
|
|
29
|
+
*
|
|
30
|
+
* @param url - The resolved preview URL.
|
|
31
|
+
*/
|
|
32
|
+
navigate?: (url: string) => unknown;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Resolve a preview session's URL. The resolved URL can be used to redirect to
|
|
36
|
+
* the previewed document.
|
|
37
|
+
*
|
|
38
|
+
* If a `navigate` function is provided, the hook will automatically navigate to
|
|
39
|
+
* the previewed document's URL.
|
|
40
|
+
*
|
|
41
|
+
* @param args - Arguments to configure how a URL is resolved.
|
|
42
|
+
*
|
|
43
|
+
* @returns A tuple containing the resolved URL and the hook's state.
|
|
44
|
+
*/
|
|
45
|
+
export declare const usePrismicPreviewResolver: (args?: UsePrismicPreviewResolverArgs) => ClientHookReturnType<string>;
|
|
@@ -69,11 +69,17 @@ const useStatefulPrismicClientMethod = (methodName, args, explicitClient) => {
|
|
|
69
69
|
});
|
|
70
70
|
}
|
|
71
71
|
},
|
|
72
|
+
// We must disable exhaustive-deps since we are using
|
|
73
|
+
// JSON.stringify on params (effectively a deep equality check).
|
|
74
|
+
// We want this effect to run again anytime params change.
|
|
75
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
72
76
|
[
|
|
73
77
|
client,
|
|
74
78
|
methodName,
|
|
75
79
|
skip,
|
|
80
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
76
81
|
JSON.stringify(argsWithoutParams),
|
|
82
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
77
83
|
JSON.stringify(params)
|
|
78
84
|
]
|
|
79
85
|
);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStatefulPrismicClientMethod.cjs","sources":["../../src/useStatefulPrismicClientMethod.ts"],"sourcesContent":["import type * as prismic from \"@prismicio/client\";\n\nimport * as React from \"react\";\n\nimport { PrismicClientHookState } from \"./types\";\nimport { usePrismicClient } from \"./usePrismicClient\";\n\ntype StateMachineState<TData> = {\n\tstate: PrismicClientHookState;\n\tdata?: TData;\n\terror?: Error;\n};\n\ntype StateMachineAction<TData> =\n\t| [type: \"start\"]\n\t| [type: \"succeed\", payload: TData]\n\t| [type: \"fail\", payload: Error];\n\nconst reducer = <TData>(\n\tstate: StateMachineState<TData>,\n\taction: StateMachineAction<TData>,\n): StateMachineState<TData> => {\n\tswitch (action[0]) {\n\t\tcase \"start\": {\n\t\t\treturn { state: \"loading\" };\n\t\t}\n\n\t\tcase \"succeed\": {\n\t\t\treturn { state: \"loaded\", data: action[1] };\n\t\t}\n\n\t\tcase \"fail\": {\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\tstate: \"failed\",\n\t\t\t\terror: action[1],\n\t\t\t};\n\t\t}\n\t}\n};\n\nconst initialState: StateMachineState<never> = {\n\tstate: \"idle\",\n};\n\ntype UnwrapPromise<T> = T extends Promise<infer U> ? U : T;\n\ntype ClientPrototype = typeof prismic.Client.prototype;\n\ntype ClientMethod<MethodName extends keyof ClientPrototype> =\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tClientPrototype[MethodName] extends (...args: any[]) => any\n\t\t? ClientPrototype[MethodName]\n\t\t: never;\n\ntype ClientMethodName = keyof {\n\t[P in keyof prismic.Client as prismic.Client[P] extends (\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t...args: any[]\n\t) => // eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tPromise<any>\n\t\t? P\n\t\t: never]: unknown;\n};\n\nexport type ClientMethodParameters<MethodName extends keyof ClientPrototype> =\n\tParameters<ClientMethod<MethodName>>;\n\nexport type HookOnlyParameters = {\n\tclient?: prismic.Client;\n\tskip?: boolean;\n};\n\n/**\n * Determines if a value is a `@prismicio/client` params object.\n *\n * @param value - The value to check.\n *\n * @returns `true` if `value` is a `@prismicio/client` params object, `false`\n * otherwise.\n */\nconst isParams = (\n\tvalue: unknown,\n): value is ClientMethodParameters<\"get\">[0] & HookOnlyParameters => {\n\t// This is a *very* naive check.\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n};\n\n/**\n * The return value of a `@prismicio/client` React hook.\n *\n * @typeParam TData - Data returned by the client.\n */\nexport type ClientHookReturnType<TData = unknown> = [\n\t/**\n\t * Data returned by the client.\n\t */\n\tdata: TData | undefined,\n\n\t/**\n\t * The current state of the hook's client method call.\n\t */\n\tstate: Pick<StateMachineState<TData>, \"state\" | \"error\">,\n];\n\n/**\n * Creates a React hook that forwards arguments to a specific method of a\n * `@prismicio/client` instance. The created hook has its own internal state\n * manager to report async status, such as pending or error statuses.\n *\n * @param methodName - The `@prismicio/client` method to which hook arguments\n * will be forwarded.\n *\n * @returns A new React hook configured for the provided method.\n *\n * @internal\n */\nexport const useStatefulPrismicClientMethod = <\n\tTMethodName extends ClientMethodName,\n\tTArgs extends Parameters<ClientMethod<TMethodName>>,\n\tTData extends UnwrapPromise<ReturnType<ClientMethod<TMethodName>>>,\n>(\n\tmethodName: TMethodName,\n\targs: TArgs,\n\texplicitClient?: prismic.Client,\n): ClientHookReturnType<TData> => {\n\tconst lastArg = args[args.length - 1];\n\tconst {\n\t\tclient: lastArgExplicitClient,\n\t\tskip,\n\t\t...params\n\t} = isParams(lastArg) ? lastArg : ({} as HookOnlyParameters);\n\tconst argsWithoutParams = isParams(lastArg) ? args.slice(0, -1) : args;\n\n\tconst client = usePrismicClient(explicitClient || lastArgExplicitClient);\n\n\tconst [state, dispatch] = React.useReducer<\n\t\tReact.Reducer<StateMachineState<TData>, StateMachineAction<TData>>\n\t>(reducer, initialState);\n\n\tReact.useEffect(\n\t\t() => {\n\t\t\t// Used to prevent dispatching an action if the hook was cleaned up.\n\t\t\tlet didCancel = false;\n\n\t\t\tif (!skip) {\n\t\t\t\tif (!didCancel) {\n\t\t\t\t\tdispatch([\"start\"]);\n\t\t\t\t}\n\n\t\t\t\tclient[methodName]\n\t\t\t\t\t.call(\n\t\t\t\t\t\tclient,\n\t\t\t\t\t\t// @ts-expect-error - Merging method arg types is too complex\n\t\t\t\t\t\t...argsWithoutParams,\n\t\t\t\t\t\tparams,\n\t\t\t\t\t)\n\t\t\t\t\t.then((result) => {\n\t\t\t\t\t\tif (!didCancel) {\n\t\t\t\t\t\t\tdispatch([\"succeed\", result as TData]);\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t\t.catch((error) => {\n\t\t\t\t\t\tif (!didCancel) {\n\t\t\t\t\t\t\tdispatch([\"fail\", error]);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t}\n\n\t\t\t// Ensure we don't dispatch an action if the hook is cleaned up.\n\t\t\t() => {\n\t\t\t\tdidCancel = true;\n\t\t\t};\n\t\t},\n\t\t// We must disable exhaustive-deps since we are using\n\t\t// JSON.stringify on params (effectively a deep equality check).\n\t\t// We want this effect to run again anytime params change.\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t\t[\n\t\t\tclient,\n\t\t\tmethodName,\n\t\t\tskip,\n\t\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t\t\tJSON.stringify(argsWithoutParams),\n\t\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t\t\tJSON.stringify(params),\n\t\t],\n\t);\n\n\treturn React.useMemo(\n\t\t() => [\n\t\t\tstate.data,\n\t\t\t{\n\t\t\t\tstate: state.state,\n\t\t\t\terror: state.error,\n\t\t\t},\n\t\t],\n\t\t[state],\n\t);\n};\n"],"names":["usePrismicClient","React"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAkBA,MAAM,UAAU,CACf,OACA,WAC6B;
|
|
1
|
+
{"version":3,"file":"useStatefulPrismicClientMethod.cjs","sources":["../../src/useStatefulPrismicClientMethod.ts"],"sourcesContent":["import type * as prismic from \"@prismicio/client\";\n\nimport * as React from \"react\";\n\nimport { PrismicClientHookState } from \"./types\";\nimport { usePrismicClient } from \"./usePrismicClient\";\n\ntype StateMachineState<TData> = {\n\tstate: PrismicClientHookState;\n\tdata?: TData;\n\terror?: Error;\n};\n\ntype StateMachineAction<TData> =\n\t| [type: \"start\"]\n\t| [type: \"succeed\", payload: TData]\n\t| [type: \"fail\", payload: Error];\n\nconst reducer = <TData>(\n\tstate: StateMachineState<TData>,\n\taction: StateMachineAction<TData>,\n): StateMachineState<TData> => {\n\tswitch (action[0]) {\n\t\tcase \"start\": {\n\t\t\treturn { state: \"loading\" };\n\t\t}\n\n\t\tcase \"succeed\": {\n\t\t\treturn { state: \"loaded\", data: action[1] };\n\t\t}\n\n\t\tcase \"fail\": {\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\tstate: \"failed\",\n\t\t\t\terror: action[1],\n\t\t\t};\n\t\t}\n\t}\n};\n\nconst initialState: StateMachineState<never> = {\n\tstate: \"idle\",\n};\n\ntype UnwrapPromise<T> = T extends Promise<infer U> ? U : T;\n\ntype ClientPrototype = typeof prismic.Client.prototype;\n\ntype ClientMethod<MethodName extends keyof ClientPrototype> =\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tClientPrototype[MethodName] extends (...args: any[]) => any\n\t\t? ClientPrototype[MethodName]\n\t\t: never;\n\ntype ClientMethodName = keyof {\n\t[P in keyof prismic.Client as prismic.Client[P] extends (\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t...args: any[]\n\t) => // eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tPromise<any>\n\t\t? P\n\t\t: never]: unknown;\n};\n\nexport type ClientMethodParameters<MethodName extends keyof ClientPrototype> =\n\tParameters<ClientMethod<MethodName>>;\n\nexport type HookOnlyParameters = {\n\tclient?: prismic.Client;\n\tskip?: boolean;\n};\n\n/**\n * Determines if a value is a `@prismicio/client` params object.\n *\n * @param value - The value to check.\n *\n * @returns `true` if `value` is a `@prismicio/client` params object, `false`\n * otherwise.\n */\nconst isParams = (\n\tvalue: unknown,\n): value is ClientMethodParameters<\"get\">[0] & HookOnlyParameters => {\n\t// This is a *very* naive check.\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n};\n\n/**\n * The return value of a `@prismicio/client` React hook.\n *\n * @typeParam TData - Data returned by the client.\n */\nexport type ClientHookReturnType<TData = unknown> = [\n\t/**\n\t * Data returned by the client.\n\t */\n\tdata: TData | undefined,\n\n\t/**\n\t * The current state of the hook's client method call.\n\t */\n\tstate: Pick<StateMachineState<TData>, \"state\" | \"error\">,\n];\n\n/**\n * Creates a React hook that forwards arguments to a specific method of a\n * `@prismicio/client` instance. The created hook has its own internal state\n * manager to report async status, such as pending or error statuses.\n *\n * @param methodName - The `@prismicio/client` method to which hook arguments\n * will be forwarded.\n *\n * @returns A new React hook configured for the provided method.\n *\n * @internal\n */\nexport const useStatefulPrismicClientMethod = <\n\tTMethodName extends ClientMethodName,\n\tTArgs extends Parameters<ClientMethod<TMethodName>>,\n\tTData extends UnwrapPromise<ReturnType<ClientMethod<TMethodName>>>,\n>(\n\tmethodName: TMethodName,\n\targs: TArgs,\n\texplicitClient?: prismic.Client,\n): ClientHookReturnType<TData> => {\n\tconst lastArg = args[args.length - 1];\n\tconst {\n\t\tclient: lastArgExplicitClient,\n\t\tskip,\n\t\t...params\n\t} = isParams(lastArg) ? lastArg : ({} as HookOnlyParameters);\n\tconst argsWithoutParams = isParams(lastArg) ? args.slice(0, -1) : args;\n\n\tconst client = usePrismicClient(explicitClient || lastArgExplicitClient);\n\n\tconst [state, dispatch] = React.useReducer<\n\t\tReact.Reducer<StateMachineState<TData>, StateMachineAction<TData>>\n\t>(reducer, initialState);\n\n\tReact.useEffect(\n\t\t() => {\n\t\t\t// Used to prevent dispatching an action if the hook was cleaned up.\n\t\t\tlet didCancel = false;\n\n\t\t\tif (!skip) {\n\t\t\t\tif (!didCancel) {\n\t\t\t\t\tdispatch([\"start\"]);\n\t\t\t\t}\n\n\t\t\t\tclient[methodName]\n\t\t\t\t\t.call(\n\t\t\t\t\t\tclient,\n\t\t\t\t\t\t// @ts-expect-error - Merging method arg types is too complex\n\t\t\t\t\t\t...argsWithoutParams,\n\t\t\t\t\t\tparams,\n\t\t\t\t\t)\n\t\t\t\t\t.then((result) => {\n\t\t\t\t\t\tif (!didCancel) {\n\t\t\t\t\t\t\tdispatch([\"succeed\", result as TData]);\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t\t.catch((error) => {\n\t\t\t\t\t\tif (!didCancel) {\n\t\t\t\t\t\t\tdispatch([\"fail\", error]);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t}\n\n\t\t\t// Ensure we don't dispatch an action if the hook is cleaned up.\n\t\t\t() => {\n\t\t\t\tdidCancel = true;\n\t\t\t};\n\t\t},\n\t\t// We must disable exhaustive-deps since we are using\n\t\t// JSON.stringify on params (effectively a deep equality check).\n\t\t// We want this effect to run again anytime params change.\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t\t[\n\t\t\tclient,\n\t\t\tmethodName,\n\t\t\tskip,\n\t\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t\t\tJSON.stringify(argsWithoutParams),\n\t\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t\t\tJSON.stringify(params),\n\t\t],\n\t);\n\n\treturn React.useMemo(\n\t\t() => [\n\t\t\tstate.data,\n\t\t\t{\n\t\t\t\tstate: state.state,\n\t\t\t\terror: state.error,\n\t\t\t},\n\t\t],\n\t\t[state],\n\t);\n};\n"],"names":["usePrismicClient","React"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAkBA,MAAM,UAAU,CACf,OACA,WAC6B;AACrB,UAAA,OAAO,CAAC,GAAG;AAAA,IAClB,KAAK,SAAS;AACN,aAAA,EAAE,OAAO;IAChB;AAAA,IAED,KAAK,WAAW;AACf,aAAO,EAAE,OAAO,UAAU,MAAM,OAAO,CAAC;IACxC;AAAA,IAED,KAAK,QAAQ;AACL,aAAA;AAAA,QACN,GAAG;AAAA,QACH,OAAO;AAAA,QACP,OAAO,OAAO,CAAC;AAAA,MAAA;AAAA,IAEhB;AAAA,EACD;AACF;AAEA,MAAM,eAAyC;AAAA,EAC9C,OAAO;;AAuCR,MAAM,WAAW,CAChB,UACmE;AAE5D,SAAA,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;AAC3E;AA+BO,MAAM,iCAAiC,CAK7C,YACA,MACA,mBACgC;AAChC,QAAM,UAAU,KAAK,KAAK,SAAS,CAAC;AAC9B,QAAA,EACL,QAAQ,uBACR,MACA,GAAG,WACA,SAAS,OAAO,IAAI,UAAW;AAC7B,QAAA,oBAAoB,SAAS,OAAO,IAAI,KAAK,MAAM,GAAG,EAAE,IAAI;AAE5D,QAAA,SAASA,iBAAAA,iBAAiB,kBAAkB,qBAAqB;AAEvE,QAAM,CAAC,OAAO,QAAQ,IAAIC,iBAAM,WAE9B,SAAS,YAAY;AAEjBA,mBAAA;AAAA,IACL,MAAK;AAIJ,UAAI,CAAC,MAAM;AACM;AACN,mBAAA,CAAC,OAAO,CAAC;AAAA,QAClB;AAED,eAAO,UAAU,EACf;AAAA,UACA;AAAA,UAEA,GAAG;AAAA,UACH;AAAA,QAAA,EAEA,KAAK,CAAC,WAAU;AACA;AACN,qBAAA,CAAC,WAAW,MAAe,CAAC;AAAA,UACrC;AAAA,QAAA,CACD,EACA,MAAM,CAAC,UAAS;AACA;AACN,qBAAA,CAAC,QAAQ,KAAK,CAAC;AAAA,UACxB;AAAA,QAAA,CACD;AAAA,MACF;AAAA,IAMF;AAAA;AAAA;AAAA;AAAA;AAAA,IAKA;AAAA,MACC;AAAA,MACA;AAAA,MACA;AAAA;AAAA,MAEA,KAAK,UAAU,iBAAiB;AAAA;AAAA,MAEhC,KAAK,UAAU,MAAM;AAAA,IACrB;AAAA,EAAA;AAGK,SAAAA,iBAAM,QACZ,MAAM;AAAA,IACL,MAAM;AAAA,IACN;AAAA,MACC,OAAO,MAAM;AAAA,MACb,OAAO,MAAM;AAAA,IACb;AAAA,EAAA,GAEF,CAAC,KAAK,CAAC;AAET;;"}
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import type * as prismic from "@prismicio/client";
|
|
2
|
-
import { PrismicClientHookState } from "./types";
|
|
3
|
-
type StateMachineState<TData> = {
|
|
4
|
-
state: PrismicClientHookState;
|
|
5
|
-
data?: TData;
|
|
6
|
-
error?: Error;
|
|
7
|
-
};
|
|
8
|
-
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
|
|
9
|
-
type ClientPrototype = typeof prismic.Client.prototype;
|
|
10
|
-
type ClientMethod<MethodName extends keyof ClientPrototype> = ClientPrototype[MethodName] extends (...args: any[]) => any ? ClientPrototype[MethodName] : never;
|
|
11
|
-
export type ClientMethodParameters<MethodName extends keyof ClientPrototype> = Parameters<ClientMethod<MethodName>>;
|
|
12
|
-
export type HookOnlyParameters = {
|
|
13
|
-
client?: prismic.Client;
|
|
14
|
-
skip?: boolean;
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* The return value of a `@prismicio/client` React hook.
|
|
18
|
-
*
|
|
19
|
-
* @typeParam TData - Data returned by the client.
|
|
20
|
-
*/
|
|
21
|
-
export type ClientHookReturnType<TData = unknown> = [
|
|
22
|
-
/**
|
|
23
|
-
* Data returned by the client.
|
|
24
|
-
*/
|
|
25
|
-
data: TData | undefined,
|
|
26
|
-
/**
|
|
27
|
-
* The current state of the hook's client method call.
|
|
28
|
-
*/
|
|
29
|
-
state: Pick<StateMachineState<TData>, "state" | "error">
|
|
30
|
-
];
|
|
31
|
-
/**
|
|
32
|
-
* Creates a React hook that forwards arguments to a specific method of a
|
|
33
|
-
* `@prismicio/client` instance. The created hook has its own internal state
|
|
34
|
-
* manager to report async status, such as pending or error statuses.
|
|
35
|
-
*
|
|
36
|
-
* @param methodName - The `@prismicio/client` method to which hook arguments
|
|
37
|
-
* will be forwarded.
|
|
38
|
-
*
|
|
39
|
-
* @returns A new React hook configured for the provided method.
|
|
40
|
-
*
|
|
41
|
-
* @internal
|
|
42
|
-
*/
|
|
43
|
-
export declare const useStatefulPrismicClientMethod: <TMethodName extends "fetchFn" | "
|
|
44
|
-
export {};
|
|
1
|
+
import type * as prismic from "@prismicio/client";
|
|
2
|
+
import { PrismicClientHookState } from "./types";
|
|
3
|
+
type StateMachineState<TData> = {
|
|
4
|
+
state: PrismicClientHookState;
|
|
5
|
+
data?: TData;
|
|
6
|
+
error?: Error;
|
|
7
|
+
};
|
|
8
|
+
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
|
|
9
|
+
type ClientPrototype = typeof prismic.Client.prototype;
|
|
10
|
+
type ClientMethod<MethodName extends keyof ClientPrototype> = ClientPrototype[MethodName] extends (...args: any[]) => any ? ClientPrototype[MethodName] : never;
|
|
11
|
+
export type ClientMethodParameters<MethodName extends keyof ClientPrototype> = Parameters<ClientMethod<MethodName>>;
|
|
12
|
+
export type HookOnlyParameters = {
|
|
13
|
+
client?: prismic.Client;
|
|
14
|
+
skip?: boolean;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* The return value of a `@prismicio/client` React hook.
|
|
18
|
+
*
|
|
19
|
+
* @typeParam TData - Data returned by the client.
|
|
20
|
+
*/
|
|
21
|
+
export type ClientHookReturnType<TData = unknown> = [
|
|
22
|
+
/**
|
|
23
|
+
* Data returned by the client.
|
|
24
|
+
*/
|
|
25
|
+
data: TData | undefined,
|
|
26
|
+
/**
|
|
27
|
+
* The current state of the hook's client method call.
|
|
28
|
+
*/
|
|
29
|
+
state: Pick<StateMachineState<TData>, "state" | "error">
|
|
30
|
+
];
|
|
31
|
+
/**
|
|
32
|
+
* Creates a React hook that forwards arguments to a specific method of a
|
|
33
|
+
* `@prismicio/client` instance. The created hook has its own internal state
|
|
34
|
+
* manager to report async status, such as pending or error statuses.
|
|
35
|
+
*
|
|
36
|
+
* @param methodName - The `@prismicio/client` method to which hook arguments
|
|
37
|
+
* will be forwarded.
|
|
38
|
+
*
|
|
39
|
+
* @returns A new React hook configured for the provided method.
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
export declare const useStatefulPrismicClientMethod: <TMethodName extends "fetchFn" | "get" | "getFirst" | "dangerouslyGetAll" | "getByID" | "getByIDs" | "getAllByIDs" | "getByUID" | "getByUIDs" | "getAllByUIDs" | "getSingle" | "getByType" | "getAllByType" | "getByTag" | "getAllByTag" | "getByEveryTag" | "getAllByEveryTag" | "getBySomeTags" | "getAllBySomeTags" | "getRepository" | "getRefs" | "getRefByID" | "getRefByLabel" | "getMasterRef" | "getReleases" | "getReleaseByID" | "getReleaseByLabel" | "getTags" | "buildQueryURL" | "resolvePreviewURL" | "graphQLFetch", TArgs extends Parameters<ClientMethod<TMethodName>>, TData extends UnwrapPromise<ReturnType<ClientMethod<TMethodName>>>>(methodName: TMethodName, args: TArgs, explicitClient?: prismic.Client) => ClientHookReturnType<TData>;
|
|
44
|
+
export {};
|
|
@@ -50,11 +50,17 @@ const useStatefulPrismicClientMethod = (methodName, args, explicitClient) => {
|
|
|
50
50
|
});
|
|
51
51
|
}
|
|
52
52
|
},
|
|
53
|
+
// We must disable exhaustive-deps since we are using
|
|
54
|
+
// JSON.stringify on params (effectively a deep equality check).
|
|
55
|
+
// We want this effect to run again anytime params change.
|
|
56
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
53
57
|
[
|
|
54
58
|
client,
|
|
55
59
|
methodName,
|
|
56
60
|
skip,
|
|
61
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
57
62
|
JSON.stringify(argsWithoutParams),
|
|
63
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
58
64
|
JSON.stringify(params)
|
|
59
65
|
]
|
|
60
66
|
);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStatefulPrismicClientMethod.js","sources":["../../src/useStatefulPrismicClientMethod.ts"],"sourcesContent":["import type * as prismic from \"@prismicio/client\";\n\nimport * as React from \"react\";\n\nimport { PrismicClientHookState } from \"./types\";\nimport { usePrismicClient } from \"./usePrismicClient\";\n\ntype StateMachineState<TData> = {\n\tstate: PrismicClientHookState;\n\tdata?: TData;\n\terror?: Error;\n};\n\ntype StateMachineAction<TData> =\n\t| [type: \"start\"]\n\t| [type: \"succeed\", payload: TData]\n\t| [type: \"fail\", payload: Error];\n\nconst reducer = <TData>(\n\tstate: StateMachineState<TData>,\n\taction: StateMachineAction<TData>,\n): StateMachineState<TData> => {\n\tswitch (action[0]) {\n\t\tcase \"start\": {\n\t\t\treturn { state: \"loading\" };\n\t\t}\n\n\t\tcase \"succeed\": {\n\t\t\treturn { state: \"loaded\", data: action[1] };\n\t\t}\n\n\t\tcase \"fail\": {\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\tstate: \"failed\",\n\t\t\t\terror: action[1],\n\t\t\t};\n\t\t}\n\t}\n};\n\nconst initialState: StateMachineState<never> = {\n\tstate: \"idle\",\n};\n\ntype UnwrapPromise<T> = T extends Promise<infer U> ? U : T;\n\ntype ClientPrototype = typeof prismic.Client.prototype;\n\ntype ClientMethod<MethodName extends keyof ClientPrototype> =\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tClientPrototype[MethodName] extends (...args: any[]) => any\n\t\t? ClientPrototype[MethodName]\n\t\t: never;\n\ntype ClientMethodName = keyof {\n\t[P in keyof prismic.Client as prismic.Client[P] extends (\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t...args: any[]\n\t) => // eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tPromise<any>\n\t\t? P\n\t\t: never]: unknown;\n};\n\nexport type ClientMethodParameters<MethodName extends keyof ClientPrototype> =\n\tParameters<ClientMethod<MethodName>>;\n\nexport type HookOnlyParameters = {\n\tclient?: prismic.Client;\n\tskip?: boolean;\n};\n\n/**\n * Determines if a value is a `@prismicio/client` params object.\n *\n * @param value - The value to check.\n *\n * @returns `true` if `value` is a `@prismicio/client` params object, `false`\n * otherwise.\n */\nconst isParams = (\n\tvalue: unknown,\n): value is ClientMethodParameters<\"get\">[0] & HookOnlyParameters => {\n\t// This is a *very* naive check.\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n};\n\n/**\n * The return value of a `@prismicio/client` React hook.\n *\n * @typeParam TData - Data returned by the client.\n */\nexport type ClientHookReturnType<TData = unknown> = [\n\t/**\n\t * Data returned by the client.\n\t */\n\tdata: TData | undefined,\n\n\t/**\n\t * The current state of the hook's client method call.\n\t */\n\tstate: Pick<StateMachineState<TData>, \"state\" | \"error\">,\n];\n\n/**\n * Creates a React hook that forwards arguments to a specific method of a\n * `@prismicio/client` instance. The created hook has its own internal state\n * manager to report async status, such as pending or error statuses.\n *\n * @param methodName - The `@prismicio/client` method to which hook arguments\n * will be forwarded.\n *\n * @returns A new React hook configured for the provided method.\n *\n * @internal\n */\nexport const useStatefulPrismicClientMethod = <\n\tTMethodName extends ClientMethodName,\n\tTArgs extends Parameters<ClientMethod<TMethodName>>,\n\tTData extends UnwrapPromise<ReturnType<ClientMethod<TMethodName>>>,\n>(\n\tmethodName: TMethodName,\n\targs: TArgs,\n\texplicitClient?: prismic.Client,\n): ClientHookReturnType<TData> => {\n\tconst lastArg = args[args.length - 1];\n\tconst {\n\t\tclient: lastArgExplicitClient,\n\t\tskip,\n\t\t...params\n\t} = isParams(lastArg) ? lastArg : ({} as HookOnlyParameters);\n\tconst argsWithoutParams = isParams(lastArg) ? args.slice(0, -1) : args;\n\n\tconst client = usePrismicClient(explicitClient || lastArgExplicitClient);\n\n\tconst [state, dispatch] = React.useReducer<\n\t\tReact.Reducer<StateMachineState<TData>, StateMachineAction<TData>>\n\t>(reducer, initialState);\n\n\tReact.useEffect(\n\t\t() => {\n\t\t\t// Used to prevent dispatching an action if the hook was cleaned up.\n\t\t\tlet didCancel = false;\n\n\t\t\tif (!skip) {\n\t\t\t\tif (!didCancel) {\n\t\t\t\t\tdispatch([\"start\"]);\n\t\t\t\t}\n\n\t\t\t\tclient[methodName]\n\t\t\t\t\t.call(\n\t\t\t\t\t\tclient,\n\t\t\t\t\t\t// @ts-expect-error - Merging method arg types is too complex\n\t\t\t\t\t\t...argsWithoutParams,\n\t\t\t\t\t\tparams,\n\t\t\t\t\t)\n\t\t\t\t\t.then((result) => {\n\t\t\t\t\t\tif (!didCancel) {\n\t\t\t\t\t\t\tdispatch([\"succeed\", result as TData]);\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t\t.catch((error) => {\n\t\t\t\t\t\tif (!didCancel) {\n\t\t\t\t\t\t\tdispatch([\"fail\", error]);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t}\n\n\t\t\t// Ensure we don't dispatch an action if the hook is cleaned up.\n\t\t\t() => {\n\t\t\t\tdidCancel = true;\n\t\t\t};\n\t\t},\n\t\t// We must disable exhaustive-deps since we are using\n\t\t// JSON.stringify on params (effectively a deep equality check).\n\t\t// We want this effect to run again anytime params change.\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t\t[\n\t\t\tclient,\n\t\t\tmethodName,\n\t\t\tskip,\n\t\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t\t\tJSON.stringify(argsWithoutParams),\n\t\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t\t\tJSON.stringify(params),\n\t\t],\n\t);\n\n\treturn React.useMemo(\n\t\t() => [\n\t\t\tstate.data,\n\t\t\t{\n\t\t\t\tstate: state.state,\n\t\t\t\terror: state.error,\n\t\t\t},\n\t\t],\n\t\t[state],\n\t);\n};\n"],"names":[],"mappings":";;AAkBA,MAAM,UAAU,CACf,OACA,WAC6B;
|
|
1
|
+
{"version":3,"file":"useStatefulPrismicClientMethod.js","sources":["../../src/useStatefulPrismicClientMethod.ts"],"sourcesContent":["import type * as prismic from \"@prismicio/client\";\n\nimport * as React from \"react\";\n\nimport { PrismicClientHookState } from \"./types\";\nimport { usePrismicClient } from \"./usePrismicClient\";\n\ntype StateMachineState<TData> = {\n\tstate: PrismicClientHookState;\n\tdata?: TData;\n\terror?: Error;\n};\n\ntype StateMachineAction<TData> =\n\t| [type: \"start\"]\n\t| [type: \"succeed\", payload: TData]\n\t| [type: \"fail\", payload: Error];\n\nconst reducer = <TData>(\n\tstate: StateMachineState<TData>,\n\taction: StateMachineAction<TData>,\n): StateMachineState<TData> => {\n\tswitch (action[0]) {\n\t\tcase \"start\": {\n\t\t\treturn { state: \"loading\" };\n\t\t}\n\n\t\tcase \"succeed\": {\n\t\t\treturn { state: \"loaded\", data: action[1] };\n\t\t}\n\n\t\tcase \"fail\": {\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\tstate: \"failed\",\n\t\t\t\terror: action[1],\n\t\t\t};\n\t\t}\n\t}\n};\n\nconst initialState: StateMachineState<never> = {\n\tstate: \"idle\",\n};\n\ntype UnwrapPromise<T> = T extends Promise<infer U> ? U : T;\n\ntype ClientPrototype = typeof prismic.Client.prototype;\n\ntype ClientMethod<MethodName extends keyof ClientPrototype> =\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tClientPrototype[MethodName] extends (...args: any[]) => any\n\t\t? ClientPrototype[MethodName]\n\t\t: never;\n\ntype ClientMethodName = keyof {\n\t[P in keyof prismic.Client as prismic.Client[P] extends (\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t...args: any[]\n\t) => // eslint-disable-next-line @typescript-eslint/no-explicit-any\n\tPromise<any>\n\t\t? P\n\t\t: never]: unknown;\n};\n\nexport type ClientMethodParameters<MethodName extends keyof ClientPrototype> =\n\tParameters<ClientMethod<MethodName>>;\n\nexport type HookOnlyParameters = {\n\tclient?: prismic.Client;\n\tskip?: boolean;\n};\n\n/**\n * Determines if a value is a `@prismicio/client` params object.\n *\n * @param value - The value to check.\n *\n * @returns `true` if `value` is a `@prismicio/client` params object, `false`\n * otherwise.\n */\nconst isParams = (\n\tvalue: unknown,\n): value is ClientMethodParameters<\"get\">[0] & HookOnlyParameters => {\n\t// This is a *very* naive check.\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n};\n\n/**\n * The return value of a `@prismicio/client` React hook.\n *\n * @typeParam TData - Data returned by the client.\n */\nexport type ClientHookReturnType<TData = unknown> = [\n\t/**\n\t * Data returned by the client.\n\t */\n\tdata: TData | undefined,\n\n\t/**\n\t * The current state of the hook's client method call.\n\t */\n\tstate: Pick<StateMachineState<TData>, \"state\" | \"error\">,\n];\n\n/**\n * Creates a React hook that forwards arguments to a specific method of a\n * `@prismicio/client` instance. The created hook has its own internal state\n * manager to report async status, such as pending or error statuses.\n *\n * @param methodName - The `@prismicio/client` method to which hook arguments\n * will be forwarded.\n *\n * @returns A new React hook configured for the provided method.\n *\n * @internal\n */\nexport const useStatefulPrismicClientMethod = <\n\tTMethodName extends ClientMethodName,\n\tTArgs extends Parameters<ClientMethod<TMethodName>>,\n\tTData extends UnwrapPromise<ReturnType<ClientMethod<TMethodName>>>,\n>(\n\tmethodName: TMethodName,\n\targs: TArgs,\n\texplicitClient?: prismic.Client,\n): ClientHookReturnType<TData> => {\n\tconst lastArg = args[args.length - 1];\n\tconst {\n\t\tclient: lastArgExplicitClient,\n\t\tskip,\n\t\t...params\n\t} = isParams(lastArg) ? lastArg : ({} as HookOnlyParameters);\n\tconst argsWithoutParams = isParams(lastArg) ? args.slice(0, -1) : args;\n\n\tconst client = usePrismicClient(explicitClient || lastArgExplicitClient);\n\n\tconst [state, dispatch] = React.useReducer<\n\t\tReact.Reducer<StateMachineState<TData>, StateMachineAction<TData>>\n\t>(reducer, initialState);\n\n\tReact.useEffect(\n\t\t() => {\n\t\t\t// Used to prevent dispatching an action if the hook was cleaned up.\n\t\t\tlet didCancel = false;\n\n\t\t\tif (!skip) {\n\t\t\t\tif (!didCancel) {\n\t\t\t\t\tdispatch([\"start\"]);\n\t\t\t\t}\n\n\t\t\t\tclient[methodName]\n\t\t\t\t\t.call(\n\t\t\t\t\t\tclient,\n\t\t\t\t\t\t// @ts-expect-error - Merging method arg types is too complex\n\t\t\t\t\t\t...argsWithoutParams,\n\t\t\t\t\t\tparams,\n\t\t\t\t\t)\n\t\t\t\t\t.then((result) => {\n\t\t\t\t\t\tif (!didCancel) {\n\t\t\t\t\t\t\tdispatch([\"succeed\", result as TData]);\n\t\t\t\t\t\t}\n\t\t\t\t\t})\n\t\t\t\t\t.catch((error) => {\n\t\t\t\t\t\tif (!didCancel) {\n\t\t\t\t\t\t\tdispatch([\"fail\", error]);\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t}\n\n\t\t\t// Ensure we don't dispatch an action if the hook is cleaned up.\n\t\t\t() => {\n\t\t\t\tdidCancel = true;\n\t\t\t};\n\t\t},\n\t\t// We must disable exhaustive-deps since we are using\n\t\t// JSON.stringify on params (effectively a deep equality check).\n\t\t// We want this effect to run again anytime params change.\n\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t\t[\n\t\t\tclient,\n\t\t\tmethodName,\n\t\t\tskip,\n\t\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t\t\tJSON.stringify(argsWithoutParams),\n\t\t\t// eslint-disable-next-line react-hooks/exhaustive-deps\n\t\t\tJSON.stringify(params),\n\t\t],\n\t);\n\n\treturn React.useMemo(\n\t\t() => [\n\t\t\tstate.data,\n\t\t\t{\n\t\t\t\tstate: state.state,\n\t\t\t\terror: state.error,\n\t\t\t},\n\t\t],\n\t\t[state],\n\t);\n};\n"],"names":[],"mappings":";;AAkBA,MAAM,UAAU,CACf,OACA,WAC6B;AACrB,UAAA,OAAO,CAAC,GAAG;AAAA,IAClB,KAAK,SAAS;AACN,aAAA,EAAE,OAAO;IAChB;AAAA,IAED,KAAK,WAAW;AACf,aAAO,EAAE,OAAO,UAAU,MAAM,OAAO,CAAC;IACxC;AAAA,IAED,KAAK,QAAQ;AACL,aAAA;AAAA,QACN,GAAG;AAAA,QACH,OAAO;AAAA,QACP,OAAO,OAAO,CAAC;AAAA,MAAA;AAAA,IAEhB;AAAA,EACD;AACF;AAEA,MAAM,eAAyC;AAAA,EAC9C,OAAO;;AAuCR,MAAM,WAAW,CAChB,UACmE;AAE5D,SAAA,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;AAC3E;AA+BO,MAAM,iCAAiC,CAK7C,YACA,MACA,mBACgC;AAChC,QAAM,UAAU,KAAK,KAAK,SAAS,CAAC;AAC9B,QAAA,EACL,QAAQ,uBACR,MACA,GAAG,WACA,SAAS,OAAO,IAAI,UAAW;AAC7B,QAAA,oBAAoB,SAAS,OAAO,IAAI,KAAK,MAAM,GAAG,EAAE,IAAI;AAE5D,QAAA,SAAS,iBAAiB,kBAAkB,qBAAqB;AAEvE,QAAM,CAAC,OAAO,QAAQ,IAAI,MAAM,WAE9B,SAAS,YAAY;AAEjB,QAAA;AAAA,IACL,MAAK;AAIJ,UAAI,CAAC,MAAM;AACM;AACN,mBAAA,CAAC,OAAO,CAAC;AAAA,QAClB;AAED,eAAO,UAAU,EACf;AAAA,UACA;AAAA,UAEA,GAAG;AAAA,UACH;AAAA,QAAA,EAEA,KAAK,CAAC,WAAU;AACA;AACN,qBAAA,CAAC,WAAW,MAAe,CAAC;AAAA,UACrC;AAAA,QAAA,CACD,EACA,MAAM,CAAC,UAAS;AACA;AACN,qBAAA,CAAC,QAAQ,KAAK,CAAC;AAAA,UACxB;AAAA,QAAA,CACD;AAAA,MACF;AAAA,IAMF;AAAA;AAAA;AAAA;AAAA;AAAA,IAKA;AAAA,MACC;AAAA,MACA;AAAA,MACA;AAAA;AAAA,MAEA,KAAK,UAAU,iBAAiB;AAAA;AAAA,MAEhC,KAAK,UAAU,MAAM;AAAA,IACrB;AAAA,EAAA;AAGK,SAAA,MAAM,QACZ,MAAM;AAAA,IACL,MAAM;AAAA,IACN;AAAA,MACC,OAAO,MAAM;AAAA,MACb,OAAO,MAAM;AAAA,IACb;AAAA,EAAA,GAEF,CAAC,KAAK,CAAC;AAET;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/react",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0-alpha.0",
|
|
4
4
|
"description": "React components and hooks to fetch and present Prismic content",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -18,15 +18,21 @@
|
|
|
18
18
|
"sideEffects": false,
|
|
19
19
|
"exports": {
|
|
20
20
|
".": {
|
|
21
|
-
"
|
|
22
|
-
|
|
21
|
+
"react-server": {
|
|
22
|
+
"import": "./dist/react-server.js",
|
|
23
|
+
"require": "./dist/react-server.cjs"
|
|
24
|
+
},
|
|
25
|
+
"default": {
|
|
26
|
+
"import": "./dist/index.js",
|
|
27
|
+
"require": "./dist/index.cjs"
|
|
28
|
+
}
|
|
23
29
|
},
|
|
24
30
|
"./package.json": "./package.json"
|
|
25
31
|
},
|
|
26
|
-
"main": "dist/index.cjs",
|
|
27
|
-
"module": "dist/index.js",
|
|
28
|
-
"react-native": "dist/index.js",
|
|
29
|
-
"types": "dist/index.d.ts",
|
|
32
|
+
"main": "./dist/index.cjs",
|
|
33
|
+
"module": "./dist/index.js",
|
|
34
|
+
"react-native": "./dist/index.js",
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
30
36
|
"files": [
|
|
31
37
|
"dist",
|
|
32
38
|
"src"
|
|
@@ -36,68 +42,57 @@
|
|
|
36
42
|
"dev": "vite build --watch",
|
|
37
43
|
"format": "prettier --write .",
|
|
38
44
|
"lint": "eslint --ext .js,.jsx,.ts,.tsx .",
|
|
45
|
+
"types": "tsc --noEmit",
|
|
39
46
|
"prepare": "npm run build",
|
|
40
47
|
"release": "npm run build && npm run test && standard-version && git push --follow-tags && npm run build && npm publish",
|
|
41
48
|
"release:alpha": "npm run build && npm run test && standard-version --release-as minor --prerelease alpha && git push --follow-tags && npm run build && npm publish --tag alpha",
|
|
42
49
|
"release:alpha:dry": "standard-version --release-as minor --prerelease alpha --dry-run",
|
|
43
50
|
"release:dry": "standard-version --dry-run",
|
|
44
|
-
"unit": "
|
|
51
|
+
"unit": "vitest run --coverage",
|
|
52
|
+
"unit:watch": "vitest watch",
|
|
45
53
|
"size": "size-limit",
|
|
46
|
-
"test": "npm run lint && npm run unit && npm run build && npm run size"
|
|
54
|
+
"test": "npm run lint && npm run types && npm run unit && npm run build && npm run size"
|
|
47
55
|
},
|
|
48
56
|
"dependencies": {
|
|
49
|
-
"@prismicio/
|
|
50
|
-
"@prismicio/richtext": "^2.1.3",
|
|
51
|
-
"@prismicio/types": "^0.2.7"
|
|
57
|
+
"@prismicio/richtext": "^2.1.4"
|
|
52
58
|
},
|
|
53
59
|
"devDependencies": {
|
|
54
|
-
"@prismicio/client": "^
|
|
55
|
-
"@prismicio/mock": "^0.
|
|
56
|
-
"@size-limit/preset-small-lib": "^8.
|
|
57
|
-
"@testing-library/react": "^
|
|
58
|
-
"@types/jsdom": "^20.0.1",
|
|
60
|
+
"@prismicio/client": "^7.0.0-alpha.2",
|
|
61
|
+
"@prismicio/mock": "^0.2.0",
|
|
62
|
+
"@size-limit/preset-small-lib": "^8.2.4",
|
|
63
|
+
"@testing-library/react": "^14.0.0",
|
|
59
64
|
"@types/node-fetch": "^3.0.3",
|
|
60
|
-
"@types/react": "^18.0.
|
|
65
|
+
"@types/react": "^18.0.38",
|
|
61
66
|
"@types/react-test-renderer": "^18.0.0",
|
|
62
|
-
"@
|
|
63
|
-
"@typescript-eslint/
|
|
64
|
-
"@
|
|
65
|
-
"@
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"esbuild": "^0.16.9",
|
|
69
|
-
"esbuild-register": "^3.4.2",
|
|
70
|
-
"eslint": "^8.30.0",
|
|
71
|
-
"eslint-config-prettier": "^8.5.0",
|
|
67
|
+
"@typescript-eslint/eslint-plugin": "^5.59.0",
|
|
68
|
+
"@typescript-eslint/parser": "^5.59.0",
|
|
69
|
+
"@vitejs/plugin-react": "^4.0.0",
|
|
70
|
+
"@vitest/coverage-c8": "^0.30.1",
|
|
71
|
+
"eslint": "^8.39.0",
|
|
72
|
+
"eslint-config-prettier": "^8.8.0",
|
|
72
73
|
"eslint-plugin-prettier": "^4.2.1",
|
|
73
|
-
"eslint-plugin-react": "^7.
|
|
74
|
+
"eslint-plugin-react": "^7.32.2",
|
|
74
75
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
75
76
|
"eslint-plugin-tsdoc": "^0.2.17",
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
"
|
|
80
|
-
"nyc": "^15.1.0",
|
|
81
|
-
"prettier": "^2.8.1",
|
|
77
|
+
"happy-dom": "^9.9.2",
|
|
78
|
+
"msw": "^1.2.1",
|
|
79
|
+
"node-fetch": "^3.3.1",
|
|
80
|
+
"prettier": "^2.8.7",
|
|
82
81
|
"prettier-plugin-jsdoc": "^0.4.2",
|
|
83
82
|
"react": "^18.2.0",
|
|
84
83
|
"react-dom": "^18.2.0",
|
|
85
84
|
"react-test-renderer": "^18.2.0",
|
|
86
|
-
"
|
|
87
|
-
"size-limit": "^8.
|
|
85
|
+
"rollup-plugin-preserve-directives": "^0.1.0",
|
|
86
|
+
"size-limit": "^8.2.4",
|
|
88
87
|
"standard-version": "^9.5.0",
|
|
89
|
-
"typescript": "^
|
|
90
|
-
"vite": "^4.
|
|
91
|
-
"vite-plugin-sdk": "^0.1.
|
|
88
|
+
"typescript": "^5.0.4",
|
|
89
|
+
"vite": "^4.3.1",
|
|
90
|
+
"vite-plugin-sdk": "^0.1.1",
|
|
91
|
+
"vitest": "^0.30.1"
|
|
92
92
|
},
|
|
93
93
|
"peerDependencies": {
|
|
94
|
-
"@prismicio/client": "^
|
|
95
|
-
"react": "^
|
|
96
|
-
},
|
|
97
|
-
"peerDependenciesMeta": {
|
|
98
|
-
"@prismicio/client": {
|
|
99
|
-
"optional": true
|
|
100
|
-
}
|
|
94
|
+
"@prismicio/client": "^7",
|
|
95
|
+
"react": "^18"
|
|
101
96
|
},
|
|
102
97
|
"engines": {
|
|
103
98
|
"node": ">=12.7.0"
|
package/src/PrismicImage.tsx
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
import * as
|
|
3
|
-
import * as prismicH from "@prismicio/helpers";
|
|
2
|
+
import * as prismic from "@prismicio/client";
|
|
4
3
|
|
|
5
4
|
import { __PRODUCTION__ } from "./lib/__PRODUCTION__";
|
|
6
5
|
import { devMsg } from "./lib/devMsg";
|
|
@@ -18,14 +17,14 @@ export type PrismicImageProps = Omit<
|
|
|
18
17
|
/**
|
|
19
18
|
* The Prismic Image field or thumbnail to render.
|
|
20
19
|
*/
|
|
21
|
-
field:
|
|
20
|
+
field: prismic.ImageFieldImage | null | undefined;
|
|
22
21
|
|
|
23
22
|
/**
|
|
24
23
|
* An object of Imgix URL API parameters to transform the image.
|
|
25
24
|
*
|
|
26
25
|
* See: https://docs.imgix.com/apis/rendering
|
|
27
26
|
*/
|
|
28
|
-
imgixParams?: Parameters<typeof
|
|
27
|
+
imgixParams?: Parameters<typeof prismic.asImageSrc>[1];
|
|
29
28
|
|
|
30
29
|
/**
|
|
31
30
|
* Declare an image as decorative by providing `alt=""`.
|
|
@@ -58,7 +57,7 @@ export type PrismicImageProps = Omit<
|
|
|
58
57
|
*/
|
|
59
58
|
widths?:
|
|
60
59
|
| NonNullable<
|
|
61
|
-
Parameters<typeof
|
|
60
|
+
Parameters<typeof prismic.asImageWidthSrcSet>[1]
|
|
62
61
|
>["widths"]
|
|
63
62
|
| "defaults";
|
|
64
63
|
/**
|
|
@@ -79,16 +78,35 @@ export type PrismicImageProps = Omit<
|
|
|
79
78
|
*/
|
|
80
79
|
pixelDensities:
|
|
81
80
|
| NonNullable<
|
|
82
|
-
Parameters<typeof
|
|
81
|
+
Parameters<typeof prismic.asImagePixelDensitySrcSet>[1]
|
|
83
82
|
>["pixelDensities"]
|
|
84
83
|
| "defaults";
|
|
85
84
|
}
|
|
86
85
|
);
|
|
87
86
|
|
|
88
|
-
|
|
87
|
+
/**
|
|
88
|
+
* React component that renders an image from a Prismic Image field or one of
|
|
89
|
+
* its thumbnails. It will automatically set the `alt` attribute using the Image
|
|
90
|
+
* field's `alt` property.
|
|
91
|
+
*
|
|
92
|
+
* By default, a widths-based srcset will be used to support responsive images.
|
|
93
|
+
* This ensures only the smallest image needed for a browser is downloaded.
|
|
94
|
+
*
|
|
95
|
+
* To use a pixel-density-based srcset, use the `pixelDensities` prop. Default
|
|
96
|
+
* pixel densities can be used by using `pixelDensities="defaults"`.
|
|
97
|
+
*
|
|
98
|
+
* **Note**: If you are using a framework that has a native image component,
|
|
99
|
+
* such as Next.js and Gatsby, prefer using those image components instead. They
|
|
100
|
+
* can provide deeper framework integration than `<PrismicImage>`.
|
|
101
|
+
*
|
|
102
|
+
* @param props - Props for the component.
|
|
103
|
+
*
|
|
104
|
+
* @returns A responsive image component for the given Image field.
|
|
105
|
+
*/
|
|
106
|
+
export const PrismicImage = React.forwardRef(function PrismicImage(
|
|
89
107
|
props: PrismicImageProps,
|
|
90
108
|
ref: React.ForwardedRef<HTMLImageElement>,
|
|
91
|
-
): JSX.Element | null
|
|
109
|
+
): JSX.Element | null {
|
|
92
110
|
const {
|
|
93
111
|
field,
|
|
94
112
|
alt,
|
|
@@ -123,12 +141,12 @@ const _PrismicImage = (
|
|
|
123
141
|
}
|
|
124
142
|
}
|
|
125
143
|
|
|
126
|
-
if (
|
|
144
|
+
if (prismic.isFilled.imageThumbnail(field)) {
|
|
127
145
|
let src: string | undefined;
|
|
128
146
|
let srcSet: string | undefined;
|
|
129
147
|
|
|
130
148
|
if (widths || !pixelDensities) {
|
|
131
|
-
const res =
|
|
149
|
+
const res = prismic.asImageWidthSrcSet(field, {
|
|
132
150
|
...imgixParams,
|
|
133
151
|
widths: widths === "defaults" ? undefined : widths,
|
|
134
152
|
});
|
|
@@ -136,7 +154,7 @@ const _PrismicImage = (
|
|
|
136
154
|
src = res.src;
|
|
137
155
|
srcSet = res.srcset;
|
|
138
156
|
} else if (pixelDensities) {
|
|
139
|
-
const res =
|
|
157
|
+
const res = prismic.asImagePixelDensitySrcSet(field, {
|
|
140
158
|
...imgixParams,
|
|
141
159
|
pixelDensities:
|
|
142
160
|
pixelDensities === "defaults" ? undefined : pixelDensities,
|
|
@@ -158,29 +176,4 @@ const _PrismicImage = (
|
|
|
158
176
|
} else {
|
|
159
177
|
return null;
|
|
160
178
|
}
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
if (!__PRODUCTION__) {
|
|
164
|
-
_PrismicImage.displayName = "PrismicImage";
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* React component that renders an image from a Prismic Image field or one of
|
|
169
|
-
* its thumbnails. It will automatically set the `alt` attribute using the Image
|
|
170
|
-
* field's `alt` property.
|
|
171
|
-
*
|
|
172
|
-
* By default, a widths-based srcset will be used to support responsive images.
|
|
173
|
-
* This ensures only the smallest image needed for a browser is downloaded.
|
|
174
|
-
*
|
|
175
|
-
* To use a pixel-density-based srcset, use the `pixelDensities` prop. Default
|
|
176
|
-
* pixel densities can be used by using `pixelDensities="defaults"`.
|
|
177
|
-
*
|
|
178
|
-
* **Note**: If you are using a framework that has a native image component,
|
|
179
|
-
* such as Next.js and Gatsby, prefer using those image components instead. They
|
|
180
|
-
* can provide deeper framework integration than `<PrismicImage>`.
|
|
181
|
-
*
|
|
182
|
-
* @param props - Props for the component.
|
|
183
|
-
*
|
|
184
|
-
* @returns A responsive image component for the given Image field.
|
|
185
|
-
*/
|
|
186
|
-
export const PrismicImage = React.forwardRef(_PrismicImage);
|
|
179
|
+
});
|