@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
|
@@ -0,0 +1,21 @@
|
|
|
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("./useActor"), exports);
|
|
18
|
+
__exportStar(require("./useCandid"), exports);
|
|
19
|
+
__exportStar(require("./agent"), exports);
|
|
20
|
+
__exportStar(require("./actor"), exports);
|
|
21
|
+
__exportStar(require("./auth"), exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { IDL } from "@dfinity/candid";
|
|
2
|
+
import { ActorManagerOptions, BaseActor } from "@ic-reactor/core/dist/types";
|
|
3
|
+
import { AgentContextType } from "../types";
|
|
4
|
+
interface DynamicActorArgs extends Omit<ActorManagerOptions, "idlFactory" | "agentManager" | "canisterId"> {
|
|
5
|
+
canisterId: string;
|
|
6
|
+
idlFactory?: IDL.InterfaceFactory;
|
|
7
|
+
agentContext?: AgentContextType;
|
|
8
|
+
didjsCanisterId?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A hook to create an actor manager and fetch the actor's candid interface.
|
|
12
|
+
*
|
|
13
|
+
* @category Hooks
|
|
14
|
+
*/
|
|
15
|
+
export declare const useActor: <A = BaseActor>({ canisterId, agentContext, idlFactory: maybeIdlFactory, didjsCanisterId, ...config }: DynamicActorArgs) => {
|
|
16
|
+
fetchCandid: () => Promise<import("@ic-reactor/core/dist/types").CandidDefenition | undefined>;
|
|
17
|
+
fetching: boolean;
|
|
18
|
+
fetchError: string | null;
|
|
19
|
+
actorManager: import("@ic-reactor/core/dist/actor").ActorManager<A> | null;
|
|
20
|
+
};
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
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.useActor = void 0;
|
|
15
|
+
const core_1 = require("@ic-reactor/core");
|
|
16
|
+
const react_1 = require("react");
|
|
17
|
+
const agent_1 = require("../context/agent");
|
|
18
|
+
const useCandid_1 = require("./useCandid");
|
|
19
|
+
/**
|
|
20
|
+
* A hook to create an actor manager and fetch the actor's candid interface.
|
|
21
|
+
*
|
|
22
|
+
* @category Hooks
|
|
23
|
+
*/
|
|
24
|
+
const useActor = (_a) => {
|
|
25
|
+
var { canisterId, agentContext, idlFactory: maybeIdlFactory, didjsCanisterId } = _a, config = __rest(_a, ["canisterId", "agentContext", "idlFactory", "didjsCanisterId"]);
|
|
26
|
+
const agentManager = (0, agent_1.useAgentManager)(agentContext);
|
|
27
|
+
const _b = (0, useCandid_1.useCandid)({
|
|
28
|
+
canisterId,
|
|
29
|
+
didjsCanisterId,
|
|
30
|
+
idlFactory: maybeIdlFactory,
|
|
31
|
+
}), { candid: { idlFactory } } = _b, rest = __rest(_b, ["candid"]);
|
|
32
|
+
const actorManager = (0, react_1.useMemo)(() => {
|
|
33
|
+
if (!idlFactory) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const manager = (0, core_1.createReActorStore)({
|
|
38
|
+
agentManager,
|
|
39
|
+
idlFactory,
|
|
40
|
+
canisterId,
|
|
41
|
+
withDevtools: config.withDevtools,
|
|
42
|
+
});
|
|
43
|
+
return manager;
|
|
44
|
+
}
|
|
45
|
+
}, [idlFactory]);
|
|
46
|
+
return Object.assign({ actorManager }, rest);
|
|
47
|
+
};
|
|
48
|
+
exports.useActor = useActor;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { IDL } from "@dfinity/candid";
|
|
2
|
+
interface UseIDLFactoryArgs {
|
|
3
|
+
canisterId: string;
|
|
4
|
+
didjsCanisterId?: string;
|
|
5
|
+
idlFactory?: IDL.InterfaceFactory;
|
|
6
|
+
}
|
|
7
|
+
export declare const useCandid: ({ canisterId, didjsCanisterId, idlFactory, }: UseIDLFactoryArgs) => {
|
|
8
|
+
fetchCandid: () => Promise<import("@ic-reactor/core/dist/types").CandidDefenition | undefined>;
|
|
9
|
+
candid: {
|
|
10
|
+
idlFactory?: IDL.InterfaceFactory | undefined;
|
|
11
|
+
init: ({ idl }: {
|
|
12
|
+
idl: typeof IDL;
|
|
13
|
+
}) => never[];
|
|
14
|
+
};
|
|
15
|
+
fetching: boolean;
|
|
16
|
+
fetchError: string | null;
|
|
17
|
+
};
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.useCandid = void 0;
|
|
13
|
+
const react_1 = require("react");
|
|
14
|
+
const core_1 = require("@ic-reactor/core");
|
|
15
|
+
const agent_1 = require("../context/agent");
|
|
16
|
+
const DEFAULT_STATE = {
|
|
17
|
+
candid: { idlFactory: undefined, init: () => [] },
|
|
18
|
+
fetching: false,
|
|
19
|
+
fetchError: null,
|
|
20
|
+
};
|
|
21
|
+
const useCandid = ({ canisterId, didjsCanisterId, idlFactory, }) => {
|
|
22
|
+
const [{ candid, fetchError, fetching }, setCandid] = (0, react_1.useState)(Object.assign(Object.assign({}, DEFAULT_STATE), { candid: {
|
|
23
|
+
idlFactory,
|
|
24
|
+
init: () => [],
|
|
25
|
+
} }));
|
|
26
|
+
const agent = (0, agent_1.useAgent)();
|
|
27
|
+
const fetchCandid = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
28
|
+
if (!canisterId || !agent)
|
|
29
|
+
return;
|
|
30
|
+
setCandid((prevState) => (Object.assign(Object.assign({}, prevState), { candid: DEFAULT_STATE.candid, fetching: true, fetchError: null })));
|
|
31
|
+
try {
|
|
32
|
+
const candidManager = (0, core_1.createCandidAdapter)({ agent, didjsCanisterId });
|
|
33
|
+
const fetchedCandid = yield candidManager.getCandidDefinition(canisterId);
|
|
34
|
+
setCandid({
|
|
35
|
+
candid: fetchedCandid,
|
|
36
|
+
fetching: false,
|
|
37
|
+
fetchError: null,
|
|
38
|
+
});
|
|
39
|
+
return fetchedCandid;
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
// eslint-disable-next-line no-console
|
|
43
|
+
console.error(err);
|
|
44
|
+
setCandid((prevState) => (Object.assign(Object.assign({}, prevState), { fetchError: `Error fetching canister ${canisterId}`, fetching: false })));
|
|
45
|
+
}
|
|
46
|
+
}), [canisterId, didjsCanisterId, agent]);
|
|
47
|
+
(0, react_1.useEffect)(() => {
|
|
48
|
+
if (!fetching && !idlFactory) {
|
|
49
|
+
fetchCandid();
|
|
50
|
+
}
|
|
51
|
+
}, [fetchCandid]);
|
|
52
|
+
return { fetchCandid, candid, fetching, fetchError };
|
|
53
|
+
};
|
|
54
|
+
exports.useCandid = useCandid;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { BaseActor, CreateReActorOptions } from "@ic-reactor/core/dist/types";
|
|
2
|
+
export declare const createReActor: <A = BaseActor>({ isLocalEnv, withVisitor, withProcessEnv, ...options }: CreateReActorOptions) => {
|
|
3
|
+
useUserPrincipal: () => import("@dfinity/principal").Principal | undefined;
|
|
4
|
+
useAuthStore: () => 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 * as agent from "./context/agent";
|
|
28
|
+
export * as actor from "./context/actor";
|
|
29
|
+
export * as hooks from "./hooks";
|
|
30
|
+
export * as types from "./types";
|
package/dist/index.js
CHANGED
|
@@ -10,8 +10,17 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
-
var
|
|
14
|
-
|
|
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;
|
|
15
24
|
};
|
|
16
25
|
var __rest = (this && this.__rest) || function (s, e) {
|
|
17
26
|
var t = {};
|
|
@@ -25,32 +34,37 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
25
34
|
return t;
|
|
26
35
|
};
|
|
27
36
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
-
exports.createReActor = void 0;
|
|
29
|
-
const
|
|
37
|
+
exports.types = exports.hooks = exports.actor = exports.agent = exports.createReActor = void 0;
|
|
38
|
+
const core_1 = require("@ic-reactor/core");
|
|
30
39
|
const actor_1 = require("./hooks/actor");
|
|
31
40
|
const auth_1 = require("./hooks/auth");
|
|
32
|
-
__exportStar(require("@ic-reactor/store"), exports);
|
|
33
|
-
__exportStar(require("./context/agent"), exports);
|
|
34
|
-
__exportStar(require("./context/actor"), exports);
|
|
35
41
|
const createReActor = (_a) => {
|
|
36
|
-
var { isLocalEnv,
|
|
42
|
+
var { isLocalEnv, withVisitor, withProcessEnv } = _a, options = __rest(_a, ["isLocalEnv", "withVisitor", "withProcessEnv"]);
|
|
37
43
|
isLocalEnv =
|
|
38
44
|
isLocalEnv ||
|
|
39
|
-
(
|
|
40
|
-
|
|
41
|
-
process.env.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
(withProcessEnv
|
|
46
|
+
? typeof process !== "undefined" &&
|
|
47
|
+
(process.env.DFX_NETWORK === "local" ||
|
|
48
|
+
process.env.NODE_ENV === "development")
|
|
49
|
+
: false);
|
|
50
|
+
const actorManager = (0, core_1.createReActorStore)(Object.assign({ isLocalEnv,
|
|
51
|
+
withVisitor }, options));
|
|
52
|
+
const getVisitFunction = () => {
|
|
53
|
+
if (!withVisitor) {
|
|
54
|
+
throw new Error("Service fields not initialized. Pass `withVisitor` to initialize service fields.");
|
|
47
55
|
}
|
|
48
|
-
return actorManager.
|
|
56
|
+
return actorManager.visitFunction;
|
|
49
57
|
};
|
|
50
58
|
const getAgent = () => {
|
|
51
59
|
return actorManager.agentManager.getAgent();
|
|
52
60
|
};
|
|
61
|
+
const actorHooks = (0, actor_1.getActorHooks)(actorManager);
|
|
62
|
+
const authHooks = (0, auth_1.getAuthHooks)(actorManager.agentManager);
|
|
53
63
|
return Object.assign(Object.assign({ getAgent,
|
|
54
|
-
|
|
64
|
+
getVisitFunction }, actorHooks), authHooks);
|
|
55
65
|
};
|
|
56
66
|
exports.createReActor = createReActor;
|
|
67
|
+
exports.agent = __importStar(require("./context/agent"));
|
|
68
|
+
exports.actor = __importStar(require("./context/actor"));
|
|
69
|
+
exports.hooks = __importStar(require("./hooks"));
|
|
70
|
+
exports.types = __importStar(require("./types"));
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import type { ActorState,
|
|
3
|
-
import type { AuthHooks } from "./hooks/
|
|
2
|
+
import type { ActorState, CanisterId, CreateReActorOptions, ActorMethodArgs, ActorMethodReturnType, HttpAgent, Identity, Principal, FunctionName, VisitService } from "@ic-reactor/core/dist/types";
|
|
3
|
+
import type { AgentHooks, AuthHooks } from "./hooks/types";
|
|
4
|
+
export * from "@ic-reactor/core/dist/types";
|
|
5
|
+
export * from "./context/agent/types";
|
|
6
|
+
export * from "./context/actor/types";
|
|
7
|
+
export * from "./hooks/types";
|
|
4
8
|
export type AuthArgs = {
|
|
5
9
|
onAuthentication?: (promise: () => Promise<Identity>) => void;
|
|
6
10
|
onAuthenticationSuccess?: (identity: Identity) => void;
|
|
@@ -10,70 +14,63 @@ export type AuthArgs = {
|
|
|
10
14
|
onLogin?: (promise: () => Promise<Principal>) => void;
|
|
11
15
|
onLoggedOut?: () => void;
|
|
12
16
|
};
|
|
13
|
-
export type ActorCallArgs<A, M extends
|
|
14
|
-
functionName: M
|
|
15
|
-
args?:
|
|
17
|
+
export type ActorCallArgs<A, M extends FunctionName<A>> = {
|
|
18
|
+
functionName: M;
|
|
19
|
+
args?: ActorMethodArgs<A[M]>;
|
|
16
20
|
onLoading?: (loading: boolean) => void;
|
|
17
21
|
onError?: (error: Error | undefined) => void;
|
|
18
|
-
onSuccess?: (data:
|
|
22
|
+
onSuccess?: (data: ActorMethodReturnType<A[M]> | undefined) => void;
|
|
19
23
|
throwOnError?: boolean;
|
|
20
24
|
};
|
|
21
|
-
export type ActorHookState<A, M extends
|
|
22
|
-
data:
|
|
25
|
+
export type ActorHookState<A, M extends FunctionName<A>> = {
|
|
26
|
+
data: ActorMethodReturnType<A[M]> | undefined;
|
|
23
27
|
error: Error | undefined;
|
|
24
28
|
loading: boolean;
|
|
25
29
|
};
|
|
26
|
-
export interface ActorUseQueryArgs<A, M extends
|
|
30
|
+
export interface ActorUseQueryArgs<A, M extends FunctionName<A>> extends ActorCallArgs<A, M> {
|
|
27
31
|
refetchOnMount?: boolean;
|
|
28
32
|
refetchInterval?: number | false;
|
|
29
33
|
}
|
|
30
|
-
export interface
|
|
31
|
-
call: (eventOrReplaceArgs?: React.MouseEvent | ExtractActorMethodArgs<A[M]>) => Promise<unknown>;
|
|
32
|
-
field: W extends true ? ExtractedFunction<A> : undefined;
|
|
33
|
-
data: unknown;
|
|
34
|
-
error: Error | undefined;
|
|
35
|
-
loading: boolean;
|
|
34
|
+
export interface ActorUseUpdateArgs<A, M extends FunctionName<A>> extends ActorCallArgs<A, M> {
|
|
36
35
|
}
|
|
37
|
-
export
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
36
|
+
export type ActorCallReturn<A, M extends FunctionName<A>> = ActorHookState<A, M> & {
|
|
37
|
+
reset: () => void;
|
|
38
|
+
call: (eventOrReplaceArgs?: React.MouseEvent | ActorMethodArgs<A[M]>) => Promise<ActorMethodReturnType<A[M]> | undefined>;
|
|
39
|
+
};
|
|
40
|
+
export type ActorCall<A> = <M extends FunctionName<A>>(args: ActorCallArgs<A, M>) => ActorCallReturn<A, M>;
|
|
41
|
+
export type ActorQueryCall<A> = <M extends FunctionName<A>>(args: ActorUseQueryArgs<A, M>) => ActorCallReturn<A, M>;
|
|
42
|
+
export type ActorUpdateCall<A> = <M extends FunctionName<A>>(args: ActorUseUpdateArgs<A, M>) => ActorCallReturn<A, M>;
|
|
43
|
+
export type ActorMethodCall<A, M extends FunctionName<A>> = (args: ActorUseMethodCallArg<A, M>) => ActorUseMethodCallReturn<A, M>;
|
|
44
|
+
export interface ActorUseMethodCallReturn<A, M extends FunctionName<A>, F extends boolean = false> extends ActorCallReturn<A, M> {
|
|
45
|
+
visit: F extends true ? VisitService<A>[M] : undefined;
|
|
46
|
+
reset: () => void;
|
|
43
47
|
error: Error | undefined;
|
|
44
48
|
loading: boolean;
|
|
45
49
|
}
|
|
46
|
-
export type
|
|
47
|
-
export type
|
|
48
|
-
export type ActorHooksWithoutField<A> = ActorDefaultHooks<A, false>;
|
|
49
|
-
export type ActorHooks<A, W extends boolean | undefined = undefined> = W extends true ? ActorHooksWithField<A> : W extends false ? ActorHooksWithoutField<A> : ActorHooksWithField<A> | ActorHooksWithoutField<A>;
|
|
50
|
+
export type ActorUseMethodCallArg<A, M extends FunctionName<A>> = ActorCallArgs<A, M>;
|
|
51
|
+
export type ActorHooks<A, F extends boolean = false> = ActorDefaultHooks<A, F> & (F extends true ? ActorFieldHooks<A> : object);
|
|
50
52
|
export interface ActorFieldHooks<A> {
|
|
51
|
-
|
|
52
|
-
useMethodFields: () => ExtractedFunction<A>[];
|
|
53
|
-
useMethodField: (functionName: keyof A & string) => ExtractedFunction<A>;
|
|
54
|
-
useMethods: () => ServiceMethodTypeAndName<A>[];
|
|
53
|
+
useVisitMethod: <M extends FunctionName<A>>(functionName: M) => VisitService<A>[M];
|
|
55
54
|
}
|
|
56
55
|
export type UseActorStoreReturn<A> = ActorState<A> & {
|
|
57
56
|
canisterId: CanisterId;
|
|
58
57
|
};
|
|
59
|
-
export interface ActorDefaultHooks<A,
|
|
58
|
+
export interface ActorDefaultHooks<A, F extends boolean> {
|
|
60
59
|
initialize: () => Promise<void>;
|
|
61
|
-
|
|
62
|
-
useQueryCall: <
|
|
63
|
-
useUpdateCall: <
|
|
64
|
-
useMethodCall: <M extends
|
|
65
|
-
type: T;
|
|
66
|
-
}) => T extends "query" ? ActorUseQueryReturn<A, M, W> : ActorUseUpdateReturn<A, M, W>;
|
|
60
|
+
useActorState: () => UseActorStoreReturn<A>;
|
|
61
|
+
useQueryCall: ActorQueryCall<A>;
|
|
62
|
+
useUpdateCall: ActorUpdateCall<A>;
|
|
63
|
+
useMethodCall: <M extends FunctionName<A>>(args: ActorUseMethodCallArg<A, M>) => ActorUseMethodCallReturn<A, M, F>;
|
|
67
64
|
}
|
|
68
65
|
export type GetFunctions<A> = {
|
|
69
66
|
getAgent: () => HttpAgent;
|
|
70
|
-
|
|
67
|
+
getVisitFunction: () => VisitService<A>;
|
|
71
68
|
};
|
|
72
69
|
export type CreateReActor = {
|
|
73
|
-
<A
|
|
74
|
-
|
|
75
|
-
}): GetFunctions<A> &
|
|
76
|
-
<A
|
|
77
|
-
|
|
78
|
-
}): GetFunctions<A> &
|
|
70
|
+
<A>(options: CreateReActorOptions & {
|
|
71
|
+
withVisitor: true;
|
|
72
|
+
}): GetFunctions<A> & ActorHooks<A, true> & AuthHooks & AgentHooks;
|
|
73
|
+
<A>(options: CreateReActorOptions & {
|
|
74
|
+
withVisitor?: false | undefined;
|
|
75
|
+
}): GetFunctions<A> & ActorHooks<A, false> & AuthHooks & AgentHooks;
|
|
79
76
|
};
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1,20 @@
|
|
|
1
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
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("@ic-reactor/core/dist/types"), exports);
|
|
18
|
+
__exportStar(require("./context/agent/types"), exports);
|
|
19
|
+
__exportStar(require("./context/actor/types"), exports);
|
|
20
|
+
__exportStar(require("./hooks/types"), exports);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ic-reactor/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "A React library for interacting with
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "A React library for interacting with Internet Computer canisters",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"bugs": {
|
|
25
25
|
"url": "https://github.com/b3hr4d/ic-reactor/issues"
|
|
26
26
|
},
|
|
27
|
-
"homepage": "https://github.
|
|
27
|
+
"homepage": "https://b3pay.github.io/ic-reactor/modules/react.html",
|
|
28
28
|
"scripts": {
|
|
29
29
|
"test": "npx jest",
|
|
30
30
|
"start": "tsc watch",
|
|
@@ -35,18 +35,18 @@
|
|
|
35
35
|
"node": ">=10"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@ic-reactor/
|
|
38
|
+
"@ic-reactor/core": "^1.0.1",
|
|
39
39
|
"zustand-utils": "^1.3"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
|
-
"@dfinity/agent": "^0
|
|
43
|
-
"@dfinity/auth-client": "^0
|
|
44
|
-
"@dfinity/candid": "0
|
|
45
|
-
"@dfinity/identity": "^0
|
|
46
|
-
"@dfinity/principal": "^0
|
|
42
|
+
"@dfinity/agent": "^1.0",
|
|
43
|
+
"@dfinity/auth-client": "^1.0",
|
|
44
|
+
"@dfinity/candid": "1.0",
|
|
45
|
+
"@dfinity/identity": "^1.0",
|
|
46
|
+
"@dfinity/principal": "^1.0",
|
|
47
47
|
"@peculiar/webcrypto": "1.4",
|
|
48
48
|
"react": ">=16.8",
|
|
49
|
-
"zustand": "4.
|
|
49
|
+
"zustand": "4.5"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "54c9942a98bcad64e63c8d52788103f8b168720e"
|
|
52
52
|
}
|
package/dist/context/actor.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import React, { PropsWithChildren } from "react";
|
|
2
|
-
import { IDL } from "@dfinity/candid";
|
|
3
|
-
import { ActorSubclass, ActorManagerOptions } from "@ic-reactor/store";
|
|
4
|
-
import { AgentContextType } from "./agent";
|
|
5
|
-
import { ActorHooksWithField } from "../types";
|
|
6
|
-
export type ActorContextType<A = ActorSubclass<any>> = ActorHooksWithField<A>;
|
|
7
|
-
export declare const ActorContext: React.Context<ActorContextType<any> | null>;
|
|
8
|
-
type UseActorType = <A = ActorSubclass<any>>() => ActorContextType<A>;
|
|
9
|
-
export declare const useActor: UseActorType;
|
|
10
|
-
interface ActorProviderProps extends PropsWithChildren, Omit<ActorManagerOptions, "idlFactory" | "agentManager"> {
|
|
11
|
-
agentContext?: AgentContextType;
|
|
12
|
-
idlFactory?: IDL.InterfaceFactory;
|
|
13
|
-
loadingComponent?: React.ReactNode;
|
|
14
|
-
}
|
|
15
|
-
export declare const ActorProvider: React.FC<ActorProviderProps>;
|
|
16
|
-
export {};
|
package/dist/context/actor.js
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
35
|
-
var t = {};
|
|
36
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
37
|
-
t[p] = s[p];
|
|
38
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
39
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
40
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
41
|
-
t[p[i]] = s[p[i]];
|
|
42
|
-
}
|
|
43
|
-
return t;
|
|
44
|
-
};
|
|
45
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
-
exports.ActorProvider = exports.useActor = exports.ActorContext = void 0;
|
|
47
|
-
const react_1 = __importStar(require("react"));
|
|
48
|
-
const index_1 = require("../index");
|
|
49
|
-
const store_1 = require("@ic-reactor/store");
|
|
50
|
-
const agent_1 = require("./agent");
|
|
51
|
-
exports.ActorContext = (0, react_1.createContext)(null);
|
|
52
|
-
const useActor = () => {
|
|
53
|
-
const context = (0, react_1.useContext)(exports.ActorContext);
|
|
54
|
-
if (!context) {
|
|
55
|
-
throw new Error("useActor must be used within a ActorProvider");
|
|
56
|
-
}
|
|
57
|
-
return context;
|
|
58
|
-
};
|
|
59
|
-
exports.useActor = useActor;
|
|
60
|
-
const ActorProvider = (_a) => {
|
|
61
|
-
var { children, canisterId, agentContext, loadingComponent = react_1.default.createElement("div", null, "Loading..."), withServiceFields = false } = _a, config = __rest(_a, ["children", "canisterId", "agentContext", "loadingComponent", "withServiceFields"]);
|
|
62
|
-
const agentManager = (0, agent_1.useAgentManager)(agentContext);
|
|
63
|
-
const [didJs, setDidJS] = (0, react_1.useState)();
|
|
64
|
-
const [fetching, setFetching] = (0, react_1.useState)(false);
|
|
65
|
-
const fetchDidJs = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
66
|
-
if (!canisterId) {
|
|
67
|
-
throw new Error("canisterId is required");
|
|
68
|
-
}
|
|
69
|
-
if (fetching) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
setFetching(true);
|
|
73
|
-
const agent = agentManager.getAgent();
|
|
74
|
-
(0, store_1.getDidJsFromMetadata)(agent, canisterId).then((idlFactory) => __awaiter(void 0, void 0, void 0, function* () {
|
|
75
|
-
if (!idlFactory) {
|
|
76
|
-
try {
|
|
77
|
-
idlFactory = yield (0, store_1.getDidJsFromTmpHack)(agent, canisterId);
|
|
78
|
-
}
|
|
79
|
-
catch (err) {
|
|
80
|
-
if (/no query method/.test(err)) {
|
|
81
|
-
console.warn(err);
|
|
82
|
-
idlFactory = undefined;
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
throw err;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
if (!idlFactory) {
|
|
89
|
-
console.warn("No query method found for canister", canisterId);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
setDidJS(idlFactory);
|
|
93
|
-
setFetching(false);
|
|
94
|
-
}));
|
|
95
|
-
}), [canisterId, agentManager]);
|
|
96
|
-
(0, react_1.useEffect)(() => {
|
|
97
|
-
const { idlFactory } = config;
|
|
98
|
-
if (idlFactory) {
|
|
99
|
-
setDidJS({ idlFactory });
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
console.log("idlFactory not provided, fetching from canister...");
|
|
103
|
-
fetchDidJs();
|
|
104
|
-
}, [fetchDidJs, config.idlFactory]);
|
|
105
|
-
const hooks = (0, react_1.useMemo)(() => {
|
|
106
|
-
if (!didJs) {
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
try {
|
|
110
|
-
return (0, index_1.createReActor)({
|
|
111
|
-
idlFactory: didJs.idlFactory,
|
|
112
|
-
agentManager,
|
|
113
|
-
canisterId,
|
|
114
|
-
withDevtools: config.withDevtools,
|
|
115
|
-
withServiceFields: withServiceFields,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
catch (err) {
|
|
119
|
-
console.error(err);
|
|
120
|
-
return null;
|
|
121
|
-
}
|
|
122
|
-
}, [canisterId, agentManager, didJs]);
|
|
123
|
-
return (react_1.default.createElement(exports.ActorContext.Provider, { value: hooks }, fetching || hooks === null ? loadingComponent : children));
|
|
124
|
-
};
|
|
125
|
-
exports.ActorProvider = ActorProvider;
|
package/dist/context/agent.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import React, { PropsWithChildren } from "react";
|
|
2
|
-
import { AgentManager, AgentManagerOptions } from "@ic-reactor/store";
|
|
3
|
-
import { getAuthHooks } from "../hooks/auth";
|
|
4
|
-
export type AgentContextValue = ReturnType<typeof getAuthHooks> & {
|
|
5
|
-
agentManager: AgentManager;
|
|
6
|
-
};
|
|
7
|
-
export type AgentContextType = React.Context<AgentContextValue | null>;
|
|
8
|
-
export declare const AgentContext: React.Context<AgentContextValue | null>;
|
|
9
|
-
export declare const useAgent: (agentContext?: AgentContextType) => AgentContextValue;
|
|
10
|
-
export declare const useAgentManager: (agentContext?: AgentContextType) => AgentManager;
|
|
11
|
-
export declare const createAgentContext: (config: AgentManagerOptions) => AgentContextValue;
|
|
12
|
-
interface AgentProviderProps extends PropsWithChildren, AgentManagerOptions {
|
|
13
|
-
agentManager?: AgentManager;
|
|
14
|
-
}
|
|
15
|
-
export declare const AgentProvider: React.FC<AgentProviderProps>;
|
|
16
|
-
export {};
|