@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
|
@@ -0,0 +1,124 @@
|
|
|
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.createAgentContext = void 0;
|
|
38
|
+
const react_1 = __importStar(require("react"));
|
|
39
|
+
const core_1 = require("@ic-reactor/core");
|
|
40
|
+
const agentHooks_1 = require("../helpers/agentHooks");
|
|
41
|
+
const authHooks_1 = require("../helpers/authHooks");
|
|
42
|
+
const extractAgentContext_1 = require("../helpers/extractAgentContext");
|
|
43
|
+
/**
|
|
44
|
+
* Creates a React context for managing IC agent and authentication states, providing hooks for interacting with the IC blockchain.
|
|
45
|
+
* This function initializes an `AgentContext` with a set of utilities and hooks based on the provided agent configuration.
|
|
46
|
+
*
|
|
47
|
+
* @param agentParameters A partial configuration object for the agent manager, allowing customization of the agent's behavior.
|
|
48
|
+
*
|
|
49
|
+
* @returns An object containing the `AgentProvider` component and various hooks for interacting with the agent and authentication state.
|
|
50
|
+
* The `AgentProvider` component is a React context provider that should wrap your app or components needing access to agent functionalities.
|
|
51
|
+
*
|
|
52
|
+
* Usage:
|
|
53
|
+
* - `AgentProvider`: React component to provide agent context to your application.
|
|
54
|
+
* - `useAgent`, `useAuthClient`, `useAuthState`, `useAgentState`, `useAgentManager`, `useUserPrincipal`: Hooks extracted from the created context for managing agent and authentication state within components.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```tsx
|
|
58
|
+
* // agent.ts
|
|
59
|
+
* import { createAgentContext } from "@ic-reactor/react";
|
|
60
|
+
* import { CreateAgentParameters } from "@ic-reactor/react/dist/types";
|
|
61
|
+
*
|
|
62
|
+
* // Optional: Define custom agent configuration
|
|
63
|
+
* const agentConfig: CreateAgentParameters = {
|
|
64
|
+
* host: "https://localhost:8000",
|
|
65
|
+
* // or
|
|
66
|
+
* // isLocalEnv: true,
|
|
67
|
+
* // port: 8000,
|
|
68
|
+
* };
|
|
69
|
+
*
|
|
70
|
+
* export const {
|
|
71
|
+
* AgentProvider,
|
|
72
|
+
* useAgent,
|
|
73
|
+
* useAuthClient,
|
|
74
|
+
* useAuthState,
|
|
75
|
+
* useAgentState,
|
|
76
|
+
* useAgentManager,
|
|
77
|
+
* useUserPrincipal,
|
|
78
|
+
* } = createAgentContext(agentConfig);
|
|
79
|
+
*
|
|
80
|
+
* // Now you can use the returned hooks in your React components
|
|
81
|
+
*
|
|
82
|
+
* // App.tsx
|
|
83
|
+
* import React from 'react';
|
|
84
|
+
* import { AgentProvider } from './agent';
|
|
85
|
+
*
|
|
86
|
+
* const App = () => (
|
|
87
|
+
* <AgentProvider>
|
|
88
|
+
* <Login />
|
|
89
|
+
* <YourActor />
|
|
90
|
+
* </AgentProvider>
|
|
91
|
+
* );
|
|
92
|
+
*
|
|
93
|
+
* const Login = () => {
|
|
94
|
+
* const { login } = useAuthClient()
|
|
95
|
+
* const principal = useUserPrincipal()
|
|
96
|
+
*
|
|
97
|
+
* return (
|
|
98
|
+
* <div>
|
|
99
|
+
* <button onClick={() => login()}>Login</button>
|
|
100
|
+
* <p>User: {principal?.toText()}</p>
|
|
101
|
+
* </div>
|
|
102
|
+
* )
|
|
103
|
+
* };
|
|
104
|
+
*
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* This setup allows you to use the agent and authentication hooks within
|
|
108
|
+
* the components wrapped by `AgentProvider`, facilitating interaction
|
|
109
|
+
* with the Internet Computer blockchain.
|
|
110
|
+
*/
|
|
111
|
+
const createAgentContext = (config = {}) => {
|
|
112
|
+
const AgentContext = (0, react_1.createContext)(null);
|
|
113
|
+
const AgentProvider = (_a) => {
|
|
114
|
+
var { children, agentManager: mybeAgentManager } = _a, options = __rest(_a, ["children", "agentManager"]);
|
|
115
|
+
const hooks = (0, react_1.useMemo)(() => {
|
|
116
|
+
const agentManager = mybeAgentManager !== null && mybeAgentManager !== void 0 ? mybeAgentManager : (0, core_1.createAgentManager)(Object.assign(Object.assign({}, options), config));
|
|
117
|
+
return Object.assign(Object.assign(Object.assign({}, (0, agentHooks_1.agentHooks)(agentManager)), (0, authHooks_1.authHooks)(agentManager)), { agentManager });
|
|
118
|
+
}, [options]);
|
|
119
|
+
return (react_1.default.createElement(AgentContext.Provider, { value: hooks }, children));
|
|
120
|
+
};
|
|
121
|
+
AgentProvider.displayName = "AgentProvider";
|
|
122
|
+
return Object.assign({ AgentProvider }, (0, extractAgentContext_1.extractAgentContext)(AgentContext));
|
|
123
|
+
};
|
|
124
|
+
exports.createAgentContext = createAgentContext;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useActor";
|
|
@@ -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("./useActor"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { IDL } from "@dfinity/candid";
|
|
3
|
+
import { AgentContext } from "../types";
|
|
4
|
+
import { ActorHooksReturnType, ActorManagerParameters, BaseActor } from "../../types";
|
|
5
|
+
export interface UseActorParameters extends Omit<ActorManagerParameters, "idlFactory" | "agentManager" | "canisterId"> {
|
|
6
|
+
canisterId: string;
|
|
7
|
+
idlFactory?: IDL.InterfaceFactory;
|
|
8
|
+
agentContext?: React.Context<AgentContext | null>;
|
|
9
|
+
didjsCanisterId?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface UseActorReturn<A = BaseActor> {
|
|
12
|
+
hooks: ActorHooksReturnType<A> | null;
|
|
13
|
+
fetching: boolean;
|
|
14
|
+
fetchError: string | null;
|
|
15
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
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 = () => {
|
|
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 && <CanisterName />}
|
|
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
|
+
* </AgentProvider>
|
|
57
|
+
* )
|
|
58
|
+
*
|
|
59
|
+
* export default App
|
|
60
|
+
*
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare const useActor: <A = BaseActor>(config: UseActorParameters) => UseActorReturn<A>;
|
|
@@ -20,57 +20,79 @@ 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 __1 = require("..");
|
|
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 = () => {
|
|
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 && <CanisterName />}
|
|
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
|
+
* </AgentProvider>
|
|
82
|
+
* )
|
|
83
|
+
*
|
|
84
|
+
* export default App
|
|
62
85
|
*
|
|
63
|
-
* export default App;
|
|
64
86
|
* ```
|
|
65
87
|
*/
|
|
66
|
-
const
|
|
67
|
-
|
|
88
|
+
const useActor = (config) => {
|
|
89
|
+
const { canisterId, idlFactory: maybeIdlFactory, agentContext, didjsCanisterId } = config, actorConfig = __rest(config, ["canisterId", "idlFactory", "agentContext", "didjsCanisterId"]);
|
|
68
90
|
const [{ idlFactory, fetching, fetchError }, setState] = (0, react_1.useState)({
|
|
69
91
|
idlFactory: maybeIdlFactory,
|
|
70
92
|
fetching: false,
|
|
71
93
|
fetchError: null,
|
|
72
94
|
});
|
|
73
|
-
const agentManager = (0,
|
|
95
|
+
const agentManager = (0, __1.useAgentManager)(agentContext);
|
|
74
96
|
const fetchCandid = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
75
97
|
if (!canisterId)
|
|
76
98
|
return;
|
|
@@ -113,9 +135,9 @@ const useReactor = (_a) => {
|
|
|
113
135
|
return null;
|
|
114
136
|
const actorManager = (0, core_1.createActorManager)(Object.assign({ agentManager,
|
|
115
137
|
idlFactory,
|
|
116
|
-
canisterId },
|
|
117
|
-
return (0, helpers_1.
|
|
138
|
+
canisterId }, actorConfig));
|
|
139
|
+
return (0, helpers_1.actorHooks)(actorManager);
|
|
118
140
|
}, [idlFactory]);
|
|
119
141
|
return { hooks, fetching, fetchError };
|
|
120
142
|
};
|
|
121
|
-
exports.
|
|
143
|
+
exports.useActor = useActor;
|
package/dist/provider/index.d.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { createActorContext } from "./actor";
|
|
3
|
+
import { createAgentContext } from "./agent";
|
|
4
|
+
export declare const ActorProvider: import("react").FC<import("./types").ActorProviderProps>, useActorState: () => import("../types").UseActorState, useQueryCall: import("../types").UseQueryCall<import("@ic-reactor/core/dist/types").BaseActor>, useUpdateCall: import("../types").UseUpdateCall<import("@ic-reactor/core/dist/types").BaseActor>, useVisitMethod: <M extends string>(functionName: M) => <V extends import("@dfinity/candid/lib/cjs/idl").Visitor<unknown, unknown>>(extractorClass: V, data?: import("@ic-reactor/core/dist/types").VisitorType<V>["data"] | undefined) => ReturnType<V["visitFunc"]>;
|
|
5
|
+
export { createActorContext };
|
|
6
|
+
export declare const AgentProvider: import("react").FC<import("./types").AgentProviderProps>, useAgent: () => import("@dfinity/agent").HttpAgent | undefined, useAuthClient: (args?: import("../types").UseAuthClientParameters | undefined) => import("../types").UseAuthClientReturnType, useAuthState: () => import("@ic-reactor/core/dist/types").AuthState, useAgentState: () => import("@ic-reactor/core/dist/types").AgentState, useAgentManager: (agentContext?: import("react").Context<import("./types").AgentContext | null> | undefined) => import("@ic-reactor/core/dist/types").AgentManager, useUserPrincipal: () => import("@dfinity/principal").Principal | undefined;
|
|
7
|
+
export { createAgentContext };
|
|
8
|
+
export * from "./hooks";
|
package/dist/provider/index.js
CHANGED
|
@@ -13,6 +13,13 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
13
13
|
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
|
+
var _a, _b;
|
|
16
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
exports.createAgentContext = exports.useUserPrincipal = exports.useAgentManager = exports.useAgentState = exports.useAuthState = exports.useAuthClient = exports.useAgent = exports.AgentProvider = exports.createActorContext = exports.useVisitMethod = exports.useUpdateCall = exports.useQueryCall = exports.useActorState = exports.ActorProvider = void 0;
|
|
19
|
+
const actor_1 = require("./actor");
|
|
20
|
+
Object.defineProperty(exports, "createActorContext", { enumerable: true, get: function () { return actor_1.createActorContext; } });
|
|
21
|
+
const agent_1 = require("./agent");
|
|
22
|
+
Object.defineProperty(exports, "createAgentContext", { enumerable: true, get: function () { return agent_1.createAgentContext; } });
|
|
23
|
+
_a = (0, actor_1.createActorContext)(), exports.ActorProvider = _a.ActorProvider, exports.useActorState = _a.useActorState, exports.useQueryCall = _a.useQueryCall, exports.useUpdateCall = _a.useUpdateCall, exports.useVisitMethod = _a.useVisitMethod;
|
|
24
|
+
_b = (0, agent_1.createAgentContext)(), exports.AgentProvider = _b.AgentProvider, exports.useAgent = _b.useAgent, exports.useAuthClient = _b.useAuthClient, exports.useAuthState = _b.useAuthState, exports.useAgentState = _b.useAgentState, exports.useAgentManager = _b.useAgentManager, exports.useUserPrincipal = _b.useUserPrincipal;
|
|
25
|
+
__exportStar(require("./hooks"), 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,5 +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("./
|
|
18
|
-
__exportStar(require("./hooks"), exports);
|
|
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.5",
|
|
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.4",
|
|
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": "6b8947ab2611bf7af4f0fadaa33a1fa231cfc26b"
|
|
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>;
|