@ic-reactor/react 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/helpers/actor.d.ts +21 -3
- package/dist/helpers/actor.js +42 -45
- package/dist/helpers/agent.d.ts +2 -5
- package/dist/helpers/agent.js +4 -6
- package/dist/helpers/auth.d.ts +2 -20
- package/dist/helpers/auth.js +65 -87
- package/dist/helpers/types.d.ts +86 -4
- package/dist/hooks/index.d.ts +1 -2
- package/dist/hooks/index.js +1 -2
- package/dist/hooks/types.d.ts +19 -0
- package/dist/hooks/useReactor.d.ts +41 -0
- package/dist/hooks/useReactor.js +121 -0
- package/dist/index.d.ts +3 -29
- package/dist/index.js +4 -39
- package/dist/main.d.ts +2 -0
- package/dist/main.js +35 -0
- package/dist/provider/actor/context.d.ts +20 -0
- package/dist/provider/actor/context.js +184 -0
- package/dist/provider/actor/index.d.ts +3 -0
- package/dist/provider/actor/index.js +9 -0
- package/dist/{context → provider}/actor/types.d.ts +4 -11
- package/dist/{context → provider}/agent/hooks.d.ts +4 -16
- package/dist/{context → provider}/agent/hooks.js +9 -9
- package/dist/provider/agent/types.js +2 -0
- package/dist/types.d.ts +7 -72
- package/dist/types.js +3 -2
- package/package.json +3 -3
- package/dist/context/actor/index.d.ts +0 -16
- package/dist/context/actor/index.js +0 -88
- package/dist/hooks/useActor.d.ts +0 -21
- package/dist/hooks/useActor.js +0 -48
- package/dist/hooks/useCandid.d.ts +0 -18
- package/dist/hooks/useCandid.js +0 -54
- /package/dist/{context/actor → hooks}/types.js +0 -0
- /package/dist/{context/agent → provider/actor}/types.js +0 -0
- /package/dist/{context → provider}/agent/context.d.ts +0 -0
- /package/dist/{context → provider}/agent/context.js +0 -0
- /package/dist/{context → provider}/agent/index.d.ts +0 -0
- /package/dist/{context → provider}/agent/index.js +0 -0
- /package/dist/{context → provider}/agent/types.d.ts +0 -0
- /package/dist/{context → provider}/index.d.ts +0 -0
- /package/dist/{context → provider}/index.js +0 -0
|
@@ -0,0 +1,121 @@
|
|
|
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
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
+
var t = {};
|
|
13
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
+
t[p] = s[p];
|
|
15
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
+
t[p[i]] = s[p[i]];
|
|
19
|
+
}
|
|
20
|
+
return t;
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
exports.useReactor = void 0;
|
|
24
|
+
const core_1 = require("@ic-reactor/core");
|
|
25
|
+
const react_1 = require("react");
|
|
26
|
+
const agent_1 = require("../provider/agent");
|
|
27
|
+
const helpers_1 = require("../helpers");
|
|
28
|
+
/**
|
|
29
|
+
* A comprehensive hook that manages both the fetching of Candid interfaces
|
|
30
|
+
* and the initialization of actor stores for Internet Computer (IC) canisters.
|
|
31
|
+
* It simplifies the process of interacting with canisters by encapsulating
|
|
32
|
+
* the logic for Candid retrieval and actor store management.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* import React from 'react';
|
|
37
|
+
* import { useReactor } from '@ic-reactor/react';
|
|
38
|
+
* import { IDL } from '@dfinity/candid';
|
|
39
|
+
*
|
|
40
|
+
* const App = () => {
|
|
41
|
+
* const { actorManager, fetchCandid, candid, fetching, fetchError } = useReactor({
|
|
42
|
+
* canisterId: 'ryjl3-tyaaa-aaaaa-aaaba-cai',
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* return (
|
|
46
|
+
* <div>
|
|
47
|
+
* <h2>IC Canister Interaction</h2>
|
|
48
|
+
* {fetching && <p>Loading Candid interface...</p>}
|
|
49
|
+
* {fetchError && <p>Error: {fetchError}</p>}
|
|
50
|
+
* {candid.idlFactory && (
|
|
51
|
+
* <div>
|
|
52
|
+
* <p>Candid interface fetched successfully.</p>
|
|
53
|
+
* <pre>{JSON.stringify(candid.idlFactory({ IDL }), null, 2)}</pre>
|
|
54
|
+
* </div>
|
|
55
|
+
* )}
|
|
56
|
+
* <button onClick={fetchCandid} disabled={fetching}>
|
|
57
|
+
* {fetching ? 'Fetching...' : 'Fetch Candid'}
|
|
58
|
+
* </button>
|
|
59
|
+
* </div>
|
|
60
|
+
* );
|
|
61
|
+
* };
|
|
62
|
+
*
|
|
63
|
+
* export default App;
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
const useReactor = (_a) => {
|
|
67
|
+
var { canisterId, agentContext, idlFactory: maybeIdlFactory, didjsCanisterId } = _a, config = __rest(_a, ["canisterId", "agentContext", "idlFactory", "didjsCanisterId"]);
|
|
68
|
+
const [{ idlFactory, fetching, fetchError }, setState] = (0, react_1.useState)({
|
|
69
|
+
idlFactory: maybeIdlFactory,
|
|
70
|
+
fetching: false,
|
|
71
|
+
fetchError: null,
|
|
72
|
+
});
|
|
73
|
+
const agentManager = (0, agent_1.useAgentManager)(agentContext);
|
|
74
|
+
const fetchCandid = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
75
|
+
if (!canisterId)
|
|
76
|
+
return;
|
|
77
|
+
setState({
|
|
78
|
+
idlFactory: undefined,
|
|
79
|
+
fetching: true,
|
|
80
|
+
fetchError: null,
|
|
81
|
+
});
|
|
82
|
+
try {
|
|
83
|
+
const candidManager = (0, core_1.createCandidAdapter)({
|
|
84
|
+
agentManager,
|
|
85
|
+
didjsCanisterId,
|
|
86
|
+
});
|
|
87
|
+
const { idlFactory } = yield candidManager.getCandidDefinition(canisterId);
|
|
88
|
+
setState({
|
|
89
|
+
idlFactory,
|
|
90
|
+
fetching: false,
|
|
91
|
+
fetchError: null,
|
|
92
|
+
});
|
|
93
|
+
return idlFactory;
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
// eslint-disable-next-line no-console
|
|
97
|
+
console.error(err);
|
|
98
|
+
setState({
|
|
99
|
+
idlFactory: undefined,
|
|
100
|
+
fetchError: `Error fetching canister ${canisterId}`,
|
|
101
|
+
fetching: false,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}), [canisterId, didjsCanisterId, agentManager]);
|
|
105
|
+
// Automatically fetch Candid if not already fetched or provided.
|
|
106
|
+
(0, react_1.useEffect)(() => {
|
|
107
|
+
if (!fetching && !idlFactory) {
|
|
108
|
+
fetchCandid();
|
|
109
|
+
}
|
|
110
|
+
}, [fetchCandid]);
|
|
111
|
+
const hooks = (0, react_1.useMemo)(() => {
|
|
112
|
+
if (!idlFactory)
|
|
113
|
+
return null;
|
|
114
|
+
const actorManager = (0, core_1.createActorManager)(Object.assign({ agentManager,
|
|
115
|
+
idlFactory,
|
|
116
|
+
canisterId }, config));
|
|
117
|
+
return (0, helpers_1.getActorHooks)(actorManager);
|
|
118
|
+
}, [idlFactory]);
|
|
119
|
+
return { hooks, fetching, fetchError };
|
|
120
|
+
};
|
|
121
|
+
exports.useReactor = useReactor;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,32 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
useAuthState: () => import("@ic-reactor/core/dist/types").AuthState;
|
|
5
|
-
useAuthClient: ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, }?: import("./types").AuthArgs) => {
|
|
6
|
-
authClient: import("@dfinity/auth-client").AuthClient | null;
|
|
7
|
-
authenticated: boolean;
|
|
8
|
-
authenticating: boolean;
|
|
9
|
-
identity: import("@ic-reactor/core/dist/types").Identity | null;
|
|
10
|
-
login: (options?: import("@dfinity/auth-client").AuthClientLoginOptions | undefined) => Promise<void>;
|
|
11
|
-
logout: (options?: {
|
|
12
|
-
returnTo?: string | undefined;
|
|
13
|
-
} | undefined) => Promise<void>;
|
|
14
|
-
authenticate: () => Promise<import("@ic-reactor/core/dist/types").Identity>;
|
|
15
|
-
loginLoading: boolean;
|
|
16
|
-
loginError: Error | null;
|
|
17
|
-
};
|
|
18
|
-
initialize: () => Promise<void>;
|
|
19
|
-
useActorState: () => import("./types").UseActorStoreReturn<A>;
|
|
20
|
-
useQueryCall: import("./types").ActorQueryCall<A>;
|
|
21
|
-
useUpdateCall: import("./types").ActorUpdateCall<A>;
|
|
22
|
-
useMethodCall: <M extends import("@ic-reactor/core/dist/types").FunctionName<A>>(args: import("./types").ActorUseMethodCallArg<A, M>) => import("./types").ActorUseMethodCallReturn<A, M, true>;
|
|
23
|
-
useVisitMethod: <M_1 extends import("@ic-reactor/core/dist/types").FunctionName<A>>(functionName: M_1) => import("@ic-reactor/core/dist/types").VisitService<A>[M_1];
|
|
24
|
-
getAgent: () => import("@ic-reactor/core/dist/types").HttpAgent;
|
|
25
|
-
getVisitFunction: () => import("@ic-reactor/core/dist/types").VisitService<A>;
|
|
26
|
-
};
|
|
27
|
-
export * from "./context/agent";
|
|
28
|
-
export * from "./context/actor";
|
|
1
|
+
export * from "./main";
|
|
2
|
+
export * from "./hooks";
|
|
3
|
+
export * from "./provider";
|
|
29
4
|
export * as helpers from "./helpers";
|
|
30
|
-
export * as hooks from "./hooks";
|
|
31
5
|
export * as types from "./types";
|
|
32
6
|
export * as core from "./core";
|
package/dist/index.js
CHANGED
|
@@ -25,46 +25,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
25
25
|
__setModuleDefault(result, mod);
|
|
26
26
|
return result;
|
|
27
27
|
};
|
|
28
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
29
|
-
var t = {};
|
|
30
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
31
|
-
t[p] = s[p];
|
|
32
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
33
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
34
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
35
|
-
t[p[i]] = s[p[i]];
|
|
36
|
-
}
|
|
37
|
-
return t;
|
|
38
|
-
};
|
|
39
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
-
exports.core = exports.types = exports.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const tools_1 = require("@ic-reactor/core/dist/tools");
|
|
45
|
-
const createReactor = (_a) => {
|
|
46
|
-
var { isLocalEnv, withVisitor, withProcessEnv } = _a, options = __rest(_a, ["isLocalEnv", "withVisitor", "withProcessEnv"]);
|
|
47
|
-
isLocalEnv = isLocalEnv || (withProcessEnv ? (0, tools_1.isInLocalOrDevelopment)() : false);
|
|
48
|
-
const actorManager = (0, core_1.createReactorStore)(Object.assign({ isLocalEnv,
|
|
49
|
-
withVisitor }, options));
|
|
50
|
-
const getVisitFunction = () => {
|
|
51
|
-
if (!withVisitor) {
|
|
52
|
-
throw new Error("Service fields not initialized. Pass `withVisitor` to initialize service fields.");
|
|
53
|
-
}
|
|
54
|
-
return actorManager.visitFunction;
|
|
55
|
-
};
|
|
56
|
-
const getAgent = () => {
|
|
57
|
-
return actorManager.agentManager.getAgent();
|
|
58
|
-
};
|
|
59
|
-
const actorHooks = (0, actor_1.getActorHooks)(actorManager);
|
|
60
|
-
const authHooks = (0, auth_1.getAuthHooks)(actorManager.agentManager);
|
|
61
|
-
return Object.assign(Object.assign({ getAgent,
|
|
62
|
-
getVisitFunction }, actorHooks), authHooks);
|
|
63
|
-
};
|
|
64
|
-
exports.createReactor = createReactor;
|
|
65
|
-
__exportStar(require("./context/agent"), exports);
|
|
66
|
-
__exportStar(require("./context/actor"), exports);
|
|
29
|
+
exports.core = exports.types = exports.helpers = void 0;
|
|
30
|
+
__exportStar(require("./main"), exports);
|
|
31
|
+
__exportStar(require("./hooks"), exports);
|
|
32
|
+
__exportStar(require("./provider"), exports);
|
|
67
33
|
exports.helpers = __importStar(require("./helpers"));
|
|
68
|
-
exports.hooks = __importStar(require("./hooks"));
|
|
69
34
|
exports.types = __importStar(require("./types"));
|
|
70
35
|
exports.core = __importStar(require("./core"));
|
package/dist/main.d.ts
ADDED
package/dist/main.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.createReactor = void 0;
|
|
15
|
+
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
|
+
const helpers_1 = require("./helpers");
|
|
20
|
+
const createReactor = (options) => {
|
|
21
|
+
const { isLocalEnv, withVisitor, withProcessEnv } = options, args = __rest(options, ["isLocalEnv", "withVisitor", "withProcessEnv"]);
|
|
22
|
+
const actorManager = (0, core_1.createReactorStore)(Object.assign({ isLocalEnv: isLocalEnv || (withProcessEnv ? (0, tools_1.isInLocalOrDevelopment)() : false), withVisitor }, args));
|
|
23
|
+
const getVisitFunction = () => {
|
|
24
|
+
return actorManager.visitFunction;
|
|
25
|
+
};
|
|
26
|
+
const getAgent = () => {
|
|
27
|
+
return actorManager.agentManager.getAgent();
|
|
28
|
+
};
|
|
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
|
+
return Object.assign(Object.assign(Object.assign({ getAgent,
|
|
33
|
+
getVisitFunction }, agentHooks), actorHooks), authHooks);
|
|
34
|
+
};
|
|
35
|
+
exports.createReactor = createReactor;
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,184 @@
|
|
|
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.extractActorHooks = exports.createReactorContext = void 0;
|
|
38
|
+
const react_1 = __importStar(require("react"));
|
|
39
|
+
const useReactor_1 = require("../../hooks/useReactor");
|
|
40
|
+
function createReactorContext(_a = {}) {
|
|
41
|
+
var { canisterId: defaultCanisterId } = _a, defaultConfig = __rest(_a, ["canisterId"]);
|
|
42
|
+
const ActorContext = (0, react_1.createContext)(null);
|
|
43
|
+
const ActorProvider = (_a) => {
|
|
44
|
+
var { children, canisterId = defaultCanisterId, loadingComponent = react_1.default.createElement("div", null, "Fetching canister...") } = _a, restConfig = __rest(_a, ["children", "canisterId", "loadingComponent"]);
|
|
45
|
+
if (!canisterId) {
|
|
46
|
+
throw new Error("canisterId is required");
|
|
47
|
+
}
|
|
48
|
+
const config = (0, react_1.useMemo)(() => (Object.assign(Object.assign({}, defaultConfig), restConfig)), [defaultConfig, restConfig]);
|
|
49
|
+
const { fetchError, fetching, hooks } = (0, useReactor_1.useReactor)(Object.assign({ canisterId }, config));
|
|
50
|
+
return (react_1.default.createElement(ActorContext.Provider, { value: hooks }, fetching || hooks === null ? fetchError !== null && fetchError !== void 0 ? fetchError : loadingComponent : children));
|
|
51
|
+
};
|
|
52
|
+
ActorProvider.displayName = "ActorProvider";
|
|
53
|
+
return Object.assign({ ActorProvider }, extractActorHooks(ActorContext));
|
|
54
|
+
}
|
|
55
|
+
exports.createReactorContext = createReactorContext;
|
|
56
|
+
function extractActorHooks(ActorContext) {
|
|
57
|
+
/**
|
|
58
|
+
* Hook for accessing the actor context, including the actor manager and state.
|
|
59
|
+
* @returns The actor context, including the actor manager and state.
|
|
60
|
+
* @example
|
|
61
|
+
* ```tsx
|
|
62
|
+
* function ActorComponent() {
|
|
63
|
+
* const { initialize, useActorState, useQueryCall, useUpdateCall, useMethodCall, useVisitMethod } = useActorContext();
|
|
64
|
+
* const { canisterId } = useActorState();
|
|
65
|
+
*
|
|
66
|
+
* return (
|
|
67
|
+
* <div>
|
|
68
|
+
* <p>Canister ID: {canisterId}</p>
|
|
69
|
+
* </div>
|
|
70
|
+
* );
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
const useActorContext = () => {
|
|
75
|
+
const context = (0, react_1.useContext)(ActorContext);
|
|
76
|
+
if (!context) {
|
|
77
|
+
throw new Error("useActor must be used within a ActorProvider");
|
|
78
|
+
}
|
|
79
|
+
return context;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Initializes the actor manager, setting up the actor's state.
|
|
83
|
+
*/
|
|
84
|
+
const initialize = () => useActorContext().initialize();
|
|
85
|
+
/**
|
|
86
|
+
* Hook for accessing the current state of the actor, including the canister ID.
|
|
87
|
+
*
|
|
88
|
+
* @returns An object containing the current state of the actor from Zustand's store and the canister ID.
|
|
89
|
+
* @example
|
|
90
|
+
* ```tsx
|
|
91
|
+
* function ActorStateComponent() {
|
|
92
|
+
* const { canisterId, initializing, error, initialized } = useActorState();
|
|
93
|
+
*
|
|
94
|
+
* return (
|
|
95
|
+
* <div>
|
|
96
|
+
* <p>Canister ID: {canisterId}</p>
|
|
97
|
+
* <p>Initializing: {initializing.toString()}</p>
|
|
98
|
+
* <p>Initialized: {initialized.toString()}</p>
|
|
99
|
+
* <p>Error: {error?.message}</p>
|
|
100
|
+
* </div>
|
|
101
|
+
* );
|
|
102
|
+
* }
|
|
103
|
+
*```
|
|
104
|
+
*/
|
|
105
|
+
const useActorState = () => useActorContext().useActorState();
|
|
106
|
+
/**
|
|
107
|
+
* Hook for making query calls to actors. It supports automatic refetching on component mount and at specified intervals.
|
|
108
|
+
*
|
|
109
|
+
* @param options Configuration object for the query call, including refetching options and other configurations passed to useReactorCall.
|
|
110
|
+
* @returns An object containing the query call function and the current call state (data, error, loading, call, reset).
|
|
111
|
+
* @example
|
|
112
|
+
* ```tsx
|
|
113
|
+
* function QueryCallComponent() {
|
|
114
|
+
* const { call, data, loading } = useQueryCall({
|
|
115
|
+
* functionName: 'getUserProfile',
|
|
116
|
+
* args: ['123'],
|
|
117
|
+
* refetchOnMount: true,
|
|
118
|
+
* refetchInterval: 5000, // refetch every 5 seconds
|
|
119
|
+
* });
|
|
120
|
+
*
|
|
121
|
+
* if (loading) return <p>Loading profile...</p>;
|
|
122
|
+
*
|
|
123
|
+
* return (
|
|
124
|
+
* <div>
|
|
125
|
+
* <p>User Profile: {JSON.stringify(data)}</p>
|
|
126
|
+
* <button onClick={call}>Refetch</button>
|
|
127
|
+
* </div>
|
|
128
|
+
* );
|
|
129
|
+
* }
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
const useQueryCall = (args) => useActorContext().useQueryCall(args);
|
|
133
|
+
/**
|
|
134
|
+
* Hook for making update calls to actors, handling loading states, and managing errors. It supports custom event handlers for loading, success, and error events.
|
|
135
|
+
*
|
|
136
|
+
* @param options Configuration object for the actor method call, including the method name, arguments, and event handlers.
|
|
137
|
+
* @returns An object containing the method call function, a reset function to reset the call state to its default, and the current call state (data, error, loading, call, reset).
|
|
138
|
+
* @example
|
|
139
|
+
* ```tsx
|
|
140
|
+
* function UpdateCallComponent() {
|
|
141
|
+
* const { call, data, loading } = useUpdateCall({
|
|
142
|
+
* functionName: 'updateUserProfile',
|
|
143
|
+
* args: ['123', { name: 'John Doe' }],
|
|
144
|
+
* onLoading: (loading) => console.log('Loading:', loading),
|
|
145
|
+
* onError: (error) => console.error('Error:', error),
|
|
146
|
+
* onSuccess: (data) => console.log('Success:', data),
|
|
147
|
+
* });
|
|
148
|
+
*
|
|
149
|
+
* if (loading) return <p>Updating profile...</p>;
|
|
150
|
+
*
|
|
151
|
+
* return (
|
|
152
|
+
* <div>
|
|
153
|
+
* <p>Updated Profile: {JSON.stringify(data)}</p>
|
|
154
|
+
* <button onClick={call}>Update</button>
|
|
155
|
+
* </div>
|
|
156
|
+
* );
|
|
157
|
+
* }
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
const useUpdateCall = (args) => useActorContext().useUpdateCall(args);
|
|
161
|
+
/**
|
|
162
|
+
* Hook that combines useVisitMethod and useReactorCall for calling actor methods. It provides both the visit service for the method and the ability to make actor calls with state management.
|
|
163
|
+
*
|
|
164
|
+
* @param args Configuration object including the function name and arguments for the actor method call.
|
|
165
|
+
* @returns An object containing the visit function for the method and the current call state (data, error, loading).
|
|
166
|
+
*/
|
|
167
|
+
const useMethodCall = (args) => useActorContext().useMethodCall(args);
|
|
168
|
+
/**
|
|
169
|
+
* Memoizes and returns a visit service function for a specific actor method.
|
|
170
|
+
*
|
|
171
|
+
* @param functionName The name of the actor method to visit.
|
|
172
|
+
* @returns The visit service function for the specified method.
|
|
173
|
+
*/
|
|
174
|
+
const useVisitMethod = (functionName) => useActorContext().useVisitMethod(functionName);
|
|
175
|
+
return {
|
|
176
|
+
useActorState,
|
|
177
|
+
useQueryCall,
|
|
178
|
+
useUpdateCall,
|
|
179
|
+
useMethodCall,
|
|
180
|
+
useVisitMethod,
|
|
181
|
+
initialize,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
exports.extractActorHooks = extractActorHooks;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export declare const ActorProvider: import("react").FC<import("./types").ActorProviderProps>, useActorState: () => import("../../types").UseActorState, useQueryCall: <M extends string>(args: import("../../types").UseQueryCallArgs<import("@ic-reactor/core/dist/types").BaseActor, M>) => import("../../types").ReactorCallReturn<import("@ic-reactor/core/dist/types").BaseActor, M>, useUpdateCall: <M extends string>(args: import("../../types").UseUpdateCallArgs<import("@ic-reactor/core/dist/types").BaseActor, M>) => import("../../types").ReactorCallReturn<import("@ic-reactor/core/dist/types").BaseActor, M>, useMethodCall: <M extends string>(args: import("../../types").UseMethodCallArg<import("@ic-reactor/core/dist/types").BaseActor, M>) => import("../../types").UseMethodCallReturn<import("@ic-reactor/core/dist/types").BaseActor, M>, useVisitMethod: (functionName: string) => <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"]>;
|
|
3
|
+
export { createReactorContext, extractActorHooks } from "./context";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.extractActorHooks = exports.createReactorContext = exports.useVisitMethod = exports.useMethodCall = exports.useUpdateCall = exports.useQueryCall = exports.useActorState = exports.ActorProvider = void 0;
|
|
5
|
+
const context_1 = require("./context");
|
|
6
|
+
_a = (0, context_1.createReactorContext)(), exports.ActorProvider = _a.ActorProvider, exports.useActorState = _a.useActorState, exports.useQueryCall = _a.useQueryCall, exports.useUpdateCall = _a.useUpdateCall, exports.useMethodCall = _a.useMethodCall, exports.useVisitMethod = _a.useVisitMethod;
|
|
7
|
+
var context_2 = require("./context");
|
|
8
|
+
Object.defineProperty(exports, "createReactorContext", { enumerable: true, get: function () { return context_2.createReactorContext; } });
|
|
9
|
+
Object.defineProperty(exports, "extractActorHooks", { enumerable: true, get: function () { return context_2.extractActorHooks; } });
|
|
@@ -2,19 +2,12 @@
|
|
|
2
2
|
import { IDL } from "@dfinity/candid";
|
|
3
3
|
import { ActorManagerOptions, BaseActor } from "@ic-reactor/core/dist/types";
|
|
4
4
|
import { ActorHooks, AgentContextType } from "../../types";
|
|
5
|
-
export
|
|
6
|
-
ActorContext: React.Context<ActorContextType<Actor
|
|
5
|
+
export interface ActorContextType<Actor = BaseActor> extends ActorHooks<Actor> {
|
|
6
|
+
ActorContext: React.Context<ActorContextType<Actor> | null>;
|
|
7
7
|
useActorContext: <A = Actor>() => ActorContextType<A>;
|
|
8
8
|
ActorProvider: React.FC<ActorProviderProps>;
|
|
9
|
-
}
|
|
10
|
-
export type CreateReactorContext =
|
|
11
|
-
<A = BaseActor>(options?: Partial<CreateActorOptions> & {
|
|
12
|
-
withVisitor: true;
|
|
13
|
-
}): ActorContextType<A, true>;
|
|
14
|
-
<A = BaseActor>(options?: Partial<CreateActorOptions> & {
|
|
15
|
-
withVisitor?: false | undefined;
|
|
16
|
-
}): ActorContextType<A, false>;
|
|
17
|
-
};
|
|
9
|
+
}
|
|
10
|
+
export type CreateReactorContext = <A = BaseActor>(options?: Partial<CreateActorOptions>) => ActorContextType<A>;
|
|
18
11
|
export interface CreateActorOptions extends Omit<ActorManagerOptions, "idlFactory" | "agentManager" | "canisterId"> {
|
|
19
12
|
didjsId?: string;
|
|
20
13
|
canisterId?: string;
|
|
@@ -1,24 +1,12 @@
|
|
|
1
1
|
import { AgentManager } from "@ic-reactor/core/dist/agent";
|
|
2
2
|
import type { AgentContextType } from "./types";
|
|
3
|
-
import {
|
|
4
|
-
export declare const
|
|
3
|
+
import { UseAuthClientArgs } from "../../types";
|
|
4
|
+
export declare const useAgentContext: (agentContext?: AgentContextType) => import("./types").AgentContextValue;
|
|
5
5
|
export declare const useAgentManager: (agentContext?: AgentContextType) => AgentManager;
|
|
6
6
|
export declare const useAgent: (agentContext?: AgentContextType) => import("@dfinity/agent/lib/cjs/agent/http").HttpAgent | undefined;
|
|
7
7
|
export declare const useAuthState: (agentContext?: AgentContextType) => import("@ic-reactor/core/dist/agent/types").AuthState;
|
|
8
8
|
export declare const useAgentState: (agentContext?: AgentContextType) => import("@ic-reactor/core/dist/agent/types").AgentState;
|
|
9
|
-
export declare const useAuthClient: ({ agentContext, ...args }:
|
|
9
|
+
export declare const useAuthClient: ({ agentContext, ...args }: UseAuthClientArgs & {
|
|
10
10
|
agentContext?: AgentContextType | undefined;
|
|
11
|
-
}) =>
|
|
12
|
-
authClient: import("@dfinity/auth-client").AuthClient | null;
|
|
13
|
-
authenticated: boolean;
|
|
14
|
-
authenticating: boolean;
|
|
15
|
-
identity: import("@dfinity/agent/lib/cjs/auth").Identity | null;
|
|
16
|
-
login: (options?: import("@dfinity/auth-client").AuthClientLoginOptions | undefined) => Promise<void>;
|
|
17
|
-
logout: (options?: {
|
|
18
|
-
returnTo?: string | undefined;
|
|
19
|
-
} | undefined) => Promise<void>;
|
|
20
|
-
authenticate: () => Promise<import("@dfinity/agent/lib/cjs/auth").Identity>;
|
|
21
|
-
loginLoading: boolean;
|
|
22
|
-
loginError: Error | null;
|
|
23
|
-
};
|
|
11
|
+
}) => import("../../types").UseAuthClientReturn;
|
|
24
12
|
export declare const useUserPrincipal: (agentContext?: AgentContextType) => import("@dfinity/principal").Principal | undefined;
|
|
@@ -11,33 +11,33 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
11
11
|
return t;
|
|
12
12
|
};
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.useUserPrincipal = exports.useAuthClient = exports.useAgentState = exports.useAuthState = exports.useAgent = exports.useAgentManager = exports.
|
|
14
|
+
exports.useUserPrincipal = exports.useAuthClient = exports.useAgentState = exports.useAuthState = exports.useAgent = exports.useAgentManager = exports.useAgentContext = void 0;
|
|
15
15
|
const react_1 = require("react");
|
|
16
16
|
const context_1 = require("./context");
|
|
17
|
-
const
|
|
17
|
+
const useAgentContext = (agentContext) => {
|
|
18
18
|
const context = (0, react_1.useContext)(agentContext || context_1.AgentContext);
|
|
19
19
|
if (!context) {
|
|
20
20
|
throw new Error("Agent context must be used within a AgentProvider");
|
|
21
21
|
}
|
|
22
22
|
return context;
|
|
23
23
|
};
|
|
24
|
-
exports.
|
|
24
|
+
exports.useAgentContext = useAgentContext;
|
|
25
25
|
const useAgentManager = (agentContext) => {
|
|
26
|
-
const context = (0, exports.
|
|
26
|
+
const context = (0, exports.useAgentContext)(agentContext);
|
|
27
27
|
return context.agentManager;
|
|
28
28
|
};
|
|
29
29
|
exports.useAgentManager = useAgentManager;
|
|
30
|
-
const useAgent = (agentContext) => (0, exports.
|
|
30
|
+
const useAgent = (agentContext) => (0, exports.useAgentContext)(agentContext).useAgent();
|
|
31
31
|
exports.useAgent = useAgent;
|
|
32
|
-
const useAuthState = (agentContext) => (0, exports.
|
|
32
|
+
const useAuthState = (agentContext) => (0, exports.useAgentContext)(agentContext).useAuthState();
|
|
33
33
|
exports.useAuthState = useAuthState;
|
|
34
|
-
const useAgentState = (agentContext) => (0, exports.
|
|
34
|
+
const useAgentState = (agentContext) => (0, exports.useAgentContext)(agentContext).useAgentState();
|
|
35
35
|
exports.useAgentState = useAgentState;
|
|
36
36
|
const useAuthClient = (_a) => {
|
|
37
37
|
var { agentContext } = _a, args = __rest(_a, ["agentContext"]);
|
|
38
|
-
const context = (0, exports.
|
|
38
|
+
const context = (0, exports.useAgentContext)(agentContext);
|
|
39
39
|
return context.useAuthClient(args);
|
|
40
40
|
};
|
|
41
41
|
exports.useAuthClient = useAuthClient;
|
|
42
|
-
const useUserPrincipal = (agentContext) => (0, exports.
|
|
42
|
+
const useUserPrincipal = (agentContext) => (0, exports.useAgentContext)(agentContext).useUserPrincipal();
|
|
43
43
|
exports.useUserPrincipal = useUserPrincipal;
|