@ic-reactor/react 0.3.7 → 0.4.0
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 +1 -0
- package/dist/context/actor.d.ts +1 -1
- package/dist/hooks/actor.d.ts +24 -10
- package/dist/hooks/actor.js +43 -14
- package/dist/hooks/auth.d.ts +2 -1
- package/dist/index.d.ts +20 -5
- package/dist/index.js +9 -5
- package/dist/types.d.ts +7 -5
- package/package.json +3 -3
package/README.md
CHANGED
package/dist/context/actor.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import React, { PropsWithChildren } from "react";
|
|
2
|
-
import { type ActorHooks } from "../index";
|
|
3
2
|
import { IDL } from "@dfinity/candid";
|
|
4
3
|
import { ActorSubclass, ActorManagerOptions } from "@ic-reactor/store";
|
|
5
4
|
import { AgentContextType } from "./agent";
|
|
5
|
+
import type { ActorHooks } from "../hooks/actor";
|
|
6
6
|
export type ActorContextType<A = ActorSubclass<any>> = ActorHooks<A>;
|
|
7
7
|
export declare const ActorContext: React.Context<ActorContextType<any> | null>;
|
|
8
8
|
type UseActorType = <A = ActorSubclass<any>>() => ActorContextType<A>;
|
package/dist/hooks/actor.d.ts
CHANGED
|
@@ -1,19 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { ActorSubclass, ExtractActorMethodArgs, ActorManager, ExtractedService, ExtractedFunction, ServiceMethodType, ServiceMethodTypeAndName } from "@ic-reactor/store";
|
|
3
|
+
import type { ActorUseMethodArg, ActorUseQueryArgs, ActorUseUpdateArgs } from "../types";
|
|
4
|
+
export type ActorHooks<A extends ActorSubclass<any>> = ReturnType<typeof getActorHooks<A>>;
|
|
5
|
+
export declare const getActorHooks: <A extends unknown>({ initialize, serviceFields, canisterId, actorStore, callMethod, }: ActorManager<A>) => {
|
|
6
|
+
initialize: (options?: import("@ic-reactor/store").UpdateAgentOptions | undefined) => Promise<void>;
|
|
7
|
+
useQueryCall: <M extends keyof A>({ refetchOnMount, refetchInterval, ...rest }: ActorUseQueryArgs<A, M>) => {
|
|
5
8
|
data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M]> | undefined;
|
|
6
9
|
error: Error | undefined;
|
|
7
10
|
loading: boolean;
|
|
8
|
-
field:
|
|
9
|
-
call: (
|
|
11
|
+
field: ExtractedFunction<A>;
|
|
12
|
+
call: (eventOrReplaceArgs?: import("react").MouseEvent<Element, MouseEvent> | ExtractActorMethodArgs<A[M]> | undefined) => Promise<import("@ic-reactor/store").ExtractActorMethodReturnType<A[M]> | undefined>;
|
|
10
13
|
};
|
|
11
14
|
useUpdateCall: <M_1 extends keyof A>(args: ActorUseUpdateArgs<A, M_1>) => {
|
|
12
15
|
data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_1]> | undefined;
|
|
13
16
|
error: Error | undefined;
|
|
14
17
|
loading: boolean;
|
|
15
|
-
call: (
|
|
16
|
-
field:
|
|
18
|
+
call: (eventOrReplaceArgs?: import("react").MouseEvent<Element, MouseEvent> | ExtractActorMethodArgs<A[M_1]> | undefined) => Promise<import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_1]> | undefined>;
|
|
19
|
+
field: ExtractedFunction<A>;
|
|
20
|
+
};
|
|
21
|
+
useMethodCall: <M_2 extends keyof A, T extends ServiceMethodType>({ type, ...rest }: ActorUseMethodArg<A, T> & {
|
|
22
|
+
type: T;
|
|
23
|
+
}) => {
|
|
24
|
+
data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_2]> | undefined;
|
|
25
|
+
error: Error | undefined;
|
|
26
|
+
loading: boolean;
|
|
27
|
+
field: ExtractedFunction<A>;
|
|
28
|
+
call: (eventOrReplaceArgs?: import("react").MouseEvent<Element, MouseEvent> | ExtractActorMethodArgs<A[M_2]> | undefined) => Promise<import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_2]> | undefined>;
|
|
17
29
|
};
|
|
18
30
|
useActorStore: () => {
|
|
19
31
|
canisterId: import("@ic-reactor/store").CanisterId;
|
|
@@ -22,6 +34,8 @@ export declare const getActorHooks: <A extends unknown>({ methodFields, canister
|
|
|
22
34
|
error: Error | undefined;
|
|
23
35
|
methodState: import("@ic-reactor/store").ActorMethodStates<A>;
|
|
24
36
|
};
|
|
25
|
-
useMethodField: (functionName: keyof A & string) =>
|
|
26
|
-
useMethodFields: () =>
|
|
37
|
+
useMethodField: (functionName: keyof A & string) => ExtractedFunction<A>;
|
|
38
|
+
useMethodFields: () => ExtractedFunction<A>[];
|
|
39
|
+
useMethodNames: () => ServiceMethodTypeAndName<A>[];
|
|
40
|
+
useServiceFields: () => ExtractedService<A>;
|
|
27
41
|
};
|
package/dist/hooks/actor.js
CHANGED
|
@@ -23,32 +23,44 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
23
23
|
exports.getActorHooks = void 0;
|
|
24
24
|
const react_1 = require("react");
|
|
25
25
|
const zustand_1 = require("zustand");
|
|
26
|
-
const getActorHooks = ({
|
|
26
|
+
const getActorHooks = ({ initialize, serviceFields, canisterId, actorStore, callMethod, }) => {
|
|
27
27
|
const useActorStore = () => {
|
|
28
28
|
const actorState = (0, zustand_1.useStore)(actorStore, (state) => state);
|
|
29
29
|
return Object.assign(Object.assign({}, actorState), { canisterId });
|
|
30
30
|
};
|
|
31
|
+
const useServiceFields = () => {
|
|
32
|
+
return serviceFields;
|
|
33
|
+
};
|
|
34
|
+
const useMethodNames = () => {
|
|
35
|
+
const serviceField = useServiceFields();
|
|
36
|
+
return serviceField.methodNames;
|
|
37
|
+
};
|
|
31
38
|
const useMethodFields = () => {
|
|
32
|
-
|
|
39
|
+
const methodFields = useServiceFields();
|
|
40
|
+
return (0, react_1.useMemo)(() => {
|
|
41
|
+
return Object.values(methodFields.methods);
|
|
42
|
+
}, [methodFields]);
|
|
33
43
|
};
|
|
34
44
|
const useMethodField = (functionName) => {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
return methodFields.find((f) => f.functionName === functionName);
|
|
38
|
-
}, [methodFields, functionName]);
|
|
39
|
-
return field;
|
|
45
|
+
const serviceMethod = useServiceFields();
|
|
46
|
+
return (0, react_1.useMemo)(() => serviceMethod.methods[functionName], [functionName, serviceMethod]);
|
|
40
47
|
};
|
|
41
|
-
const useReActorCall = ({ onError, onSuccess, onLoading, args = [], functionName, }) => {
|
|
48
|
+
const useReActorCall = ({ onError, onSuccess, onLoading, args = [], functionName, throwOnError = false, }) => {
|
|
42
49
|
const [state, setState] = (0, react_1.useState)({
|
|
43
50
|
data: undefined,
|
|
44
51
|
error: undefined,
|
|
45
52
|
loading: false,
|
|
46
53
|
});
|
|
47
|
-
const call = (0, react_1.useCallback)((
|
|
54
|
+
const call = (0, react_1.useCallback)((eventOrReplaceArgs) => __awaiter(void 0, void 0, void 0, function* () {
|
|
48
55
|
onLoading === null || onLoading === void 0 ? void 0 : onLoading(true);
|
|
49
56
|
onError === null || onError === void 0 ? void 0 : onError(undefined);
|
|
50
57
|
setState((prevState) => (Object.assign(Object.assign({}, prevState), { loading: true, error: undefined })));
|
|
51
58
|
try {
|
|
59
|
+
const replaceArgs = eventOrReplaceArgs !== undefined
|
|
60
|
+
? eventOrReplaceArgs.length > 0
|
|
61
|
+
? eventOrReplaceArgs
|
|
62
|
+
: undefined
|
|
63
|
+
: undefined;
|
|
52
64
|
const data = yield callMethod(functionName, ...(replaceArgs !== null && replaceArgs !== void 0 ? replaceArgs : args));
|
|
53
65
|
onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
|
|
54
66
|
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(data);
|
|
@@ -60,41 +72,58 @@ const getActorHooks = ({ methodFields, canisterId, actorStore, callMethod, }) =>
|
|
|
60
72
|
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
61
73
|
onLoading === null || onLoading === void 0 ? void 0 : onLoading(false);
|
|
62
74
|
setState((prevState) => (Object.assign(Object.assign({}, prevState), { error: error, loading: false })));
|
|
75
|
+
if (throwOnError)
|
|
76
|
+
throw error;
|
|
63
77
|
}
|
|
64
78
|
}), [args, functionName, onError, onSuccess, onLoading]);
|
|
65
79
|
const field = useMethodField(functionName);
|
|
66
80
|
return Object.assign({ call, field }, state);
|
|
67
81
|
};
|
|
68
82
|
const useQueryCall = (_a) => {
|
|
69
|
-
var {
|
|
83
|
+
var { refetchOnMount = false, refetchInterval = false } = _a, rest = __rest(_a, ["refetchOnMount", "refetchInterval"]);
|
|
70
84
|
const _b = useReActorCall(rest), { call } = _b, state = __rest(_b, ["call"]);
|
|
71
85
|
let intervalId = (0, react_1.useRef)(undefined);
|
|
72
86
|
(0, react_1.useEffect)(() => {
|
|
73
87
|
// Auto-refresh logic
|
|
74
|
-
if (
|
|
88
|
+
if (refetchInterval) {
|
|
75
89
|
intervalId.current = setInterval(() => {
|
|
76
90
|
call();
|
|
77
|
-
},
|
|
91
|
+
}, refetchInterval);
|
|
78
92
|
}
|
|
79
93
|
// Initial call logic
|
|
80
|
-
if (
|
|
94
|
+
if (refetchOnMount) {
|
|
81
95
|
call();
|
|
82
96
|
}
|
|
83
97
|
return () => {
|
|
84
98
|
clearInterval(intervalId.current);
|
|
85
99
|
};
|
|
86
|
-
}, [
|
|
100
|
+
}, [refetchOnMount, refetchInterval]);
|
|
87
101
|
return Object.assign({ call }, state);
|
|
88
102
|
};
|
|
89
103
|
const useUpdateCall = (args) => {
|
|
90
104
|
return useReActorCall(args);
|
|
91
105
|
};
|
|
106
|
+
const useMethodCall = (_a) => {
|
|
107
|
+
var { type } = _a, rest = __rest(_a, ["type"]);
|
|
108
|
+
switch (type) {
|
|
109
|
+
case "query":
|
|
110
|
+
return useQueryCall(rest);
|
|
111
|
+
case "update":
|
|
112
|
+
return useUpdateCall(rest);
|
|
113
|
+
default:
|
|
114
|
+
throw new Error(`Invalid type: ${type}`);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
92
117
|
return {
|
|
118
|
+
initialize,
|
|
93
119
|
useQueryCall,
|
|
94
120
|
useUpdateCall,
|
|
121
|
+
useMethodCall,
|
|
95
122
|
useActorStore,
|
|
96
123
|
useMethodField,
|
|
97
124
|
useMethodFields,
|
|
125
|
+
useMethodNames,
|
|
126
|
+
useServiceFields,
|
|
98
127
|
};
|
|
99
128
|
};
|
|
100
129
|
exports.getActorHooks = getActorHooks;
|
package/dist/hooks/auth.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { AuthClientLoginOptions } from "@dfinity/auth-client";
|
|
2
2
|
import type { AgentManager } from "@ic-reactor/store";
|
|
3
|
+
export type AuthHooks = ReturnType<typeof getAuthHooks>;
|
|
3
4
|
export declare const getAuthHooks: (agentManager: AgentManager) => {
|
|
4
5
|
useAgentManager: () => AgentManager;
|
|
5
|
-
useAuthStore: () => import("@ic-reactor/store").
|
|
6
|
+
useAuthStore: () => import("@ic-reactor/store").AgentAuthState;
|
|
6
7
|
useAuthClient: () => {
|
|
7
8
|
authClient: import("@dfinity/auth-client").AuthClient | null;
|
|
8
9
|
authenticated: boolean;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import type { CreateReActorOptions } from "@ic-reactor/store";
|
|
2
|
+
import { ActorHooks } from "./hooks/actor";
|
|
3
|
+
import { AuthHooks } from "./hooks/auth";
|
|
4
4
|
export { createReActorStore, createAgentManager, createActorManager, } from "@ic-reactor/store";
|
|
5
5
|
export * from "./context/agent";
|
|
6
6
|
export * from "./context/actor";
|
|
7
|
-
export
|
|
8
|
-
|
|
7
|
+
export declare const createReActor: <A extends unknown>({ isLocalEnv, ...options }: CreateReActorOptions) => ActorHooks<A> & {
|
|
8
|
+
useAgentManager: () => import("@ic-reactor/store").AgentManager;
|
|
9
|
+
useAuthStore: () => import("@ic-reactor/store").AgentAuthState;
|
|
10
|
+
useAuthClient: () => {
|
|
11
|
+
authClient: import("@dfinity/auth-client").AuthClient | null;
|
|
12
|
+
authenticated: boolean;
|
|
13
|
+
authenticating: boolean;
|
|
14
|
+
identity: import("@ic-reactor/store").Identity | null;
|
|
15
|
+
login: (options?: import("@dfinity/auth-client").AuthClientLoginOptions | undefined) => Promise<void>;
|
|
16
|
+
logout: (options?: {
|
|
17
|
+
returnTo?: string | undefined;
|
|
18
|
+
} | undefined) => Promise<void>;
|
|
19
|
+
authenticate: () => Promise<void>;
|
|
20
|
+
loginLoading: boolean;
|
|
21
|
+
loginError: unknown;
|
|
22
|
+
};
|
|
23
|
+
};
|
package/dist/index.js
CHANGED
|
@@ -36,16 +36,17 @@ Object.defineProperty(exports, "createActorManager", { enumerable: true, get: fu
|
|
|
36
36
|
__exportStar(require("./context/agent"), exports);
|
|
37
37
|
__exportStar(require("./context/actor"), exports);
|
|
38
38
|
const createReActor = (_a) => {
|
|
39
|
-
var {
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
var { isLocalEnv } = _a, options = __rest(_a, ["isLocalEnv"]);
|
|
40
|
+
isLocalEnv =
|
|
41
|
+
isLocalEnv ||
|
|
42
42
|
(typeof process !== "undefined" &&
|
|
43
43
|
(process.env.NODE_ENV === "development" ||
|
|
44
44
|
process.env.DFX_NETWORK === "local"));
|
|
45
|
-
const actorManager = (0, store_1.createReActorStore)(Object.assign({
|
|
45
|
+
const actorManager = (0, store_1.createReActorStore)(Object.assign({ isLocalEnv }, options));
|
|
46
46
|
const { useAuthClient, useAgentManager, useAuthStore } = (0, auth_1.getAuthHooks)(actorManager.agentManager);
|
|
47
|
-
const { useActorStore, useQueryCall, useMethodField, useMethodFields,
|
|
47
|
+
const { initialize, useActorStore, useQueryCall, useUpdateCall, useMethodCall, useMethodField, useMethodFields, useMethodNames, useServiceFields, } = (0, actor_1.getActorHooks)(actorManager);
|
|
48
48
|
return {
|
|
49
|
+
initialize,
|
|
49
50
|
useAgentManager,
|
|
50
51
|
useMethodFields,
|
|
51
52
|
useMethodField,
|
|
@@ -53,7 +54,10 @@ const createReActor = (_a) => {
|
|
|
53
54
|
useAuthStore,
|
|
54
55
|
useQueryCall,
|
|
55
56
|
useUpdateCall,
|
|
57
|
+
useMethodCall,
|
|
56
58
|
useAuthClient,
|
|
59
|
+
useMethodNames,
|
|
60
|
+
useServiceFields,
|
|
57
61
|
};
|
|
58
62
|
};
|
|
59
63
|
exports.createReActor = createReActor;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import type { ExtractActorMethodArgs, ExtractActorMethodReturnType } from "@ic-reactor/store";
|
|
1
|
+
import type { ExtractActorMethodArgs, ExtractActorMethodReturnType, ServiceMethodType } from "@ic-reactor/store";
|
|
2
2
|
export type * from "@ic-reactor/store";
|
|
3
|
+
export type * from "@ic-reactor/store/dist/actor/types";
|
|
3
4
|
export type ActorCallArgs<A, M extends keyof A> = {
|
|
4
5
|
functionName: M & string;
|
|
5
6
|
args?: ExtractActorMethodArgs<A[M]>;
|
|
6
7
|
onLoading?: (loading: boolean) => void;
|
|
7
|
-
onError?: (error: Error |
|
|
8
|
+
onError?: (error: Error | undefined) => void;
|
|
8
9
|
onSuccess?: (data: ExtractActorMethodReturnType<A[M]> | undefined) => void;
|
|
10
|
+
throwOnError?: boolean;
|
|
9
11
|
};
|
|
10
12
|
export type ActorHookState<A, M extends keyof A> = {
|
|
11
13
|
data: ExtractActorMethodReturnType<A[M]> | undefined;
|
|
@@ -13,9 +15,9 @@ export type ActorHookState<A, M extends keyof A> = {
|
|
|
13
15
|
loading: boolean;
|
|
14
16
|
};
|
|
15
17
|
export interface ActorUseQueryArgs<A, M extends keyof A> extends ActorCallArgs<A, M> {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
refreshInterval?: number;
|
|
18
|
+
refetchOnMount?: boolean;
|
|
19
|
+
refetchInterval?: number | false;
|
|
19
20
|
}
|
|
20
21
|
export interface ActorUseUpdateArgs<A, M extends keyof A> extends ActorCallArgs<A, M> {
|
|
21
22
|
}
|
|
23
|
+
export type ActorUseMethodArg<A, T extends ServiceMethodType> = T extends "query" ? ActorUseQueryArgs<A, keyof A> : ActorUseUpdateArgs<A, keyof A>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "A React library for interacting with Dfinity actors",
|
|
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/store": "^0.
|
|
38
|
+
"@ic-reactor/store": "^0.4.0",
|
|
39
39
|
"zustand-utils": "^1.3"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"react": ">=16.8",
|
|
49
49
|
"zustand": "4.4"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "b91b8bec6e0329d0a7cde22014d9222814311f23"
|
|
52
52
|
}
|