@fractalshq/sync 0.0.3 → 0.0.5

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.
@@ -1,9 +1,8 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode } from 'react';
3
3
  import { UseQueryOptions, UseQueryResult, UseMutationOptions, UseMutationResult } from '@tanstack/react-query';
4
- import { g as ListDistributionsResponse, a as Distribution, o as ClaimableResponse, q as ClaimHistoryResponse, B as BuildClaimTransactionRequest, l as BuildClaimTransactionResponse, m as CommitClaimRequest, n as CommitClaimResponse } from '../api-CQqyHbnT.js';
4
+ import { F as Fetcher, g as ListDistributionsResponse, K as SyncReactError, a as Distribution, B as BuildDistributionTransactionRequest, k as DistributionTransactionPayload, q as CommitDistributionSignatureInput, p as CommitDistributionResponse, v as ClaimableResponse, x as ClaimHistoryResponse, r as BuildClaimTransactionRequest, s as BuildClaimTransactionResponse, t as CommitClaimRequest, u as CommitClaimResponse } from '../api-Cl-FACri.js';
5
5
 
6
- type Fetcher = typeof fetch;
7
6
  interface SyncContextValue {
8
7
  basePath: string;
9
8
  fetcher?: Fetcher;
@@ -21,20 +20,64 @@ interface SyncClient {
21
20
  post<T>(path: string, body?: unknown, init?: RequestInit): Promise<T>;
22
21
  }
23
22
  declare function useSyncClient(): SyncClient;
24
- declare class SyncReactError extends Error {
25
- readonly status: number;
26
- readonly code?: string | undefined;
27
- readonly details?: unknown | undefined;
28
- constructor(status: number, code?: string | undefined, details?: unknown | undefined);
29
- }
30
23
  type QueryOptions<T> = Omit<UseQueryOptions<T, SyncReactError, T>, "queryKey" | "queryFn">;
31
- declare function useDistributions<TResponse = ListDistributionsResponse>(options?: QueryOptions<TResponse>): UseQueryResult<TResponse, SyncReactError>;
24
+ interface DistributionsQueryParams {
25
+ role?: "claimant" | "creator";
26
+ status?: string | null;
27
+ }
28
+ declare function useDistributions<TResponse = ListDistributionsResponse>(paramsOrOptions?: DistributionsQueryParams | QueryOptions<TResponse>, optionsMaybe?: QueryOptions<TResponse>): UseQueryResult<TResponse, SyncReactError>;
32
29
  declare function useDistribution<TResponse = Distribution>(distributionId: string | null | undefined, options?: QueryOptions<TResponse>): UseQueryResult<TResponse, SyncReactError>;
30
+ type DistributionTransactionInput = BuildDistributionTransactionRequest;
31
+ declare function useDistributionTransaction(options?: UseMutationOptions<DistributionTransactionPayload, SyncReactError, DistributionTransactionInput>): UseMutationResult<DistributionTransactionPayload, SyncReactError, DistributionTransactionInput>;
32
+ type DistributionCommitInput = CommitDistributionSignatureInput;
33
+ declare function useCommitDistribution(options?: UseMutationOptions<CommitDistributionResponse, SyncReactError, DistributionCommitInput>): UseMutationResult<CommitDistributionResponse, SyncReactError, DistributionCommitInput>;
34
+ interface DistributionFlowState {
35
+ latestPayload: DistributionTransactionPayload | null;
36
+ building: boolean;
37
+ committing: boolean;
38
+ error: SyncReactError | null;
39
+ }
40
+ type DistributionFlowSignerInput = Partial<Omit<CommitDistributionSignatureInput, "distributionId">> & {
41
+ [key: string]: unknown;
42
+ };
43
+ type DistributionFlowSigner = (payload: DistributionTransactionPayload) => Promise<DistributionFlowSignerInput> | DistributionFlowSignerInput;
44
+ interface DistributionFlowCommitResult {
45
+ payload: DistributionTransactionPayload;
46
+ commit: CommitDistributionResponse;
47
+ signerInput: DistributionFlowSignerInput;
48
+ }
49
+ interface DistributionFlowResult {
50
+ state: DistributionFlowState;
51
+ build: (input: DistributionTransactionInput) => Promise<DistributionTransactionPayload>;
52
+ commit: (signer: DistributionFlowSigner, input?: DistributionTransactionInput) => Promise<DistributionFlowCommitResult>;
53
+ claim: (signer: DistributionFlowSigner, input?: DistributionTransactionInput) => Promise<DistributionFlowCommitResult>;
54
+ reset: () => void;
55
+ }
56
+ declare function useDistributionFlow(): DistributionFlowResult;
33
57
  declare function useClaimableDistributions<TResponse = ClaimableResponse>(options?: QueryOptions<TResponse>): UseQueryResult<TResponse, SyncReactError>;
34
58
  declare function useClaimHistory<TResponse = ClaimHistoryResponse>(options?: QueryOptions<TResponse>): UseQueryResult<TResponse, SyncReactError>;
35
59
  type ClaimTransactionInput = Partial<Omit<BuildClaimTransactionRequest, "distributionId" | "claimant">>;
36
60
  declare function useClaimTransaction(distributionId: string | null | undefined, options?: UseMutationOptions<BuildClaimTransactionResponse, SyncReactError, ClaimTransactionInput>): UseMutationResult<BuildClaimTransactionResponse, SyncReactError, ClaimTransactionInput>;
37
61
  type ClaimCommitInput = Omit<CommitClaimRequest, "distributionId">;
38
62
  declare function useCommitClaim(distributionId: string | null | undefined, options?: UseMutationOptions<CommitClaimResponse, SyncReactError, ClaimCommitInput>): UseMutationResult<CommitClaimResponse, SyncReactError, ClaimCommitInput>;
63
+ interface ClaimFlowState {
64
+ latestPayload: BuildClaimTransactionResponse | null;
65
+ building: boolean;
66
+ claiming: boolean;
67
+ error: SyncReactError | null;
68
+ }
69
+ type ClaimFlowSigner = (payload: BuildClaimTransactionResponse) => Promise<ClaimCommitInput> | ClaimCommitInput;
70
+ interface ClaimFlowClaimResult {
71
+ payload: BuildClaimTransactionResponse;
72
+ commit: CommitClaimResponse;
73
+ signerInput: ClaimCommitInput;
74
+ }
75
+ interface ClaimFlowResult {
76
+ state: ClaimFlowState;
77
+ build: (input?: ClaimTransactionInput) => Promise<BuildClaimTransactionResponse>;
78
+ claim: (signer: ClaimFlowSigner, input?: ClaimTransactionInput) => Promise<ClaimFlowClaimResult>;
79
+ reset: () => void;
80
+ }
81
+ declare function useClaimFlow(distributionId: string | null | undefined): ClaimFlowResult;
39
82
 
40
- export { type ClaimCommitInput, type ClaimTransactionInput, type SyncClient, SyncProvider, type SyncProviderProps, SyncReactError, useClaimHistory, useClaimTransaction, useClaimableDistributions, useCommitClaim, useDistribution, useDistributions, useSyncClient };
83
+ export { type ClaimCommitInput, type ClaimFlowClaimResult, type ClaimFlowResult, type ClaimFlowSigner, type ClaimFlowState, type ClaimTransactionInput, type DistributionCommitInput, type DistributionFlowCommitResult, type DistributionFlowResult, type DistributionFlowSigner, type DistributionFlowSignerInput, type DistributionFlowState, type DistributionTransactionInput, type DistributionsQueryParams, type SyncClient, SyncProvider, type SyncProviderProps, useClaimFlow, useClaimHistory, useClaimTransaction, useClaimableDistributions, useCommitClaim, useCommitDistribution, useDistribution, useDistributionFlow, useDistributionTransaction, useDistributions, useSyncClient };
@@ -20,6 +20,18 @@ var __spreadValues = (a, b) => {
20
20
  return a;
21
21
  };
22
22
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
+ var __objRest = (source, exclude) => {
24
+ var target = {};
25
+ for (var prop in source)
26
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
27
+ target[prop] = source[prop];
28
+ if (source != null && __getOwnPropSymbols)
29
+ for (var prop of __getOwnPropSymbols(source)) {
30
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
31
+ target[prop] = source[prop];
32
+ }
33
+ return target;
34
+ };
23
35
  var __export = (target, all) => {
24
36
  for (var name in all)
25
37
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -38,22 +50,191 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
38
50
  var react_exports = {};
39
51
  __export(react_exports, {
40
52
  SyncProvider: () => SyncProvider,
41
- SyncReactError: () => SyncReactError,
53
+ useClaimFlow: () => useClaimFlow,
42
54
  useClaimHistory: () => useClaimHistory,
43
55
  useClaimTransaction: () => useClaimTransaction,
44
56
  useClaimableDistributions: () => useClaimableDistributions,
45
57
  useCommitClaim: () => useCommitClaim,
58
+ useCommitDistribution: () => useCommitDistribution,
46
59
  useDistribution: () => useDistribution,
60
+ useDistributionFlow: () => useDistributionFlow,
61
+ useDistributionTransaction: () => useDistributionTransaction,
47
62
  useDistributions: () => useDistributions,
48
63
  useSyncClient: () => useSyncClient
49
64
  });
50
65
  module.exports = __toCommonJS(react_exports);
51
66
  var import_react = require("react");
52
67
  var import_react_query = require("@tanstack/react-query");
68
+
69
+ // src/core/constants.ts
70
+ var DEFAULT_RPC_ENDPOINT = process.env.NEXT_PUBLIC_SOLANA_RPC_URL || "https://api.mainnet-beta.solana.com";
71
+
72
+ // src/core/persistence.ts
73
+ var STORAGE_PREFIX = "distribution:persistence-warning:";
74
+ var PERSISTENCE_WARNING_EVENT = "distribution:persistence-warning";
75
+ function isBrowser() {
76
+ return typeof window !== "undefined" && typeof localStorage !== "undefined";
77
+ }
78
+ function persistToStorage(record) {
79
+ if (!isBrowser()) return;
80
+ try {
81
+ localStorage.setItem(`${STORAGE_PREFIX}${record.id}`, JSON.stringify(record));
82
+ } catch (error) {
83
+ console.error("[sync:persistence-warning] Failed to write to localStorage", error);
84
+ }
85
+ }
86
+ function emitWarning(record) {
87
+ if (!isBrowser()) return;
88
+ try {
89
+ window.dispatchEvent(new CustomEvent(PERSISTENCE_WARNING_EVENT, { detail: record }));
90
+ } catch (error) {
91
+ console.error("[sync:persistence-warning] Failed to emit warning event", error);
92
+ }
93
+ }
94
+ function recordPersistenceWarning(input) {
95
+ var _a2;
96
+ if (!isBrowser()) return void 0;
97
+ const record = __spreadProps(__spreadValues({}, input), {
98
+ createdAt: (_a2 = input.createdAt) != null ? _a2 : Date.now()
99
+ });
100
+ persistToStorage(record);
101
+ emitWarning(record);
102
+ return record;
103
+ }
104
+ function handlePersistenceWarningResponse(response) {
105
+ var _a2;
106
+ if (!isBrowser() || !response || typeof response !== "object") return;
107
+ const warning = response.persistenceWarning;
108
+ if (!warning) return;
109
+ const distributionId = warning.distributionId || response.distributionId || "unknown";
110
+ const exportJson = (_a2 = response.exportJson) != null ? _a2 : warning.exportJson;
111
+ const reason = warning.reason || "persist_distribution_failed";
112
+ const message = warning.message || "We were unable to save your distribution draft. Download the JSON to avoid data loss.";
113
+ const key = warning.id || `${distributionId}:${reason}:${Date.now()}`;
114
+ recordPersistenceWarning({
115
+ id: key,
116
+ distributionId,
117
+ reason,
118
+ message,
119
+ detail: warning.detail,
120
+ exportJson
121
+ });
122
+ }
123
+
124
+ // src/core/http.ts
53
125
  var FALLBACK_BASE_PATH = "/api/v1/fractals-sync";
54
126
  var _a, _b;
55
127
  var ENV_BASE_PATH = typeof process !== "undefined" && (((_a = process.env) == null ? void 0 : _a.NEXT_PUBLIC_SYNC_PATH) || ((_b = process.env) == null ? void 0 : _b.NEXT_PUBLIC_FRACTALS_SYNC_PATH)) || void 0;
56
128
  var DEFAULT_BASE_PATH = sanitizeBasePath(ENV_BASE_PATH || FALLBACK_BASE_PATH);
129
+ function resolveRequestConfig(options) {
130
+ return {
131
+ basePath: sanitizeBasePath((options == null ? void 0 : options.basePath) || DEFAULT_BASE_PATH),
132
+ fetcher: resolveFetcher(options == null ? void 0 : options.fetcher)
133
+ };
134
+ }
135
+ var SyncReactError = class extends Error {
136
+ constructor(status, code, details) {
137
+ super(code || `Sync request failed with status ${status}`);
138
+ this.status = status;
139
+ this.code = code;
140
+ this.details = details;
141
+ this.name = "SyncReactError";
142
+ }
143
+ };
144
+ async function requestJSON(config, path, init) {
145
+ var _a2;
146
+ const url = buildUrl(config.basePath, path);
147
+ const requestInit = prepareInit(init);
148
+ requestInit.credentials = (_a2 = requestInit.credentials) != null ? _a2 : isRelativeUrl(url) ? "include" : void 0;
149
+ const response = await config.fetcher(url, requestInit);
150
+ const payload = await parseResponseBody(response);
151
+ if (!response.ok) {
152
+ const errorCode = typeof payload === "object" && payload && "error" in payload ? String(payload.error) : void 0;
153
+ throw new SyncReactError(response.status, errorCode, payload);
154
+ }
155
+ return payload;
156
+ }
157
+ function resolveFetcher(custom) {
158
+ if (custom) return custom;
159
+ if (typeof fetch === "function") return fetch.bind(globalThis);
160
+ throw new Error("Global fetch is not available. Provide a fetcher via Sync configuration.");
161
+ }
162
+ function sanitizeBasePath(path) {
163
+ if (!path) return FALLBACK_BASE_PATH;
164
+ const trimmed = path.trim();
165
+ if (!trimmed || trimmed === "/") return "/";
166
+ return trimmed.replace(/\/+$/, "");
167
+ }
168
+ function buildUrl(basePath, path) {
169
+ if (path && isAbsoluteUrl(path)) return path;
170
+ const normalizedBase = sanitizeBasePath(basePath);
171
+ const suffix = path ? `/${path.replace(/^\/+/, "")}` : "";
172
+ if (!normalizedBase || normalizedBase === "/") {
173
+ return suffix ? suffix : normalizedBase;
174
+ }
175
+ return `${normalizedBase}${suffix}`;
176
+ }
177
+ function isAbsoluteUrl(path) {
178
+ return /^https?:\/\//i.test(path);
179
+ }
180
+ function isRelativeUrl(path) {
181
+ return !isAbsoluteUrl(path);
182
+ }
183
+ function prepareInit(init) {
184
+ const finalInit = __spreadValues({}, init);
185
+ if ((init == null ? void 0 : init.headers) instanceof Headers) {
186
+ const cloned = new Headers(init.headers);
187
+ if (!cloned.has("content-type")) cloned.set("content-type", "application/json");
188
+ finalInit.headers = cloned;
189
+ return finalInit;
190
+ }
191
+ const headers = new Headers(init == null ? void 0 : init.headers);
192
+ if (!headers.has("content-type")) {
193
+ headers.set("content-type", "application/json");
194
+ }
195
+ finalInit.headers = headers;
196
+ return finalInit;
197
+ }
198
+ async function parseResponseBody(response) {
199
+ if (response.status === 204) return void 0;
200
+ const text = await response.text();
201
+ if (!text) return void 0;
202
+ try {
203
+ return JSON.parse(text);
204
+ } catch (e) {
205
+ return text;
206
+ }
207
+ }
208
+
209
+ // src/core/api.ts
210
+ async function buildDistributionTransaction(input, options) {
211
+ const config = resolveRequestConfig(options);
212
+ const path = (input == null ? void 0 : input.distributionId) ? `/distributions/${encodeURIComponent(input.distributionId)}/create-transaction` : "/distributions/create-transaction";
213
+ const payload = await requestJSON(config, path, {
214
+ method: "POST",
215
+ body: JSON.stringify(input)
216
+ });
217
+ handlePersistenceWarningResponse(payload);
218
+ return payload;
219
+ }
220
+ async function commitDistributionSignature(input, options) {
221
+ const distributionId = input == null ? void 0 : input.distributionId;
222
+ if (!distributionId) {
223
+ throw new SyncReactError(400, "distribution_id_required");
224
+ }
225
+ const config = resolveRequestConfig(options);
226
+ const _a2 = input, { distributionId: _unused } = _a2, body = __objRest(_a2, ["distributionId"]);
227
+ return requestJSON(
228
+ config,
229
+ `/distributions/${encodeURIComponent(distributionId)}/commit`,
230
+ {
231
+ method: "POST",
232
+ body: JSON.stringify(body)
233
+ }
234
+ );
235
+ }
236
+
237
+ // src/react/index.tsx
57
238
  var SyncContext = (0, import_react.createContext)({ basePath: DEFAULT_BASE_PATH });
58
239
  function SyncProvider({ basePath, fetcher, children }) {
59
240
  const value = (0, import_react.useMemo)(
@@ -66,13 +247,11 @@ function SyncProvider({ basePath, fetcher, children }) {
66
247
  return (0, import_react.createElement)(SyncContext.Provider, { value }, children);
67
248
  }
68
249
  function useSyncClient() {
69
- const context = (0, import_react.useContext)(SyncContext);
70
- const basePath = sanitizeBasePath((context == null ? void 0 : context.basePath) || DEFAULT_BASE_PATH);
71
- const fetcher = resolveFetcher(context == null ? void 0 : context.fetcher);
250
+ const requestConfig = useSyncRequestConfig();
72
251
  return (0, import_react.useMemo)(() => {
73
- const request = (path, init) => requestJSON({ basePath, fetcher }, path, init);
252
+ const request = (path, init) => requestJSON(requestConfig, path, init);
74
253
  return {
75
- basePath,
254
+ basePath: requestConfig.basePath,
76
255
  request,
77
256
  get: (path, init) => request(path, __spreadProps(__spreadValues({}, init), { method: "GET" })),
78
257
  post: (path, body, init) => {
@@ -83,22 +262,16 @@ function useSyncClient() {
83
262
  return request(path, finalInit);
84
263
  }
85
264
  };
86
- }, [basePath, fetcher]);
265
+ }, [requestConfig]);
87
266
  }
88
- var SyncReactError = class extends Error {
89
- constructor(status, code, details) {
90
- super(code || `Sync request failed with status ${status}`);
91
- this.status = status;
92
- this.code = code;
93
- this.details = details;
94
- this.name = "SyncReactError";
95
- }
96
- };
97
- function useDistributions(options) {
267
+ function useDistributions(paramsOrOptions, optionsMaybe) {
268
+ var _a2, _b2;
98
269
  const client = useSyncClient();
270
+ const { params, options } = resolveDistributionsArgs(paramsOrOptions, optionsMaybe);
271
+ const path = buildDistributionsPath(params);
99
272
  return (0, import_react_query.useQuery)(__spreadValues({
100
- queryKey: ["sync", "distributions", "me"],
101
- queryFn: () => client.get("/distributions/me")
273
+ queryKey: ["sync", "distributions", "me", (_a2 = params == null ? void 0 : params.role) != null ? _a2 : "claimant", (_b2 = params == null ? void 0 : params.status) != null ? _b2 : "all"],
274
+ queryFn: () => client.get(path)
102
275
  }, options));
103
276
  }
104
277
  function useDistribution(distributionId, options) {
@@ -112,6 +285,88 @@ function useDistribution(distributionId, options) {
112
285
  enabled: Boolean(id) && ((_a2 = options == null ? void 0 : options.enabled) != null ? _a2 : true)
113
286
  }));
114
287
  }
288
+ function useDistributionTransaction(options) {
289
+ const requestConfig = useSyncRequestConfig();
290
+ return (0, import_react_query.useMutation)(__spreadValues({
291
+ mutationFn: async (input) => {
292
+ if (!input) throw new SyncReactError(400, "distribution_input_required");
293
+ return buildDistributionTransaction(input, requestConfig);
294
+ }
295
+ }, options));
296
+ }
297
+ function useCommitDistribution(options) {
298
+ const requestConfig = useSyncRequestConfig();
299
+ return (0, import_react_query.useMutation)(__spreadValues({
300
+ mutationFn: async (input) => {
301
+ if (!(input == null ? void 0 : input.distributionId)) throw new SyncReactError(400, "distribution_id_required");
302
+ return commitDistributionSignature(input, requestConfig);
303
+ }
304
+ }, options));
305
+ }
306
+ function useDistributionFlow() {
307
+ var _a2, _b2;
308
+ const buildMutation = useDistributionTransaction();
309
+ const commitMutation = useCommitDistribution();
310
+ const [latestPayload, setLatestPayload] = (0, import_react.useState)(null);
311
+ const [latestInput, setLatestInput] = (0, import_react.useState)(null);
312
+ const build = (0, import_react.useCallback)(
313
+ async (input) => {
314
+ const payload = await buildMutation.mutateAsync(input);
315
+ setLatestPayload(payload);
316
+ setLatestInput(cloneDistributionInput(input));
317
+ return payload;
318
+ },
319
+ [buildMutation]
320
+ );
321
+ const doCommit = (0, import_react.useCallback)(
322
+ async (signer, input) => {
323
+ let payload = null;
324
+ if (input) {
325
+ payload = await build(input);
326
+ } else if (latestInput) {
327
+ payload = await build(latestInput);
328
+ } else if (latestPayload) {
329
+ payload = latestPayload;
330
+ }
331
+ if (!payload) {
332
+ throw new SyncReactError(400, "distribution_payload_missing");
333
+ }
334
+ if (!payload.distributionId) {
335
+ throw new SyncReactError(400, "distribution_id_required");
336
+ }
337
+ const signerInput = await signer(payload);
338
+ if (!signerInput || !signerInput.signature && !signerInput.transaction && !signerInput.signedTransactionBase64) {
339
+ throw new SyncReactError(400, "distribution_signature_required");
340
+ }
341
+ const commitPayload = __spreadProps(__spreadValues({}, signerInput), {
342
+ distributionId: payload.distributionId
343
+ });
344
+ if (!commitPayload.exportJson && payload.exportJson) {
345
+ commitPayload.exportJson = payload.exportJson;
346
+ }
347
+ const commitResult = await commitMutation.mutateAsync(commitPayload);
348
+ return { payload, commit: commitResult, signerInput };
349
+ },
350
+ [build, latestInput, latestPayload, commitMutation]
351
+ );
352
+ const reset = (0, import_react.useCallback)(() => {
353
+ setLatestPayload(null);
354
+ setLatestInput(null);
355
+ buildMutation.reset();
356
+ commitMutation.reset();
357
+ }, [buildMutation, commitMutation]);
358
+ const error = (_b2 = (_a2 = buildMutation.error) != null ? _a2 : commitMutation.error) != null ? _b2 : null;
359
+ const state = (0, import_react.useMemo)(
360
+ () => ({
361
+ latestPayload,
362
+ building: buildMutation.isPending,
363
+ committing: commitMutation.isPending,
364
+ error
365
+ }),
366
+ [latestPayload, buildMutation.isPending, commitMutation.isPending, error]
367
+ );
368
+ return { state, build, commit: doCommit, claim: doCommit, reset };
369
+ }
115
370
  function useClaimableDistributions(options) {
116
371
  const client = useSyncClient();
117
372
  return (0, import_react_query.useQuery)(__spreadValues({
@@ -149,79 +404,86 @@ function useCommitClaim(distributionId, options) {
149
404
  }
150
405
  }, options));
151
406
  }
152
- async function requestJSON(config, path, init) {
153
- var _a2;
154
- const url = buildUrl(config.basePath, path);
155
- const requestInit = prepareInit(init);
156
- requestInit.credentials = (_a2 = requestInit.credentials) != null ? _a2 : isRelativeUrl(url) ? "include" : void 0;
157
- const response = await config.fetcher(url, requestInit);
158
- const payload = await parseResponseBody(response);
159
- if (!response.ok) {
160
- const errorCode = typeof payload === "object" && payload && "error" in payload ? String(payload.error) : void 0;
161
- throw new SyncReactError(response.status, errorCode, payload);
162
- }
163
- return payload;
164
- }
165
- function prepareInit(init) {
166
- const finalInit = __spreadValues({}, init);
167
- if ((init == null ? void 0 : init.headers) instanceof Headers) {
168
- const cloned = new Headers(init.headers);
169
- if (!cloned.has("content-type")) cloned.set("content-type", "application/json");
170
- finalInit.headers = cloned;
171
- return finalInit;
172
- }
173
- const headers = new Headers(init == null ? void 0 : init.headers);
174
- if (!headers.has("content-type")) {
175
- headers.set("content-type", "application/json");
176
- }
177
- finalInit.headers = headers;
178
- return finalInit;
179
- }
180
- async function parseResponseBody(response) {
181
- if (response.status === 204) return void 0;
182
- const text = await response.text();
183
- if (!text) return void 0;
184
- try {
185
- return JSON.parse(text);
186
- } catch (e) {
187
- return text;
188
- }
407
+ function useClaimFlow(distributionId) {
408
+ var _a2, _b2;
409
+ const buildMutation = useClaimTransaction(distributionId);
410
+ const commitMutation = useCommitClaim(distributionId);
411
+ const [latestPayload, setLatestPayload] = (0, import_react.useState)(null);
412
+ const build = (0, import_react.useCallback)(
413
+ async (input) => {
414
+ const payload = await buildMutation.mutateAsync(input != null ? input : {});
415
+ setLatestPayload(payload);
416
+ return payload;
417
+ },
418
+ [buildMutation]
419
+ );
420
+ const claim = (0, import_react.useCallback)(
421
+ async (signer, input) => {
422
+ const payload = await build(input);
423
+ const signerInput = await signer(payload);
424
+ if (!signerInput || !signerInput.signature && !signerInput.transaction && !signerInput.signedTransactionBase64) {
425
+ throw new SyncReactError(400, "claim_signature_required");
426
+ }
427
+ const commit = await commitMutation.mutateAsync(signerInput);
428
+ return { payload, commit, signerInput };
429
+ },
430
+ [build, commitMutation]
431
+ );
432
+ const reset = (0, import_react.useCallback)(() => {
433
+ setLatestPayload(null);
434
+ }, []);
435
+ const state = {
436
+ latestPayload,
437
+ building: buildMutation.isPending,
438
+ claiming: commitMutation.isPending,
439
+ error: (_b2 = (_a2 = buildMutation.error) != null ? _a2 : commitMutation.error) != null ? _b2 : null
440
+ };
441
+ return { state, build, claim, reset };
189
442
  }
190
- function resolveFetcher(custom) {
191
- if (custom) return custom;
192
- if (typeof fetch === "function") return fetch.bind(globalThis);
193
- throw new Error("Global fetch is not available. Provide a fetcher via <SyncProvider fetcher={...}>.");
443
+ function useSyncRequestConfig() {
444
+ const context = (0, import_react.useContext)(SyncContext);
445
+ const basePath = sanitizeBasePath((context == null ? void 0 : context.basePath) || DEFAULT_BASE_PATH);
446
+ const fetcher = resolveFetcher(context == null ? void 0 : context.fetcher);
447
+ return (0, import_react.useMemo)(() => ({ basePath, fetcher }), [basePath, fetcher]);
194
448
  }
195
- function sanitizeBasePath(path) {
196
- if (!path) return FALLBACK_BASE_PATH;
197
- const trimmed = path.trim();
198
- if (!trimmed || trimmed === "/") return "/";
199
- return trimmed.replace(/\/+$/, "");
449
+ function cloneDistributionInput(input) {
450
+ return __spreadProps(__spreadValues({}, input), {
451
+ recipients: input.recipients.map((recipient) => __spreadValues({}, recipient))
452
+ });
200
453
  }
201
- function buildUrl(basePath, path) {
202
- if (path && isAbsoluteUrl(path)) return path;
203
- const normalizedBase = sanitizeBasePath(basePath);
204
- const suffix = path ? `/${path.replace(/^\/+/, "")}` : "";
205
- if (!normalizedBase || normalizedBase === "/") {
206
- return suffix ? suffix : normalizedBase;
454
+ function resolveDistributionsArgs(paramsOrOptions, optionsMaybe) {
455
+ if (isDistributionsParams(paramsOrOptions)) {
456
+ return { params: paramsOrOptions, options: optionsMaybe };
207
457
  }
208
- return `${normalizedBase}${suffix}`;
458
+ return { params: void 0, options: paramsOrOptions };
209
459
  }
210
- function isAbsoluteUrl(path) {
211
- return /^https?:\/\//i.test(path);
460
+ function isDistributionsParams(arg) {
461
+ if (!arg || typeof arg !== "object") return false;
462
+ return "role" in arg || "status" in arg;
212
463
  }
213
- function isRelativeUrl(path) {
214
- return !isAbsoluteUrl(path);
464
+ function buildDistributionsPath(params) {
465
+ const search = new URLSearchParams();
466
+ if ((params == null ? void 0 : params.role) && params.role !== "claimant") {
467
+ search.set("role", params.role);
468
+ }
469
+ if (params == null ? void 0 : params.status) {
470
+ search.set("status", params.status);
471
+ }
472
+ const qs = search.toString();
473
+ return qs ? `/distributions/me?${qs}` : "/distributions/me";
215
474
  }
216
475
  // Annotate the CommonJS export names for ESM import in node:
217
476
  0 && (module.exports = {
218
477
  SyncProvider,
219
- SyncReactError,
478
+ useClaimFlow,
220
479
  useClaimHistory,
221
480
  useClaimTransaction,
222
481
  useClaimableDistributions,
223
482
  useCommitClaim,
483
+ useCommitDistribution,
224
484
  useDistribution,
485
+ useDistributionFlow,
486
+ useDistributionTransaction,
225
487
  useDistributions,
226
488
  useSyncClient
227
489
  });