@ic-reactor/react 1.16.0 → 2.0.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/dist/context/actor/create.js +19 -22
- package/dist/context/actor/hooks/useActorInterface.d.ts +1 -1
- package/dist/context/actor/hooks/useMethod.d.ts +3 -3
- package/dist/context/actor/hooks/useMethod.js +3 -3
- package/dist/context/actor/hooks/useQueryCall.d.ts +2 -2
- package/dist/context/actor/hooks/useQueryCall.js +2 -2
- package/dist/context/actor/hooks/useUpdateCall.d.ts +3 -3
- package/dist/context/actor/hooks/useUpdateCall.js +3 -3
- package/dist/context/actor/hooks/useVisitMethod.d.ts +1 -1
- package/dist/context/adapter/create.d.ts +2 -2
- package/dist/context/adapter/create.js +23 -30
- package/dist/context/adapter/index.d.ts +1 -1
- package/dist/context/adapter/types.d.ts +33 -6
- package/dist/context/agent/create.js +10 -17
- package/dist/createReactor.js +7 -2
- package/dist/helpers/actorHooks.js +77 -66
- package/dist/helpers/authHooks.js +46 -44
- package/dist/helpers/extractActorContext.js +1 -1
- package/dist/helpers/types.d.ts +123 -13
- package/dist/hooks/types.d.ts +2 -2
- package/dist/hooks/useActor.js +36 -38
- package/dist/index.js +17 -7
- package/dist/types.d.ts +1 -2
- package/package.json +17 -24
|
@@ -15,33 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
};
|
|
34
|
-
|
|
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
|
-
};
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
45
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
36
|
exports.actorHooks = void 0;
|
|
47
37
|
const React = __importStar(require("react"));
|
|
@@ -52,6 +42,7 @@ const DEFAULT_STATE = {
|
|
|
52
42
|
data: undefined,
|
|
53
43
|
error: undefined,
|
|
54
44
|
loading: false,
|
|
45
|
+
isLoading: false,
|
|
55
46
|
};
|
|
56
47
|
/**
|
|
57
48
|
* Provides a set of React hooks designed for interacting with actors in an Internet Computer (IC) project using the React framework and Zustand for state management.
|
|
@@ -78,13 +69,15 @@ const actorHooks = (actorManager) => {
|
|
|
78
69
|
name: state.name,
|
|
79
70
|
error: state.error,
|
|
80
71
|
version: state.version,
|
|
81
|
-
initialized: state.
|
|
82
|
-
|
|
72
|
+
initialized: state.isInitialized,
|
|
73
|
+
isInitialized: state.isInitialized,
|
|
74
|
+
initializing: state.isInitializing,
|
|
75
|
+
isInitializing: state.isInitializing,
|
|
83
76
|
canisterId,
|
|
84
77
|
}));
|
|
85
78
|
};
|
|
86
79
|
const useMethodState = (functionName, requestKey) => {
|
|
87
|
-
const state = useActorStore((state) =>
|
|
80
|
+
const state = useActorStore((state) => state.methodState[functionName]?.[requestKey]);
|
|
88
81
|
const setSharedState = React.useCallback((newState) => {
|
|
89
82
|
updateMethodState(functionName, requestKey, newState);
|
|
90
83
|
}, [functionName, requestKey]);
|
|
@@ -113,31 +106,35 @@ const actorHooks = (actorManager) => {
|
|
|
113
106
|
return visitFunction[functionName];
|
|
114
107
|
}, [functionName]);
|
|
115
108
|
};
|
|
116
|
-
const useSharedCall = (
|
|
117
|
-
var { args = [], functionName, throwOnError = false, onError, onLoading, onSuccess, onSuccessResult, onErrorResult } = _a, options = __rest(_a, ["args", "functionName", "throwOnError", "onError", "onLoading", "onSuccess", "onSuccessResult", "onErrorResult"]);
|
|
109
|
+
const useSharedCall = ({ args = [], functionName, throwOnError = false, onError, onLoading, onSuccess, onSuccessResult, onErrorResult, ...options }) => {
|
|
118
110
|
const requestKey = React.useMemo(() => (0, utils_1.generateRequestHash)(args), [args]);
|
|
119
111
|
const [sharedState, setSharedState] = useMethodState(functionName, requestKey);
|
|
120
|
-
const latestDataRef = React.useRef();
|
|
112
|
+
const latestDataRef = React.useRef(null);
|
|
121
113
|
const reset = React.useCallback(() => {
|
|
122
114
|
updateMethodState(functionName, requestKey, DEFAULT_STATE);
|
|
123
|
-
latestDataRef.current =
|
|
115
|
+
latestDataRef.current = null;
|
|
124
116
|
}, [functionName, requestKey]);
|
|
125
|
-
const call = React.useCallback((eventOrReplaceArgs) =>
|
|
126
|
-
setSharedState({ error: undefined, loading: true });
|
|
127
|
-
onLoading
|
|
117
|
+
const call = React.useCallback(async (eventOrReplaceArgs) => {
|
|
118
|
+
setSharedState({ error: undefined, loading: true, isLoading: true });
|
|
119
|
+
onLoading?.(true);
|
|
128
120
|
try {
|
|
129
121
|
const replaceArgs = eventOrReplaceArgs instanceof Array ? eventOrReplaceArgs : args;
|
|
130
|
-
const data =
|
|
122
|
+
const data = await callMethodWithOptions(options)(functionName, ...(replaceArgs ?? args));
|
|
131
123
|
latestDataRef.current = data;
|
|
132
|
-
setSharedState({
|
|
133
|
-
|
|
134
|
-
|
|
124
|
+
setSharedState({
|
|
125
|
+
data,
|
|
126
|
+
error: undefined,
|
|
127
|
+
loading: false,
|
|
128
|
+
isLoading: false,
|
|
129
|
+
});
|
|
130
|
+
onLoading?.(false);
|
|
131
|
+
onSuccess?.(data);
|
|
135
132
|
const { isOk, value, error } = (0, utils_1.createCompiledResult)(data);
|
|
136
133
|
if (isOk) {
|
|
137
|
-
onSuccessResult
|
|
134
|
+
onSuccessResult?.(value);
|
|
138
135
|
}
|
|
139
136
|
else {
|
|
140
|
-
onErrorResult
|
|
137
|
+
onErrorResult?.(error);
|
|
141
138
|
if (throwOnError) {
|
|
142
139
|
throw error;
|
|
143
140
|
}
|
|
@@ -147,17 +144,19 @@ const actorHooks = (actorManager) => {
|
|
|
147
144
|
catch (error) {
|
|
148
145
|
// eslint-disable-next-line no-console
|
|
149
146
|
console.error(`Error calling method ${functionName}:`, error);
|
|
150
|
-
latestDataRef.current =
|
|
147
|
+
latestDataRef.current = null;
|
|
151
148
|
setSharedState({
|
|
152
149
|
error: error,
|
|
153
150
|
loading: false,
|
|
151
|
+
isLoading: false,
|
|
154
152
|
});
|
|
155
|
-
onError
|
|
156
|
-
onLoading
|
|
153
|
+
onError?.(error);
|
|
154
|
+
onLoading?.(false);
|
|
157
155
|
if (throwOnError)
|
|
158
156
|
throw error;
|
|
157
|
+
return undefined;
|
|
159
158
|
}
|
|
160
|
-
}
|
|
159
|
+
}, [
|
|
161
160
|
args,
|
|
162
161
|
functionName,
|
|
163
162
|
options,
|
|
@@ -168,19 +167,20 @@ const actorHooks = (actorManager) => {
|
|
|
168
167
|
throwOnError,
|
|
169
168
|
]);
|
|
170
169
|
const compileResult = () => {
|
|
171
|
-
return (0, utils_1.createCompiledResult)(latestDataRef.current ||
|
|
170
|
+
return (0, utils_1.createCompiledResult)(latestDataRef.current || sharedState?.data);
|
|
172
171
|
};
|
|
173
|
-
return
|
|
172
|
+
return {
|
|
173
|
+
call,
|
|
174
174
|
reset,
|
|
175
175
|
compileResult,
|
|
176
|
-
requestKey
|
|
176
|
+
requestKey,
|
|
177
|
+
...sharedState,
|
|
178
|
+
};
|
|
177
179
|
};
|
|
178
|
-
const useQueryCall = (
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
const intervalId = React.useRef();
|
|
180
|
+
const useQueryCall = ({ refetchOnMount = true, refetchInterval = false, ...rest }) => {
|
|
181
|
+
const { call, requestKey, ...state } = useSharedCall(rest);
|
|
182
|
+
const intervalId = React.useRef(null);
|
|
182
183
|
React.useEffect(() => {
|
|
183
|
-
var _a, _b, _c;
|
|
184
184
|
if (refetchInterval) {
|
|
185
185
|
intervalId.current = setInterval(call, refetchInterval);
|
|
186
186
|
}
|
|
@@ -188,21 +188,25 @@ const actorHooks = (actorManager) => {
|
|
|
188
188
|
call();
|
|
189
189
|
}
|
|
190
190
|
else if (refetchOnMount && state.data !== undefined) {
|
|
191
|
-
|
|
191
|
+
rest.onSuccess?.(state.data);
|
|
192
192
|
const { isOk, value, error } = (0, utils_1.createCompiledResult)(state.data);
|
|
193
193
|
if (isOk) {
|
|
194
|
-
|
|
194
|
+
rest.onSuccessResult?.(value);
|
|
195
195
|
}
|
|
196
196
|
else {
|
|
197
|
-
|
|
197
|
+
rest.onErrorResult?.(error);
|
|
198
198
|
}
|
|
199
199
|
}
|
|
200
|
-
return () =>
|
|
200
|
+
return () => {
|
|
201
|
+
if (intervalId.current) {
|
|
202
|
+
clearInterval(intervalId.current);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
201
205
|
}, [refetchInterval, refetchOnMount, requestKey]);
|
|
202
206
|
const refetch = () => {
|
|
203
207
|
call();
|
|
204
208
|
};
|
|
205
|
-
return
|
|
209
|
+
return { call, refetch, intervalId, requestKey, ...state };
|
|
206
210
|
};
|
|
207
211
|
const useUpdateCall = useSharedCall;
|
|
208
212
|
const useMethod = (params) => {
|
|
@@ -236,21 +240,28 @@ const actorHooks = (actorManager) => {
|
|
|
236
240
|
}, [attributes]);
|
|
237
241
|
let refetchOnMount = params.refetchOnMount;
|
|
238
242
|
let refetchInterval = params.refetchInterval;
|
|
239
|
-
let
|
|
243
|
+
let isFormRequired = true;
|
|
240
244
|
switch (attributes.type) {
|
|
241
245
|
case "query":
|
|
242
246
|
if (validateArgs(params.args)) {
|
|
243
|
-
|
|
247
|
+
isFormRequired = params.refetchOnMount === false ? true : false;
|
|
244
248
|
}
|
|
245
249
|
else {
|
|
246
250
|
refetchOnMount = false;
|
|
247
251
|
refetchInterval = false;
|
|
248
252
|
}
|
|
249
|
-
return
|
|
250
|
-
|
|
251
|
-
|
|
253
|
+
return {
|
|
254
|
+
visit,
|
|
255
|
+
validateArgs,
|
|
256
|
+
...useQueryCall({
|
|
257
|
+
...params,
|
|
258
|
+
refetchOnMount,
|
|
259
|
+
refetchInterval,
|
|
260
|
+
}),
|
|
261
|
+
isFormRequired,
|
|
262
|
+
};
|
|
252
263
|
case "update":
|
|
253
|
-
return
|
|
264
|
+
return { visit, validateArgs, ...useUpdateCall(params), isFormRequired };
|
|
254
265
|
default:
|
|
255
266
|
throw new Error(`Method type ${attributes.type} not found`);
|
|
256
267
|
}
|
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -19,93 +10,104 @@ const utils_1 = require("@ic-reactor/core/dist/utils");
|
|
|
19
10
|
const authHooks = (agentManager) => {
|
|
20
11
|
const { authenticate: authenticator, getIsLocal, getAuth, authStore, } = agentManager;
|
|
21
12
|
const useAuthState = () => (0, zustand_1.useStore)(authStore);
|
|
22
|
-
const useUserPrincipal = () =>
|
|
13
|
+
const useUserPrincipal = () => useAuthState()?.identity?.getPrincipal();
|
|
23
14
|
const useAuth = ({ onAuthentication, onAuthenticationSuccess, onAuthenticationFailure, onLogin, onLoginSuccess, onLoginError, onLoggedOut, } = {}) => {
|
|
24
15
|
const [loginState, setLoginState] = react_1.default.useState({
|
|
25
16
|
loading: false,
|
|
17
|
+
isLoading: false,
|
|
26
18
|
error: undefined,
|
|
27
19
|
});
|
|
28
|
-
const {
|
|
29
|
-
const authenticate = react_1.default.useCallback(() =>
|
|
20
|
+
const { isAuthenticated, isAuthenticating, error, identity } = useAuthState();
|
|
21
|
+
const authenticate = react_1.default.useCallback(async () => {
|
|
30
22
|
const authenticatePromise = new Promise((resolve, reject) => {
|
|
31
23
|
authenticator()
|
|
32
24
|
.then((identity) => {
|
|
33
|
-
onAuthenticationSuccess
|
|
25
|
+
onAuthenticationSuccess?.(identity);
|
|
34
26
|
resolve(identity);
|
|
35
27
|
})
|
|
36
28
|
.catch((e) => {
|
|
37
|
-
onAuthenticationFailure
|
|
29
|
+
onAuthenticationFailure?.(e);
|
|
38
30
|
reject(e);
|
|
39
31
|
});
|
|
40
32
|
});
|
|
41
|
-
onAuthentication
|
|
33
|
+
onAuthentication?.(() => authenticatePromise);
|
|
42
34
|
return authenticatePromise;
|
|
43
|
-
}
|
|
35
|
+
}, [
|
|
44
36
|
authenticator,
|
|
45
37
|
onAuthentication,
|
|
46
38
|
onAuthenticationSuccess,
|
|
47
39
|
onAuthenticationFailure,
|
|
48
40
|
]);
|
|
49
|
-
const login = react_1.default.useCallback((options) =>
|
|
50
|
-
setLoginState({ loading: true, error: undefined });
|
|
41
|
+
const login = react_1.default.useCallback(async (options) => {
|
|
42
|
+
setLoginState({ loading: true, isLoading: true, error: undefined });
|
|
51
43
|
const loginPromise = new Promise((resolve, reject) => {
|
|
52
44
|
try {
|
|
53
45
|
const authClient = getAuth();
|
|
54
46
|
if (!authClient) {
|
|
55
47
|
throw new Error("Auth client not initialized");
|
|
56
48
|
}
|
|
57
|
-
authClient.login(
|
|
49
|
+
authClient.login({
|
|
50
|
+
identityProvider: getIsLocal()
|
|
58
51
|
? utils_1.LOCAL_INTERNET_IDENTITY_PROVIDER
|
|
59
|
-
: utils_1.IC_INTERNET_IDENTITY_PROVIDER
|
|
52
|
+
: utils_1.IC_INTERNET_IDENTITY_PROVIDER,
|
|
53
|
+
...options,
|
|
54
|
+
onSuccess: (msg) => {
|
|
60
55
|
authenticate()
|
|
61
56
|
.then((identity) => {
|
|
62
|
-
var _a;
|
|
63
57
|
const principal = identity.getPrincipal();
|
|
64
|
-
|
|
65
|
-
onLoginSuccess
|
|
58
|
+
options?.onSuccess?.(msg);
|
|
59
|
+
onLoginSuccess?.(principal);
|
|
66
60
|
resolve(principal);
|
|
67
|
-
setLoginState({
|
|
61
|
+
setLoginState({
|
|
62
|
+
loading: false,
|
|
63
|
+
isLoading: false,
|
|
64
|
+
error: undefined,
|
|
65
|
+
});
|
|
68
66
|
})
|
|
69
67
|
.catch((error) => {
|
|
70
|
-
setLoginState({ loading: false, error });
|
|
71
|
-
onLoginError
|
|
68
|
+
setLoginState({ loading: false, isLoading: false, error });
|
|
69
|
+
onLoginError?.(error);
|
|
72
70
|
reject(error);
|
|
73
71
|
});
|
|
74
|
-
},
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
setLoginState({ loading: false, error });
|
|
78
|
-
onLoginError
|
|
72
|
+
},
|
|
73
|
+
onError: (error) => {
|
|
74
|
+
options?.onError?.(error);
|
|
75
|
+
setLoginState({ loading: false, isLoading: false, error });
|
|
76
|
+
onLoginError?.(error);
|
|
79
77
|
reject(error);
|
|
80
|
-
}
|
|
78
|
+
},
|
|
79
|
+
});
|
|
81
80
|
}
|
|
82
81
|
catch (e) {
|
|
83
82
|
const error = e;
|
|
84
|
-
setLoginState({ loading: false, error });
|
|
85
|
-
onLoginError
|
|
83
|
+
setLoginState({ loading: false, isLoading: false, error });
|
|
84
|
+
onLoginError?.(error);
|
|
86
85
|
reject(error);
|
|
87
86
|
}
|
|
88
87
|
});
|
|
89
|
-
onLogin
|
|
90
|
-
}
|
|
91
|
-
const logout = react_1.default.useCallback((options) =>
|
|
88
|
+
onLogin?.(() => loginPromise);
|
|
89
|
+
}, [onLogin, onLoginSuccess, onLoginError, authenticate]);
|
|
90
|
+
const logout = react_1.default.useCallback(async (options) => {
|
|
92
91
|
const authClient = getAuth();
|
|
93
92
|
if (!authClient) {
|
|
94
93
|
throw new Error("Auth client not initialized");
|
|
95
94
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
onLoggedOut
|
|
99
|
-
}
|
|
95
|
+
await authClient.logout(options);
|
|
96
|
+
await authenticate();
|
|
97
|
+
onLoggedOut?.();
|
|
98
|
+
}, [onLoggedOut]);
|
|
100
99
|
return {
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
isAuthenticated,
|
|
101
|
+
isAuthenticating,
|
|
102
|
+
authenticated: isAuthenticated,
|
|
103
|
+
authenticating: isAuthenticating,
|
|
103
104
|
identity,
|
|
104
105
|
error,
|
|
105
106
|
login,
|
|
106
107
|
logout,
|
|
107
108
|
authenticate,
|
|
108
|
-
loginLoading: loginState.
|
|
109
|
+
loginLoading: loginState.isLoading,
|
|
110
|
+
isLoginLoading: loginState.isLoading,
|
|
109
111
|
loginError: loginState.error,
|
|
110
112
|
};
|
|
111
113
|
};
|
|
@@ -26,7 +26,7 @@ function extractActorContext(actorContext) {
|
|
|
26
26
|
const useVisitMethod = (functionName) => useActorContext().useVisitMethod(functionName);
|
|
27
27
|
const useVisitService = () => useActorContext().useVisitService();
|
|
28
28
|
const useActorInterface = () => useActorContext().useActorInterface();
|
|
29
|
-
const useInitializeActor = () =>
|
|
29
|
+
const useInitializeActor = () => useActorContext().useInitializeActor?.();
|
|
30
30
|
return {
|
|
31
31
|
useActorStore,
|
|
32
32
|
useActorState,
|
package/dist/helpers/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CallConfig } from "@dfinity/agent";
|
|
1
|
+
import { AgentError, CallConfig } from "@dfinity/agent";
|
|
2
2
|
import type { IDL, ActorState, AuthClientLoginOptions, ActorMethodParameters, ActorMethodReturnType, Identity, Principal, FunctionName, VisitService, AuthState, HttpAgent, AgentState, BaseActor, MethodAttributes, CompiledResult, ExtractOk, ExtractErr } from "../types";
|
|
3
3
|
export interface AgentHooksReturnType {
|
|
4
4
|
useAgent: () => HttpAgent | undefined;
|
|
@@ -18,19 +18,80 @@ export interface UseAuthParameters {
|
|
|
18
18
|
onLogin?: (promise: () => Promise<Principal>) => void;
|
|
19
19
|
onLoggedOut?: () => void;
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* The return type for authentication hooks.
|
|
23
|
+
*/
|
|
21
24
|
export interface UseAuthReturnType {
|
|
25
|
+
/**
|
|
26
|
+
* Any non-login related error that occurred.
|
|
27
|
+
*/
|
|
22
28
|
error: Error | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* @deprecated Use `isAuthenticated` instead.
|
|
31
|
+
* Indicates whether the user is authenticated.
|
|
32
|
+
*/
|
|
23
33
|
authenticated: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Indicates whether the user is authenticated.
|
|
36
|
+
*/
|
|
37
|
+
isAuthenticated: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* @deprecated Use `isAuthenticating` instead.
|
|
40
|
+
* Indicates whether an authentication request is in progress.
|
|
41
|
+
*/
|
|
24
42
|
authenticating: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Indicates whether an authentication request is in progress.
|
|
45
|
+
*/
|
|
46
|
+
isAuthenticating: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* The current identity object, or `null` if not authenticated.
|
|
49
|
+
*/
|
|
25
50
|
identity: Identity | null;
|
|
51
|
+
/**
|
|
52
|
+
* Initiates the login flow with optional parameters.
|
|
53
|
+
* @param options Login parameters (e.g. redirect URL).
|
|
54
|
+
*/
|
|
26
55
|
login: (options?: LoginParameters) => Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Logs the user out with optional parameters.
|
|
58
|
+
* @param options Logout parameters (e.g. return URL).
|
|
59
|
+
*/
|
|
27
60
|
logout: (options?: LogoutParameters) => Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Triggers the authentication flow and resolves to an `Identity`.
|
|
63
|
+
*/
|
|
28
64
|
authenticate: () => Promise<Identity>;
|
|
65
|
+
/**
|
|
66
|
+
* @deprecated Use `isLoginLoading` instead.
|
|
67
|
+
* Indicates whether the login operation is in progress.
|
|
68
|
+
*/
|
|
29
69
|
loginLoading: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Indicates whether the login operation is in progress.
|
|
72
|
+
*/
|
|
73
|
+
isLoginLoading: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* The error message, if any, occurred during login.
|
|
76
|
+
*/
|
|
30
77
|
loginError: string | undefined;
|
|
31
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Represents the state of a login operation.
|
|
81
|
+
*/
|
|
32
82
|
export type LoginState = {
|
|
83
|
+
/**
|
|
84
|
+
* @deprecated Use `isLoading` instead.
|
|
85
|
+
* Indicates whether the login operation is in progress.
|
|
86
|
+
*/
|
|
33
87
|
loading: boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Indicates whether the login operation is in progress.
|
|
90
|
+
*/
|
|
91
|
+
isLoading: boolean;
|
|
92
|
+
/**
|
|
93
|
+
* The error message, if any, occurred during login.
|
|
94
|
+
*/
|
|
34
95
|
error: string | undefined;
|
|
35
96
|
};
|
|
36
97
|
export type LoginParameters = AuthClientLoginOptions;
|
|
@@ -41,21 +102,37 @@ export type UseActorStore<A> = <T>(callback?: (state: ActorState<A>) => T) => T;
|
|
|
41
102
|
export interface UseActorStateReturnType extends Omit<ActorState, "methodState"> {
|
|
42
103
|
canisterId: string;
|
|
43
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* State for shared calls, including the result, error, and loading status.
|
|
107
|
+
*/
|
|
44
108
|
export type UseSharedCallState<A, M extends FunctionName<A>> = {
|
|
109
|
+
/**
|
|
110
|
+
* The data returned from the call, or `undefined` if not yet available.
|
|
111
|
+
*/
|
|
45
112
|
data: ActorMethodReturnType<A[M]> | undefined;
|
|
46
|
-
|
|
113
|
+
/**
|
|
114
|
+
* The error that occurred during the call, or `undefined` if none.
|
|
115
|
+
*/
|
|
116
|
+
error: AgentError | undefined;
|
|
117
|
+
/**
|
|
118
|
+
* @deprecated Use `isLoading` instead.
|
|
119
|
+
* Indicates whether the call is in progress.
|
|
120
|
+
*/
|
|
47
121
|
loading: boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Indicates whether the call is in progress.
|
|
124
|
+
*/
|
|
125
|
+
isLoading: boolean;
|
|
48
126
|
};
|
|
49
127
|
export interface UseSharedCallParameters<A, M extends FunctionName<A>> extends CallConfig {
|
|
50
128
|
functionName: M;
|
|
51
129
|
args?: ActorMethodParameters<A[M]>;
|
|
52
130
|
onLoading?: (loading: boolean) => void;
|
|
53
|
-
onError?: (error:
|
|
131
|
+
onError?: (error: AgentError | undefined) => void;
|
|
54
132
|
onSuccess?: (data: ActorMethodReturnType<A[M]>) => void;
|
|
55
133
|
onSuccessResult?: (value: ExtractOk<ActorMethodReturnType<A[M]>>) => void;
|
|
56
134
|
onErrorResult?: (error: ExtractErr<ActorMethodReturnType<A[M]>>) => void;
|
|
57
135
|
throwOnError?: boolean;
|
|
58
|
-
compileResult?: boolean;
|
|
59
136
|
}
|
|
60
137
|
export interface UseSharedCallReturnType<A, M extends FunctionName<A> = FunctionName<A>> extends UseSharedCallState<A, M> {
|
|
61
138
|
requestKey: string;
|
|
@@ -72,26 +149,59 @@ export interface UseQueryCallReturnType<A, M extends FunctionName<A>> extends Us
|
|
|
72
149
|
refetch: () => void;
|
|
73
150
|
}
|
|
74
151
|
export type UseQueryCall<A> = <M extends FunctionName<A>>(params: UseQueryCallParameters<A, M>) => UseQueryCallReturnType<A, M>;
|
|
75
|
-
export
|
|
76
|
-
|
|
77
|
-
export interface UseUpdateCallReturnType<A, M extends FunctionName<A>> extends UseSharedCallReturnType<A, M> {
|
|
78
|
-
}
|
|
152
|
+
export type UseUpdateCallParameters<A, M extends FunctionName<A>> = UseSharedCallParameters<A, M>;
|
|
153
|
+
export type UseUpdateCallReturnType<A, M extends FunctionName<A>> = UseSharedCallReturnType<A, M>;
|
|
79
154
|
export type UseUpdateCall<A> = <M extends FunctionName<A>>(params: UseUpdateCallParameters<A, M>) => UseUpdateCallReturnType<A, M>;
|
|
80
155
|
export interface DynamicDataArgs<V = unknown> {
|
|
81
156
|
label: string;
|
|
82
157
|
value: V;
|
|
83
158
|
}
|
|
84
|
-
export
|
|
85
|
-
}
|
|
159
|
+
export type UseMethodParameters<A, M extends FunctionName<A>> = UseQueryCallParameters<A, M>;
|
|
86
160
|
export interface UseMethodReturnType<A, M extends FunctionName<A> = FunctionName<A>> {
|
|
161
|
+
/**
|
|
162
|
+
* @deprecated Use `isLoading` instead.
|
|
163
|
+
* Indicates whether the method call is in progress.
|
|
164
|
+
*/
|
|
87
165
|
loading: boolean;
|
|
88
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Indicates whether the method call is in progress.
|
|
168
|
+
*/
|
|
169
|
+
isLoading: boolean;
|
|
170
|
+
/**
|
|
171
|
+
* Indicates whether the argument form is required for the method.
|
|
172
|
+
*/
|
|
173
|
+
isFormRequired: boolean;
|
|
174
|
+
/**
|
|
175
|
+
* A unique key representing the current request instance.
|
|
176
|
+
*/
|
|
89
177
|
requestKey: string;
|
|
90
|
-
|
|
178
|
+
/**
|
|
179
|
+
* The error that occurred during the method call, if any.
|
|
180
|
+
*/
|
|
181
|
+
error: AgentError | undefined;
|
|
182
|
+
/**
|
|
183
|
+
* The data returned from the method call, or `undefined` if not yet available.
|
|
184
|
+
*/
|
|
91
185
|
data: ActorMethodReturnType<A[M]> | undefined;
|
|
92
|
-
|
|
186
|
+
/**
|
|
187
|
+
* Validates the provided arguments against the method signature.
|
|
188
|
+
* @param args Optional arguments for the method.
|
|
189
|
+
* @returns `true` if the arguments match the expected signature, otherwise `false`.
|
|
190
|
+
*/
|
|
191
|
+
validateArgs: (args?: ActorMethodParameters<A[M]>) => boolean;
|
|
192
|
+
/**
|
|
193
|
+
* The visit service function corresponding to this method.
|
|
194
|
+
*/
|
|
93
195
|
visit: VisitService<A>[M];
|
|
196
|
+
/**
|
|
197
|
+
* Resets the method state (data, error, loading) to initial values.
|
|
198
|
+
*/
|
|
94
199
|
reset: () => void;
|
|
200
|
+
/**
|
|
201
|
+
* Invokes the method.
|
|
202
|
+
* @param eventOrReplaceArgs Either the arguments for the call or a React mouse event.
|
|
203
|
+
* @returns A promise resolving to the method's return data or `undefined`.
|
|
204
|
+
*/
|
|
95
205
|
call: (eventOrReplaceArgs?: ActorMethodParameters<A[M]> | React.MouseEvent) => Promise<ActorMethodReturnType<A[M]> | undefined>;
|
|
96
206
|
}
|
|
97
207
|
export type UseMethod<A> = <M extends FunctionName<A>>(args: UseMethodParameters<A, M>) => UseMethodReturnType<A, M>;
|
package/dist/hooks/types.d.ts
CHANGED
|
@@ -11,9 +11,9 @@ export interface UseActorManagerParameters<A> extends ActorReConfigParameters {
|
|
|
11
11
|
}
|
|
12
12
|
export interface UseActorReturn<A = BaseActor> {
|
|
13
13
|
hooks: ActorHooksReturnType<A> | null;
|
|
14
|
-
|
|
14
|
+
isFetching: boolean;
|
|
15
15
|
fetchError: string | null;
|
|
16
|
-
|
|
16
|
+
isAuthenticating: boolean;
|
|
17
17
|
initializeActor: InitializeActor;
|
|
18
18
|
}
|
|
19
19
|
export interface UseActorManagerReturn<A = BaseActor> {
|