@ic-reactor/react 1.0.4 → 1.0.6
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 +13 -23
- 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 +123 -0
- package/dist/helpers/extractActorContext.d.ts +3 -0
- package/dist/helpers/extractActorContext.js +51 -0
- package/dist/helpers/extractAgentContext.d.ts +27 -0
- package/dist/helpers/extractAgentContext.js +56 -0
- package/dist/helpers/index.d.ts +5 -3
- package/dist/helpers/index.js +5 -3
- package/dist/helpers/types.d.ts +30 -40
- 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 +26 -0
- package/dist/provider/ActorProvider.js +29 -0
- package/dist/provider/AgentProvider.d.ts +29 -0
- package/dist/provider/AgentProvider.js +32 -0
- package/dist/provider/actorHooks.d.ts +1 -0
- package/dist/provider/actorHooks.js +5 -0
- package/dist/provider/agentHooks.d.ts +1 -0
- package/dist/provider/agentHooks.js +5 -0
- package/dist/provider/context/actor.d.ts +64 -0
- package/dist/provider/context/actor.js +117 -0
- package/dist/provider/context/agent.d.ts +71 -0
- package/dist/provider/context/agent.js +124 -0
- package/dist/provider/hooks/actor/index.d.ts +4 -0
- package/dist/provider/hooks/actor/index.js +20 -0
- package/dist/provider/hooks/actor/useActorState.d.ts +21 -0
- package/dist/provider/hooks/actor/useActorState.js +25 -0
- package/dist/provider/hooks/actor/useQueryCall.d.ts +28 -0
- package/dist/provider/hooks/actor/useQueryCall.js +34 -0
- package/dist/provider/hooks/actor/useUpdateCall.d.ts +29 -0
- package/dist/provider/hooks/actor/useUpdateCall.js +35 -0
- package/dist/provider/hooks/actor/useVisitMethod.d.ts +8 -0
- package/dist/provider/hooks/actor/useVisitMethod.js +14 -0
- package/dist/provider/hooks/agent/index.d.ts +6 -0
- package/dist/provider/hooks/agent/index.js +22 -0
- package/dist/provider/hooks/agent/useAgent.d.ts +14 -0
- package/dist/provider/hooks/agent/useAgent.js +18 -0
- package/dist/provider/hooks/agent/useAgentManager.d.ts +15 -0
- package/dist/provider/hooks/agent/useAgentManager.js +18 -0
- package/dist/provider/hooks/agent/useAgentState.d.ts +21 -0
- package/dist/provider/hooks/agent/useAgentState.js +25 -0
- package/dist/provider/hooks/agent/useAuth.d.ts +57 -0
- package/dist/provider/hooks/agent/useAuth.js +61 -0
- package/dist/provider/hooks/agent/useAuthState.d.ts +19 -0
- package/dist/provider/hooks/agent/useAuthState.js +23 -0
- package/dist/provider/hooks/agent/useUserPrincipal.d.ts +17 -0
- package/dist/provider/hooks/agent/useUserPrincipal.js +21 -0
- package/dist/provider/hooks/index.d.ts +3 -0
- package/dist/provider/{agent → hooks}/index.js +3 -2
- package/dist/provider/hooks/types.d.ts +15 -0
- package/dist/provider/hooks/useActor.d.ts +65 -0
- package/dist/{hooks/useReactor.js → provider/hooks/useActor.js} +53 -29
- package/dist/provider/index.d.ts +5 -2
- package/dist/provider/index.js +8 -2
- package/dist/provider/types.d.ts +27 -0
- package/dist/{hooks/index.js → provider/types.js} +1 -1
- 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/context.js +0 -184
- 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
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { BaseActor } from "../../types";
|
|
2
|
+
import type { UseActorParameters, UseActorReturn } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* A comprehensive hook that manages both the fetching of Candid interfaces
|
|
5
|
+
* and the initialization of actor stores for Internet Computer (IC) canisters.
|
|
6
|
+
* It simplifies the process of interacting with canisters by encapsulating
|
|
7
|
+
* the logic for Candid retrieval and actor store management.
|
|
8
|
+
*
|
|
9
|
+
* You can use react context to share the actor hooks across your application.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* import { AgentProvider, extractActorHooks, useActor } from "@ic-reactor/react"
|
|
14
|
+
* import { createContext } from "react"
|
|
15
|
+
* import type { ActorHooks } from "@ic-reactor/react/dist/types"
|
|
16
|
+
* // With this import, you can have type safety for the actor's interface.
|
|
17
|
+
* // You can get it from the `.did.d.ts` file generated by the DFX tool.
|
|
18
|
+
* // or from dashboard https://dashboard.internetcomputer.org/canisters/<canister-id>
|
|
19
|
+
* import type { Ledger } from "../declarations/ledger"
|
|
20
|
+
*
|
|
21
|
+
* const ActorContext = createContext<ActorHooks<Ledger> | null>(null)
|
|
22
|
+
*
|
|
23
|
+
* export const { useQueryCall, useUpdateCall } = extractActorHooks(ActorContext)
|
|
24
|
+
*
|
|
25
|
+
* const LedgerActor = ({ children }) => {
|
|
26
|
+
* const { hooks, fetching, fetchError } = useActor<Ledger>({
|
|
27
|
+
* canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai", // ICP Ledger canister
|
|
28
|
+
* })
|
|
29
|
+
*
|
|
30
|
+
* return (
|
|
31
|
+
* <ActorContext.Provider value={hooks}>
|
|
32
|
+
* <h2>IC Canister Interaction</h2>
|
|
33
|
+
* {fetching && <p>Loading Candid interface...</p>}
|
|
34
|
+
* {fetchError && <p>Error: {fetchError}</p>}
|
|
35
|
+
* {hooks && children}
|
|
36
|
+
* </ActorContext.Provider>
|
|
37
|
+
* )
|
|
38
|
+
* }
|
|
39
|
+
* // later in the code
|
|
40
|
+
* const CanisterName = () => {
|
|
41
|
+
* const { data } = useQueryCall({
|
|
42
|
+
* functionName: "name",
|
|
43
|
+
* })
|
|
44
|
+
*
|
|
45
|
+
* return (
|
|
46
|
+
* <div>
|
|
47
|
+
* <h3>Query Call</h3>
|
|
48
|
+
* <p>Result: {JSON.stringify(data)}</p>
|
|
49
|
+
* </div>
|
|
50
|
+
* )
|
|
51
|
+
* }
|
|
52
|
+
*
|
|
53
|
+
* const App = () => (
|
|
54
|
+
* <AgentProvider withDevtools>
|
|
55
|
+
* <LedgerActor>
|
|
56
|
+
* <CanisterName />
|
|
57
|
+
* </LedgerActor>
|
|
58
|
+
* </AgentProvider>
|
|
59
|
+
* )
|
|
60
|
+
*
|
|
61
|
+
* export default App
|
|
62
|
+
*
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare const useActor: <A = BaseActor>(config: UseActorParameters) => UseActorReturn<A>;
|
|
@@ -20,57 +20,81 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
20
20
|
return t;
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.
|
|
23
|
+
exports.useActor = void 0;
|
|
24
24
|
const core_1 = require("@ic-reactor/core");
|
|
25
25
|
const react_1 = require("react");
|
|
26
|
-
const
|
|
27
|
-
const helpers_1 = require("
|
|
26
|
+
const useAgentManager_1 = require("./agent/useAgentManager");
|
|
27
|
+
const helpers_1 = require("../../helpers");
|
|
28
28
|
/**
|
|
29
29
|
* A comprehensive hook that manages both the fetching of Candid interfaces
|
|
30
30
|
* and the initialization of actor stores for Internet Computer (IC) canisters.
|
|
31
31
|
* It simplifies the process of interacting with canisters by encapsulating
|
|
32
32
|
* the logic for Candid retrieval and actor store management.
|
|
33
33
|
*
|
|
34
|
+
* You can use react context to share the actor hooks across your application.
|
|
35
|
+
*
|
|
34
36
|
* @example
|
|
35
37
|
* ```tsx
|
|
36
|
-
* import
|
|
37
|
-
* import {
|
|
38
|
-
* import {
|
|
38
|
+
* import { AgentProvider, extractActorHooks, useActor } from "@ic-reactor/react"
|
|
39
|
+
* import { createContext } from "react"
|
|
40
|
+
* import type { ActorHooks } from "@ic-reactor/react/dist/types"
|
|
41
|
+
* // With this import, you can have type safety for the actor's interface.
|
|
42
|
+
* // You can get it from the `.did.d.ts` file generated by the DFX tool.
|
|
43
|
+
* // or from dashboard https://dashboard.internetcomputer.org/canisters/<canister-id>
|
|
44
|
+
* import type { Ledger } from "../declarations/ledger"
|
|
45
|
+
*
|
|
46
|
+
* const ActorContext = createContext<ActorHooks<Ledger> | null>(null)
|
|
47
|
+
*
|
|
48
|
+
* export const { useQueryCall, useUpdateCall } = extractActorHooks(ActorContext)
|
|
39
49
|
*
|
|
40
|
-
* const
|
|
41
|
-
* const {
|
|
42
|
-
* canisterId:
|
|
43
|
-
* })
|
|
50
|
+
* const LedgerActor = ({ children }) => {
|
|
51
|
+
* const { hooks, fetching, fetchError } = useActor<Ledger>({
|
|
52
|
+
* canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai", // ICP Ledger canister
|
|
53
|
+
* })
|
|
44
54
|
*
|
|
45
55
|
* return (
|
|
46
|
-
* <
|
|
56
|
+
* <ActorContext.Provider value={hooks}>
|
|
47
57
|
* <h2>IC Canister Interaction</h2>
|
|
48
58
|
* {fetching && <p>Loading Candid interface...</p>}
|
|
49
59
|
* {fetchError && <p>Error: {fetchError}</p>}
|
|
50
|
-
* {
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
60
|
+
* {hooks && children}
|
|
61
|
+
* </ActorContext.Provider>
|
|
62
|
+
* )
|
|
63
|
+
* }
|
|
64
|
+
* // later in the code
|
|
65
|
+
* const CanisterName = () => {
|
|
66
|
+
* const { data } = useQueryCall({
|
|
67
|
+
* functionName: "name",
|
|
68
|
+
* })
|
|
69
|
+
*
|
|
70
|
+
* return (
|
|
71
|
+
* <div>
|
|
72
|
+
* <h3>Query Call</h3>
|
|
73
|
+
* <p>Result: {JSON.stringify(data)}</p>
|
|
59
74
|
* </div>
|
|
60
|
-
* )
|
|
61
|
-
* }
|
|
75
|
+
* )
|
|
76
|
+
* }
|
|
77
|
+
*
|
|
78
|
+
* const App = () => (
|
|
79
|
+
* <AgentProvider withDevtools>
|
|
80
|
+
* <LedgerActor>
|
|
81
|
+
* <CanisterName />
|
|
82
|
+
* </LedgerActor>
|
|
83
|
+
* </AgentProvider>
|
|
84
|
+
* )
|
|
85
|
+
*
|
|
86
|
+
* export default App
|
|
62
87
|
*
|
|
63
|
-
* export default App;
|
|
64
88
|
* ```
|
|
65
89
|
*/
|
|
66
|
-
const
|
|
67
|
-
|
|
90
|
+
const useActor = (config) => {
|
|
91
|
+
const { canisterId, idlFactory: maybeIdlFactory, agentContext, didjsCanisterId } = config, actorConfig = __rest(config, ["canisterId", "idlFactory", "agentContext", "didjsCanisterId"]);
|
|
68
92
|
const [{ idlFactory, fetching, fetchError }, setState] = (0, react_1.useState)({
|
|
69
93
|
idlFactory: maybeIdlFactory,
|
|
70
94
|
fetching: false,
|
|
71
95
|
fetchError: null,
|
|
72
96
|
});
|
|
73
|
-
const agentManager = (0,
|
|
97
|
+
const agentManager = (0, useAgentManager_1.useAgentManager)(agentContext);
|
|
74
98
|
const fetchCandid = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
75
99
|
if (!canisterId)
|
|
76
100
|
return;
|
|
@@ -113,9 +137,9 @@ const useReactor = (_a) => {
|
|
|
113
137
|
return null;
|
|
114
138
|
const actorManager = (0, core_1.createActorManager)(Object.assign({ agentManager,
|
|
115
139
|
idlFactory,
|
|
116
|
-
canisterId },
|
|
117
|
-
return (0, helpers_1.
|
|
140
|
+
canisterId }, actorConfig));
|
|
141
|
+
return (0, helpers_1.actorHooks)(actorManager);
|
|
118
142
|
}, [idlFactory]);
|
|
119
143
|
return { hooks, fetching, fetchError };
|
|
120
144
|
};
|
|
121
|
-
exports.
|
|
145
|
+
exports.useActor = useActor;
|
package/dist/provider/index.d.ts
CHANGED
package/dist/provider/index.js
CHANGED
|
@@ -14,5 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
exports.createAgentContext = exports.createActorContext = void 0;
|
|
18
|
+
var actor_1 = require("./context/actor");
|
|
19
|
+
Object.defineProperty(exports, "createActorContext", { enumerable: true, get: function () { return actor_1.createActorContext; } });
|
|
20
|
+
var agent_1 = require("./context/agent");
|
|
21
|
+
Object.defineProperty(exports, "createAgentContext", { enumerable: true, get: function () { return agent_1.createAgentContext; } });
|
|
22
|
+
__exportStar(require("./hooks"), exports);
|
|
23
|
+
__exportStar(require("./ActorProvider"), exports);
|
|
24
|
+
__exportStar(require("./AgentProvider"), exports);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { IDL, ActorHooksReturnType, AgentHooksReturnType, AuthHooksReturnType, BaseActor, AgentManager, ActorManagerParameters, AgentManagerParameters } from "../types";
|
|
2
|
+
import type { PropsWithChildren } from "react";
|
|
3
|
+
export * from "./hooks/types";
|
|
4
|
+
export interface AgentContext extends AgentHooksReturnType, AuthHooksReturnType {
|
|
5
|
+
agentManager: AgentManager;
|
|
6
|
+
}
|
|
7
|
+
export interface CreateAgentContextReturnType extends AgentHooksReturnType, AuthHooksReturnType {
|
|
8
|
+
useAgentManager: (agentContext?: React.Context<AgentContext | null>) => AgentManager;
|
|
9
|
+
AgentProvider: React.FC<AgentProviderProps>;
|
|
10
|
+
}
|
|
11
|
+
export interface AgentProviderProps extends PropsWithChildren, AgentManagerParameters {
|
|
12
|
+
agentManager?: AgentManager;
|
|
13
|
+
}
|
|
14
|
+
export interface CreateActorContextReturnType<A = BaseActor> extends ActorHooksReturnType<A> {
|
|
15
|
+
ActorProvider: React.FC<ActorProviderProps>;
|
|
16
|
+
}
|
|
17
|
+
export interface ActorProviderProps extends CreateActorContextParameters {
|
|
18
|
+
children?: React.ReactNode | undefined;
|
|
19
|
+
loadingComponent?: React.ReactNode;
|
|
20
|
+
}
|
|
21
|
+
export interface CreateActorContextParameters extends Omit<ActorManagerParameters, "idlFactory" | "agentManager" | "canisterId"> {
|
|
22
|
+
didjsId?: string;
|
|
23
|
+
canisterId?: string;
|
|
24
|
+
agentContext?: React.Context<AgentContext | null>;
|
|
25
|
+
idlFactory?: IDL.InterfaceFactory;
|
|
26
|
+
loadingComponent?: React.ReactNode;
|
|
27
|
+
}
|
|
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./hooks/types"), exports);
|
package/dist/types.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { HttpAgent, VisitService } from "@ic-reactor/core/dist/types";
|
|
2
|
-
import type {
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
export
|
|
6
|
-
export * from "./hooks/types";
|
|
7
|
-
export * from "./helpers/types";
|
|
8
|
-
export interface CreateReactorReturn<A> extends ActorHooks<A>, AuthHooks, AgentHooks {
|
|
1
|
+
import type { HttpAgent, CreateReactorCoreParameters, VisitService } from "@ic-reactor/core/dist/types";
|
|
2
|
+
import type { ActorHooksReturnType, AgentHooksReturnType, AuthHooksReturnType } from "./helpers/types";
|
|
3
|
+
export interface CreateReactorParameters extends CreateReactorCoreParameters {
|
|
4
|
+
}
|
|
5
|
+
export interface CreateReactorReturnType<A> extends ActorHooksReturnType<A>, AuthHooksReturnType, AgentHooksReturnType {
|
|
9
6
|
getAgent: () => HttpAgent;
|
|
10
7
|
getVisitFunction: () => VisitService<A>;
|
|
11
8
|
}
|
|
9
|
+
export * from "./provider/types";
|
|
10
|
+
export * from "./helpers/types";
|
|
11
|
+
export * from "@ic-reactor/core/dist/types";
|
package/dist/types.js
CHANGED
|
@@ -14,8 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("
|
|
18
|
-
__exportStar(require("./provider/agent/types"), exports);
|
|
19
|
-
__exportStar(require("./provider/actor/types"), exports);
|
|
20
|
-
__exportStar(require("./hooks/types"), exports);
|
|
17
|
+
__exportStar(require("./provider/types"), exports);
|
|
21
18
|
__exportStar(require("./helpers/types"), exports);
|
|
19
|
+
__exportStar(require("@ic-reactor/core/dist/types"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/react",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "A React library for interacting with Internet Computer canisters",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"node": ">=10"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@ic-reactor/core": "^1.0.
|
|
38
|
+
"@ic-reactor/core": "^1.0.5",
|
|
39
39
|
"zustand-utils": "^1.3"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"react": ">=16.8",
|
|
49
49
|
"zustand": "4.5"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "c52ec12bc91d5a0e2c5fc7c6e163530a1aa5e47f"
|
|
52
52
|
}
|
package/dist/helpers/agent.d.ts
DELETED
package/dist/helpers/auth.d.ts
DELETED
package/dist/helpers/auth.js
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.getAuthHooks = void 0;
|
|
13
|
-
const zustand_1 = require("zustand");
|
|
14
|
-
const react_1 = require("react");
|
|
15
|
-
const tools_1 = require("@ic-reactor/core/dist/tools");
|
|
16
|
-
const getAuthHooks = (agentManager) => {
|
|
17
|
-
const { authenticate: authenticator, authStore, isLocalEnv } = agentManager;
|
|
18
|
-
const useAuthState = () => (0, zustand_1.useStore)(authStore);
|
|
19
|
-
const useUserPrincipal = () => { var _a, _b; return (_b = (_a = useAuthState()) === null || _a === void 0 ? void 0 : _a.identity) === null || _b === void 0 ? void 0 : _b.getPrincipal(); };
|
|
20
|
-
const useAuthClient = (events) => {
|
|
21
|
-
const [loginState, setLoginState] = (0, react_1.useState)({
|
|
22
|
-
loading: false,
|
|
23
|
-
error: null,
|
|
24
|
-
});
|
|
25
|
-
const { authClient, authenticated, authenticating, identity, error } = useAuthState();
|
|
26
|
-
const authenticate = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
|
-
var _a, _b;
|
|
28
|
-
try {
|
|
29
|
-
const identity = yield authenticator();
|
|
30
|
-
(_a = events === null || events === void 0 ? void 0 : events.onAuthenticationSuccess) === null || _a === void 0 ? void 0 : _a.call(events, identity);
|
|
31
|
-
return identity;
|
|
32
|
-
}
|
|
33
|
-
catch (e) {
|
|
34
|
-
(_b = events === null || events === void 0 ? void 0 : events.onAuthenticationFailure) === null || _b === void 0 ? void 0 : _b.call(events, e);
|
|
35
|
-
throw e;
|
|
36
|
-
}
|
|
37
|
-
}), [authenticator, events]);
|
|
38
|
-
const login = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
39
|
-
var _c;
|
|
40
|
-
if (!authClient) {
|
|
41
|
-
throw new Error("Auth client not initialized");
|
|
42
|
-
}
|
|
43
|
-
try {
|
|
44
|
-
setLoginState({ loading: true, error: null });
|
|
45
|
-
yield authClient.login(Object.assign(Object.assign({ identityProvider: isLocalEnv
|
|
46
|
-
? tools_1.LOCAL_INTERNET_IDENTITY_PROVIDER
|
|
47
|
-
: tools_1.IC_INTERNET_IDENTITY_PROVIDER }, options), { onSuccess: () => __awaiter(void 0, void 0, void 0, function* () {
|
|
48
|
-
var _d, _e, _f;
|
|
49
|
-
try {
|
|
50
|
-
const identity = yield authenticate();
|
|
51
|
-
const principal = identity.getPrincipal();
|
|
52
|
-
(_d = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _d === void 0 ? void 0 : _d.call(options);
|
|
53
|
-
(_e = events === null || events === void 0 ? void 0 : events.onLoginSuccess) === null || _e === void 0 ? void 0 : _e.call(events, principal);
|
|
54
|
-
setLoginState({ loading: false, error: null });
|
|
55
|
-
}
|
|
56
|
-
catch (e) {
|
|
57
|
-
const error = e;
|
|
58
|
-
setLoginState({ loading: false, error });
|
|
59
|
-
(_f = events === null || events === void 0 ? void 0 : events.onLoginError) === null || _f === void 0 ? void 0 : _f.call(events, error);
|
|
60
|
-
}
|
|
61
|
-
}), onError: (e) => {
|
|
62
|
-
var _a;
|
|
63
|
-
const error = new Error(`Login failed: ${e}`);
|
|
64
|
-
setLoginState({ loading: false, error });
|
|
65
|
-
(_a = events === null || events === void 0 ? void 0 : events.onLoginError) === null || _a === void 0 ? void 0 : _a.call(events, error);
|
|
66
|
-
} }));
|
|
67
|
-
}
|
|
68
|
-
catch (e) {
|
|
69
|
-
const error = e;
|
|
70
|
-
setLoginState({ loading: false, error });
|
|
71
|
-
(_c = events === null || events === void 0 ? void 0 : events.onLoginError) === null || _c === void 0 ? void 0 : _c.call(events, error);
|
|
72
|
-
}
|
|
73
|
-
}), [authClient, authenticate, isLocalEnv, events]);
|
|
74
|
-
const logout = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
75
|
-
var _g;
|
|
76
|
-
if (!authClient) {
|
|
77
|
-
throw new Error("Auth client not initialized");
|
|
78
|
-
}
|
|
79
|
-
yield authClient.logout(options);
|
|
80
|
-
yield authenticate();
|
|
81
|
-
(_g = events === null || events === void 0 ? void 0 : events.onLoggedOut) === null || _g === void 0 ? void 0 : _g.call(events);
|
|
82
|
-
}), [authClient, authenticate, events]);
|
|
83
|
-
(0, react_1.useEffect)(() => {
|
|
84
|
-
if (!authClient && !authenticating) {
|
|
85
|
-
// eslint-disable-next-line no-console
|
|
86
|
-
authenticate().catch(console.error);
|
|
87
|
-
}
|
|
88
|
-
}, [authClient, authenticating, authenticate]);
|
|
89
|
-
return {
|
|
90
|
-
error,
|
|
91
|
-
authClient,
|
|
92
|
-
authenticated,
|
|
93
|
-
authenticating,
|
|
94
|
-
identity,
|
|
95
|
-
login,
|
|
96
|
-
logout,
|
|
97
|
-
authenticate,
|
|
98
|
-
loginLoading: loginState.loading,
|
|
99
|
-
loginError: loginState.error,
|
|
100
|
-
};
|
|
101
|
-
};
|
|
102
|
-
return {
|
|
103
|
-
useUserPrincipal,
|
|
104
|
-
useAuthState,
|
|
105
|
-
useAuthClient,
|
|
106
|
-
};
|
|
107
|
-
};
|
|
108
|
-
exports.getAuthHooks = getAuthHooks;
|
package/dist/hooks/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./useReactor";
|
package/dist/hooks/types.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { IDL } from "@dfinity/candid";
|
|
2
|
-
import { ActorManagerOptions, BaseActor } from "@ic-reactor/core/dist/types";
|
|
3
|
-
import { ActorHooks, AgentContextType } from "../types";
|
|
4
|
-
export interface UseReactorOptions extends Omit<ActorManagerOptions, "idlFactory" | "agentManager" | "canisterId"> {
|
|
5
|
-
canisterId: string;
|
|
6
|
-
idlFactory?: IDL.InterfaceFactory;
|
|
7
|
-
agentContext?: AgentContextType;
|
|
8
|
-
didjsCanisterId?: string;
|
|
9
|
-
}
|
|
10
|
-
export interface UseReactorState {
|
|
11
|
-
idlFactory?: IDL.InterfaceFactory;
|
|
12
|
-
fetching: boolean;
|
|
13
|
-
fetchError: string | null;
|
|
14
|
-
}
|
|
15
|
-
export interface UseReactorReturn<A = BaseActor> {
|
|
16
|
-
hooks: ActorHooks<A> | null;
|
|
17
|
-
fetching: boolean;
|
|
18
|
-
fetchError: string | null;
|
|
19
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { BaseActor } from "../types";
|
|
2
|
-
import { UseReactorOptions, UseReactorReturn } from "./types";
|
|
3
|
-
/**
|
|
4
|
-
* A comprehensive hook that manages both the fetching of Candid interfaces
|
|
5
|
-
* and the initialization of actor stores for Internet Computer (IC) canisters.
|
|
6
|
-
* It simplifies the process of interacting with canisters by encapsulating
|
|
7
|
-
* the logic for Candid retrieval and actor store management.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```tsx
|
|
11
|
-
* import React from 'react';
|
|
12
|
-
* import { useReactor } from '@ic-reactor/react';
|
|
13
|
-
* import { IDL } from '@dfinity/candid';
|
|
14
|
-
*
|
|
15
|
-
* const App = () => {
|
|
16
|
-
* const { actorManager, fetchCandid, candid, fetching, fetchError } = useReactor({
|
|
17
|
-
* canisterId: 'ryjl3-tyaaa-aaaaa-aaaba-cai',
|
|
18
|
-
* });
|
|
19
|
-
*
|
|
20
|
-
* return (
|
|
21
|
-
* <div>
|
|
22
|
-
* <h2>IC Canister Interaction</h2>
|
|
23
|
-
* {fetching && <p>Loading Candid interface...</p>}
|
|
24
|
-
* {fetchError && <p>Error: {fetchError}</p>}
|
|
25
|
-
* {candid.idlFactory && (
|
|
26
|
-
* <div>
|
|
27
|
-
* <p>Candid interface fetched successfully.</p>
|
|
28
|
-
* <pre>{JSON.stringify(candid.idlFactory({ IDL }), null, 2)}</pre>
|
|
29
|
-
* </div>
|
|
30
|
-
* )}
|
|
31
|
-
* <button onClick={fetchCandid} disabled={fetching}>
|
|
32
|
-
* {fetching ? 'Fetching...' : 'Fetch Candid'}
|
|
33
|
-
* </button>
|
|
34
|
-
* </div>
|
|
35
|
-
* );
|
|
36
|
-
* };
|
|
37
|
-
*
|
|
38
|
-
* export default App;
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
export declare const useReactor: <A = BaseActor>({ canisterId, agentContext, idlFactory: maybeIdlFactory, didjsCanisterId, ...config }: UseReactorOptions) => UseReactorReturn<A>;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import { BaseActor, FunctionName, UseMethodCallArg, UseQueryCallArgs, UseUpdateCallArgs } from "../../types";
|
|
3
|
-
import { CreateActorOptions, ActorContextType, ActorProviderProps } from "./types";
|
|
4
|
-
export declare function createReactorContext<A = BaseActor>({ canisterId: defaultCanisterId, ...defaultConfig }?: Partial<CreateActorOptions>): {
|
|
5
|
-
useActorState: () => import("../../types").UseActorState;
|
|
6
|
-
useQueryCall: <M extends FunctionName<A>>(args: UseQueryCallArgs<A, M>) => import("../../types").ReactorCallReturn<A, M>;
|
|
7
|
-
useUpdateCall: <M_1 extends FunctionName<A>>(args: UseUpdateCallArgs<A, M_1>) => import("../../types").ReactorCallReturn<A, M_1>;
|
|
8
|
-
useMethodCall: <M_2 extends FunctionName<A>>(args: UseMethodCallArg<A, M_2>) => import("../../types").UseMethodCallReturn<A, M_2>;
|
|
9
|
-
useVisitMethod: (functionName: FunctionName<A>) => import("@ic-reactor/core/dist/actor/types").VisitService<A>[FunctionName<A>];
|
|
10
|
-
initialize: () => Promise<void>;
|
|
11
|
-
ActorProvider: React.FC<ActorProviderProps>;
|
|
12
|
-
};
|
|
13
|
-
export declare function extractActorHooks<A = BaseActor>(ActorContext: React.Context<ActorContextType<A> | null>): {
|
|
14
|
-
useActorState: () => import("../../types").UseActorState;
|
|
15
|
-
useQueryCall: <M extends FunctionName<A>>(args: UseQueryCallArgs<A, M>) => import("../../types").ReactorCallReturn<A, M>;
|
|
16
|
-
useUpdateCall: <M_1 extends FunctionName<A>>(args: UseUpdateCallArgs<A, M_1>) => import("../../types").ReactorCallReturn<A, M_1>;
|
|
17
|
-
useMethodCall: <M_2 extends FunctionName<A>>(args: UseMethodCallArg<A, M_2>) => import("../../types").UseMethodCallReturn<A, M_2>;
|
|
18
|
-
useVisitMethod: (functionName: FunctionName<A>) => import("@ic-reactor/core/dist/actor/types").VisitService<A>[FunctionName<A>];
|
|
19
|
-
initialize: () => Promise<void>;
|
|
20
|
-
};
|