@capxul/sdk-react 0.2.0-alpha.4 → 1.0.0-alpha.6
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/index.d.mts +263 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +568 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +37 -75
- package/CHANGELOG.md +0 -304
- package/LICENSE +0 -44
- package/README.md +0 -187
- package/dist/index.cjs +0 -1289
- package/dist/index.d.cts +0 -673
- package/dist/index.d.ts +0 -673
- package/dist/index.js +0 -1230
- package/dist/proof/index.cjs +0 -12785
- package/dist/proof/index.d.cts +0 -753
- package/dist/proof/index.d.ts +0 -753
- package/dist/proof/index.js +0 -12758
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { QueryClient, QueryClientProvider, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
4
|
+
import { createCapxulClient, isSettingUpLifecycle } from "@capxul/sdk";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
import { Errors } from "@capxul/config";
|
|
7
|
+
//#region src/internal/capxul-bootstrap-context.tsx
|
|
8
|
+
const CapxulBootstrapContext = createContext(null);
|
|
9
|
+
function CapxulBootstrapProvider({ value, children }) {
|
|
10
|
+
return /* @__PURE__ */ jsx(CapxulBootstrapContext.Provider, {
|
|
11
|
+
value,
|
|
12
|
+
children
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
function useCapxul() {
|
|
16
|
+
const state = useContext(CapxulBootstrapContext);
|
|
17
|
+
if (state === null) throw new Error("useCapxul must be used within <CapxulProvider>");
|
|
18
|
+
return state;
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/internal/capxul-client-context.tsx
|
|
22
|
+
const MISSING_CAPXUL_CLIENT_PROVIDER = Symbol("MISSING_CAPXUL_CLIENT_PROVIDER");
|
|
23
|
+
const CapxulClientContext = createContext(MISSING_CAPXUL_CLIENT_PROVIDER);
|
|
24
|
+
function CapxulClientProvider({ client, children }) {
|
|
25
|
+
return /* @__PURE__ */ jsx(CapxulClientContext.Provider, {
|
|
26
|
+
value: client,
|
|
27
|
+
children
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function useCapxulClient() {
|
|
31
|
+
const client = useCapxulClientOrNull();
|
|
32
|
+
if (client === null) throw new Error("useCapxulClient called before <CapxulProvider> bootstrap resolved");
|
|
33
|
+
return client;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns the client, or `null` while `<CapxulProvider>` is still bootstrapping.
|
|
37
|
+
* Data hooks use this so they can sit in `isPending` (disabled query) until the
|
|
38
|
+
* client resolves, rather than throwing during bootstrap.
|
|
39
|
+
*/
|
|
40
|
+
function useCapxulClientOrNull() {
|
|
41
|
+
const client = useContext(CapxulClientContext);
|
|
42
|
+
if (client === MISSING_CAPXUL_CLIENT_PROVIDER) throw new Error("useCapxulClient must be used within <CapxulProvider>");
|
|
43
|
+
return client;
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/provider.tsx
|
|
47
|
+
function makeDefaultQueryClient() {
|
|
48
|
+
return new QueryClient({ defaultOptions: {
|
|
49
|
+
queries: {
|
|
50
|
+
retry: 2,
|
|
51
|
+
staleTime: 3e4
|
|
52
|
+
},
|
|
53
|
+
mutations: { retry: 0 }
|
|
54
|
+
} });
|
|
55
|
+
}
|
|
56
|
+
function isCapxulQueryKey(queryKey) {
|
|
57
|
+
return queryKey[0] === "capxul";
|
|
58
|
+
}
|
|
59
|
+
function clearClientScopedQueries(queryClient, ownsQueryClient) {
|
|
60
|
+
if (ownsQueryClient) {
|
|
61
|
+
queryClient.clear();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
queryClient.removeQueries({ predicate: (query) => isCapxulQueryKey(query.queryKey) });
|
|
65
|
+
}
|
|
66
|
+
function CapxulProvider(props) {
|
|
67
|
+
const { publishableKey, client: injectedClient, requirement, signer, queryClient, children } = props;
|
|
68
|
+
const [resolvedQueryClient] = useState(() => queryClient ?? makeDefaultQueryClient());
|
|
69
|
+
const [ownsQueryClient] = useState(() => queryClient === void 0);
|
|
70
|
+
const [client, setClient] = useState(injectedClient ?? null);
|
|
71
|
+
const previousClientRef = useRef(injectedClient ?? null);
|
|
72
|
+
const [status, setStatus] = useState(injectedClient === void 0 ? "bootstrapping" : "ready");
|
|
73
|
+
const [error, setError] = useState(null);
|
|
74
|
+
const [attempt, setAttempt] = useState(0);
|
|
75
|
+
const retry = useCallback(() => {
|
|
76
|
+
setAttempt((n) => n + 1);
|
|
77
|
+
}, []);
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
if (publishableKey === void 0) return;
|
|
80
|
+
let cancelled = false;
|
|
81
|
+
let created = null;
|
|
82
|
+
setStatus("bootstrapping");
|
|
83
|
+
setError(null);
|
|
84
|
+
setClient(null);
|
|
85
|
+
(async () => {
|
|
86
|
+
const result = await createCapxulClient({
|
|
87
|
+
publishableKey,
|
|
88
|
+
...requirement === void 0 ? {} : { requirement },
|
|
89
|
+
...signer === void 0 ? {} : { signer }
|
|
90
|
+
});
|
|
91
|
+
if (cancelled) {
|
|
92
|
+
if (result.ok) await result.value._internal.close?.();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (result.ok) {
|
|
96
|
+
created = result.value;
|
|
97
|
+
setClient(result.value);
|
|
98
|
+
setStatus("ready");
|
|
99
|
+
} else {
|
|
100
|
+
setError(result.error);
|
|
101
|
+
setStatus("error");
|
|
102
|
+
}
|
|
103
|
+
})();
|
|
104
|
+
return () => {
|
|
105
|
+
cancelled = true;
|
|
106
|
+
created?._internal.close?.();
|
|
107
|
+
};
|
|
108
|
+
}, [
|
|
109
|
+
publishableKey,
|
|
110
|
+
requirement,
|
|
111
|
+
signer,
|
|
112
|
+
attempt
|
|
113
|
+
]);
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
if (injectedClient === void 0) return;
|
|
116
|
+
setClient(injectedClient);
|
|
117
|
+
setStatus("ready");
|
|
118
|
+
setError(null);
|
|
119
|
+
}, [injectedClient]);
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
const previous = previousClientRef.current;
|
|
122
|
+
if (previous !== null && previous !== client) clearClientScopedQueries(resolvedQueryClient, ownsQueryClient);
|
|
123
|
+
previousClientRef.current = client;
|
|
124
|
+
}, [
|
|
125
|
+
client,
|
|
126
|
+
ownsQueryClient,
|
|
127
|
+
resolvedQueryClient
|
|
128
|
+
]);
|
|
129
|
+
const bootstrapState = useMemo(() => ({
|
|
130
|
+
status,
|
|
131
|
+
error,
|
|
132
|
+
retry
|
|
133
|
+
}), [
|
|
134
|
+
status,
|
|
135
|
+
error,
|
|
136
|
+
retry
|
|
137
|
+
]);
|
|
138
|
+
if (publishableKey === void 0 === (injectedClient === void 0)) throw new Error("CapxulProvider requires exactly one of `publishableKey` or `client`");
|
|
139
|
+
return /* @__PURE__ */ jsx(QueryClientProvider, {
|
|
140
|
+
client: resolvedQueryClient,
|
|
141
|
+
children: /* @__PURE__ */ jsx(CapxulBootstrapProvider, {
|
|
142
|
+
value: bootstrapState,
|
|
143
|
+
children: /* @__PURE__ */ jsx(CapxulClientProvider, {
|
|
144
|
+
client,
|
|
145
|
+
children
|
|
146
|
+
})
|
|
147
|
+
})
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
//#endregion
|
|
151
|
+
//#region src/internal/require-bootstrapped-client.ts
|
|
152
|
+
/**
|
|
153
|
+
* Narrow the bootstrap-nullable client to a ready client inside a query /
|
|
154
|
+
* mutation function (sdk-provider-owned-bootstrap.md). Data hooks gate their
|
|
155
|
+
* queries on `enabled: client !== null`, so this only ever throws for a mutation
|
|
156
|
+
* triggered while `<CapxulProvider>` is still bootstrapping.
|
|
157
|
+
*/
|
|
158
|
+
function requireBootstrappedClient(client, method) {
|
|
159
|
+
if (client === null) throw Errors.wrongState({
|
|
160
|
+
method,
|
|
161
|
+
currentState: "bootstrapping",
|
|
162
|
+
validStates: ["ready"]
|
|
163
|
+
});
|
|
164
|
+
return client;
|
|
165
|
+
}
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/internal/reactivity-keys.ts
|
|
168
|
+
const capxulKeys = {
|
|
169
|
+
session: ["capxul", "session"],
|
|
170
|
+
profile: ["capxul", "profile"],
|
|
171
|
+
account: ["capxul", "account"],
|
|
172
|
+
accountLifecycle: ["capxul", "accountLifecycle"],
|
|
173
|
+
provisioning: ["capxul", "provisioning"],
|
|
174
|
+
binding: ["capxul", "binding"],
|
|
175
|
+
accountBalance: ["capxul", "accountBalance"],
|
|
176
|
+
subAccounts: (accountId) => [
|
|
177
|
+
"capxul",
|
|
178
|
+
"subAccounts",
|
|
179
|
+
accountId ?? "pending"
|
|
180
|
+
],
|
|
181
|
+
orgs: ["capxul", "orgs"],
|
|
182
|
+
org: (orgId) => [
|
|
183
|
+
"capxul",
|
|
184
|
+
"org",
|
|
185
|
+
orgId ?? "pending"
|
|
186
|
+
],
|
|
187
|
+
orgMembers: (orgId) => [
|
|
188
|
+
"capxul",
|
|
189
|
+
"org",
|
|
190
|
+
orgId ?? "pending",
|
|
191
|
+
"members"
|
|
192
|
+
],
|
|
193
|
+
orgRoles: (orgId) => [
|
|
194
|
+
"capxul",
|
|
195
|
+
"org",
|
|
196
|
+
orgId ?? "pending",
|
|
197
|
+
"roles"
|
|
198
|
+
],
|
|
199
|
+
orgTreasury: (orgId) => [
|
|
200
|
+
"capxul",
|
|
201
|
+
"org",
|
|
202
|
+
orgId ?? "pending",
|
|
203
|
+
"treasury"
|
|
204
|
+
]
|
|
205
|
+
};
|
|
206
|
+
//#endregion
|
|
207
|
+
//#region src/internal/unwrap-capxul-result.ts
|
|
208
|
+
/** Unwrap a `CapxulResult` for TanStack query/mutation functions — throws into error paths. */
|
|
209
|
+
function unwrapCapxulResult(result) {
|
|
210
|
+
if (result.ok) return result.value;
|
|
211
|
+
throw result.error;
|
|
212
|
+
}
|
|
213
|
+
//#endregion
|
|
214
|
+
//#region src/hooks/use-capxul-session.ts
|
|
215
|
+
function useCapxulSession() {
|
|
216
|
+
const client = useCapxulClientOrNull();
|
|
217
|
+
return useQuery({
|
|
218
|
+
queryKey: capxulKeys.session,
|
|
219
|
+
queryFn: async () => unwrapCapxulResult(await requireBootstrappedClient(client, "auth.getSession").auth.getSession()),
|
|
220
|
+
enabled: client !== null
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
//#endregion
|
|
224
|
+
//#region src/hooks/use-capxul-profile.ts
|
|
225
|
+
function useCapxulProfile() {
|
|
226
|
+
const client = useCapxulClientOrNull();
|
|
227
|
+
return useQuery({
|
|
228
|
+
queryKey: capxulKeys.profile,
|
|
229
|
+
queryFn: async () => unwrapCapxulResult(await requireBootstrappedClient(client, "identity.loadCurrent").identity.loadCurrent()),
|
|
230
|
+
enabled: client !== null
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
//#endregion
|
|
234
|
+
//#region src/internal/is-vitest-runtime.ts
|
|
235
|
+
/** True under Vitest — disables hook polling intervals that fight fake timers. */
|
|
236
|
+
function isVitestRuntime() {
|
|
237
|
+
return typeof process !== "undefined" && process.env["VITEST"] === "true";
|
|
238
|
+
}
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/internal/invalidate-auth-boundary.ts
|
|
241
|
+
/** Background refetch after verifyOtp — avoids hard reset cancel errors in UI. */
|
|
242
|
+
async function invalidateAuthBoundary(queryClient) {
|
|
243
|
+
await Promise.all([
|
|
244
|
+
queryClient.invalidateQueries({ queryKey: capxulKeys.session }),
|
|
245
|
+
queryClient.invalidateQueries({ queryKey: capxulKeys.profile }),
|
|
246
|
+
queryClient.invalidateQueries({ queryKey: capxulKeys.accountLifecycle }),
|
|
247
|
+
queryClient.invalidateQueries({ queryKey: capxulKeys.accountBalance })
|
|
248
|
+
]);
|
|
249
|
+
}
|
|
250
|
+
/** Hard reset after signOut — drop cached authenticated rows immediately. */
|
|
251
|
+
async function resetAuthBoundary(queryClient) {
|
|
252
|
+
await queryClient.cancelQueries({ queryKey: capxulKeys.accountBalance });
|
|
253
|
+
await Promise.all([
|
|
254
|
+
queryClient.resetQueries({ queryKey: capxulKeys.session }),
|
|
255
|
+
queryClient.resetQueries({ queryKey: capxulKeys.profile }),
|
|
256
|
+
queryClient.resetQueries({ queryKey: capxulKeys.accountLifecycle }),
|
|
257
|
+
queryClient.resetQueries({ queryKey: capxulKeys.accountBalance })
|
|
258
|
+
]);
|
|
259
|
+
}
|
|
260
|
+
//#endregion
|
|
261
|
+
//#region src/hooks/use-capxul-account-lifecycle.ts
|
|
262
|
+
const LOADING_LIFECYCLE = { status: "loading" };
|
|
263
|
+
function useCapxulAccountLifecycle() {
|
|
264
|
+
const client = useCapxulClientOrNull();
|
|
265
|
+
const queryClient = useQueryClient();
|
|
266
|
+
const query = useQuery({
|
|
267
|
+
queryKey: capxulKeys.accountLifecycle,
|
|
268
|
+
queryFn: async () => unwrapCapxulResult(await requireBootstrappedClient(client, "account.getLifecycle").account.getLifecycle()),
|
|
269
|
+
enabled: client !== null,
|
|
270
|
+
refetchInterval: (q) => {
|
|
271
|
+
if (isVitestRuntime()) return false;
|
|
272
|
+
const data = q.state.data;
|
|
273
|
+
if (data === void 0) return false;
|
|
274
|
+
if (data.status === "loading" || isSettingUpLifecycle(data)) return 2e3;
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
const retryMutation = useMutation({
|
|
279
|
+
mutationFn: async () => unwrapCapxulResult(await requireBootstrappedClient(client, "account.retrySetup").account.retrySetup()),
|
|
280
|
+
onSuccess: async () => {
|
|
281
|
+
await invalidateAuthBoundary(queryClient);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
const lifecycle = query.data ?? LOADING_LIFECYCLE;
|
|
285
|
+
const failedError = lifecycle.status === "failed" ? lifecycle.error : null;
|
|
286
|
+
return {
|
|
287
|
+
lifecycle,
|
|
288
|
+
isSettingUp: isSettingUpLifecycle(lifecycle),
|
|
289
|
+
error: failedError ?? (query.isError ? query.error : null),
|
|
290
|
+
isLoading: query.isLoading,
|
|
291
|
+
isFetching: query.isFetching,
|
|
292
|
+
isError: query.isError,
|
|
293
|
+
retry: retryMutation.mutateAsync,
|
|
294
|
+
isRetrying: retryMutation.isPending
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
//#endregion
|
|
298
|
+
//#region src/hooks/use-capxul-account-balance.ts
|
|
299
|
+
function useCapxulAccountBalance(options) {
|
|
300
|
+
const client = useCapxulClientOrNull();
|
|
301
|
+
return useQuery({
|
|
302
|
+
queryKey: capxulKeys.accountBalance,
|
|
303
|
+
queryFn: async () => unwrapCapxulResult(await requireBootstrappedClient(client, "accounts.read").accounts.read()),
|
|
304
|
+
enabled: client !== null && (options?.enabled ?? true)
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
//#endregion
|
|
308
|
+
//#region src/hooks/use-capxul-account-fund.ts
|
|
309
|
+
function useCapxulAccountFund() {
|
|
310
|
+
const client = useCapxulClientOrNull();
|
|
311
|
+
const queryClient = useQueryClient();
|
|
312
|
+
return useMutation({
|
|
313
|
+
mutationFn: async (amount) => unwrapCapxulResult(await requireBootstrappedClient(client, "_internal.accounts.fund")._internal.accounts.fund(amount)),
|
|
314
|
+
onSuccess: async () => {
|
|
315
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.accountBalance });
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
//#endregion
|
|
320
|
+
//#region src/hooks/use-capxul-sign-in.ts
|
|
321
|
+
function useCapxulSignIn() {
|
|
322
|
+
const client = useCapxulClientOrNull();
|
|
323
|
+
return useMutation({ mutationFn: async (input) => unwrapCapxulResult(await requireBootstrappedClient(client, "auth.signIn").auth.signIn(input)) });
|
|
324
|
+
}
|
|
325
|
+
//#endregion
|
|
326
|
+
//#region src/hooks/use-capxul-verify-otp.ts
|
|
327
|
+
function useCapxulVerifyOtp() {
|
|
328
|
+
const client = useCapxulClientOrNull();
|
|
329
|
+
const queryClient = useQueryClient();
|
|
330
|
+
return useMutation({
|
|
331
|
+
mutationFn: async (input) => unwrapCapxulResult(await requireBootstrappedClient(client, "auth.verifyOtp").auth.verifyOtp(input)),
|
|
332
|
+
onSuccess: async () => {
|
|
333
|
+
await invalidateAuthBoundary(queryClient);
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
//#endregion
|
|
338
|
+
//#region src/hooks/use-capxul-sign-out.ts
|
|
339
|
+
function useCapxulSignOut() {
|
|
340
|
+
const client = useCapxulClientOrNull();
|
|
341
|
+
const queryClient = useQueryClient();
|
|
342
|
+
return useMutation({
|
|
343
|
+
onMutate: async () => {
|
|
344
|
+
await queryClient.cancelQueries({ queryKey: capxulKeys.accountBalance });
|
|
345
|
+
},
|
|
346
|
+
mutationFn: async () => unwrapCapxulResult(await requireBootstrappedClient(client, "auth.signOut").auth.signOut()),
|
|
347
|
+
onSuccess: () => resetAuthBoundary(queryClient)
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
//#endregion
|
|
351
|
+
//#region src/hooks/use-capxul-sub-accounts.ts
|
|
352
|
+
function useCapxulSubAccountsList(accountId, options) {
|
|
353
|
+
const client = useCapxulClientOrNull();
|
|
354
|
+
return useQuery({
|
|
355
|
+
queryKey: capxulKeys.subAccounts(accountId),
|
|
356
|
+
queryFn: async () => {
|
|
357
|
+
if (accountId === void 0) throw Errors.invalidInput("accountId", "required for subAccounts.list");
|
|
358
|
+
return unwrapCapxulResult(await requireBootstrappedClient(client, "subAccounts.list").subAccounts.list(accountId));
|
|
359
|
+
},
|
|
360
|
+
enabled: client !== null && (options?.enabled ?? true) && accountId !== void 0
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
function useCapxulSubAccountCreate() {
|
|
364
|
+
const client = useCapxulClientOrNull();
|
|
365
|
+
const queryClient = useQueryClient();
|
|
366
|
+
return useMutation({
|
|
367
|
+
mutationFn: async (input) => unwrapCapxulResult(await requireBootstrappedClient(client, "subAccounts.create").subAccounts.create(input.accountId, { name: input.name })),
|
|
368
|
+
onSuccess: async (_value, variables) => {
|
|
369
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.subAccounts(variables.accountId) });
|
|
370
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.accountBalance });
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
function useCapxulSubAccountRename() {
|
|
375
|
+
const client = useCapxulClientOrNull();
|
|
376
|
+
const queryClient = useQueryClient();
|
|
377
|
+
return useMutation({
|
|
378
|
+
mutationFn: async (input) => unwrapCapxulResult(await requireBootstrappedClient(client, "subAccounts.rename").subAccounts.rename(input.subAccountId, input.name)),
|
|
379
|
+
onSuccess: async (_value, variables) => {
|
|
380
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.subAccounts(variables.accountId) });
|
|
381
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.accountBalance });
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
function useCapxulSubAccountDelete() {
|
|
386
|
+
const client = useCapxulClientOrNull();
|
|
387
|
+
const queryClient = useQueryClient();
|
|
388
|
+
return useMutation({
|
|
389
|
+
mutationFn: async (input) => unwrapCapxulResult(await requireBootstrappedClient(client, "subAccounts.delete").subAccounts.delete(input.subAccountId)),
|
|
390
|
+
onSuccess: async (_value, variables) => {
|
|
391
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.subAccounts(variables.accountId) });
|
|
392
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.accountBalance });
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
function useCapxulTransfer() {
|
|
397
|
+
const client = useCapxulClientOrNull();
|
|
398
|
+
const queryClient = useQueryClient();
|
|
399
|
+
return useMutation({
|
|
400
|
+
mutationFn: async ({ from, to, amount }) => unwrapCapxulResult(await requireBootstrappedClient(client, "subAccounts.transfer").subAccounts.transfer({
|
|
401
|
+
from,
|
|
402
|
+
to,
|
|
403
|
+
amount
|
|
404
|
+
})),
|
|
405
|
+
onSuccess: async (_value, variables) => {
|
|
406
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.subAccounts(variables.accountId) });
|
|
407
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.accountBalance });
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
//#endregion
|
|
412
|
+
//#region src/hooks/use-capxul-orgs.ts
|
|
413
|
+
function useCapxulOrgs(options) {
|
|
414
|
+
const client = useCapxulClient();
|
|
415
|
+
return useQuery({
|
|
416
|
+
queryKey: capxulKeys.orgs,
|
|
417
|
+
queryFn: async () => unwrapCapxulResult(await client.orgs()),
|
|
418
|
+
enabled: options?.enabled ?? true
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
//#endregion
|
|
422
|
+
//#region src/hooks/use-capxul-org.ts
|
|
423
|
+
function useCapxulOrg(orgId, options) {
|
|
424
|
+
const client = useCapxulClient();
|
|
425
|
+
return useQuery({
|
|
426
|
+
queryKey: capxulKeys.org(orgId),
|
|
427
|
+
queryFn: async () => {
|
|
428
|
+
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulOrg");
|
|
429
|
+
return unwrapCapxulResult(await client.orgs()).find((org) => org.id === orgId) ?? null;
|
|
430
|
+
},
|
|
431
|
+
enabled: (options?.enabled ?? true) && orgId !== void 0
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
//#endregion
|
|
435
|
+
//#region src/hooks/use-capxul-org-members.ts
|
|
436
|
+
function useCapxulOrgMembers(orgId, options) {
|
|
437
|
+
const client = useCapxulClient();
|
|
438
|
+
return useQuery({
|
|
439
|
+
queryKey: capxulKeys.orgMembers(orgId),
|
|
440
|
+
queryFn: async () => {
|
|
441
|
+
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulOrgMembers");
|
|
442
|
+
return unwrapCapxulResult(await client.org(orgId).members());
|
|
443
|
+
},
|
|
444
|
+
enabled: (options?.enabled ?? true) && orgId !== void 0
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
//#endregion
|
|
448
|
+
//#region src/hooks/use-capxul-org-roles.ts
|
|
449
|
+
function useCapxulOrgRoles(orgId, options) {
|
|
450
|
+
const client = useCapxulClient();
|
|
451
|
+
return useQuery({
|
|
452
|
+
queryKey: capxulKeys.orgRoles(orgId),
|
|
453
|
+
queryFn: async () => {
|
|
454
|
+
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulOrgRoles");
|
|
455
|
+
return unwrapCapxulResult(await client.org(orgId).roles());
|
|
456
|
+
},
|
|
457
|
+
enabled: (options?.enabled ?? true) && orgId !== void 0
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
//#endregion
|
|
461
|
+
//#region src/hooks/use-capxul-org-deploy-roles.ts
|
|
462
|
+
function useCapxulOrgDeployRoles() {
|
|
463
|
+
const client = useCapxulClient();
|
|
464
|
+
const queryClient = useQueryClient();
|
|
465
|
+
return useMutation({
|
|
466
|
+
mutationFn: async (orgId) => {
|
|
467
|
+
return unwrapCapxulResult(await client.org(orgId).deployRoles());
|
|
468
|
+
},
|
|
469
|
+
onSuccess: async (_roles, orgId) => {
|
|
470
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.orgRoles(orgId) });
|
|
471
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.org(orgId) });
|
|
472
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.orgs });
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
//#endregion
|
|
477
|
+
//#region src/hooks/use-capxul-org-treasury.ts
|
|
478
|
+
function useCapxulOrgTreasury(orgId, options) {
|
|
479
|
+
const client = useCapxulClient();
|
|
480
|
+
return useQuery({
|
|
481
|
+
queryKey: capxulKeys.orgTreasury(orgId),
|
|
482
|
+
queryFn: async () => {
|
|
483
|
+
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulOrgTreasury");
|
|
484
|
+
return unwrapCapxulResult(await client.org(orgId).treasury());
|
|
485
|
+
},
|
|
486
|
+
enabled: (options?.enabled ?? true) && orgId !== void 0
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
//#endregion
|
|
490
|
+
//#region src/hooks/use-capxul-create-org.ts
|
|
491
|
+
function useCapxulCreateOrg() {
|
|
492
|
+
const client = useCapxulClient();
|
|
493
|
+
const queryClient = useQueryClient();
|
|
494
|
+
return useMutation({
|
|
495
|
+
mutationFn: async (input) => unwrapCapxulResult(await client.createOrg(input)),
|
|
496
|
+
onSuccess: async () => {
|
|
497
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.orgs });
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
//#endregion
|
|
502
|
+
//#region src/hooks/use-capxul-invite-member.ts
|
|
503
|
+
function useCapxulInviteMember(orgId) {
|
|
504
|
+
const client = useCapxulClient();
|
|
505
|
+
const queryClient = useQueryClient();
|
|
506
|
+
return useMutation({
|
|
507
|
+
mutationFn: async (input) => {
|
|
508
|
+
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulInviteMember");
|
|
509
|
+
return unwrapCapxulResult(await client.org(orgId).invite(input));
|
|
510
|
+
},
|
|
511
|
+
onSuccess: async () => {
|
|
512
|
+
if (orgId === void 0) return;
|
|
513
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.orgMembers(orgId) });
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
//#endregion
|
|
518
|
+
//#region src/hooks/use-capxul-remove-member.ts
|
|
519
|
+
function useCapxulRemoveMember(orgId) {
|
|
520
|
+
const client = useCapxulClient();
|
|
521
|
+
const queryClient = useQueryClient();
|
|
522
|
+
return useMutation({
|
|
523
|
+
mutationFn: async (input) => {
|
|
524
|
+
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulRemoveMember");
|
|
525
|
+
return unwrapCapxulResult(await client.org(orgId).removeMember(input));
|
|
526
|
+
},
|
|
527
|
+
onSuccess: async () => {
|
|
528
|
+
if (orgId === void 0) return;
|
|
529
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.orgMembers(orgId) });
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
//#endregion
|
|
534
|
+
//#region src/hooks/use-capxul-assign-role.ts
|
|
535
|
+
function useCapxulAssignRole(orgId) {
|
|
536
|
+
const client = useCapxulClient();
|
|
537
|
+
const queryClient = useQueryClient();
|
|
538
|
+
return useMutation({
|
|
539
|
+
mutationFn: async (input) => {
|
|
540
|
+
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulAssignRole");
|
|
541
|
+
return unwrapCapxulResult(await client.org(orgId).assignRole(input));
|
|
542
|
+
},
|
|
543
|
+
onSuccess: async () => {
|
|
544
|
+
if (orgId === void 0) return;
|
|
545
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.orgMembers(orgId) });
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
//#endregion
|
|
550
|
+
//#region src/hooks/use-capxul-org-spend.ts
|
|
551
|
+
function useCapxulOrgSpend(orgId) {
|
|
552
|
+
const client = useCapxulClient();
|
|
553
|
+
const queryClient = useQueryClient();
|
|
554
|
+
return useMutation({
|
|
555
|
+
mutationFn: async (input) => {
|
|
556
|
+
if (orgId === void 0) throw Errors.invalidInput("orgId", "org is not selected");
|
|
557
|
+
return unwrapCapxulResult(await client.org(orgId).spend(input));
|
|
558
|
+
},
|
|
559
|
+
onSuccess: async () => {
|
|
560
|
+
if (orgId === void 0) return;
|
|
561
|
+
await queryClient.invalidateQueries({ queryKey: capxulKeys.orgTreasury(orgId) });
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
//#endregion
|
|
566
|
+
export { CapxulProvider, useCapxul, useCapxulAccountBalance, useCapxulAccountFund, useCapxulAccountLifecycle, useCapxulAssignRole, useCapxulCreateOrg, useCapxulInviteMember, useCapxulOrg, useCapxulOrgDeployRoles, useCapxulOrgMembers, useCapxulOrgRoles, useCapxulOrgSpend, useCapxulOrgTreasury, useCapxulOrgs, useCapxulProfile, useCapxulRemoveMember, useCapxulSession, useCapxulSignIn, useCapxulSignOut, useCapxulSubAccountCreate, useCapxulSubAccountDelete, useCapxulSubAccountRename, useCapxulSubAccountsList, useCapxulTransfer, useCapxulVerifyOtp };
|
|
567
|
+
|
|
568
|
+
//# sourceMappingURL=index.mjs.map
|