@ic-reactor/react 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -17
- package/dist/helpers/{actor.d.ts → actorHooks.d.ts} +4 -7
- package/dist/helpers/{actor.js → actorHooks.js} +8 -15
- package/dist/helpers/agentHooks.d.ts +3 -0
- package/dist/helpers/{agent.js → agentHooks.js} +3 -3
- package/dist/helpers/authHooks.d.ts +2 -0
- package/dist/helpers/authHooks.js +127 -0
- package/dist/helpers/extractActorContext.d.ts +3 -0
- package/dist/{provider/actor/context.js → helpers/extractActorContext.js} +14 -65
- package/dist/helpers/extractAgentContext.d.ts +27 -0
- package/dist/helpers/extractAgentContext.js +134 -0
- package/dist/helpers/index.d.ts +5 -3
- package/dist/helpers/index.js +5 -3
- package/dist/helpers/types.d.ts +20 -30
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/main.d.ts +38 -2
- package/dist/main.js +39 -21
- package/dist/provider/ActorProvider.d.ts +27 -0
- package/dist/provider/ActorProvider.js +30 -0
- package/dist/provider/AgentProvider.d.ts +30 -0
- package/dist/provider/AgentProvider.js +33 -0
- package/dist/provider/actor.d.ts +64 -0
- package/dist/provider/actor.js +117 -0
- package/dist/provider/agent.d.ts +71 -0
- package/dist/provider/agent.js +124 -0
- package/dist/provider/hooks/index.d.ts +1 -0
- package/dist/{hooks → provider/hooks}/index.js +1 -1
- package/dist/provider/hooks/types.d.ts +15 -0
- package/dist/provider/hooks/useActor.d.ts +63 -0
- package/dist/{hooks/useReactor.js → provider/hooks/useActor.js} +51 -29
- package/dist/provider/index.d.ts +8 -2
- package/dist/provider/index.js +9 -2
- package/dist/provider/types.d.ts +27 -0
- package/dist/provider/{agent/index.js → types.js} +1 -2
- package/dist/types.d.ts +8 -8
- package/dist/types.js +2 -4
- package/package.json +3 -3
- package/dist/helpers/agent.d.ts +0 -3
- package/dist/helpers/auth.d.ts +0 -3
- package/dist/helpers/auth.js +0 -108
- package/dist/hooks/index.d.ts +0 -1
- package/dist/hooks/types.d.ts +0 -19
- package/dist/hooks/useReactor.d.ts +0 -41
- package/dist/provider/actor/context.d.ts +0 -20
- package/dist/provider/actor/index.d.ts +0 -3
- package/dist/provider/actor/index.js +0 -9
- package/dist/provider/actor/types.d.ts +0 -21
- package/dist/provider/actor/types.js +0 -2
- package/dist/provider/agent/context.d.ts +0 -7
- package/dist/provider/agent/context.js +0 -56
- package/dist/provider/agent/hooks.d.ts +0 -12
- package/dist/provider/agent/hooks.js +0 -43
- package/dist/provider/agent/index.d.ts +0 -2
- package/dist/provider/agent/types.d.ts +0 -12
- package/dist/provider/agent/types.js +0 -2
- /package/dist/{hooks → provider/hooks}/types.js +0 -0
package/dist/helpers/types.d.ts
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import type { AuthClientLoginOptions } from "@
|
|
3
|
-
|
|
4
|
-
export interface AgentHooks {
|
|
2
|
+
import type { ActorState, CanisterId, AuthClientLoginOptions, ActorMethodParameters, ActorMethodReturnType, Identity, Principal, FunctionName, VisitService, AuthClient, AuthState, HttpAgent, AgentState, BaseActor } from "@ic-reactor/core/dist/types";
|
|
3
|
+
export interface AgentHooksReturnType {
|
|
5
4
|
useAgent: () => HttpAgent | undefined;
|
|
6
5
|
useAgentState: () => AgentState;
|
|
7
6
|
}
|
|
8
|
-
export interface
|
|
7
|
+
export interface AuthHooksReturnType {
|
|
9
8
|
useUserPrincipal: () => Principal | undefined;
|
|
10
9
|
useAuthState: () => AuthState;
|
|
11
|
-
useAuthClient: (
|
|
10
|
+
useAuthClient: (args?: UseAuthClientParameters) => UseAuthClientReturnType;
|
|
12
11
|
}
|
|
13
|
-
export interface
|
|
12
|
+
export interface ActorHooksReturnType<A = BaseActor> {
|
|
14
13
|
initialize: () => Promise<void>;
|
|
15
14
|
useActorState: () => UseActorState;
|
|
16
15
|
useQueryCall: UseQueryCall<A>;
|
|
17
16
|
useUpdateCall: UseUpdateCall<A>;
|
|
18
|
-
useMethodCall: UseMethodCall<A>;
|
|
19
17
|
useVisitMethod: <M extends FunctionName<A>>(functionName: M) => VisitService<A>[M];
|
|
20
18
|
}
|
|
21
|
-
export interface
|
|
19
|
+
export interface UseAuthClientParameters {
|
|
22
20
|
onAuthentication?: (promise: () => Promise<Identity>) => void;
|
|
23
21
|
onAuthenticationSuccess?: (identity: Identity) => void;
|
|
24
22
|
onAuthenticationFailure?: (error: Error) => void;
|
|
@@ -27,14 +25,14 @@ export interface UseAuthClientArgs {
|
|
|
27
25
|
onLogin?: (promise: () => Promise<Principal>) => void;
|
|
28
26
|
onLoggedOut?: () => void;
|
|
29
27
|
}
|
|
30
|
-
export interface
|
|
28
|
+
export interface UseAuthClientReturnType {
|
|
31
29
|
error: Error | undefined;
|
|
32
30
|
authClient: AuthClient | null;
|
|
33
31
|
authenticated: boolean;
|
|
34
32
|
authenticating: boolean;
|
|
35
33
|
identity: Identity | null;
|
|
36
|
-
login: (options?:
|
|
37
|
-
logout: (options?:
|
|
34
|
+
login: (options?: LoginParameters) => Promise<void>;
|
|
35
|
+
logout: (options?: LogoutParameters) => Promise<void>;
|
|
38
36
|
authenticate: () => Promise<Identity>;
|
|
39
37
|
loginLoading: boolean;
|
|
40
38
|
loginError: Error | null;
|
|
@@ -43,13 +41,13 @@ export type LoginState = {
|
|
|
43
41
|
loading: boolean;
|
|
44
42
|
error: Error | null;
|
|
45
43
|
};
|
|
46
|
-
export type
|
|
47
|
-
export type
|
|
44
|
+
export type LoginParameters = AuthClientLoginOptions;
|
|
45
|
+
export type LogoutParameters = {
|
|
48
46
|
returnTo?: string;
|
|
49
47
|
};
|
|
50
|
-
export type
|
|
48
|
+
export type UseMethodCallParameters<A, M extends FunctionName<A>> = {
|
|
51
49
|
functionName: M;
|
|
52
|
-
args?:
|
|
50
|
+
args?: ActorMethodParameters<A[M]>;
|
|
53
51
|
onLoading?: (loading: boolean) => void;
|
|
54
52
|
onError?: (error: Error | undefined) => void;
|
|
55
53
|
onSuccess?: (data: ActorMethodReturnType<A[M]> | undefined) => void;
|
|
@@ -60,27 +58,19 @@ export type ActorCallState<A, M extends FunctionName<A>> = {
|
|
|
60
58
|
error: Error | undefined;
|
|
61
59
|
loading: boolean;
|
|
62
60
|
};
|
|
63
|
-
export interface
|
|
61
|
+
export interface UseQueryCallParameters<A, M extends FunctionName<A>> extends UseMethodCallParameters<A, M> {
|
|
64
62
|
refetchOnMount?: boolean;
|
|
65
63
|
refetchInterval?: number | false;
|
|
66
64
|
}
|
|
67
|
-
export interface
|
|
65
|
+
export interface UseUpdateCallParameters<A, M extends FunctionName<A>> extends UseMethodCallParameters<A, M> {
|
|
68
66
|
}
|
|
69
|
-
export interface
|
|
67
|
+
export interface UseMethodCallReturnType<A, M extends FunctionName<A> = FunctionName<A>> extends ActorCallState<A, M> {
|
|
70
68
|
reset: () => void;
|
|
71
|
-
call: (eventOrReplaceArgs?: React.MouseEvent |
|
|
69
|
+
call: (eventOrReplaceArgs?: React.MouseEvent | ActorMethodParameters<A[M]>) => Promise<ActorMethodReturnType<A[M]> | undefined>;
|
|
72
70
|
}
|
|
73
|
-
export type
|
|
74
|
-
export type UseQueryCall<A> = <M extends FunctionName<A>>(args:
|
|
75
|
-
export type UseUpdateCall<A> = <M extends FunctionName<A>>(args:
|
|
76
|
-
export type UseMethodCall<A> = <M extends FunctionName<A>>(args: UseMethodCallArg<A, M>) => UseMethodCallReturn<A, M>;
|
|
77
|
-
export interface UseMethodCallReturn<A, M extends FunctionName<A> = FunctionName<A>> extends ReactorCallReturn<A, M> {
|
|
78
|
-
visit: VisitService<A>[M];
|
|
79
|
-
reset: () => void;
|
|
80
|
-
error: Error | undefined;
|
|
81
|
-
loading: boolean;
|
|
82
|
-
}
|
|
83
|
-
export type UseMethodCallArg<A, M extends FunctionName<A>> = ReactorCallArgs<A, M>;
|
|
71
|
+
export type UseMethodCall<A> = <M extends FunctionName<A>>(args: UseMethodCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
|
|
72
|
+
export type UseQueryCall<A> = <M extends FunctionName<A>>(args: UseQueryCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
|
|
73
|
+
export type UseUpdateCall<A> = <M extends FunctionName<A>>(args: UseUpdateCallParameters<A, M>) => UseMethodCallReturnType<A, M>;
|
|
84
74
|
export interface UseActorState extends Omit<ActorState, "methodState"> {
|
|
85
75
|
canisterId: CanisterId;
|
|
86
76
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -28,7 +28,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.core = exports.types = exports.helpers = void 0;
|
|
30
30
|
__exportStar(require("./main"), exports);
|
|
31
|
-
__exportStar(require("./hooks"), exports);
|
|
32
31
|
__exportStar(require("./provider"), exports);
|
|
33
32
|
exports.helpers = __importStar(require("./helpers"));
|
|
34
33
|
exports.types = __importStar(require("./types"));
|
package/dist/main.d.ts
CHANGED
|
@@ -1,2 +1,38 @@
|
|
|
1
|
-
import { BaseActor,
|
|
2
|
-
|
|
1
|
+
import type { BaseActor, CreateReactorParameters, CreateReactorReturnType } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Initializes and configures the reactor environment for interacting with the Internet Computer (IC) blockchain within a React application.
|
|
4
|
+
* It encapsulates the creation of actors, authentication, and agent management, offering a streamlined interface for blockchain interactions.
|
|
5
|
+
*
|
|
6
|
+
* @param config Configuration config for the reactor, including:
|
|
7
|
+
* - withProcessEnv (optional): Specifies whether to use process environment variables to determine if the environment is local or development. Defaults to false.
|
|
8
|
+
* - isLocalEnv (optional): Indicates if the current environment is local or development, influencing the agent and actor behavior. Useful for testing or development.
|
|
9
|
+
* - port (optional): Port number for the local or development environment.
|
|
10
|
+
* Extends `CreateReactorStoreParameters` which includes HTTP agent config, actor manager config (excluding `agentManager`), and an optional custom agent manager.
|
|
11
|
+
*
|
|
12
|
+
* @returns An object containing various hooks and utilities:
|
|
13
|
+
* - getAgent: Function to retrieve the configured IC agent.
|
|
14
|
+
* - getVisitFunction: Function to access the visit function from the actor manager, used for visitor pattern implementations.
|
|
15
|
+
* - Includes actor, auth, and agent hooks for state management, authentication, and agent operations.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { createReactor } from "@ic-reactor/react";
|
|
20
|
+
* import type { CreateReactorCoreParameters } from "@ic-reactor/react/dist/types";
|
|
21
|
+
* import { canisterId, idlFactory, yourActor } from "./declaration/yourActor"
|
|
22
|
+
*
|
|
23
|
+
* const config: CreateReactorCoreParameters = {
|
|
24
|
+
* canisterId,
|
|
25
|
+
* idlFactory,
|
|
26
|
+
* host: "https://localhost:8000", // IC network host |
|
|
27
|
+
* isLocalEnv: true, // Set true for local network | one of these
|
|
28
|
+
* withProcessEnv: true, // Use process.env to determine host |
|
|
29
|
+
* port: 8000, // Port number for local network |
|
|
30
|
+
* };
|
|
31
|
+
*
|
|
32
|
+
* export type YourActor = typeof yourActor;
|
|
33
|
+
*
|
|
34
|
+
* export const { useAuthClient, useQueryCall, useUpdateCall } = createReactor<YourActor>(config);
|
|
35
|
+
* // Now you can use the returned hooks in your React components
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare const createReactor: <A = BaseActor>(config: CreateReactorParameters) => CreateReactorReturnType<A>;
|
package/dist/main.js
CHANGED
|
@@ -1,35 +1,53 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
-
var t = {};
|
|
4
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
-
t[p] = s[p];
|
|
6
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
-
t[p[i]] = s[p[i]];
|
|
10
|
-
}
|
|
11
|
-
return t;
|
|
12
|
-
};
|
|
13
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
3
|
exports.createReactor = void 0;
|
|
15
4
|
const core_1 = require("@ic-reactor/core");
|
|
16
|
-
const actor_1 = require("./helpers/actor");
|
|
17
|
-
const auth_1 = require("./helpers/auth");
|
|
18
|
-
const tools_1 = require("@ic-reactor/core/dist/tools");
|
|
19
5
|
const helpers_1 = require("./helpers");
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Initializes and configures the reactor environment for interacting with the Internet Computer (IC) blockchain within a React application.
|
|
8
|
+
* It encapsulates the creation of actors, authentication, and agent management, offering a streamlined interface for blockchain interactions.
|
|
9
|
+
*
|
|
10
|
+
* @param config Configuration config for the reactor, including:
|
|
11
|
+
* - withProcessEnv (optional): Specifies whether to use process environment variables to determine if the environment is local or development. Defaults to false.
|
|
12
|
+
* - isLocalEnv (optional): Indicates if the current environment is local or development, influencing the agent and actor behavior. Useful for testing or development.
|
|
13
|
+
* - port (optional): Port number for the local or development environment.
|
|
14
|
+
* Extends `CreateReactorStoreParameters` which includes HTTP agent config, actor manager config (excluding `agentManager`), and an optional custom agent manager.
|
|
15
|
+
*
|
|
16
|
+
* @returns An object containing various hooks and utilities:
|
|
17
|
+
* - getAgent: Function to retrieve the configured IC agent.
|
|
18
|
+
* - getVisitFunction: Function to access the visit function from the actor manager, used for visitor pattern implementations.
|
|
19
|
+
* - Includes actor, auth, and agent hooks for state management, authentication, and agent operations.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { createReactor } from "@ic-reactor/react";
|
|
24
|
+
* import type { CreateReactorCoreParameters } from "@ic-reactor/react/dist/types";
|
|
25
|
+
* import { canisterId, idlFactory, yourActor } from "./declaration/yourActor"
|
|
26
|
+
*
|
|
27
|
+
* const config: CreateReactorCoreParameters = {
|
|
28
|
+
* canisterId,
|
|
29
|
+
* idlFactory,
|
|
30
|
+
* host: "https://localhost:8000", // IC network host |
|
|
31
|
+
* isLocalEnv: true, // Set true for local network | one of these
|
|
32
|
+
* withProcessEnv: true, // Use process.env to determine host |
|
|
33
|
+
* port: 8000, // Port number for local network |
|
|
34
|
+
* };
|
|
35
|
+
*
|
|
36
|
+
* export type YourActor = typeof yourActor;
|
|
37
|
+
*
|
|
38
|
+
* export const { useAuthClient, useQueryCall, useUpdateCall } = createReactor<YourActor>(config);
|
|
39
|
+
* // Now you can use the returned hooks in your React components
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
const createReactor = (config) => {
|
|
43
|
+
const actorManager = (0, core_1.createReactorStore)(config);
|
|
23
44
|
const getVisitFunction = () => {
|
|
24
45
|
return actorManager.visitFunction;
|
|
25
46
|
};
|
|
26
47
|
const getAgent = () => {
|
|
27
48
|
return actorManager.agentManager.getAgent();
|
|
28
49
|
};
|
|
29
|
-
const actorHooks = (0, actor_1.getActorHooks)(actorManager);
|
|
30
|
-
const authHooks = (0, auth_1.getAuthHooks)(actorManager.agentManager);
|
|
31
|
-
const agentHooks = (0, helpers_1.getAgentHooks)(actorManager.agentManager);
|
|
32
50
|
return Object.assign(Object.assign(Object.assign({ getAgent,
|
|
33
|
-
getVisitFunction },
|
|
51
|
+
getVisitFunction }, (0, helpers_1.actorHooks)(actorManager)), (0, helpers_1.authHooks)(actorManager.agentManager)), (0, helpers_1.agentHooks)(actorManager.agentManager));
|
|
34
52
|
};
|
|
35
53
|
exports.createReactor = createReactor;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export declare const ActorHooks: import("./types").CreateActorContextReturnType<import("@ic-reactor/core/dist/types").BaseActor>;
|
|
3
|
+
/**
|
|
4
|
+
* `ActorProvider` is a React functional component that serves as a context provider for IC actor interactions within a React application.
|
|
5
|
+
* It wraps child components, providing them access to actor-specific hooks and functionalities based on the provided canister ID and configuration.
|
|
6
|
+
*
|
|
7
|
+
* Props:
|
|
8
|
+
* - `children`: React Node - The child components that will have access to the actor context.
|
|
9
|
+
* - `canisterId` (optional): string - The Canister ID for actor interactions. If not provided, the default from `createActorContext` is used.
|
|
10
|
+
* - `loadingComponent` (optional): React Node - A component displayed during the loading/fetching state. Defaults to a simple message.
|
|
11
|
+
* - `...restConfig`: Additional configuration options that will be merged with the default configuration provided during context creation.
|
|
12
|
+
*
|
|
13
|
+
* Behavior:
|
|
14
|
+
* - Validates the presence of a `canisterId`. Throws an error if it is missing, ensuring that a valid canister ID is always used for actor operations.
|
|
15
|
+
* - Utilizes `useMemo` to combine the default configuration with the props provided to the `ActorProvider`, optimizing for performance by avoiding unnecessary recalculations.
|
|
16
|
+
* - Employs the `useActor` hook to initiate actor interactions based on the combined configuration, managing states such as `fetching`, `fetchError`, and the actor `hooks`.
|
|
17
|
+
* - Conditionally renders the `loadingComponent` or `fetchError` based on the actor fetching state. Once the actor is ready and no errors are present, it renders the child components, effectively providing them access to the actor context.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```jsx
|
|
21
|
+
* <ActorProvider canisterId="your-canister-id">
|
|
22
|
+
* <YourComponent />
|
|
23
|
+
* </ActorProvider>
|
|
24
|
+
* ```
|
|
25
|
+
* This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor through the context provided by `ActorProvider`.
|
|
26
|
+
*/
|
|
27
|
+
export declare const ActorProvider: import("react").FC<import("./types").ActorProviderProps>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ActorProvider = exports.ActorHooks = void 0;
|
|
4
|
+
const actor_1 = require("./actor");
|
|
5
|
+
exports.ActorHooks = (0, actor_1.createActorContext)();
|
|
6
|
+
/**
|
|
7
|
+
* `ActorProvider` is a React functional component that serves as a context provider for IC actor interactions within a React application.
|
|
8
|
+
* It wraps child components, providing them access to actor-specific hooks and functionalities based on the provided canister ID and configuration.
|
|
9
|
+
*
|
|
10
|
+
* Props:
|
|
11
|
+
* - `children`: React Node - The child components that will have access to the actor context.
|
|
12
|
+
* - `canisterId` (optional): string - The Canister ID for actor interactions. If not provided, the default from `createActorContext` is used.
|
|
13
|
+
* - `loadingComponent` (optional): React Node - A component displayed during the loading/fetching state. Defaults to a simple message.
|
|
14
|
+
* - `...restConfig`: Additional configuration options that will be merged with the default configuration provided during context creation.
|
|
15
|
+
*
|
|
16
|
+
* Behavior:
|
|
17
|
+
* - Validates the presence of a `canisterId`. Throws an error if it is missing, ensuring that a valid canister ID is always used for actor operations.
|
|
18
|
+
* - Utilizes `useMemo` to combine the default configuration with the props provided to the `ActorProvider`, optimizing for performance by avoiding unnecessary recalculations.
|
|
19
|
+
* - Employs the `useActor` hook to initiate actor interactions based on the combined configuration, managing states such as `fetching`, `fetchError`, and the actor `hooks`.
|
|
20
|
+
* - Conditionally renders the `loadingComponent` or `fetchError` based on the actor fetching state. Once the actor is ready and no errors are present, it renders the child components, effectively providing them access to the actor context.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```jsx
|
|
24
|
+
* <ActorProvider canisterId="your-canister-id">
|
|
25
|
+
* <YourComponent />
|
|
26
|
+
* </ActorProvider>
|
|
27
|
+
* ```
|
|
28
|
+
* This setup ensures that `YourComponent` and any of its children can interact with the specified IC actor through the context provided by `ActorProvider`.
|
|
29
|
+
*/
|
|
30
|
+
exports.ActorProvider = exports.ActorHooks.ActorProvider;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export declare const AgentHooks: import("./types").CreateAgentContextReturnType;
|
|
3
|
+
/**
|
|
4
|
+
* `AgentProvider` is a React functional component that serves as a context provider for IC agent and authentication hooks.
|
|
5
|
+
* It enables any child components to access and use the agent and authentication functionalities seamlessly.
|
|
6
|
+
*
|
|
7
|
+
* The provider encapsulates the logic for initializing and managing an agent manager instance, which is then used to
|
|
8
|
+
* create various hooks related to agent operations and authentication processes. These hooks are made available to all
|
|
9
|
+
* child components through the context, facilitating a centralized and efficient way to interact with the Internet Computer (IC) blockchain.
|
|
10
|
+
*
|
|
11
|
+
* @param children - Child components that can consume the context.
|
|
12
|
+
* @param agentManager - An optional `AgentManager` instance to be used by the provider. If not provided, a new instance
|
|
13
|
+
* will be created based on the provided options combined with default configuration.
|
|
14
|
+
* @param options - Configuration options for the `AgentManager`. These options are merged with any default configurations
|
|
15
|
+
* specified during the context creation and can include custom settings for the agent, such as identity,
|
|
16
|
+
* host URL, etc.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* Wrap your component tree with `AgentProvider` to provide all child components access to IC agent and authentication hooks.
|
|
20
|
+
*
|
|
21
|
+
* ```jsx
|
|
22
|
+
* <AgentProvider>
|
|
23
|
+
* <YourComponent />
|
|
24
|
+
* </AgentProvider>
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* Inside `YourComponent` or any of its children, you can use the hooks provided through the context to interact with the IC,
|
|
28
|
+
* manage authentication, and perform other agent-related tasks.
|
|
29
|
+
*/
|
|
30
|
+
export declare const AgentProvider: import("react").FC<import("./types").AgentProviderProps>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AgentProvider = exports.AgentHooks = void 0;
|
|
4
|
+
const agent_1 = require("./agent");
|
|
5
|
+
exports.AgentHooks = (0, agent_1.createAgentContext)();
|
|
6
|
+
/**
|
|
7
|
+
* `AgentProvider` is a React functional component that serves as a context provider for IC agent and authentication hooks.
|
|
8
|
+
* It enables any child components to access and use the agent and authentication functionalities seamlessly.
|
|
9
|
+
*
|
|
10
|
+
* The provider encapsulates the logic for initializing and managing an agent manager instance, which is then used to
|
|
11
|
+
* create various hooks related to agent operations and authentication processes. These hooks are made available to all
|
|
12
|
+
* child components through the context, facilitating a centralized and efficient way to interact with the Internet Computer (IC) blockchain.
|
|
13
|
+
*
|
|
14
|
+
* @param children - Child components that can consume the context.
|
|
15
|
+
* @param agentManager - An optional `AgentManager` instance to be used by the provider. If not provided, a new instance
|
|
16
|
+
* will be created based on the provided options combined with default configuration.
|
|
17
|
+
* @param options - Configuration options for the `AgentManager`. These options are merged with any default configurations
|
|
18
|
+
* specified during the context creation and can include custom settings for the agent, such as identity,
|
|
19
|
+
* host URL, etc.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* Wrap your component tree with `AgentProvider` to provide all child components access to IC agent and authentication hooks.
|
|
23
|
+
*
|
|
24
|
+
* ```jsx
|
|
25
|
+
* <AgentProvider>
|
|
26
|
+
* <YourComponent />
|
|
27
|
+
* </AgentProvider>
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* Inside `YourComponent` or any of its children, you can use the hooks provided through the context to interact with the IC,
|
|
31
|
+
* manage authentication, and perform other agent-related tasks.
|
|
32
|
+
*/
|
|
33
|
+
exports.AgentProvider = exports.AgentHooks.AgentProvider;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { BaseActor } from "../types";
|
|
2
|
+
import { CreateActorContextParameters, CreateActorContextReturnType } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a React context specifically designed for managing the state and interactions with an actor on the Internet Computer (IC) blockchain.
|
|
5
|
+
* This context facilitates the dynamic creation and management of IC actors within React applications, leveraging the provided configuration options.
|
|
6
|
+
*
|
|
7
|
+
* @param reactorParameters A partial configuration object for the actor context, allowing customization and specification of actor-related settings.
|
|
8
|
+
* - `canisterId`: The default Canister ID to be used if not overridden in the `ActorProvider` component.
|
|
9
|
+
* - Other configurations can include properties related to the actor's interaction, such as agent options or authentication requirements.
|
|
10
|
+
*
|
|
11
|
+
* @returns An object containing the `ActorProvider` component and various hooks for interacting with the actor.
|
|
12
|
+
* - `ActorProvider`: A context provider component that allows child components to access and interact with the configured actor.
|
|
13
|
+
* - Hooks: Custom hooks derived from the actor context, facilitating interactions like querying or updating the actor's state.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```jsx
|
|
17
|
+
* import React from 'react';
|
|
18
|
+
* import { createActorContext } from '@ic-reactor/react';
|
|
19
|
+
* import { backend, canisterId, idlFactory } from './declarations/candid'; // Assuming 'declarations/candid' is where your actor interface is defined.
|
|
20
|
+
*
|
|
21
|
+
* // Initialize the actor context with configuration options
|
|
22
|
+
* const { ActorProvider, useActorState, useQueryCall, useUpdateCall } = createActorContext<typeof backend>({
|
|
23
|
+
* canisterId,
|
|
24
|
+
* idlFactory, // Optional
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // A sample component that utilizes the actor context
|
|
28
|
+
* const App = () => (
|
|
29
|
+
* <AgentProvider>
|
|
30
|
+
* <ActorProvider>
|
|
31
|
+
* <div>
|
|
32
|
+
* <h1>IC Actor Interaction Example</h1>
|
|
33
|
+
* <ActorComponent />
|
|
34
|
+
* </div>
|
|
35
|
+
* </ActorProvider>
|
|
36
|
+
* </AgentProvider>
|
|
37
|
+
* );
|
|
38
|
+
*
|
|
39
|
+
* export default App;
|
|
40
|
+
*
|
|
41
|
+
* // A sample component that uses the actor hooks
|
|
42
|
+
* const ActorComponent = () => {
|
|
43
|
+
* const { data, loading, error } = useQueryCall({
|
|
44
|
+
* functionName: 'backendMethodName',
|
|
45
|
+
* args: [],
|
|
46
|
+
* refetchInterval: 10000,
|
|
47
|
+
* refetchOnMount: true,
|
|
48
|
+
* });
|
|
49
|
+
|
|
50
|
+
* return (
|
|
51
|
+
* <div>
|
|
52
|
+
* {loading && <p>Loading...</p>}
|
|
53
|
+
* {error && <p>Error: {error.message}</p>}
|
|
54
|
+
* {data && <p>Actor data: {data}</p>}
|
|
55
|
+
* </div>
|
|
56
|
+
* );
|
|
57
|
+
* };
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* This function streamlines the process of setting up a context for IC actor interactions within a React app, making it easier
|
|
61
|
+
* to manage actor state and perform actions such as queries or updates. It abstracts away the complexities involved in directly
|
|
62
|
+
* managing IC agents and actors, providing a simple, declarative API for developers.
|
|
63
|
+
*/
|
|
64
|
+
export declare function createActorContext<A = BaseActor>(config?: CreateActorContextParameters): CreateActorContextReturnType<A>;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
26
|
+
var t = {};
|
|
27
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
28
|
+
t[p] = s[p];
|
|
29
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
30
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
31
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
32
|
+
t[p[i]] = s[p[i]];
|
|
33
|
+
}
|
|
34
|
+
return t;
|
|
35
|
+
};
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.createActorContext = void 0;
|
|
38
|
+
const react_1 = __importStar(require("react"));
|
|
39
|
+
const useActor_1 = require("./hooks/useActor");
|
|
40
|
+
const extractActorContext_1 = require("../helpers/extractActorContext");
|
|
41
|
+
/**
|
|
42
|
+
* Creates a React context specifically designed for managing the state and interactions with an actor on the Internet Computer (IC) blockchain.
|
|
43
|
+
* This context facilitates the dynamic creation and management of IC actors within React applications, leveraging the provided configuration options.
|
|
44
|
+
*
|
|
45
|
+
* @param reactorParameters A partial configuration object for the actor context, allowing customization and specification of actor-related settings.
|
|
46
|
+
* - `canisterId`: The default Canister ID to be used if not overridden in the `ActorProvider` component.
|
|
47
|
+
* - Other configurations can include properties related to the actor's interaction, such as agent options or authentication requirements.
|
|
48
|
+
*
|
|
49
|
+
* @returns An object containing the `ActorProvider` component and various hooks for interacting with the actor.
|
|
50
|
+
* - `ActorProvider`: A context provider component that allows child components to access and interact with the configured actor.
|
|
51
|
+
* - Hooks: Custom hooks derived from the actor context, facilitating interactions like querying or updating the actor's state.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```jsx
|
|
55
|
+
* import React from 'react';
|
|
56
|
+
* import { createActorContext } from '@ic-reactor/react';
|
|
57
|
+
* import { backend, canisterId, idlFactory } from './declarations/candid'; // Assuming 'declarations/candid' is where your actor interface is defined.
|
|
58
|
+
*
|
|
59
|
+
* // Initialize the actor context with configuration options
|
|
60
|
+
* const { ActorProvider, useActorState, useQueryCall, useUpdateCall } = createActorContext<typeof backend>({
|
|
61
|
+
* canisterId,
|
|
62
|
+
* idlFactory, // Optional
|
|
63
|
+
* });
|
|
64
|
+
*
|
|
65
|
+
* // A sample component that utilizes the actor context
|
|
66
|
+
* const App = () => (
|
|
67
|
+
* <AgentProvider>
|
|
68
|
+
* <ActorProvider>
|
|
69
|
+
* <div>
|
|
70
|
+
* <h1>IC Actor Interaction Example</h1>
|
|
71
|
+
* <ActorComponent />
|
|
72
|
+
* </div>
|
|
73
|
+
* </ActorProvider>
|
|
74
|
+
* </AgentProvider>
|
|
75
|
+
* );
|
|
76
|
+
*
|
|
77
|
+
* export default App;
|
|
78
|
+
*
|
|
79
|
+
* // A sample component that uses the actor hooks
|
|
80
|
+
* const ActorComponent = () => {
|
|
81
|
+
* const { data, loading, error } = useQueryCall({
|
|
82
|
+
* functionName: 'backendMethodName',
|
|
83
|
+
* args: [],
|
|
84
|
+
* refetchInterval: 10000,
|
|
85
|
+
* refetchOnMount: true,
|
|
86
|
+
* });
|
|
87
|
+
|
|
88
|
+
* return (
|
|
89
|
+
* <div>
|
|
90
|
+
* {loading && <p>Loading...</p>}
|
|
91
|
+
* {error && <p>Error: {error.message}</p>}
|
|
92
|
+
* {data && <p>Actor data: {data}</p>}
|
|
93
|
+
* </div>
|
|
94
|
+
* );
|
|
95
|
+
* };
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* This function streamlines the process of setting up a context for IC actor interactions within a React app, making it easier
|
|
99
|
+
* to manage actor state and perform actions such as queries or updates. It abstracts away the complexities involved in directly
|
|
100
|
+
* managing IC agents and actors, providing a simple, declarative API for developers.
|
|
101
|
+
*/
|
|
102
|
+
function createActorContext(config = {}) {
|
|
103
|
+
const { canisterId: defaultCanisterId } = config, defaultConfig = __rest(config, ["canisterId"]);
|
|
104
|
+
const ActorContext = (0, react_1.createContext)(null);
|
|
105
|
+
const ActorProvider = (_a) => {
|
|
106
|
+
var { children, canisterId = defaultCanisterId, loadingComponent = react_1.default.createElement("div", null, "Fetching canister...") } = _a, restConfig = __rest(_a, ["children", "canisterId", "loadingComponent"]);
|
|
107
|
+
if (!canisterId) {
|
|
108
|
+
throw new Error("canisterId is required");
|
|
109
|
+
}
|
|
110
|
+
const config = (0, react_1.useMemo)(() => (Object.assign(Object.assign({}, defaultConfig), restConfig)), [defaultConfig, restConfig]);
|
|
111
|
+
const { fetchError, fetching, hooks } = (0, useActor_1.useActor)(Object.assign({ canisterId }, config));
|
|
112
|
+
return (react_1.default.createElement(ActorContext.Provider, { value: hooks }, fetching || hooks === null ? fetchError !== null && fetchError !== void 0 ? fetchError : loadingComponent : children));
|
|
113
|
+
};
|
|
114
|
+
ActorProvider.displayName = "ActorProvider";
|
|
115
|
+
return Object.assign({ ActorProvider }, (0, extractActorContext_1.extractActorContext)(ActorContext));
|
|
116
|
+
}
|
|
117
|
+
exports.createActorContext = createActorContext;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { AgentManagerParameters } from "../types";
|
|
2
|
+
import type { CreateAgentContextReturnType } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a React context for managing IC agent and authentication states, providing hooks for interacting with the IC blockchain.
|
|
5
|
+
* This function initializes an `AgentContext` with a set of utilities and hooks based on the provided agent configuration.
|
|
6
|
+
*
|
|
7
|
+
* @param agentParameters A partial configuration object for the agent manager, allowing customization of the agent's behavior.
|
|
8
|
+
*
|
|
9
|
+
* @returns An object containing the `AgentProvider` component and various hooks for interacting with the agent and authentication state.
|
|
10
|
+
* The `AgentProvider` component is a React context provider that should wrap your app or components needing access to agent functionalities.
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* - `AgentProvider`: React component to provide agent context to your application.
|
|
14
|
+
* - `useAgent`, `useAuthClient`, `useAuthState`, `useAgentState`, `useAgentManager`, `useUserPrincipal`: Hooks extracted from the created context for managing agent and authentication state within components.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* // agent.ts
|
|
19
|
+
* import { createAgentContext } from "@ic-reactor/react";
|
|
20
|
+
* import { CreateAgentParameters } from "@ic-reactor/react/dist/types";
|
|
21
|
+
*
|
|
22
|
+
* // Optional: Define custom agent configuration
|
|
23
|
+
* const agentConfig: CreateAgentParameters = {
|
|
24
|
+
* host: "https://localhost:8000",
|
|
25
|
+
* // or
|
|
26
|
+
* // isLocalEnv: true,
|
|
27
|
+
* // port: 8000,
|
|
28
|
+
* };
|
|
29
|
+
*
|
|
30
|
+
* export const {
|
|
31
|
+
* AgentProvider,
|
|
32
|
+
* useAgent,
|
|
33
|
+
* useAuthClient,
|
|
34
|
+
* useAuthState,
|
|
35
|
+
* useAgentState,
|
|
36
|
+
* useAgentManager,
|
|
37
|
+
* useUserPrincipal,
|
|
38
|
+
* } = createAgentContext(agentConfig);
|
|
39
|
+
*
|
|
40
|
+
* // Now you can use the returned hooks in your React components
|
|
41
|
+
*
|
|
42
|
+
* // App.tsx
|
|
43
|
+
* import React from 'react';
|
|
44
|
+
* import { AgentProvider } from './agent';
|
|
45
|
+
*
|
|
46
|
+
* const App = () => (
|
|
47
|
+
* <AgentProvider>
|
|
48
|
+
* <Login />
|
|
49
|
+
* <YourActor />
|
|
50
|
+
* </AgentProvider>
|
|
51
|
+
* );
|
|
52
|
+
*
|
|
53
|
+
* const Login = () => {
|
|
54
|
+
* const { login } = useAuthClient()
|
|
55
|
+
* const principal = useUserPrincipal()
|
|
56
|
+
*
|
|
57
|
+
* return (
|
|
58
|
+
* <div>
|
|
59
|
+
* <button onClick={() => login()}>Login</button>
|
|
60
|
+
* <p>User: {principal?.toText()}</p>
|
|
61
|
+
* </div>
|
|
62
|
+
* )
|
|
63
|
+
* };
|
|
64
|
+
*
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* This setup allows you to use the agent and authentication hooks within
|
|
68
|
+
* the components wrapped by `AgentProvider`, facilitating interaction
|
|
69
|
+
* with the Internet Computer blockchain.
|
|
70
|
+
*/
|
|
71
|
+
export declare const createAgentContext: (config?: AgentManagerParameters) => CreateAgentContextReturnType;
|