@ic-reactor/react 0.5.5 → 1.0.1
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 +0 -23
- package/dist/context/actor/index.d.ts +16 -0
- package/dist/context/actor/index.js +88 -0
- package/dist/context/actor/types.d.ts +28 -0
- package/dist/context/actor/types.js +2 -0
- package/dist/context/agent/context.d.ts +5 -0
- package/dist/context/{agent.js → agent/context.js} +12 -30
- package/dist/context/agent/hooks.d.ts +24 -0
- package/dist/context/agent/hooks.js +43 -0
- package/dist/context/agent/index.d.ts +2 -0
- package/dist/context/agent/index.js +18 -0
- package/dist/context/agent/types.d.ts +12 -0
- package/dist/context/agent/types.js +2 -0
- package/dist/context/index.d.ts +2 -0
- package/dist/context/index.js +18 -0
- package/dist/hooks/actor.d.ts +3 -3
- package/dist/hooks/actor.js +30 -54
- package/dist/hooks/agent.d.ts +6 -0
- package/dist/hooks/agent.js +21 -0
- package/dist/hooks/auth.d.ts +3 -4
- package/dist/hooks/auth.js +25 -22
- package/dist/hooks/index.d.ts +5 -0
- package/dist/hooks/index.js +21 -0
- package/dist/hooks/types.d.ts +4 -0
- package/dist/hooks/types.js +2 -0
- package/dist/hooks/useActor.d.ts +21 -0
- package/dist/hooks/useActor.js +48 -0
- package/dist/hooks/useCandid.d.ts +18 -0
- package/dist/hooks/useCandid.js +54 -0
- package/dist/index.d.ts +30 -5
- package/dist/index.js +32 -18
- package/dist/types.d.ts +40 -43
- package/dist/types.js +18 -0
- package/package.json +11 -11
- package/dist/context/actor.d.ts +0 -16
- package/dist/context/actor.js +0 -125
- package/dist/context/agent.d.ts +0 -16
package/README.md
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
# IC-ReActor - React
|
|
2
|
-
|
|
3
1
|
`@ic-reactor/react` is a comprehensive React library designed to streamline interactions with the Internet Computer (IC) blockchain. It provides React hooks and utilities for efficient state management, authentication, and interaction with IC actors.
|
|
4
2
|
|
|
5
3
|
## Features
|
|
@@ -131,24 +129,3 @@ const Login = () => {
|
|
|
131
129
|
|
|
132
130
|
export default Login
|
|
133
131
|
```
|
|
134
|
-
|
|
135
|
-
## API Reference
|
|
136
|
-
|
|
137
|
-
The library provides various hooks and utilities for interacting with IC actors:
|
|
138
|
-
|
|
139
|
-
- `useActorStore`: Hook for managing actor states.
|
|
140
|
-
- `useAuthStore` Hook for managing authentication states.
|
|
141
|
-
- `useAuthClient`: Hook for managing authentication with the IC blockchain.
|
|
142
|
-
- `useQueryCall`: Hook for querying data from an actor.
|
|
143
|
-
- `useUpdateCall`: Hook for updating data in an actor.
|
|
144
|
-
- Additional hooks for handling loading, errors, authentication, and more.
|
|
145
|
-
|
|
146
|
-
For detailed API usage and options, please refer to the [documentation](#).
|
|
147
|
-
|
|
148
|
-
## Contributing
|
|
149
|
-
|
|
150
|
-
Contributions to `@ic-reactor/react` are welcome! Please read our [contributing guidelines](#) for more information.
|
|
151
|
-
|
|
152
|
-
## License
|
|
153
|
-
|
|
154
|
-
`@ic-reactor/react` is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { ActorUseMethodCallArg, ActorUseQueryArgs, ActorUseUpdateArgs } from "../../types";
|
|
3
|
+
import { CreateActorOptions, ActorContextType, ActorProviderProps } from "./types";
|
|
4
|
+
import type { ActorSubclass } from "@dfinity/agent";
|
|
5
|
+
export declare const ActorContext: React.Context<ActorContextType | null>, ActorProvider: React.FC<ActorProviderProps>, useActorContext: <A extends unknown = unknown>() => ActorContextType<A>, useActorState: () => import("../../types").UseActorStoreReturn<unknown>, useQueryCall: <M extends never>(args: ActorUseQueryArgs<unknown, M>) => import("../../types").ActorCallReturn<unknown, M>, useUpdateCall: <M extends never>(args: ActorUseUpdateArgs<unknown, M>) => import("../../types").ActorCallReturn<unknown, M>, useMethodCall: <M extends never>(args: ActorUseMethodCallArg<unknown, M>) => import("../../types").ActorUseMethodCallReturn<unknown, M, true>, useVisitMethod: (functionName: never) => never;
|
|
6
|
+
export declare function createReActorContext<Actor extends ActorSubclass<any>>({ canisterId: defaultCanisterId, ...defaultConfig }?: Partial<CreateActorOptions>): {
|
|
7
|
+
ActorContext: React.Context<ActorContextType | null>;
|
|
8
|
+
ActorProvider: React.FC<ActorProviderProps>;
|
|
9
|
+
useActorContext: <A extends unknown = Actor>() => ActorContextType<A>;
|
|
10
|
+
useActorState: () => import("../../types").UseActorStoreReturn<Actor>;
|
|
11
|
+
useQueryCall: <M extends keyof Actor & string>(args: ActorUseQueryArgs<Actor, M>) => import("../../types").ActorCallReturn<Actor, M>;
|
|
12
|
+
useUpdateCall: <M_1 extends keyof Actor & string>(args: ActorUseUpdateArgs<Actor, M_1>) => import("../../types").ActorCallReturn<Actor, M_1>;
|
|
13
|
+
useMethodCall: <M_2 extends keyof Actor & string>(args: ActorUseMethodCallArg<Actor, M_2>) => import("../../types").ActorUseMethodCallReturn<Actor, M_2, true>;
|
|
14
|
+
useVisitMethod: (functionName: keyof Actor & string) => import("@ic-reactor/core/dist/actor/types").VisitService<Actor>[keyof Actor & string];
|
|
15
|
+
initialize: () => Promise<void>;
|
|
16
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
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
|
+
var _a;
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.createReActorContext = exports.useVisitMethod = exports.useMethodCall = exports.useUpdateCall = exports.useQueryCall = exports.useActorState = exports.useActorContext = exports.ActorProvider = exports.ActorContext = void 0;
|
|
39
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
40
|
+
const react_1 = __importStar(require("react"));
|
|
41
|
+
const actor_1 = require("../../hooks/actor");
|
|
42
|
+
const useActor_1 = require("../../hooks/useActor");
|
|
43
|
+
_a = createReActorContext(), exports.ActorContext = _a.ActorContext, exports.ActorProvider = _a.ActorProvider, exports.useActorContext = _a.useActorContext, exports.useActorState = _a.useActorState, exports.useQueryCall = _a.useQueryCall, exports.useUpdateCall = _a.useUpdateCall, exports.useMethodCall = _a.useMethodCall, exports.useVisitMethod = _a.useVisitMethod;
|
|
44
|
+
function createReActorContext(_a = {}) {
|
|
45
|
+
var { canisterId: defaultCanisterId } = _a, defaultConfig = __rest(_a, ["canisterId"]);
|
|
46
|
+
const ActorContext = (0, react_1.createContext)(null);
|
|
47
|
+
const ActorProvider = (_a) => {
|
|
48
|
+
var { children, canisterId = defaultCanisterId, loadingComponent = react_1.default.createElement("div", null, "Fetching canister...") } = _a, restConfig = __rest(_a, ["children", "canisterId", "loadingComponent"]);
|
|
49
|
+
if (!canisterId) {
|
|
50
|
+
throw new Error("canisterId is required");
|
|
51
|
+
}
|
|
52
|
+
const config = (0, react_1.useMemo)(() => (Object.assign(Object.assign({}, defaultConfig), restConfig)), [defaultConfig, restConfig]);
|
|
53
|
+
const { actorManager, fetchError, fetching } = (0, useActor_1.useActor)(Object.assign({ canisterId }, config));
|
|
54
|
+
const hooks = (0, react_1.useMemo)(() => {
|
|
55
|
+
if (actorManager) {
|
|
56
|
+
return (0, actor_1.getActorHooks)(actorManager);
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}, [actorManager === null || actorManager === void 0 ? void 0 : actorManager.canisterId]);
|
|
60
|
+
return (react_1.default.createElement(ActorContext.Provider, { value: hooks }, fetching || hooks === null ? fetchError !== null && fetchError !== void 0 ? fetchError : loadingComponent : children));
|
|
61
|
+
};
|
|
62
|
+
ActorProvider.displayName = "ActorProvider";
|
|
63
|
+
const useActorContext = () => {
|
|
64
|
+
const context = (0, react_1.useContext)(ActorContext);
|
|
65
|
+
if (!context) {
|
|
66
|
+
throw new Error("useActor must be used within a ActorProvider");
|
|
67
|
+
}
|
|
68
|
+
return context;
|
|
69
|
+
};
|
|
70
|
+
const initialize = () => useActorContext().initialize();
|
|
71
|
+
const useActorState = () => useActorContext().useActorState();
|
|
72
|
+
const useQueryCall = (args) => useActorContext().useQueryCall(args);
|
|
73
|
+
const useUpdateCall = (args) => useActorContext().useUpdateCall(args);
|
|
74
|
+
const useMethodCall = (args) => useActorContext().useMethodCall(args);
|
|
75
|
+
const useVisitMethod = (functionName) => useActorContext().useVisitMethod(functionName);
|
|
76
|
+
return {
|
|
77
|
+
ActorContext,
|
|
78
|
+
ActorProvider,
|
|
79
|
+
useActorContext,
|
|
80
|
+
useActorState,
|
|
81
|
+
useQueryCall,
|
|
82
|
+
useUpdateCall,
|
|
83
|
+
useMethodCall,
|
|
84
|
+
useVisitMethod,
|
|
85
|
+
initialize,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
exports.createReActorContext = createReActorContext;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { IDL } from "@dfinity/candid";
|
|
3
|
+
import { ActorManagerOptions, BaseActor } from "@ic-reactor/core/dist/types";
|
|
4
|
+
import { ActorHooks, AgentContextType } from "../../types";
|
|
5
|
+
export type ActorContextType<Actor = BaseActor, F extends boolean = true> = ActorHooks<Actor, F> & {
|
|
6
|
+
ActorContext: React.Context<ActorContextType<Actor, F> | null>;
|
|
7
|
+
useActorContext: <A = Actor>() => ActorContextType<A>;
|
|
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
|
+
};
|
|
18
|
+
export interface CreateActorOptions extends Omit<ActorManagerOptions, "idlFactory" | "agentManager" | "canisterId"> {
|
|
19
|
+
didjsId?: string;
|
|
20
|
+
canisterId?: string;
|
|
21
|
+
agentContext?: AgentContextType;
|
|
22
|
+
idlFactory?: IDL.InterfaceFactory;
|
|
23
|
+
loadingComponent?: React.ReactNode;
|
|
24
|
+
}
|
|
25
|
+
export interface ActorProviderProps extends CreateActorOptions {
|
|
26
|
+
children?: React.ReactNode | undefined;
|
|
27
|
+
loadingComponent?: React.ReactNode;
|
|
28
|
+
}
|
|
@@ -34,40 +34,22 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
34
34
|
return t;
|
|
35
35
|
};
|
|
36
36
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
-
exports.AgentProvider = exports.
|
|
37
|
+
exports.AgentProvider = exports.AgentContext = void 0;
|
|
38
38
|
const react_1 = __importStar(require("react"));
|
|
39
|
-
const
|
|
40
|
-
const
|
|
39
|
+
const core_1 = require("@ic-reactor/core");
|
|
40
|
+
const agent_1 = require("../../hooks/agent");
|
|
41
|
+
const auth_1 = require("../../hooks/auth");
|
|
41
42
|
exports.AgentContext = (0, react_1.createContext)(null);
|
|
42
|
-
const useAgent = (agentContext) => {
|
|
43
|
-
const context = (0, react_1.useContext)(agentContext || exports.AgentContext);
|
|
44
|
-
if (!context) {
|
|
45
|
-
throw new Error("useAgent must be used within a AgentProvider");
|
|
46
|
-
}
|
|
47
|
-
return context;
|
|
48
|
-
};
|
|
49
|
-
exports.useAgent = useAgent;
|
|
50
|
-
const useAgentManager = (agentContext) => {
|
|
51
|
-
const context = (0, react_1.useContext)(agentContext || exports.AgentContext);
|
|
52
|
-
if (!context) {
|
|
53
|
-
throw new Error("useAgentManager must be used within a AgentProvider");
|
|
54
|
-
}
|
|
55
|
-
return context.agentManager;
|
|
56
|
-
};
|
|
57
|
-
exports.useAgentManager = useAgentManager;
|
|
58
|
-
const createAgentContext = (config) => {
|
|
59
|
-
const agentManager = (0, store_1.createAgentManager)(config);
|
|
60
|
-
const hooks = (0, auth_1.getAuthHooks)(agentManager);
|
|
61
|
-
return Object.assign(Object.assign({}, hooks), { agentManager });
|
|
62
|
-
};
|
|
63
|
-
exports.createAgentContext = createAgentContext;
|
|
64
43
|
const AgentProvider = (_a) => {
|
|
65
44
|
var { children } = _a, config = __rest(_a, ["children"]);
|
|
66
|
-
const value = (0, react_1.useMemo)(() =>
|
|
67
|
-
const agentManager = config.agentManager || (0, store_1.createAgentManager)(config);
|
|
68
|
-
const hooks = (0, auth_1.getAuthHooks)(agentManager);
|
|
69
|
-
return Object.assign(Object.assign({}, hooks), { agentManager });
|
|
70
|
-
}, [config]);
|
|
45
|
+
const value = (0, react_1.useMemo)(() => createAgentContext(config), [config]);
|
|
71
46
|
return react_1.default.createElement(exports.AgentContext.Provider, { value: value }, children);
|
|
72
47
|
};
|
|
73
48
|
exports.AgentProvider = AgentProvider;
|
|
49
|
+
AgentProvider.displayName = "AgentProvider";
|
|
50
|
+
const createAgentContext = (config) => {
|
|
51
|
+
const agentManager = (0, core_1.createAgentManager)(config);
|
|
52
|
+
const agenthooks = (0, agent_1.getAgentHooks)(agentManager);
|
|
53
|
+
const authHooks = (0, auth_1.getAuthHooks)(agentManager);
|
|
54
|
+
return Object.assign(Object.assign(Object.assign({}, agenthooks), authHooks), { agentManager });
|
|
55
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { AgentManager } from "@ic-reactor/core/dist/agent";
|
|
2
|
+
import type { AgentContextType } from "./types";
|
|
3
|
+
import { AuthArgs } from "../../types";
|
|
4
|
+
export declare const useAgentManagerContext: (agentContext?: AgentContextType) => import("./types").AgentContextValue;
|
|
5
|
+
export declare const useAgentManager: (agentContext?: AgentContextType) => AgentManager;
|
|
6
|
+
export declare const useAgent: (agentContext?: AgentContextType) => import("@dfinity/agent/lib/cjs/agent/http").HttpAgent | undefined;
|
|
7
|
+
export declare const useAuthStore: (agentContext?: AgentContextType) => import("@ic-reactor/core/dist/agent/types").AuthState;
|
|
8
|
+
export declare const useAgentState: (agentContext?: AgentContextType) => import("@ic-reactor/core/dist/agent/types").AgentState;
|
|
9
|
+
export declare const useAuthClient: ({ agentContext, ...args }: AuthArgs & {
|
|
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
|
+
};
|
|
24
|
+
export declare const useUserPrincipal: (agentContext?: AgentContextType) => import("@dfinity/principal").Principal | undefined;
|
|
@@ -0,0 +1,43 @@
|
|
|
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.useUserPrincipal = exports.useAuthClient = exports.useAgentState = exports.useAuthStore = exports.useAgent = exports.useAgentManager = exports.useAgentManagerContext = void 0;
|
|
15
|
+
const react_1 = require("react");
|
|
16
|
+
const context_1 = require("./context");
|
|
17
|
+
const useAgentManagerContext = (agentContext) => {
|
|
18
|
+
const context = (0, react_1.useContext)(agentContext || context_1.AgentContext);
|
|
19
|
+
if (!context) {
|
|
20
|
+
throw new Error("Agent context must be used within a AgentProvider");
|
|
21
|
+
}
|
|
22
|
+
return context;
|
|
23
|
+
};
|
|
24
|
+
exports.useAgentManagerContext = useAgentManagerContext;
|
|
25
|
+
const useAgentManager = (agentContext) => {
|
|
26
|
+
const context = (0, exports.useAgentManagerContext)(agentContext);
|
|
27
|
+
return context.agentManager;
|
|
28
|
+
};
|
|
29
|
+
exports.useAgentManager = useAgentManager;
|
|
30
|
+
const useAgent = (agentContext) => (0, exports.useAgentManagerContext)(agentContext).useAgent();
|
|
31
|
+
exports.useAgent = useAgent;
|
|
32
|
+
const useAuthStore = (agentContext) => (0, exports.useAgentManagerContext)(agentContext).useAuthStore();
|
|
33
|
+
exports.useAuthStore = useAuthStore;
|
|
34
|
+
const useAgentState = (agentContext) => (0, exports.useAgentManagerContext)(agentContext).useAgentState();
|
|
35
|
+
exports.useAgentState = useAgentState;
|
|
36
|
+
const useAuthClient = (_a) => {
|
|
37
|
+
var { agentContext } = _a, args = __rest(_a, ["agentContext"]);
|
|
38
|
+
const context = (0, exports.useAgentManagerContext)(agentContext);
|
|
39
|
+
return context.useAuthClient(args);
|
|
40
|
+
};
|
|
41
|
+
exports.useAuthClient = useAuthClient;
|
|
42
|
+
const useUserPrincipal = (agentContext) => (0, exports.useAgentManagerContext)(agentContext).useUserPrincipal();
|
|
43
|
+
exports.useUserPrincipal = useUserPrincipal;
|
|
@@ -0,0 +1,18 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./context"), exports);
|
|
18
|
+
__exportStar(require("./hooks"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React, { PropsWithChildren } from "react";
|
|
2
|
+
import type { AgentManagerOptions } from "@ic-reactor/core/dist/types";
|
|
3
|
+
import type { AgentManager } from "@ic-reactor/core/dist/agent";
|
|
4
|
+
import type { getAuthHooks } from "../../hooks/auth";
|
|
5
|
+
import type { getAgentHooks } from "../../hooks/agent";
|
|
6
|
+
export type AgentContextValue = ReturnType<typeof getAuthHooks> & ReturnType<typeof getAgentHooks> & {
|
|
7
|
+
agentManager: AgentManager;
|
|
8
|
+
};
|
|
9
|
+
export type AgentContextType = React.Context<AgentContextValue | null>;
|
|
10
|
+
export interface AgentProviderProps extends PropsWithChildren, AgentManagerOptions {
|
|
11
|
+
agentManager?: AgentManager;
|
|
12
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./actor"), exports);
|
|
18
|
+
__exportStar(require("./agent"), exports);
|
package/dist/hooks/actor.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
3
|
-
export declare const getActorHooks: <A
|
|
1
|
+
import { ActorHooks } from "../types";
|
|
2
|
+
import type { ActorManager } from "@ic-reactor/core/dist/actor";
|
|
3
|
+
export declare const getActorHooks: <A>({ initialize, canisterId, actorStore, callMethod, visitFunction, }: ActorManager<A>) => ActorHooks<A, true>;
|
package/dist/hooks/actor.js
CHANGED
|
@@ -21,43 +21,31 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
23
|
exports.getActorHooks = void 0;
|
|
24
|
+
/* eslint-disable no-console */
|
|
24
25
|
const react_1 = require("react");
|
|
25
26
|
const zustand_1 = require("zustand");
|
|
26
|
-
const
|
|
27
|
-
|
|
27
|
+
const DEFAULT_STATE = {
|
|
28
|
+
data: undefined,
|
|
29
|
+
error: undefined,
|
|
30
|
+
loading: false,
|
|
31
|
+
};
|
|
32
|
+
const getActorHooks = ({ initialize, canisterId, actorStore, callMethod, visitFunction, }) => {
|
|
33
|
+
const useActorState = () => {
|
|
28
34
|
const actorState = (0, zustand_1.useStore)(actorStore, (state) => state);
|
|
29
35
|
return Object.assign(Object.assign({}, actorState), { canisterId });
|
|
30
36
|
};
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
throw new Error("Service fields not initialized. Pass `withServiceFields` to initialize service fields.");
|
|
34
|
-
}
|
|
35
|
-
return serviceFields;
|
|
36
|
-
};
|
|
37
|
-
const useMethods = () => {
|
|
38
|
-
const serviceFields = useServiceFields();
|
|
39
|
-
return (0, react_1.useMemo)(() => {
|
|
40
|
-
return Object.values(serviceFields.methods);
|
|
41
|
-
}, [serviceFields]);
|
|
37
|
+
const useVisitFunction = () => {
|
|
38
|
+
return visitFunction;
|
|
42
39
|
};
|
|
43
|
-
const
|
|
44
|
-
const serviceFields =
|
|
40
|
+
const useVisitMethod = (functionName) => {
|
|
41
|
+
const serviceFields = useVisitFunction();
|
|
45
42
|
return (0, react_1.useMemo)(() => {
|
|
46
|
-
return
|
|
47
|
-
}, [serviceFields]);
|
|
48
|
-
};
|
|
49
|
-
const useMethodField = (functionName) => {
|
|
50
|
-
const serviceMethod = useServiceFields();
|
|
51
|
-
return (0, react_1.useMemo)(() => {
|
|
52
|
-
return serviceMethod.methodFields[functionName];
|
|
53
|
-
}, [functionName, serviceMethod]);
|
|
43
|
+
return serviceFields[functionName];
|
|
44
|
+
}, [functionName, serviceFields]);
|
|
54
45
|
};
|
|
55
46
|
const useReActorCall = ({ onError, onSuccess, onLoading, args = [], functionName, throwOnError = false, }) => {
|
|
56
|
-
const [state, setState] = (0, react_1.useState)(
|
|
57
|
-
|
|
58
|
-
error: undefined,
|
|
59
|
-
loading: false,
|
|
60
|
-
});
|
|
47
|
+
const [state, setState] = (0, react_1.useState)(DEFAULT_STATE);
|
|
48
|
+
const reset = (0, react_1.useCallback)(() => setState(DEFAULT_STATE), []);
|
|
61
49
|
const call = (0, react_1.useCallback)((eventOrReplaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
62
50
|
onLoading === null || onLoading === void 0 ? void 0 : onLoading(true);
|
|
63
51
|
onError === null || onError === void 0 ? void 0 : onError(undefined);
|
|
@@ -83,15 +71,12 @@ const getActorHooks = ({ initialize, serviceFields, withServiceFields, canisterI
|
|
|
83
71
|
throw error;
|
|
84
72
|
}
|
|
85
73
|
}), [args, functionName, onError, onSuccess, onLoading]);
|
|
86
|
-
|
|
87
|
-
return serviceFields === null || serviceFields === void 0 ? void 0 : serviceFields.methodFields[functionName];
|
|
88
|
-
}, [functionName]);
|
|
89
|
-
return Object.assign({ call, field }, state);
|
|
74
|
+
return Object.assign({ call, reset }, state);
|
|
90
75
|
};
|
|
91
76
|
const useQueryCall = (_a) => {
|
|
92
|
-
var { refetchOnMount =
|
|
77
|
+
var { refetchOnMount = true, refetchInterval = false } = _a, rest = __rest(_a, ["refetchOnMount", "refetchInterval"]);
|
|
93
78
|
const _b = useReActorCall(rest), { call } = _b, state = __rest(_b, ["call"]);
|
|
94
|
-
|
|
79
|
+
const intervalId = (0, react_1.useRef)(undefined);
|
|
95
80
|
(0, react_1.useEffect)(() => {
|
|
96
81
|
// Auto-refresh logic
|
|
97
82
|
if (refetchInterval) {
|
|
@@ -99,40 +84,31 @@ const getActorHooks = ({ initialize, serviceFields, withServiceFields, canisterI
|
|
|
99
84
|
call();
|
|
100
85
|
}, refetchInterval);
|
|
101
86
|
}
|
|
102
|
-
// Initial call logic
|
|
103
|
-
if (refetchOnMount) {
|
|
104
|
-
call();
|
|
105
|
-
}
|
|
106
87
|
return () => {
|
|
107
88
|
clearInterval(intervalId.current);
|
|
108
89
|
};
|
|
109
|
-
}, [
|
|
90
|
+
}, [refetchInterval]);
|
|
91
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
92
|
+
if (refetchOnMount) {
|
|
93
|
+
call();
|
|
94
|
+
}
|
|
95
|
+
}, []);
|
|
110
96
|
return Object.assign({ call }, state);
|
|
111
97
|
};
|
|
112
98
|
const useUpdateCall = (args) => {
|
|
113
99
|
return useReActorCall(args);
|
|
114
100
|
};
|
|
115
|
-
const useMethodCall = (
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
case "query":
|
|
119
|
-
return useQueryCall(rest);
|
|
120
|
-
case "update":
|
|
121
|
-
return useUpdateCall(rest);
|
|
122
|
-
default:
|
|
123
|
-
throw new Error(`Invalid type: ${type}`);
|
|
124
|
-
}
|
|
101
|
+
const useMethodCall = (args) => {
|
|
102
|
+
const visit = visitFunction[args.functionName];
|
|
103
|
+
return Object.assign({ visit }, useReActorCall(args));
|
|
125
104
|
};
|
|
126
105
|
return {
|
|
127
106
|
initialize,
|
|
128
107
|
useQueryCall,
|
|
129
108
|
useUpdateCall,
|
|
109
|
+
useVisitMethod,
|
|
110
|
+
useActorState,
|
|
130
111
|
useMethodCall,
|
|
131
|
-
useActorStore,
|
|
132
|
-
useMethods,
|
|
133
|
-
useMethodField,
|
|
134
|
-
useMethodFields,
|
|
135
|
-
useServiceFields,
|
|
136
112
|
};
|
|
137
113
|
};
|
|
138
114
|
exports.getActorHooks = getActorHooks;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AgentManager } from "@ic-reactor/core/dist/agent";
|
|
2
|
+
import type { HttpAgent } from "@ic-reactor/core/dist/types";
|
|
3
|
+
export declare const getAgentHooks: (agentManager: AgentManager) => {
|
|
4
|
+
useAgent: () => HttpAgent | undefined;
|
|
5
|
+
useAgentState: () => import("@ic-reactor/core/dist/types").AgentState;
|
|
6
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAgentHooks = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const zustand_1 = require("zustand");
|
|
6
|
+
const getAgentHooks = (agentManager) => {
|
|
7
|
+
const { agentStore, getAgent } = agentManager;
|
|
8
|
+
const useAgentState = () => {
|
|
9
|
+
return (0, zustand_1.useStore)(agentStore, (state) => state);
|
|
10
|
+
};
|
|
11
|
+
const useAgent = () => {
|
|
12
|
+
const [agent, setAgent] = (0, react_1.useState)(getAgent());
|
|
13
|
+
(0, react_1.useEffect)(() => agentManager.subscribeAgent(setAgent), []);
|
|
14
|
+
return agent;
|
|
15
|
+
};
|
|
16
|
+
return {
|
|
17
|
+
useAgent,
|
|
18
|
+
useAgentState,
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
exports.getAgentHooks = getAgentHooks;
|
package/dist/hooks/auth.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import type { AuthClientLoginOptions } from "@dfinity/auth-client";
|
|
2
|
-
import type {
|
|
2
|
+
import type { Identity, Principal } from "@ic-reactor/core/dist/types";
|
|
3
|
+
import type { AgentManager } from "@ic-reactor/core/dist/agent";
|
|
3
4
|
import { AuthArgs } from "../types";
|
|
4
|
-
export type AuthHooks = ReturnType<typeof getAuthHooks>;
|
|
5
5
|
export declare const getAuthHooks: (agentManager: AgentManager) => {
|
|
6
6
|
useUserPrincipal: () => Principal | undefined;
|
|
7
|
-
|
|
8
|
-
useAuthStore: () => import("@ic-reactor/store").AgentAuthState;
|
|
7
|
+
useAuthStore: () => import("@ic-reactor/core/dist/types").AuthState;
|
|
9
8
|
useAuthClient: ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, }?: AuthArgs) => {
|
|
10
9
|
authClient: import("@dfinity/auth-client").AuthClient | null;
|
|
11
10
|
authenticated: boolean;
|
package/dist/hooks/auth.js
CHANGED
|
@@ -14,9 +14,6 @@ const react_1 = require("react");
|
|
|
14
14
|
const zustand_1 = require("zustand");
|
|
15
15
|
const getAuthHooks = (agentManager) => {
|
|
16
16
|
const { authenticate: authenticator, authStore, isLocalEnv } = agentManager;
|
|
17
|
-
const useAgentManager = () => {
|
|
18
|
-
return agentManager;
|
|
19
|
-
};
|
|
20
17
|
const useAuthStore = () => {
|
|
21
18
|
const authState = (0, zustand_1.useStore)(authStore, (state) => state);
|
|
22
19
|
return authState;
|
|
@@ -30,17 +27,17 @@ const getAuthHooks = (agentManager) => {
|
|
|
30
27
|
const [loginError, setLoginError] = (0, react_1.useState)(null);
|
|
31
28
|
const { authClient, authenticated, authenticating, identity } = useAuthStore();
|
|
32
29
|
const authenticate = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
33
|
-
const authenticatePromise = new Promise((resolve, reject) =>
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
const authenticatePromise = new Promise((resolve, reject) => {
|
|
31
|
+
authenticator()
|
|
32
|
+
.then((identity) => {
|
|
36
33
|
onAuthenticationSuccess === null || onAuthenticationSuccess === void 0 ? void 0 : onAuthenticationSuccess(identity);
|
|
37
34
|
resolve(identity);
|
|
38
|
-
}
|
|
39
|
-
|
|
35
|
+
})
|
|
36
|
+
.catch((e) => {
|
|
40
37
|
onAuthenticationFailure === null || onAuthenticationFailure === void 0 ? void 0 : onAuthenticationFailure(e);
|
|
41
38
|
reject(e);
|
|
42
|
-
}
|
|
43
|
-
})
|
|
39
|
+
});
|
|
40
|
+
});
|
|
44
41
|
onAuthentication === null || onAuthentication === void 0 ? void 0 : onAuthentication(() => authenticatePromise);
|
|
45
42
|
return authenticatePromise;
|
|
46
43
|
}), [
|
|
@@ -52,21 +49,28 @@ const getAuthHooks = (agentManager) => {
|
|
|
52
49
|
const login = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
50
|
setLoginLoading(true);
|
|
54
51
|
setLoginError(null);
|
|
55
|
-
const loginPromise = new Promise((resolve, reject) =>
|
|
52
|
+
const loginPromise = new Promise((resolve, reject) => {
|
|
56
53
|
try {
|
|
57
54
|
if (!authClient) {
|
|
58
55
|
throw new Error("Auth client not initialized");
|
|
59
56
|
}
|
|
60
|
-
|
|
57
|
+
authClient.login(Object.assign(Object.assign({ identityProvider: isLocalEnv
|
|
61
58
|
? "https://identity.ic0.app/#authorize"
|
|
62
|
-
: "http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943/#authorize" }, options), { onSuccess: () =>
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
59
|
+
: "http://rdmx6-jaaaa-aaaaa-aaadq-cai.localhost:4943/#authorize" }, options), { onSuccess: () => {
|
|
60
|
+
authenticate()
|
|
61
|
+
.then((identity) => {
|
|
62
|
+
var _a;
|
|
63
|
+
const principal = identity.getPrincipal();
|
|
64
|
+
(_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options);
|
|
65
|
+
onLoginSuccess === null || onLoginSuccess === void 0 ? void 0 : onLoginSuccess(principal);
|
|
66
|
+
resolve(principal);
|
|
67
|
+
})
|
|
68
|
+
.catch((e) => {
|
|
69
|
+
setLoginError(e);
|
|
70
|
+
onLoginError === null || onLoginError === void 0 ? void 0 : onLoginError(e);
|
|
71
|
+
reject(e);
|
|
72
|
+
});
|
|
73
|
+
}, onError: (e) => {
|
|
70
74
|
var _a;
|
|
71
75
|
(_a = options === null || options === void 0 ? void 0 : options.onError) === null || _a === void 0 ? void 0 : _a.call(options, e);
|
|
72
76
|
const error = new Error("Login failed: " + e);
|
|
@@ -83,7 +87,7 @@ const getAuthHooks = (agentManager) => {
|
|
|
83
87
|
finally {
|
|
84
88
|
setLoginLoading(false);
|
|
85
89
|
}
|
|
86
|
-
})
|
|
90
|
+
});
|
|
87
91
|
onLogin === null || onLogin === void 0 ? void 0 : onLogin(() => loginPromise);
|
|
88
92
|
}), [
|
|
89
93
|
authClient,
|
|
@@ -120,7 +124,6 @@ const getAuthHooks = (agentManager) => {
|
|
|
120
124
|
};
|
|
121
125
|
return {
|
|
122
126
|
useUserPrincipal,
|
|
123
|
-
useAgentManager,
|
|
124
127
|
useAuthStore,
|
|
125
128
|
useAuthClient,
|
|
126
129
|
};
|