@ic-reactor/react 0.4.5 → 0.5.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/dist/context/actor.d.ts +2 -2
- package/dist/context/actor.js +2 -1
- package/dist/hooks/actor.d.ts +3 -41
- package/dist/hooks/actor.js +19 -10
- package/dist/hooks/auth.d.ts +2 -1
- package/dist/hooks/auth.js +5 -0
- package/dist/index.d.ts +3 -21
- package/dist/index.js +8 -25
- package/dist/types.d.ts +46 -1
- package/package.json +3 -3
package/dist/context/actor.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import React, { PropsWithChildren } from "react";
|
|
|
2
2
|
import { IDL } from "@dfinity/candid";
|
|
3
3
|
import { ActorSubclass, ActorManagerOptions } from "@ic-reactor/store";
|
|
4
4
|
import { AgentContextType } from "./agent";
|
|
5
|
-
import
|
|
6
|
-
export type ActorContextType<A = ActorSubclass<any>> =
|
|
5
|
+
import { ActorHooksWithField } from "../types";
|
|
6
|
+
export type ActorContextType<A = ActorSubclass<any>> = ActorHooksWithField<A>;
|
|
7
7
|
export declare const ActorContext: React.Context<ActorContextType<any> | null>;
|
|
8
8
|
type UseActorType = <A = ActorSubclass<any>>() => ActorContextType<A>;
|
|
9
9
|
export declare const useActor: UseActorType;
|
package/dist/context/actor.js
CHANGED
|
@@ -58,7 +58,7 @@ const useActor = () => {
|
|
|
58
58
|
};
|
|
59
59
|
exports.useActor = useActor;
|
|
60
60
|
const ActorProvider = (_a) => {
|
|
61
|
-
var { children, canisterId, agentContext, loadingComponent = react_1.default.createElement("div", null, "Loading...") } = _a, config = __rest(_a, ["children", "canisterId", "agentContext", "loadingComponent"]);
|
|
61
|
+
var { children, canisterId, agentContext, loadingComponent = react_1.default.createElement("div", null, "Loading..."), withServiceField = false } = _a, config = __rest(_a, ["children", "canisterId", "agentContext", "loadingComponent", "withServiceField"]);
|
|
62
62
|
const agentManager = (0, agent_1.useAgentManager)(agentContext);
|
|
63
63
|
const [didJs, setDidJS] = (0, react_1.useState)();
|
|
64
64
|
const [fetching, setFetching] = (0, react_1.useState)(false);
|
|
@@ -112,6 +112,7 @@ const ActorProvider = (_a) => {
|
|
|
112
112
|
agentManager,
|
|
113
113
|
canisterId,
|
|
114
114
|
withDevtools: config.withDevtools,
|
|
115
|
+
withServiceField: withServiceField,
|
|
115
116
|
});
|
|
116
117
|
}
|
|
117
118
|
catch (err) {
|
package/dist/hooks/actor.d.ts
CHANGED
|
@@ -1,41 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
import type {
|
|
3
|
-
|
|
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>) => {
|
|
8
|
-
data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M]> | undefined;
|
|
9
|
-
error: Error | undefined;
|
|
10
|
-
loading: boolean;
|
|
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>;
|
|
13
|
-
};
|
|
14
|
-
useUpdateCall: <M_1 extends keyof A>(args: ActorUseUpdateArgs<A, M_1>) => {
|
|
15
|
-
data: import("@ic-reactor/store").ExtractActorMethodReturnType<A[M_1]> | undefined;
|
|
16
|
-
error: Error | undefined;
|
|
17
|
-
loading: boolean;
|
|
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>;
|
|
29
|
-
};
|
|
30
|
-
useActorStore: () => {
|
|
31
|
-
canisterId: import("@ic-reactor/store").CanisterId;
|
|
32
|
-
initialized: boolean;
|
|
33
|
-
initializing: boolean;
|
|
34
|
-
error: Error | undefined;
|
|
35
|
-
methodState: import("@ic-reactor/store").ActorMethodStates<A>;
|
|
36
|
-
};
|
|
37
|
-
useMethodField: (functionName: keyof A & string) => ExtractedFunction<A>;
|
|
38
|
-
useMethodFields: () => ExtractedFunction<A>[];
|
|
39
|
-
useMethodNames: () => ServiceMethodTypeAndName<A>[];
|
|
40
|
-
useServiceFields: () => ExtractedService<A>;
|
|
41
|
-
};
|
|
1
|
+
import type { ActorManager } from "@ic-reactor/store";
|
|
2
|
+
import type { ActorHooks } from "../types";
|
|
3
|
+
export declare const getActorHooks: <A extends unknown>({ initialize, serviceFields, withServiceField, canisterId, actorStore, callMethod, }: ActorManager<A>) => ActorHooks<A, boolean>;
|
package/dist/hooks/actor.js
CHANGED
|
@@ -23,27 +23,34 @@ 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 = ({ initialize, serviceFields, canisterId, actorStore, callMethod, }) => {
|
|
26
|
+
const getActorHooks = ({ initialize, serviceFields, withServiceField, 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
31
|
const useServiceFields = () => {
|
|
32
|
+
if (!withServiceField || !serviceFields) {
|
|
33
|
+
throw new Error("Service fields not initialized. Pass `withServiceField` to initialize service fields.");
|
|
34
|
+
}
|
|
32
35
|
return serviceFields;
|
|
33
36
|
};
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
return
|
|
37
|
+
const useMethods = () => {
|
|
38
|
+
const serviceFields = useServiceFields();
|
|
39
|
+
return (0, react_1.useMemo)(() => {
|
|
40
|
+
return Object.values(serviceFields.methods);
|
|
41
|
+
}, [serviceFields]);
|
|
37
42
|
};
|
|
38
43
|
const useMethodFields = () => {
|
|
39
|
-
const
|
|
44
|
+
const serviceFields = useServiceFields();
|
|
40
45
|
return (0, react_1.useMemo)(() => {
|
|
41
|
-
return Object.values(methodFields
|
|
42
|
-
}, [
|
|
46
|
+
return Object.values(serviceFields.methodFields);
|
|
47
|
+
}, [serviceFields]);
|
|
43
48
|
};
|
|
44
49
|
const useMethodField = (functionName) => {
|
|
45
50
|
const serviceMethod = useServiceFields();
|
|
46
|
-
return (0, react_1.useMemo)(() =>
|
|
51
|
+
return (0, react_1.useMemo)(() => {
|
|
52
|
+
return serviceMethod.methodFields[functionName];
|
|
53
|
+
}, [functionName, serviceMethod]);
|
|
47
54
|
};
|
|
48
55
|
const useReActorCall = ({ onError, onSuccess, onLoading, args = [], functionName, throwOnError = false, }) => {
|
|
49
56
|
const [state, setState] = (0, react_1.useState)({
|
|
@@ -76,7 +83,9 @@ const getActorHooks = ({ initialize, serviceFields, canisterId, actorStore, call
|
|
|
76
83
|
throw error;
|
|
77
84
|
}
|
|
78
85
|
}), [args, functionName, onError, onSuccess, onLoading]);
|
|
79
|
-
const field =
|
|
86
|
+
const field = (0, react_1.useMemo)(() => {
|
|
87
|
+
return serviceFields === null || serviceFields === void 0 ? void 0 : serviceFields.methodFields[functionName];
|
|
88
|
+
}, [functionName]);
|
|
80
89
|
return Object.assign({ call, field }, state);
|
|
81
90
|
};
|
|
82
91
|
const useQueryCall = (_a) => {
|
|
@@ -116,13 +125,13 @@ const getActorHooks = ({ initialize, serviceFields, canisterId, actorStore, call
|
|
|
116
125
|
};
|
|
117
126
|
return {
|
|
118
127
|
initialize,
|
|
128
|
+
useMethods,
|
|
119
129
|
useQueryCall,
|
|
120
130
|
useUpdateCall,
|
|
121
131
|
useMethodCall,
|
|
122
132
|
useActorStore,
|
|
123
133
|
useMethodField,
|
|
124
134
|
useMethodFields,
|
|
125
|
-
useMethodNames,
|
|
126
135
|
useServiceFields,
|
|
127
136
|
};
|
|
128
137
|
};
|
package/dist/hooks/auth.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { AuthClientLoginOptions } from "@dfinity/auth-client";
|
|
2
|
-
import type { AgentManager, Identity } from "@ic-reactor/store";
|
|
2
|
+
import type { AgentManager, Identity, Principal } from "@ic-reactor/store";
|
|
3
3
|
import { AuthArgs } from "../types";
|
|
4
4
|
export type AuthHooks = ReturnType<typeof getAuthHooks>;
|
|
5
5
|
export declare const getAuthHooks: (agentManager: AgentManager) => {
|
|
6
|
+
useUserPrincipal: () => Principal | undefined;
|
|
6
7
|
useAgentManager: () => AgentManager;
|
|
7
8
|
useAuthStore: () => import("@ic-reactor/store").AgentAuthState;
|
|
8
9
|
useAuthClient: ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, }?: AuthArgs) => {
|
package/dist/hooks/auth.js
CHANGED
|
@@ -21,6 +21,10 @@ const getAuthHooks = (agentManager) => {
|
|
|
21
21
|
const authState = (0, zustand_1.useStore)(authStore, (state) => state);
|
|
22
22
|
return authState;
|
|
23
23
|
};
|
|
24
|
+
const useUserPrincipal = () => {
|
|
25
|
+
const { identity } = useAuthStore();
|
|
26
|
+
return identity === null || identity === void 0 ? void 0 : identity.getPrincipal();
|
|
27
|
+
};
|
|
24
28
|
const useAuthClient = ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, } = {}) => {
|
|
25
29
|
const [loginLoading, setLoginLoading] = (0, react_1.useState)(false);
|
|
26
30
|
const [loginError, setLoginError] = (0, react_1.useState)(null);
|
|
@@ -115,6 +119,7 @@ const getAuthHooks = (agentManager) => {
|
|
|
115
119
|
};
|
|
116
120
|
};
|
|
117
121
|
return {
|
|
122
|
+
useUserPrincipal,
|
|
118
123
|
useAgentManager,
|
|
119
124
|
useAuthStore,
|
|
120
125
|
useAuthClient,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,23 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { AuthHooks } from "./hooks/auth";
|
|
4
|
-
export { createReActorStore, createAgentManager, createActorManager, } from "@ic-reactor/store";
|
|
1
|
+
import { CreateReActor } from "./types";
|
|
2
|
+
export * from "@ic-reactor/store";
|
|
5
3
|
export * from "./context/agent";
|
|
6
4
|
export * from "./context/actor";
|
|
7
|
-
export declare const createReActor:
|
|
8
|
-
useAgentManager: () => import("@ic-reactor/store").AgentManager;
|
|
9
|
-
useAuthStore: () => import("@ic-reactor/store").AgentAuthState;
|
|
10
|
-
useAuthClient: ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, }?: import("./types").AuthArgs) => {
|
|
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<import("@ic-reactor/store").Identity>;
|
|
20
|
-
loginLoading: boolean;
|
|
21
|
-
loginError: Error | null;
|
|
22
|
-
};
|
|
23
|
-
};
|
|
5
|
+
export declare const createReActor: CreateReActor;
|
package/dist/index.js
CHANGED
|
@@ -25,39 +25,22 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
25
25
|
return t;
|
|
26
26
|
};
|
|
27
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
-
exports.createReActor =
|
|
28
|
+
exports.createReActor = void 0;
|
|
29
29
|
const store_1 = require("@ic-reactor/store");
|
|
30
30
|
const actor_1 = require("./hooks/actor");
|
|
31
31
|
const auth_1 = require("./hooks/auth");
|
|
32
|
-
|
|
33
|
-
Object.defineProperty(exports, "createReActorStore", { enumerable: true, get: function () { return store_2.createReActorStore; } });
|
|
34
|
-
Object.defineProperty(exports, "createAgentManager", { enumerable: true, get: function () { return store_2.createAgentManager; } });
|
|
35
|
-
Object.defineProperty(exports, "createActorManager", { enumerable: true, get: function () { return store_2.createActorManager; } });
|
|
32
|
+
__exportStar(require("@ic-reactor/store"), exports);
|
|
36
33
|
__exportStar(require("./context/agent"), exports);
|
|
37
34
|
__exportStar(require("./context/actor"), exports);
|
|
38
35
|
const createReActor = (_a) => {
|
|
39
|
-
var { isLocalEnv } = _a, options = __rest(_a, ["isLocalEnv"]);
|
|
36
|
+
var { isLocalEnv, withServiceField } = _a, options = __rest(_a, ["isLocalEnv", "withServiceField"]);
|
|
40
37
|
isLocalEnv =
|
|
41
38
|
isLocalEnv ||
|
|
42
39
|
(typeof process !== "undefined" &&
|
|
43
|
-
(process.env.
|
|
44
|
-
process.env.
|
|
45
|
-
const actorManager = (0, store_1.createReActorStore)(Object.assign({ isLocalEnv
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
initialize,
|
|
50
|
-
useAgentManager,
|
|
51
|
-
useMethodFields,
|
|
52
|
-
useMethodField,
|
|
53
|
-
useActorStore,
|
|
54
|
-
useAuthStore,
|
|
55
|
-
useQueryCall,
|
|
56
|
-
useUpdateCall,
|
|
57
|
-
useMethodCall,
|
|
58
|
-
useAuthClient,
|
|
59
|
-
useMethodNames,
|
|
60
|
-
useServiceFields,
|
|
61
|
-
};
|
|
40
|
+
(process.env.DFX_NETWORK === "local" ||
|
|
41
|
+
process.env.NODE_ENV === "development"));
|
|
42
|
+
const actorManager = (0, store_1.createReActorStore)(Object.assign({ isLocalEnv,
|
|
43
|
+
withServiceField }, options));
|
|
44
|
+
return Object.assign(Object.assign({}, (0, actor_1.getActorHooks)(actorManager)), (0, auth_1.getAuthHooks)(actorManager.agentManager));
|
|
62
45
|
};
|
|
63
46
|
exports.createReActor = createReActor;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { ActorState, ActorSubclass, CanisterId, CreateReActorOptions, ExtractActorMethodArgs, ExtractActorMethodReturnType, ExtractedFunction, ExtractedService, Identity, Principal, ServiceMethodType, ServiceMethodTypeAndName } from "@ic-reactor/store";
|
|
3
|
+
import { AuthHooks } from "./hooks/auth";
|
|
2
4
|
export type * from "@ic-reactor/store";
|
|
3
5
|
export type * from "@ic-reactor/store/dist/actor/types";
|
|
4
6
|
export type AuthArgs = {
|
|
@@ -27,6 +29,49 @@ export interface ActorUseQueryArgs<A, M extends keyof A> extends ActorCallArgs<A
|
|
|
27
29
|
refetchOnMount?: boolean;
|
|
28
30
|
refetchInterval?: number | false;
|
|
29
31
|
}
|
|
32
|
+
export interface ActorUseQueryReturn<A, M extends keyof A, W extends boolean = false> {
|
|
33
|
+
call: (eventOrReplaceArgs?: React.MouseEvent | ExtractActorMethodArgs<A[M]>) => Promise<unknown>;
|
|
34
|
+
field: W extends true ? ExtractedFunction<A> : undefined;
|
|
35
|
+
data: unknown;
|
|
36
|
+
error: Error | undefined;
|
|
37
|
+
loading: boolean;
|
|
38
|
+
}
|
|
30
39
|
export interface ActorUseUpdateArgs<A, M extends keyof A> extends ActorCallArgs<A, M> {
|
|
31
40
|
}
|
|
41
|
+
export interface ActorUseUpdateReturn<A, M extends keyof A, W extends boolean = false> {
|
|
42
|
+
call: (eventOrReplaceArgs?: React.MouseEvent | ExtractActorMethodArgs<A[M]>) => Promise<unknown>;
|
|
43
|
+
field: W extends true ? ExtractedFunction<A> : undefined;
|
|
44
|
+
data: unknown;
|
|
45
|
+
error: Error | undefined;
|
|
46
|
+
loading: boolean;
|
|
47
|
+
}
|
|
32
48
|
export type ActorUseMethodArg<A, T extends ServiceMethodType> = T extends "query" ? ActorUseQueryArgs<A, keyof A> : ActorUseUpdateArgs<A, keyof A>;
|
|
49
|
+
export type ActorHooksWithField<A> = ActorDefaultHooks<A, true> & ActorFieldHooks<A>;
|
|
50
|
+
export type ActorHooksWithoutField<A> = ActorDefaultHooks<A, false>;
|
|
51
|
+
export type ActorHooks<A, W extends boolean | undefined = undefined> = W extends true ? ActorHooksWithField<A> : W extends false ? ActorHooksWithoutField<A> : ActorHooksWithField<A> | ActorHooksWithoutField<A>;
|
|
52
|
+
export interface ActorFieldHooks<A> {
|
|
53
|
+
useServiceFields: () => ExtractedService<A>;
|
|
54
|
+
useMethodFields: () => ExtractedFunction<A>[];
|
|
55
|
+
useMethodField: (functionName: keyof A & string) => ExtractedFunction<A>;
|
|
56
|
+
}
|
|
57
|
+
export type UseActorStoreReturn<A> = ActorState<A> & {
|
|
58
|
+
canisterId: CanisterId;
|
|
59
|
+
};
|
|
60
|
+
export interface ActorDefaultHooks<A, W extends boolean = false> {
|
|
61
|
+
initialize: () => Promise<void>;
|
|
62
|
+
useMethods: () => ServiceMethodTypeAndName<A>[];
|
|
63
|
+
useActorStore: () => UseActorStoreReturn<A>;
|
|
64
|
+
useQueryCall: <M extends keyof A>(args: ActorUseQueryArgs<A, M>) => ActorUseQueryReturn<A, M, W>;
|
|
65
|
+
useUpdateCall: <M extends keyof A>(args: ActorUseUpdateArgs<A, M>) => ActorUseUpdateReturn<A, M, W>;
|
|
66
|
+
useMethodCall: <M extends keyof A, T extends ServiceMethodType>(args: ActorUseMethodArg<A, T> & {
|
|
67
|
+
type: T;
|
|
68
|
+
}) => T extends "query" ? ActorUseQueryReturn<A, M, W> : ActorUseUpdateReturn<A, M, W>;
|
|
69
|
+
}
|
|
70
|
+
export type CreateReActor = {
|
|
71
|
+
<A extends ActorSubclass<any>>(options: CreateReActorOptions & {
|
|
72
|
+
withServiceField: true;
|
|
73
|
+
}): ActorHooksWithField<A> & AuthHooks;
|
|
74
|
+
<A extends ActorSubclass<any>>(options: CreateReActorOptions & {
|
|
75
|
+
withServiceField?: false | undefined;
|
|
76
|
+
}): ActorHooksWithoutField<A> & AuthHooks;
|
|
77
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
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.5.1",
|
|
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": "86103827fe9a74cd8e910879168aa52e14eef4b8"
|
|
52
52
|
}
|