@capxul/sdk-react 0.2.0-alpha.5 → 1.0.0-alpha.7
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 +137 -43
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +335 -97
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
|
|
2
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
import { QueryClient, QueryClientProvider, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
4
|
-
import { createCapxulClient } from "@capxul/sdk";
|
|
4
|
+
import { captureExceptionSync, createCapxulClient, isSettingUpLifecycle } from "@capxul/sdk";
|
|
5
5
|
import { jsx } from "react/jsx-runtime";
|
|
6
|
-
import { Errors } from "@capxul/config";
|
|
7
6
|
//#region src/internal/capxul-bootstrap-context.tsx
|
|
8
7
|
const CapxulBootstrapContext = createContext(null);
|
|
9
8
|
function CapxulBootstrapProvider({ value, children }) {
|
|
@@ -19,7 +18,8 @@ function useCapxul() {
|
|
|
19
18
|
}
|
|
20
19
|
//#endregion
|
|
21
20
|
//#region src/internal/capxul-client-context.tsx
|
|
22
|
-
const
|
|
21
|
+
const MISSING_CAPXUL_CLIENT_PROVIDER = Symbol("MISSING_CAPXUL_CLIENT_PROVIDER");
|
|
22
|
+
const CapxulClientContext = createContext(MISSING_CAPXUL_CLIENT_PROVIDER);
|
|
23
23
|
function CapxulClientProvider({ client, children }) {
|
|
24
24
|
return /* @__PURE__ */ jsx(CapxulClientContext.Provider, {
|
|
25
25
|
value: client,
|
|
@@ -37,7 +37,9 @@ function useCapxulClient() {
|
|
|
37
37
|
* client resolves, rather than throwing during bootstrap.
|
|
38
38
|
*/
|
|
39
39
|
function useCapxulClientOrNull() {
|
|
40
|
-
|
|
40
|
+
const client = useContext(CapxulClientContext);
|
|
41
|
+
if (client === MISSING_CAPXUL_CLIENT_PROVIDER) throw new Error("useCapxulClient must be used within <CapxulProvider>");
|
|
42
|
+
return client;
|
|
41
43
|
}
|
|
42
44
|
//#endregion
|
|
43
45
|
//#region src/provider.tsx
|
|
@@ -50,10 +52,22 @@ function makeDefaultQueryClient() {
|
|
|
50
52
|
mutations: { retry: 0 }
|
|
51
53
|
} });
|
|
52
54
|
}
|
|
55
|
+
function isCapxulQueryKey(queryKey) {
|
|
56
|
+
return queryKey[0] === "capxul";
|
|
57
|
+
}
|
|
58
|
+
function clearClientScopedQueries(queryClient, ownsQueryClient) {
|
|
59
|
+
if (ownsQueryClient) {
|
|
60
|
+
queryClient.clear();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
queryClient.removeQueries({ predicate: (query) => isCapxulQueryKey(query.queryKey) });
|
|
64
|
+
}
|
|
53
65
|
function CapxulProvider(props) {
|
|
54
|
-
const { publishableKey, client: injectedClient, requirement,
|
|
66
|
+
const { publishableKey, client: injectedClient, requirement, signer, queryClient, children } = props;
|
|
55
67
|
const [resolvedQueryClient] = useState(() => queryClient ?? makeDefaultQueryClient());
|
|
68
|
+
const [ownsQueryClient] = useState(() => queryClient === void 0);
|
|
56
69
|
const [client, setClient] = useState(injectedClient ?? null);
|
|
70
|
+
const previousClientRef = useRef(injectedClient ?? null);
|
|
57
71
|
const [status, setStatus] = useState(injectedClient === void 0 ? "bootstrapping" : "ready");
|
|
58
72
|
const [error, setError] = useState(null);
|
|
59
73
|
const [attempt, setAttempt] = useState(0);
|
|
@@ -71,10 +85,6 @@ function CapxulProvider(props) {
|
|
|
71
85
|
const result = await createCapxulClient({
|
|
72
86
|
publishableKey,
|
|
73
87
|
...requirement === void 0 ? {} : { requirement },
|
|
74
|
-
...origin === void 0 ? {} : { origin },
|
|
75
|
-
...bootstrapBaseUrl === void 0 ? {} : { bootstrapBaseUrl },
|
|
76
|
-
...authCache === void 0 ? {} : { authCache },
|
|
77
|
-
...invokeTimeoutMs === void 0 ? {} : { invokeTimeoutMs },
|
|
78
88
|
...signer === void 0 ? {} : { signer }
|
|
79
89
|
});
|
|
80
90
|
if (cancelled) {
|
|
@@ -97,10 +107,6 @@ function CapxulProvider(props) {
|
|
|
97
107
|
}, [
|
|
98
108
|
publishableKey,
|
|
99
109
|
requirement,
|
|
100
|
-
origin,
|
|
101
|
-
bootstrapBaseUrl,
|
|
102
|
-
authCache,
|
|
103
|
-
invokeTimeoutMs,
|
|
104
110
|
signer,
|
|
105
111
|
attempt
|
|
106
112
|
]);
|
|
@@ -110,6 +116,15 @@ function CapxulProvider(props) {
|
|
|
110
116
|
setStatus("ready");
|
|
111
117
|
setError(null);
|
|
112
118
|
}, [injectedClient]);
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
const previous = previousClientRef.current;
|
|
121
|
+
if (previous !== null && previous !== client) clearClientScopedQueries(resolvedQueryClient, ownsQueryClient);
|
|
122
|
+
previousClientRef.current = client;
|
|
123
|
+
}, [
|
|
124
|
+
client,
|
|
125
|
+
ownsQueryClient,
|
|
126
|
+
resolvedQueryClient
|
|
127
|
+
]);
|
|
113
128
|
const bootstrapState = useMemo(() => ({
|
|
114
129
|
status,
|
|
115
130
|
error,
|
|
@@ -132,6 +147,156 @@ function CapxulProvider(props) {
|
|
|
132
147
|
});
|
|
133
148
|
}
|
|
134
149
|
//#endregion
|
|
150
|
+
//#region ../errors/src/errors.ts
|
|
151
|
+
const CAPXUL_ERROR_CODES = [
|
|
152
|
+
"NOT_AUTHENTICATED",
|
|
153
|
+
"EMAIL_DELIVERY_FAILED",
|
|
154
|
+
"PROFILE_NOT_FOUND",
|
|
155
|
+
"SMART_ACCOUNT_MISSING",
|
|
156
|
+
"PLAYER_NOT_FOUND",
|
|
157
|
+
"ACCOUNT_NOT_FOUND",
|
|
158
|
+
"PROVIDER_ERROR",
|
|
159
|
+
"INVALID_INPUT",
|
|
160
|
+
"ENV_MISSING",
|
|
161
|
+
"NOT_IMPLEMENTED",
|
|
162
|
+
"VERIFICATION_REQUIRED",
|
|
163
|
+
"INSUFFICIENT_BALANCE",
|
|
164
|
+
"INVALID_RECIPIENT",
|
|
165
|
+
"ROLE_PERMISSION_DENIED",
|
|
166
|
+
"TRANSACTION_FAILED",
|
|
167
|
+
"RATE_LIMITED",
|
|
168
|
+
"NETWORK_ERROR",
|
|
169
|
+
"UNKNOWN",
|
|
170
|
+
"OTP_EXPIRED",
|
|
171
|
+
"SIGNER_REJECTED",
|
|
172
|
+
"CANCELLED",
|
|
173
|
+
"WRONG_STATE"
|
|
174
|
+
];
|
|
175
|
+
var CapxulError = class extends Error {
|
|
176
|
+
code;
|
|
177
|
+
details;
|
|
178
|
+
correlationId;
|
|
179
|
+
layer;
|
|
180
|
+
constructor(code, message, options = {}) {
|
|
181
|
+
super(message, "cause" in options ? { cause: options.cause } : void 0);
|
|
182
|
+
this.name = "CapxulError";
|
|
183
|
+
this.code = code;
|
|
184
|
+
if (options.details !== void 0) this.details = options.details;
|
|
185
|
+
if (options.correlationId !== void 0) this.correlationId = options.correlationId;
|
|
186
|
+
if (options.layer !== void 0) this.layer = options.layer;
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
const Errors = {
|
|
190
|
+
notAuthenticated: (message, opts) => new CapxulError("NOT_AUTHENTICATED", message ?? "Not authenticated", opts?.failure_mode ? { details: { failure_mode: opts.failure_mode } } : void 0),
|
|
191
|
+
emailDeliveryFailed: (detail) => new CapxulError("EMAIL_DELIVERY_FAILED", "Failed to send email", { details: { detail } }),
|
|
192
|
+
profileNotFound: (authUserId) => new CapxulError("PROFILE_NOT_FOUND", `Profile not found for user ${authUserId}`, { details: { authUserId } }),
|
|
193
|
+
smartAccountMissing: (authUserId) => new CapxulError("SMART_ACCOUNT_MISSING", "Smart account not provisioned", { details: { authUserId } }),
|
|
194
|
+
playerNotFound: (playerId) => new CapxulError("PLAYER_NOT_FOUND", playerId ? `Openfort player ${playerId} not found` : "Openfort player not found", playerId === void 0 ? void 0 : { details: { playerId } }),
|
|
195
|
+
accountNotFound: (accountId) => new CapxulError("ACCOUNT_NOT_FOUND", accountId ? `Openfort account ${accountId} not found` : "Openfort account not found", accountId === void 0 ? void 0 : { details: { accountId } }),
|
|
196
|
+
providerError: (provider, operation, cause, opts) => {
|
|
197
|
+
const details = {
|
|
198
|
+
provider,
|
|
199
|
+
operation
|
|
200
|
+
};
|
|
201
|
+
if (opts?.failure_mode) details.failure_mode = opts.failure_mode;
|
|
202
|
+
return new CapxulError("PROVIDER_ERROR", `Provider error: ${provider} ${operation}`, {
|
|
203
|
+
cause,
|
|
204
|
+
details
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
invalidInput: (field, reason) => new CapxulError("INVALID_INPUT", `Invalid ${field}: ${reason}`, { details: {
|
|
208
|
+
field,
|
|
209
|
+
reason
|
|
210
|
+
} }),
|
|
211
|
+
envMissing: (name) => new CapxulError("ENV_MISSING", `Environment variable ${name} not configured`, { details: { name } }),
|
|
212
|
+
notImplemented: (domain, method) => new CapxulError("NOT_IMPLEMENTED", `${domain}.${method} is not yet implemented. This feature is planned for a future release.`, { details: {
|
|
213
|
+
domain,
|
|
214
|
+
method
|
|
215
|
+
} }),
|
|
216
|
+
/**
|
|
217
|
+
* Sibling factory to {@link Errors.providerError} for the per-state timeout
|
|
218
|
+
* path in flows (initially ProvisioningFlow's mint_openfort / deploy_safe /
|
|
219
|
+
* register_indexer `after:` timers). Same `PROVIDER_ERROR` code as
|
|
220
|
+
* `providerError`, plus a `details.reason: "timeout"` discriminator so
|
|
221
|
+
* downstream observers can distinguish failure modes without parsing the
|
|
222
|
+
* message string. The redacted message names the timeout budget; the
|
|
223
|
+
* native `cause` carries the same information for `reportError` fidelity.
|
|
224
|
+
*/
|
|
225
|
+
providerTimeout: (provider, operation, timeoutMs) => new CapxulError("PROVIDER_ERROR", `Provider error: ${provider} ${operation} (timeout exceeded ${timeoutMs}ms)`, {
|
|
226
|
+
details: {
|
|
227
|
+
provider,
|
|
228
|
+
operation,
|
|
229
|
+
reason: "timeout"
|
|
230
|
+
},
|
|
231
|
+
cause: /* @__PURE__ */ new Error(`timeout: ${operation} exceeded ${timeoutMs}ms`)
|
|
232
|
+
}),
|
|
233
|
+
verificationRequired: (details) => {
|
|
234
|
+
return new CapxulError("VERIFICATION_REQUIRED", "rail" in details ? `Verification is required before ${details.rail} can use ${details.currentKind}.` : `Verification tier ${details.requiredTier} is required.`, { details });
|
|
235
|
+
},
|
|
236
|
+
insufficientBalance: (asset, available, required) => new CapxulError("INSUFFICIENT_BALANCE", `Insufficient ${asset} balance`, { details: {
|
|
237
|
+
asset,
|
|
238
|
+
available,
|
|
239
|
+
required
|
|
240
|
+
} }),
|
|
241
|
+
invalidRecipient: (reason) => new CapxulError("INVALID_RECIPIENT", `Invalid recipient: ${reason}`, { details: { reason } }),
|
|
242
|
+
/**
|
|
243
|
+
* The org Zodiac Roles modifier REFUSED the spend on-chain (G4 · #547): the
|
|
244
|
+
* member's role condition (per-tx cap, per-day allowance, allowed recipient,
|
|
245
|
+
* or membership) was violated, so `execTransactionWithRole` reverted. This is
|
|
246
|
+
* a PERMISSION denial — explicitly NOT an `INSUFFICIENT_BALANCE` (the treasury
|
|
247
|
+
* held the funds; the role's authority is what bound). `reason` discriminates
|
|
248
|
+
* the violated condition (`over_cap` / `daily_cap` / `not_member` /
|
|
249
|
+
* `disallowed_recipient` / `condition_violation`); leak-safe — no on-chain
|
|
250
|
+
* identifiers ever enter the details.
|
|
251
|
+
*/
|
|
252
|
+
rolePermissionDenied: (details) => new CapxulError("ROLE_PERMISSION_DENIED", `Org role denied this spend on-chain (${details.reason}).`, { details: details.operation === void 0 ? { reason: details.reason } : {
|
|
253
|
+
reason: details.reason,
|
|
254
|
+
operation: details.operation
|
|
255
|
+
} }),
|
|
256
|
+
/**
|
|
257
|
+
* A transaction (or sponsored UserOp) failed. `details.reason` discriminates
|
|
258
|
+
* the failure mode for callers that must distinguish a CONFIRMED on-chain
|
|
259
|
+
* revert (`"onchain_revert"` — the op executed and reverted, e.g. a Zodiac
|
|
260
|
+
* Roles condition violation) from an inconclusive infra failure. A confirmed
|
|
261
|
+
* revert is the ONLY mode the org spend port may map to a roles denial.
|
|
262
|
+
*/
|
|
263
|
+
transactionFailed: (operation, cause, extra) => new CapxulError("TRANSACTION_FAILED", `Transaction failed: ${operation}`, {
|
|
264
|
+
cause,
|
|
265
|
+
details: extra?.reason === void 0 ? { operation } : {
|
|
266
|
+
operation,
|
|
267
|
+
reason: extra.reason
|
|
268
|
+
}
|
|
269
|
+
}),
|
|
270
|
+
rateLimited: (details) => new CapxulError("RATE_LIMITED", "Rate limit exceeded", details === void 0 ? void 0 : { details: { ...details } }),
|
|
271
|
+
networkError: (operation, cause) => new CapxulError("NETWORK_ERROR", `Network error during ${operation}`, {
|
|
272
|
+
cause,
|
|
273
|
+
details: { operation }
|
|
274
|
+
}),
|
|
275
|
+
unknown: (cause) => new CapxulError("UNKNOWN", "Unknown error", { cause }),
|
|
276
|
+
otpExpired: (details) => new CapxulError("OTP_EXPIRED", "Verification code has expired. Request a new one.", details === void 0 ? void 0 : { details: { ...details } }),
|
|
277
|
+
signerRejected: (details) => new CapxulError("SIGNER_REJECTED", "Signer rejected the request.", {
|
|
278
|
+
cause: details.cause,
|
|
279
|
+
details: details.reason === void 0 ? { source: details.source } : {
|
|
280
|
+
source: details.source,
|
|
281
|
+
reason: details.reason
|
|
282
|
+
}
|
|
283
|
+
}),
|
|
284
|
+
cancelled: (details) => new CapxulError("CANCELLED", "Operation was cancelled.", details === void 0 ? void 0 : { details: { ...details } }),
|
|
285
|
+
/**
|
|
286
|
+
* Method called from a flow state where its precondition fails (TA16). The
|
|
287
|
+
* SDK's method API short-circuits with this error before driving the
|
|
288
|
+
* internal state machine. `currentState` is the Effect-machine snapshot
|
|
289
|
+
* tag (stringified — substrate is `@effect/experimental/Machine` per
|
|
290
|
+
* `docs/canon/decisions/state-machine-substrate.md`); `validStates`
|
|
291
|
+
* enumerates the states the method accepts.
|
|
292
|
+
*/
|
|
293
|
+
wrongState: (details) => new CapxulError("WRONG_STATE", `${details.method} called from state '${details.currentState}'; valid states: ${details.validStates.join(", ")}`, { details: {
|
|
294
|
+
...details,
|
|
295
|
+
validStates: [...details.validStates]
|
|
296
|
+
} })
|
|
297
|
+
};
|
|
298
|
+
new Set(CAPXUL_ERROR_CODES);
|
|
299
|
+
//#endregion
|
|
135
300
|
//#region src/internal/require-bootstrapped-client.ts
|
|
136
301
|
/**
|
|
137
302
|
* Narrow the bootstrap-nullable client to a ready client inside a query /
|
|
@@ -153,6 +318,9 @@ const capxulKeys = {
|
|
|
153
318
|
session: ["capxul", "session"],
|
|
154
319
|
profile: ["capxul", "profile"],
|
|
155
320
|
account: ["capxul", "account"],
|
|
321
|
+
accountLifecycle: ["capxul", "accountLifecycle"],
|
|
322
|
+
provisioning: ["capxul", "provisioning"],
|
|
323
|
+
binding: ["capxul", "binding"],
|
|
156
324
|
accountBalance: ["capxul", "accountBalance"],
|
|
157
325
|
subAccounts: (accountId) => [
|
|
158
326
|
"capxul",
|
|
@@ -186,9 +354,23 @@ const capxulKeys = {
|
|
|
186
354
|
};
|
|
187
355
|
//#endregion
|
|
188
356
|
//#region src/internal/unwrap-capxul-result.ts
|
|
189
|
-
/**
|
|
190
|
-
|
|
357
|
+
/**
|
|
358
|
+
* Unwrap a `CapxulResult` for TanStack query/mutation functions —
|
|
359
|
+
* throws into error paths, optionally reporting the error to telemetry first.
|
|
360
|
+
*
|
|
361
|
+
* When `telemetry` is provided and the result is `{ ok: false }`,
|
|
362
|
+
* `captureExceptionSync` is called (fire-and-forget) before the throw. `operation`
|
|
363
|
+
* tags the telemetry event so query reads and mutation writes stay
|
|
364
|
+
* distinguishable in error tracking (defaults to `"query"`).
|
|
365
|
+
*/
|
|
366
|
+
function unwrapCapxulResult(result, telemetry, operation = "query") {
|
|
191
367
|
if (result.ok) return result.value;
|
|
368
|
+
if (telemetry) try {
|
|
369
|
+
captureExceptionSync(telemetry, result.error, {
|
|
370
|
+
layer: "react-query",
|
|
371
|
+
operation
|
|
372
|
+
});
|
|
373
|
+
} catch {}
|
|
192
374
|
throw result.error;
|
|
193
375
|
}
|
|
194
376
|
//#endregion
|
|
@@ -197,7 +379,7 @@ function useCapxulSession() {
|
|
|
197
379
|
const client = useCapxulClientOrNull();
|
|
198
380
|
return useQuery({
|
|
199
381
|
queryKey: capxulKeys.session,
|
|
200
|
-
queryFn: async () => unwrapCapxulResult(await requireBootstrappedClient(client, "auth.getSession").auth.getSession()),
|
|
382
|
+
queryFn: async () => unwrapCapxulResult(await requireBootstrappedClient(client, "auth.getSession").auth.getSession(), client._internal.telemetry),
|
|
201
383
|
enabled: client !== null
|
|
202
384
|
});
|
|
203
385
|
}
|
|
@@ -207,19 +389,84 @@ function useCapxulProfile() {
|
|
|
207
389
|
const client = useCapxulClientOrNull();
|
|
208
390
|
return useQuery({
|
|
209
391
|
queryKey: capxulKeys.profile,
|
|
210
|
-
queryFn: async () => unwrapCapxulResult(await requireBootstrappedClient(client, "identity.loadCurrent").identity.loadCurrent()),
|
|
392
|
+
queryFn: async () => unwrapCapxulResult(await requireBootstrappedClient(client, "identity.loadCurrent").identity.loadCurrent(), client._internal.telemetry),
|
|
211
393
|
enabled: client !== null
|
|
212
394
|
});
|
|
213
395
|
}
|
|
214
396
|
//#endregion
|
|
215
|
-
//#region src/
|
|
216
|
-
|
|
397
|
+
//#region src/internal/is-vitest-runtime.ts
|
|
398
|
+
/** True under Vitest — disables hook polling intervals that fight fake timers. */
|
|
399
|
+
function isVitestRuntime() {
|
|
400
|
+
return typeof process !== "undefined" && process.env["VITEST"] === "true";
|
|
401
|
+
}
|
|
402
|
+
//#endregion
|
|
403
|
+
//#region src/internal/invalidate-auth-boundary.ts
|
|
404
|
+
/** Background refetch after verifyOtp — avoids hard reset cancel errors in UI. */
|
|
405
|
+
async function invalidateAuthBoundary(queryClient) {
|
|
406
|
+
await Promise.all([
|
|
407
|
+
queryClient.invalidateQueries({ queryKey: capxulKeys.session }),
|
|
408
|
+
queryClient.invalidateQueries({ queryKey: capxulKeys.profile }),
|
|
409
|
+
queryClient.invalidateQueries({ queryKey: capxulKeys.accountLifecycle }),
|
|
410
|
+
queryClient.invalidateQueries({ queryKey: capxulKeys.accountBalance })
|
|
411
|
+
]);
|
|
412
|
+
}
|
|
413
|
+
/** Hard reset after signOut — drop cached authenticated rows immediately. */
|
|
414
|
+
async function resetAuthBoundary(queryClient) {
|
|
415
|
+
await queryClient.cancelQueries({ queryKey: capxulKeys.accountBalance });
|
|
416
|
+
await Promise.all([
|
|
417
|
+
queryClient.resetQueries({ queryKey: capxulKeys.session }),
|
|
418
|
+
queryClient.resetQueries({ queryKey: capxulKeys.profile }),
|
|
419
|
+
queryClient.resetQueries({ queryKey: capxulKeys.accountLifecycle }),
|
|
420
|
+
queryClient.resetQueries({ queryKey: capxulKeys.accountBalance })
|
|
421
|
+
]);
|
|
422
|
+
}
|
|
423
|
+
//#endregion
|
|
424
|
+
//#region src/hooks/use-capxul-account-lifecycle.ts
|
|
425
|
+
const LOADING_LIFECYCLE = { status: "loading" };
|
|
426
|
+
function useCapxulAccountLifecycle() {
|
|
217
427
|
const client = useCapxulClientOrNull();
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
428
|
+
const queryClient = useQueryClient();
|
|
429
|
+
const query = useQuery({
|
|
430
|
+
queryKey: capxulKeys.accountLifecycle,
|
|
431
|
+
queryFn: async () => {
|
|
432
|
+
const bootstrappedClient = requireBootstrappedClient(client, "account.getLifecycle");
|
|
433
|
+
return unwrapCapxulResult(await bootstrappedClient.account.getLifecycle(), bootstrappedClient._internal.telemetry);
|
|
434
|
+
},
|
|
435
|
+
enabled: client !== null,
|
|
436
|
+
refetchInterval: (q) => {
|
|
437
|
+
if (isVitestRuntime()) return false;
|
|
438
|
+
const data = q.state.data;
|
|
439
|
+
if (data === void 0) return false;
|
|
440
|
+
if (data.status === "loading" || isSettingUpLifecycle(data)) return 2e3;
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
const retryMutation = useMutation({
|
|
445
|
+
mutationFn: async () => {
|
|
446
|
+
const bootstrappedClient = requireBootstrappedClient(client, "account.retrySetup");
|
|
447
|
+
return unwrapCapxulResult(await bootstrappedClient.account.retrySetup(), bootstrappedClient._internal.telemetry, "mutation");
|
|
448
|
+
},
|
|
449
|
+
onSuccess: async () => {
|
|
450
|
+
await invalidateAuthBoundary(queryClient);
|
|
451
|
+
}
|
|
222
452
|
});
|
|
453
|
+
const lifecycle = query.data ?? LOADING_LIFECYCLE;
|
|
454
|
+
const failedError = lifecycle.status === "failed" ? lifecycle.error : null;
|
|
455
|
+
const queryError = query.isError ? query.error : null;
|
|
456
|
+
return {
|
|
457
|
+
lifecycle: queryError !== null && lifecycle.status === "loading" ? {
|
|
458
|
+
status: "failed",
|
|
459
|
+
at: "connecting",
|
|
460
|
+
error: queryError
|
|
461
|
+
} : lifecycle,
|
|
462
|
+
isSettingUp: isSettingUpLifecycle(lifecycle),
|
|
463
|
+
error: failedError ?? queryError,
|
|
464
|
+
isLoading: query.isLoading,
|
|
465
|
+
isFetching: query.isFetching,
|
|
466
|
+
isError: query.isError,
|
|
467
|
+
retry: retryMutation.mutateAsync,
|
|
468
|
+
isRetrying: retryMutation.isPending
|
|
469
|
+
};
|
|
223
470
|
}
|
|
224
471
|
//#endregion
|
|
225
472
|
//#region src/hooks/use-capxul-account-balance.ts
|
|
@@ -227,7 +474,10 @@ function useCapxulAccountBalance(options) {
|
|
|
227
474
|
const client = useCapxulClientOrNull();
|
|
228
475
|
return useQuery({
|
|
229
476
|
queryKey: capxulKeys.accountBalance,
|
|
230
|
-
queryFn: async () =>
|
|
477
|
+
queryFn: async () => {
|
|
478
|
+
const bootstrappedClient = requireBootstrappedClient(client, "accounts.read");
|
|
479
|
+
return unwrapCapxulResult(await bootstrappedClient.accounts.read(), bootstrappedClient._internal.telemetry);
|
|
480
|
+
},
|
|
231
481
|
enabled: client !== null && (options?.enabled ?? true)
|
|
232
482
|
});
|
|
233
483
|
}
|
|
@@ -237,7 +487,10 @@ function useCapxulAccountFund() {
|
|
|
237
487
|
const client = useCapxulClientOrNull();
|
|
238
488
|
const queryClient = useQueryClient();
|
|
239
489
|
return useMutation({
|
|
240
|
-
mutationFn: async (amount) =>
|
|
490
|
+
mutationFn: async (amount) => {
|
|
491
|
+
const bootstrappedClient = requireBootstrappedClient(client, "_internal.accounts.fund");
|
|
492
|
+
return unwrapCapxulResult(await bootstrappedClient._internal.accounts.fund(amount), bootstrappedClient._internal.telemetry, "mutation");
|
|
493
|
+
},
|
|
241
494
|
onSuccess: async () => {
|
|
242
495
|
await queryClient.invalidateQueries({ queryKey: capxulKeys.accountBalance });
|
|
243
496
|
}
|
|
@@ -247,28 +500,10 @@ function useCapxulAccountFund() {
|
|
|
247
500
|
//#region src/hooks/use-capxul-sign-in.ts
|
|
248
501
|
function useCapxulSignIn() {
|
|
249
502
|
const client = useCapxulClientOrNull();
|
|
250
|
-
return useMutation({ mutationFn: async (input) =>
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
/** Background refetch after verifyOtp — avoids hard reset cancel errors in UI. */
|
|
255
|
-
async function invalidateAuthBoundary(queryClient) {
|
|
256
|
-
await Promise.all([
|
|
257
|
-
queryClient.invalidateQueries({ queryKey: capxulKeys.session }),
|
|
258
|
-
queryClient.invalidateQueries({ queryKey: capxulKeys.profile }),
|
|
259
|
-
queryClient.invalidateQueries({ queryKey: capxulKeys.account }),
|
|
260
|
-
queryClient.invalidateQueries({ queryKey: capxulKeys.accountBalance })
|
|
261
|
-
]);
|
|
262
|
-
}
|
|
263
|
-
/** Hard reset after signOut — drop cached authenticated rows immediately. */
|
|
264
|
-
async function resetAuthBoundary(queryClient) {
|
|
265
|
-
await queryClient.cancelQueries({ queryKey: capxulKeys.accountBalance });
|
|
266
|
-
await Promise.all([
|
|
267
|
-
queryClient.resetQueries({ queryKey: capxulKeys.session }),
|
|
268
|
-
queryClient.resetQueries({ queryKey: capxulKeys.profile }),
|
|
269
|
-
queryClient.resetQueries({ queryKey: capxulKeys.account }),
|
|
270
|
-
queryClient.resetQueries({ queryKey: capxulKeys.accountBalance })
|
|
271
|
-
]);
|
|
503
|
+
return useMutation({ mutationFn: async (input) => {
|
|
504
|
+
const bootstrappedClient = requireBootstrappedClient(client, "auth.signIn");
|
|
505
|
+
return unwrapCapxulResult(await bootstrappedClient.auth.signIn(input), bootstrappedClient._internal.telemetry, "mutation");
|
|
506
|
+
} });
|
|
272
507
|
}
|
|
273
508
|
//#endregion
|
|
274
509
|
//#region src/hooks/use-capxul-verify-otp.ts
|
|
@@ -276,8 +511,13 @@ function useCapxulVerifyOtp() {
|
|
|
276
511
|
const client = useCapxulClientOrNull();
|
|
277
512
|
const queryClient = useQueryClient();
|
|
278
513
|
return useMutation({
|
|
279
|
-
mutationFn: async (input) =>
|
|
280
|
-
|
|
514
|
+
mutationFn: async (input) => {
|
|
515
|
+
const bootstrappedClient = requireBootstrappedClient(client, "auth.verifyOtp");
|
|
516
|
+
return unwrapCapxulResult(await bootstrappedClient.auth.verifyOtp(input), bootstrappedClient._internal.telemetry, "mutation");
|
|
517
|
+
},
|
|
518
|
+
onSuccess: async () => {
|
|
519
|
+
await invalidateAuthBoundary(queryClient);
|
|
520
|
+
}
|
|
281
521
|
});
|
|
282
522
|
}
|
|
283
523
|
//#endregion
|
|
@@ -289,21 +529,14 @@ function useCapxulSignOut() {
|
|
|
289
529
|
onMutate: async () => {
|
|
290
530
|
await queryClient.cancelQueries({ queryKey: capxulKeys.accountBalance });
|
|
291
531
|
},
|
|
292
|
-
mutationFn: async () =>
|
|
532
|
+
mutationFn: async () => {
|
|
533
|
+
const bootstrappedClient = requireBootstrappedClient(client, "auth.signOut");
|
|
534
|
+
return unwrapCapxulResult(await bootstrappedClient.auth.signOut(), bootstrappedClient._internal.telemetry, "mutation");
|
|
535
|
+
},
|
|
293
536
|
onSuccess: () => resetAuthBoundary(queryClient)
|
|
294
537
|
});
|
|
295
538
|
}
|
|
296
539
|
//#endregion
|
|
297
|
-
//#region src/hooks/use-capxul-ensure-ready.ts
|
|
298
|
-
function useCapxulEnsureReady() {
|
|
299
|
-
const client = useCapxulClientOrNull();
|
|
300
|
-
const queryClient = useQueryClient();
|
|
301
|
-
return useMutation({
|
|
302
|
-
mutationFn: async () => unwrapCapxulResult(await requireBootstrappedClient(client, "account.ensureReady").account.ensureReady()),
|
|
303
|
-
onSuccess: () => invalidateAuthBoundary(queryClient)
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
//#endregion
|
|
307
540
|
//#region src/hooks/use-capxul-sub-accounts.ts
|
|
308
541
|
function useCapxulSubAccountsList(accountId, options) {
|
|
309
542
|
const client = useCapxulClientOrNull();
|
|
@@ -311,7 +544,8 @@ function useCapxulSubAccountsList(accountId, options) {
|
|
|
311
544
|
queryKey: capxulKeys.subAccounts(accountId),
|
|
312
545
|
queryFn: async () => {
|
|
313
546
|
if (accountId === void 0) throw Errors.invalidInput("accountId", "required for subAccounts.list");
|
|
314
|
-
|
|
547
|
+
const bootstrappedClient = requireBootstrappedClient(client, "subAccounts.list");
|
|
548
|
+
return unwrapCapxulResult(await bootstrappedClient.subAccounts.list(accountId), bootstrappedClient._internal.telemetry);
|
|
315
549
|
},
|
|
316
550
|
enabled: client !== null && (options?.enabled ?? true) && accountId !== void 0
|
|
317
551
|
});
|
|
@@ -320,7 +554,10 @@ function useCapxulSubAccountCreate() {
|
|
|
320
554
|
const client = useCapxulClientOrNull();
|
|
321
555
|
const queryClient = useQueryClient();
|
|
322
556
|
return useMutation({
|
|
323
|
-
mutationFn: async (input) =>
|
|
557
|
+
mutationFn: async (input) => {
|
|
558
|
+
const bootstrappedClient = requireBootstrappedClient(client, "subAccounts.create");
|
|
559
|
+
return unwrapCapxulResult(await bootstrappedClient.subAccounts.create(input.accountId, { name: input.name }), bootstrappedClient._internal.telemetry, "mutation");
|
|
560
|
+
},
|
|
324
561
|
onSuccess: async (_value, variables) => {
|
|
325
562
|
await queryClient.invalidateQueries({ queryKey: capxulKeys.subAccounts(variables.accountId) });
|
|
326
563
|
await queryClient.invalidateQueries({ queryKey: capxulKeys.accountBalance });
|
|
@@ -331,7 +568,10 @@ function useCapxulSubAccountRename() {
|
|
|
331
568
|
const client = useCapxulClientOrNull();
|
|
332
569
|
const queryClient = useQueryClient();
|
|
333
570
|
return useMutation({
|
|
334
|
-
mutationFn: async (input) =>
|
|
571
|
+
mutationFn: async (input) => {
|
|
572
|
+
const bootstrappedClient = requireBootstrappedClient(client, "subAccounts.rename");
|
|
573
|
+
return unwrapCapxulResult(await bootstrappedClient.subAccounts.rename(input.subAccountId, input.name), bootstrappedClient._internal.telemetry, "mutation");
|
|
574
|
+
},
|
|
335
575
|
onSuccess: async (_value, variables) => {
|
|
336
576
|
await queryClient.invalidateQueries({ queryKey: capxulKeys.subAccounts(variables.accountId) });
|
|
337
577
|
await queryClient.invalidateQueries({ queryKey: capxulKeys.accountBalance });
|
|
@@ -342,7 +582,10 @@ function useCapxulSubAccountDelete() {
|
|
|
342
582
|
const client = useCapxulClientOrNull();
|
|
343
583
|
const queryClient = useQueryClient();
|
|
344
584
|
return useMutation({
|
|
345
|
-
mutationFn: async (input) =>
|
|
585
|
+
mutationFn: async (input) => {
|
|
586
|
+
const bootstrappedClient = requireBootstrappedClient(client, "subAccounts.delete");
|
|
587
|
+
return unwrapCapxulResult(await bootstrappedClient.subAccounts.delete(input.subAccountId), bootstrappedClient._internal.telemetry, "mutation");
|
|
588
|
+
},
|
|
346
589
|
onSuccess: async (_value, variables) => {
|
|
347
590
|
await queryClient.invalidateQueries({ queryKey: capxulKeys.subAccounts(variables.accountId) });
|
|
348
591
|
await queryClient.invalidateQueries({ queryKey: capxulKeys.accountBalance });
|
|
@@ -353,11 +596,14 @@ function useCapxulTransfer() {
|
|
|
353
596
|
const client = useCapxulClientOrNull();
|
|
354
597
|
const queryClient = useQueryClient();
|
|
355
598
|
return useMutation({
|
|
356
|
-
mutationFn: async ({ from, to, amount }) =>
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
599
|
+
mutationFn: async ({ from, to, amount }) => {
|
|
600
|
+
const bootstrappedClient = requireBootstrappedClient(client, "subAccounts.transfer");
|
|
601
|
+
return unwrapCapxulResult(await bootstrappedClient.subAccounts.transfer({
|
|
602
|
+
from,
|
|
603
|
+
to,
|
|
604
|
+
amount
|
|
605
|
+
}), bootstrappedClient._internal.telemetry, "mutation");
|
|
606
|
+
},
|
|
361
607
|
onSuccess: async (_value, variables) => {
|
|
362
608
|
await queryClient.invalidateQueries({ queryKey: capxulKeys.subAccounts(variables.accountId) });
|
|
363
609
|
await queryClient.invalidateQueries({ queryKey: capxulKeys.accountBalance });
|
|
@@ -367,11 +613,14 @@ function useCapxulTransfer() {
|
|
|
367
613
|
//#endregion
|
|
368
614
|
//#region src/hooks/use-capxul-orgs.ts
|
|
369
615
|
function useCapxulOrgs(options) {
|
|
370
|
-
const client =
|
|
616
|
+
const client = useCapxulClientOrNull();
|
|
371
617
|
return useQuery({
|
|
372
618
|
queryKey: capxulKeys.orgs,
|
|
373
|
-
queryFn: async () =>
|
|
374
|
-
|
|
619
|
+
queryFn: async () => {
|
|
620
|
+
const bootstrappedClient = requireBootstrappedClient(client, "orgs");
|
|
621
|
+
return unwrapCapxulResult(await bootstrappedClient.orgs(), bootstrappedClient._internal.telemetry);
|
|
622
|
+
},
|
|
623
|
+
enabled: client !== null && (options?.enabled ?? true)
|
|
375
624
|
});
|
|
376
625
|
}
|
|
377
626
|
//#endregion
|
|
@@ -382,7 +631,7 @@ function useCapxulOrg(orgId, options) {
|
|
|
382
631
|
queryKey: capxulKeys.org(orgId),
|
|
383
632
|
queryFn: async () => {
|
|
384
633
|
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulOrg");
|
|
385
|
-
return unwrapCapxulResult(await client.orgs()).find((org) => org.id === orgId) ?? null;
|
|
634
|
+
return unwrapCapxulResult(await client.orgs(), client._internal.telemetry).find((org) => org.id === orgId) ?? null;
|
|
386
635
|
},
|
|
387
636
|
enabled: (options?.enabled ?? true) && orgId !== void 0
|
|
388
637
|
});
|
|
@@ -395,7 +644,7 @@ function useCapxulOrgMembers(orgId, options) {
|
|
|
395
644
|
queryKey: capxulKeys.orgMembers(orgId),
|
|
396
645
|
queryFn: async () => {
|
|
397
646
|
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulOrgMembers");
|
|
398
|
-
return unwrapCapxulResult(await client.org(orgId).members());
|
|
647
|
+
return unwrapCapxulResult(await client.org(orgId).members(), client._internal.telemetry);
|
|
399
648
|
},
|
|
400
649
|
enabled: (options?.enabled ?? true) && orgId !== void 0
|
|
401
650
|
});
|
|
@@ -408,7 +657,7 @@ function useCapxulOrgRoles(orgId, options) {
|
|
|
408
657
|
queryKey: capxulKeys.orgRoles(orgId),
|
|
409
658
|
queryFn: async () => {
|
|
410
659
|
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulOrgRoles");
|
|
411
|
-
return unwrapCapxulResult(await client.org(orgId).roles());
|
|
660
|
+
return unwrapCapxulResult(await client.org(orgId).roles(), client._internal.telemetry);
|
|
412
661
|
},
|
|
413
662
|
enabled: (options?.enabled ?? true) && orgId !== void 0
|
|
414
663
|
});
|
|
@@ -420,7 +669,7 @@ function useCapxulOrgDeployRoles() {
|
|
|
420
669
|
const queryClient = useQueryClient();
|
|
421
670
|
return useMutation({
|
|
422
671
|
mutationFn: async (orgId) => {
|
|
423
|
-
return unwrapCapxulResult(await client.org(orgId).deployRoles());
|
|
672
|
+
return unwrapCapxulResult(await client.org(orgId).deployRoles(), client._internal.telemetry, "mutation");
|
|
424
673
|
},
|
|
425
674
|
onSuccess: async (_roles, orgId) => {
|
|
426
675
|
await queryClient.invalidateQueries({ queryKey: capxulKeys.orgRoles(orgId) });
|
|
@@ -437,7 +686,7 @@ function useCapxulOrgTreasury(orgId, options) {
|
|
|
437
686
|
queryKey: capxulKeys.orgTreasury(orgId),
|
|
438
687
|
queryFn: async () => {
|
|
439
688
|
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulOrgTreasury");
|
|
440
|
-
return unwrapCapxulResult(await client.org(orgId).treasury());
|
|
689
|
+
return unwrapCapxulResult(await client.org(orgId).treasury(), client._internal.telemetry);
|
|
441
690
|
},
|
|
442
691
|
enabled: (options?.enabled ?? true) && orgId !== void 0
|
|
443
692
|
});
|
|
@@ -448,7 +697,7 @@ function useCapxulCreateOrg() {
|
|
|
448
697
|
const client = useCapxulClient();
|
|
449
698
|
const queryClient = useQueryClient();
|
|
450
699
|
return useMutation({
|
|
451
|
-
mutationFn: async (input) => unwrapCapxulResult(await client.createOrg(input)),
|
|
700
|
+
mutationFn: async (input) => unwrapCapxulResult(await client.createOrg(input), client._internal.telemetry, "mutation"),
|
|
452
701
|
onSuccess: async () => {
|
|
453
702
|
await queryClient.invalidateQueries({ queryKey: capxulKeys.orgs });
|
|
454
703
|
}
|
|
@@ -462,7 +711,7 @@ function useCapxulInviteMember(orgId) {
|
|
|
462
711
|
return useMutation({
|
|
463
712
|
mutationFn: async (input) => {
|
|
464
713
|
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulInviteMember");
|
|
465
|
-
return unwrapCapxulResult(await client.org(orgId).invite(input));
|
|
714
|
+
return unwrapCapxulResult(await client.org(orgId).invite(input), client._internal.telemetry, "mutation");
|
|
466
715
|
},
|
|
467
716
|
onSuccess: async () => {
|
|
468
717
|
if (orgId === void 0) return;
|
|
@@ -478,7 +727,7 @@ function useCapxulRemoveMember(orgId) {
|
|
|
478
727
|
return useMutation({
|
|
479
728
|
mutationFn: async (input) => {
|
|
480
729
|
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulRemoveMember");
|
|
481
|
-
return unwrapCapxulResult(await client.org(orgId).removeMember(input));
|
|
730
|
+
return unwrapCapxulResult(await client.org(orgId).removeMember(input), client._internal.telemetry, "mutation");
|
|
482
731
|
},
|
|
483
732
|
onSuccess: async () => {
|
|
484
733
|
if (orgId === void 0) return;
|
|
@@ -494,7 +743,7 @@ function useCapxulAssignRole(orgId) {
|
|
|
494
743
|
return useMutation({
|
|
495
744
|
mutationFn: async (input) => {
|
|
496
745
|
if (orgId === void 0) throw Errors.invalidInput("orgId", "required for useCapxulAssignRole");
|
|
497
|
-
return unwrapCapxulResult(await client.org(orgId).assignRole(input));
|
|
746
|
+
return unwrapCapxulResult(await client.org(orgId).assignRole(input), client._internal.telemetry, "mutation");
|
|
498
747
|
},
|
|
499
748
|
onSuccess: async () => {
|
|
500
749
|
if (orgId === void 0) return;
|
|
@@ -503,22 +752,11 @@ function useCapxulAssignRole(orgId) {
|
|
|
503
752
|
});
|
|
504
753
|
}
|
|
505
754
|
//#endregion
|
|
506
|
-
//#region src/hooks/use-capxul-
|
|
507
|
-
function
|
|
508
|
-
|
|
509
|
-
const queryClient = useQueryClient();
|
|
510
|
-
return useMutation({
|
|
511
|
-
mutationFn: async (input) => {
|
|
512
|
-
if (orgId === void 0) throw Errors.invalidInput("orgId", "org is not selected");
|
|
513
|
-
return unwrapCapxulResult(await client.org(orgId).spend(input));
|
|
514
|
-
},
|
|
515
|
-
onSuccess: async () => {
|
|
516
|
-
if (orgId === void 0) return;
|
|
517
|
-
await queryClient.invalidateQueries({ queryKey: capxulKeys.orgTreasury(orgId) });
|
|
518
|
-
}
|
|
519
|
-
});
|
|
755
|
+
//#region src/hooks/use-capxul-switch-acting-entity.ts
|
|
756
|
+
function useCapxulSwitchActingEntity() {
|
|
757
|
+
return useMutation({ mutationFn: async (_input) => void 0 });
|
|
520
758
|
}
|
|
521
759
|
//#endregion
|
|
522
|
-
export { CapxulProvider, useCapxul,
|
|
760
|
+
export { CapxulProvider, useCapxul, useCapxulAccountBalance, useCapxulAccountFund, useCapxulAccountLifecycle, useCapxulAssignRole, useCapxulClientOrNull, useCapxulCreateOrg, useCapxulInviteMember, useCapxulOrg, useCapxulOrgDeployRoles, useCapxulOrgMembers, useCapxulOrgRoles, useCapxulOrgTreasury, useCapxulOrgs, useCapxulProfile, useCapxulRemoveMember, useCapxulSession, useCapxulSignIn, useCapxulSignOut, useCapxulSubAccountCreate, useCapxulSubAccountDelete, useCapxulSubAccountRename, useCapxulSubAccountsList, useCapxulSwitchActingEntity, useCapxulTransfer, useCapxulVerifyOtp };
|
|
523
761
|
|
|
524
762
|
//# sourceMappingURL=index.mjs.map
|