@fractalshq/sync 0.0.10 → 0.0.11

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.
@@ -0,0 +1,212 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __spreadValues = (a, b) => {
10
+ for (var prop in b || (b = {}))
11
+ if (__hasOwnProp.call(b, prop))
12
+ __defNormalProp(a, prop, b[prop]);
13
+ if (__getOwnPropSymbols)
14
+ for (var prop of __getOwnPropSymbols(b)) {
15
+ if (__propIsEnum.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ }
18
+ return a;
19
+ };
20
+ var __objRest = (source, exclude) => {
21
+ var target = {};
22
+ for (var prop in source)
23
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
24
+ target[prop] = source[prop];
25
+ if (source != null && __getOwnPropSymbols)
26
+ for (var prop of __getOwnPropSymbols(source)) {
27
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
28
+ target[prop] = source[prop];
29
+ }
30
+ return target;
31
+ };
32
+ var __export = (target, all) => {
33
+ for (var name in all)
34
+ __defProp(target, name, { get: all[name], enumerable: true });
35
+ };
36
+ var __copyProps = (to, from, except, desc) => {
37
+ if (from && typeof from === "object" || typeof from === "function") {
38
+ for (let key of __getOwnPropNames(from))
39
+ if (!__hasOwnProp.call(to, key) && key !== except)
40
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
41
+ }
42
+ return to;
43
+ };
44
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
45
+
46
+ // src/v2/index.ts
47
+ var v2_exports = {};
48
+ __export(v2_exports, {
49
+ DEFAULT_BASE_PATH: () => DEFAULT_BASE_PATH,
50
+ SyncReactError: () => SyncReactError,
51
+ buildClaimTransaction: () => buildClaimTransaction,
52
+ buildUrl: () => buildUrl,
53
+ commitClaim: () => commitClaim,
54
+ isAbsoluteUrl: () => isAbsoluteUrl,
55
+ isRelativeUrl: () => isRelativeUrl,
56
+ listClaimHistory: () => listClaimHistory,
57
+ listClaimable: () => listClaimable,
58
+ requestJSON: () => requestJSON,
59
+ resolveFetcher: () => resolveFetcher,
60
+ resolveRequestConfig: () => resolveRequestConfig,
61
+ sanitizeBasePath: () => sanitizeBasePath
62
+ });
63
+ module.exports = __toCommonJS(v2_exports);
64
+
65
+ // src/v2/core/http.ts
66
+ var FALLBACK_BASE_PATH = "/api/v2";
67
+ var _a, _b;
68
+ var ENV_BASE_PATH = typeof process !== "undefined" && (((_a = process.env) == null ? void 0 : _a.NEXT_PUBLIC_SYNC_V2_PATH) || ((_b = process.env) == null ? void 0 : _b.NEXT_PUBLIC_FRACTALS_SYNC_V2_PATH)) || void 0;
69
+ var DEFAULT_BASE_PATH = sanitizeBasePath(ENV_BASE_PATH || FALLBACK_BASE_PATH);
70
+ function resolveRequestConfig(options) {
71
+ return {
72
+ basePath: sanitizeBasePath((options == null ? void 0 : options.basePath) || DEFAULT_BASE_PATH),
73
+ fetcher: resolveFetcher(options == null ? void 0 : options.fetcher)
74
+ };
75
+ }
76
+ var SyncReactError = class extends Error {
77
+ constructor(status, code, details) {
78
+ super(code || `Sync request failed with status ${status}`);
79
+ this.status = status;
80
+ this.code = code;
81
+ this.details = details;
82
+ this.name = "SyncReactError";
83
+ }
84
+ };
85
+ async function requestJSON(config, path, init) {
86
+ var _a2;
87
+ const url = buildUrl(config.basePath, path);
88
+ const requestInit = prepareInit(init);
89
+ requestInit.credentials = (_a2 = requestInit.credentials) != null ? _a2 : isRelativeUrl(url) ? "include" : void 0;
90
+ const response = await config.fetcher(url, requestInit);
91
+ const payload = await parseResponseBody(response);
92
+ if (!response.ok) {
93
+ const errorCode = typeof payload === "object" && payload && "error" in payload ? String(payload.error) : void 0;
94
+ throw new SyncReactError(response.status, errorCode, payload);
95
+ }
96
+ return payload;
97
+ }
98
+ function resolveFetcher(custom) {
99
+ if (custom) return custom;
100
+ if (typeof fetch === "function") return fetch.bind(globalThis);
101
+ throw new Error("Global fetch is not available. Provide a fetcher via Sync configuration.");
102
+ }
103
+ function sanitizeBasePath(path) {
104
+ if (!path) return FALLBACK_BASE_PATH;
105
+ const trimmed = path.trim();
106
+ if (!trimmed || trimmed === "/") return "/";
107
+ return trimmed.replace(/\/+$/, "");
108
+ }
109
+ function buildUrl(basePath, path) {
110
+ if (path && isAbsoluteUrl(path)) return path;
111
+ const normalizedBase = sanitizeBasePath(basePath);
112
+ const suffix = path ? `/${path.replace(/^\/+/, "")}` : "";
113
+ if (!normalizedBase || normalizedBase === "/") {
114
+ return suffix ? suffix : normalizedBase;
115
+ }
116
+ return `${normalizedBase}${suffix}`;
117
+ }
118
+ function isAbsoluteUrl(path) {
119
+ return /^https?:\/\//i.test(path);
120
+ }
121
+ function isRelativeUrl(path) {
122
+ return !isAbsoluteUrl(path);
123
+ }
124
+ function prepareInit(init) {
125
+ const finalInit = __spreadValues({}, init);
126
+ if ((init == null ? void 0 : init.headers) instanceof Headers) {
127
+ const cloned = new Headers(init.headers);
128
+ if (!cloned.has("content-type")) cloned.set("content-type", "application/json");
129
+ finalInit.headers = cloned;
130
+ return finalInit;
131
+ }
132
+ const headers = new Headers(init == null ? void 0 : init.headers);
133
+ if (!headers.has("content-type")) {
134
+ headers.set("content-type", "application/json");
135
+ }
136
+ finalInit.headers = headers;
137
+ return finalInit;
138
+ }
139
+ async function parseResponseBody(response) {
140
+ if (response.status === 204) return void 0;
141
+ const text = await response.text();
142
+ if (!text) return void 0;
143
+ try {
144
+ return JSON.parse(text);
145
+ } catch (e) {
146
+ return text;
147
+ }
148
+ }
149
+
150
+ // src/v2/core/api.ts
151
+ async function buildClaimTransaction(input, options) {
152
+ const distributionId = input == null ? void 0 : input.distributionId;
153
+ if (!distributionId) {
154
+ throw new SyncReactError(400, "distribution_id_required");
155
+ }
156
+ const config = resolveRequestConfig(options);
157
+ const _a2 = input, { distributionId: _unused } = _a2, body = __objRest(_a2, ["distributionId"]);
158
+ return requestJSON(
159
+ config,
160
+ `/claims/${encodeURIComponent(distributionId)}/create-transaction`,
161
+ {
162
+ method: "POST",
163
+ body: JSON.stringify(body)
164
+ }
165
+ );
166
+ }
167
+ async function commitClaim(input, options) {
168
+ const distributionId = input == null ? void 0 : input.distributionId;
169
+ if (!distributionId) {
170
+ throw new SyncReactError(400, "distribution_id_required");
171
+ }
172
+ if (!(input == null ? void 0 : input.signedTransactionBase64)) {
173
+ throw new SyncReactError(400, "signed_transaction_required");
174
+ }
175
+ if (!(input == null ? void 0 : input.claimantPubkey)) {
176
+ throw new SyncReactError(400, "claimant_pubkey_required");
177
+ }
178
+ const config = resolveRequestConfig(options);
179
+ const _a2 = input, { distributionId: _unused } = _a2, body = __objRest(_a2, ["distributionId"]);
180
+ return requestJSON(
181
+ config,
182
+ `/claims/${encodeURIComponent(distributionId)}/commit`,
183
+ {
184
+ method: "POST",
185
+ body: JSON.stringify(body)
186
+ }
187
+ );
188
+ }
189
+ async function listClaimable(options) {
190
+ const config = resolveRequestConfig(options);
191
+ return requestJSON(config, "/claims/me", { method: "GET" });
192
+ }
193
+ async function listClaimHistory(options) {
194
+ const config = resolveRequestConfig(options);
195
+ return requestJSON(config, "/claims/history", { method: "GET" });
196
+ }
197
+ // Annotate the CommonJS export names for ESM import in node:
198
+ 0 && (module.exports = {
199
+ DEFAULT_BASE_PATH,
200
+ SyncReactError,
201
+ buildClaimTransaction,
202
+ buildUrl,
203
+ commitClaim,
204
+ isAbsoluteUrl,
205
+ isRelativeUrl,
206
+ listClaimHistory,
207
+ listClaimable,
208
+ requestJSON,
209
+ resolveFetcher,
210
+ resolveRequestConfig,
211
+ sanitizeBasePath
212
+ });
@@ -0,0 +1,31 @@
1
+ import {
2
+ DEFAULT_BASE_PATH,
3
+ SyncReactError,
4
+ buildClaimTransaction,
5
+ buildUrl,
6
+ commitClaim,
7
+ isAbsoluteUrl,
8
+ isRelativeUrl,
9
+ listClaimHistory,
10
+ listClaimable,
11
+ requestJSON,
12
+ resolveFetcher,
13
+ resolveRequestConfig,
14
+ sanitizeBasePath
15
+ } from "../chunk-RTLKOAT7.mjs";
16
+ import "../chunk-FWCSY2DS.mjs";
17
+ export {
18
+ DEFAULT_BASE_PATH,
19
+ SyncReactError,
20
+ buildClaimTransaction,
21
+ buildUrl,
22
+ commitClaim,
23
+ isAbsoluteUrl,
24
+ isRelativeUrl,
25
+ listClaimHistory,
26
+ listClaimable,
27
+ requestJSON,
28
+ resolveFetcher,
29
+ resolveRequestConfig,
30
+ sanitizeBasePath
31
+ };
@@ -0,0 +1,50 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { UseQueryOptions, UseQueryResult, UseMutationOptions, UseMutationResult } from '@tanstack/react-query';
4
+ import { Fetcher, ClaimableResponse, SyncReactError, ClaimHistoryResponse, BuildClaimTransactionRequest, BuildClaimTransactionResponse, CommitClaimRequest, CommitClaimResponse } from '../core/index.mjs';
5
+
6
+ interface SyncContextValue {
7
+ basePath: string;
8
+ fetcher?: Fetcher;
9
+ }
10
+ interface SyncProviderProps {
11
+ basePath?: string;
12
+ fetcher?: Fetcher;
13
+ children: ReactNode;
14
+ }
15
+ declare function SyncProvider({ basePath, fetcher, children }: SyncProviderProps): react.FunctionComponentElement<react.ProviderProps<SyncContextValue>>;
16
+ interface SyncClient {
17
+ basePath: string;
18
+ request<T>(path: string, init?: RequestInit): Promise<T>;
19
+ get<T>(path: string, init?: RequestInit): Promise<T>;
20
+ post<T>(path: string, body?: unknown, init?: RequestInit): Promise<T>;
21
+ }
22
+ declare function useSyncClient(): SyncClient;
23
+ type QueryOptions<T> = Omit<UseQueryOptions<T, SyncReactError, T>, "queryKey" | "queryFn">;
24
+ declare function useClaimableDistributions<TResponse = ClaimableResponse>(options?: QueryOptions<TResponse>): UseQueryResult<TResponse, SyncReactError>;
25
+ declare function useClaimHistory<TResponse = ClaimHistoryResponse>(options?: QueryOptions<TResponse>): UseQueryResult<TResponse, SyncReactError>;
26
+ type ClaimTransactionInput = Partial<Omit<BuildClaimTransactionRequest, "distributionId" | "claimant">>;
27
+ declare function useClaimTransaction(distributionId: string | null | undefined, options?: UseMutationOptions<BuildClaimTransactionResponse, SyncReactError, ClaimTransactionInput>): UseMutationResult<BuildClaimTransactionResponse, SyncReactError, ClaimTransactionInput>;
28
+ type ClaimCommitInput = Omit<CommitClaimRequest, "distributionId">;
29
+ declare function useCommitClaim(distributionId: string | null | undefined, options?: UseMutationOptions<CommitClaimResponse, SyncReactError, ClaimCommitInput>): UseMutationResult<CommitClaimResponse, SyncReactError, ClaimCommitInput>;
30
+ interface ClaimFlowState {
31
+ latestPayload: BuildClaimTransactionResponse | null;
32
+ building: boolean;
33
+ claiming: boolean;
34
+ error: SyncReactError | null;
35
+ }
36
+ type ClaimFlowSigner = (payload: BuildClaimTransactionResponse) => Promise<ClaimCommitInput> | ClaimCommitInput;
37
+ interface ClaimFlowClaimResult {
38
+ payload: BuildClaimTransactionResponse;
39
+ commit: CommitClaimResponse;
40
+ signerInput: ClaimCommitInput;
41
+ }
42
+ interface ClaimFlowResult {
43
+ state: ClaimFlowState;
44
+ build: (input?: ClaimTransactionInput) => Promise<BuildClaimTransactionResponse>;
45
+ claim: (signer: ClaimFlowSigner, input?: ClaimTransactionInput) => Promise<ClaimFlowClaimResult>;
46
+ reset: () => void;
47
+ }
48
+ declare function useClaimFlow(distributionId: string | null | undefined): ClaimFlowResult;
49
+
50
+ export { type ClaimCommitInput, type ClaimFlowClaimResult, type ClaimFlowResult, type ClaimFlowSigner, type ClaimFlowState, type ClaimTransactionInput, type SyncClient, SyncProvider, type SyncProviderProps, useClaimFlow, useClaimHistory, useClaimTransaction, useClaimableDistributions, useCommitClaim, useSyncClient };
@@ -0,0 +1,50 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { UseQueryOptions, UseQueryResult, UseMutationOptions, UseMutationResult } from '@tanstack/react-query';
4
+ import { Fetcher, ClaimableResponse, SyncReactError, ClaimHistoryResponse, BuildClaimTransactionRequest, BuildClaimTransactionResponse, CommitClaimRequest, CommitClaimResponse } from '../core/index.js';
5
+
6
+ interface SyncContextValue {
7
+ basePath: string;
8
+ fetcher?: Fetcher;
9
+ }
10
+ interface SyncProviderProps {
11
+ basePath?: string;
12
+ fetcher?: Fetcher;
13
+ children: ReactNode;
14
+ }
15
+ declare function SyncProvider({ basePath, fetcher, children }: SyncProviderProps): react.FunctionComponentElement<react.ProviderProps<SyncContextValue>>;
16
+ interface SyncClient {
17
+ basePath: string;
18
+ request<T>(path: string, init?: RequestInit): Promise<T>;
19
+ get<T>(path: string, init?: RequestInit): Promise<T>;
20
+ post<T>(path: string, body?: unknown, init?: RequestInit): Promise<T>;
21
+ }
22
+ declare function useSyncClient(): SyncClient;
23
+ type QueryOptions<T> = Omit<UseQueryOptions<T, SyncReactError, T>, "queryKey" | "queryFn">;
24
+ declare function useClaimableDistributions<TResponse = ClaimableResponse>(options?: QueryOptions<TResponse>): UseQueryResult<TResponse, SyncReactError>;
25
+ declare function useClaimHistory<TResponse = ClaimHistoryResponse>(options?: QueryOptions<TResponse>): UseQueryResult<TResponse, SyncReactError>;
26
+ type ClaimTransactionInput = Partial<Omit<BuildClaimTransactionRequest, "distributionId" | "claimant">>;
27
+ declare function useClaimTransaction(distributionId: string | null | undefined, options?: UseMutationOptions<BuildClaimTransactionResponse, SyncReactError, ClaimTransactionInput>): UseMutationResult<BuildClaimTransactionResponse, SyncReactError, ClaimTransactionInput>;
28
+ type ClaimCommitInput = Omit<CommitClaimRequest, "distributionId">;
29
+ declare function useCommitClaim(distributionId: string | null | undefined, options?: UseMutationOptions<CommitClaimResponse, SyncReactError, ClaimCommitInput>): UseMutationResult<CommitClaimResponse, SyncReactError, ClaimCommitInput>;
30
+ interface ClaimFlowState {
31
+ latestPayload: BuildClaimTransactionResponse | null;
32
+ building: boolean;
33
+ claiming: boolean;
34
+ error: SyncReactError | null;
35
+ }
36
+ type ClaimFlowSigner = (payload: BuildClaimTransactionResponse) => Promise<ClaimCommitInput> | ClaimCommitInput;
37
+ interface ClaimFlowClaimResult {
38
+ payload: BuildClaimTransactionResponse;
39
+ commit: CommitClaimResponse;
40
+ signerInput: ClaimCommitInput;
41
+ }
42
+ interface ClaimFlowResult {
43
+ state: ClaimFlowState;
44
+ build: (input?: ClaimTransactionInput) => Promise<BuildClaimTransactionResponse>;
45
+ claim: (signer: ClaimFlowSigner, input?: ClaimTransactionInput) => Promise<ClaimFlowClaimResult>;
46
+ reset: () => void;
47
+ }
48
+ declare function useClaimFlow(distributionId: string | null | undefined): ClaimFlowResult;
49
+
50
+ export { type ClaimCommitInput, type ClaimFlowClaimResult, type ClaimFlowResult, type ClaimFlowSigner, type ClaimFlowState, type ClaimTransactionInput, type SyncClient, SyncProvider, type SyncProviderProps, useClaimFlow, useClaimHistory, useClaimTransaction, useClaimableDistributions, useCommitClaim, useSyncClient };
@@ -0,0 +1,317 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
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
+ };
35
+ var __export = (target, all) => {
36
+ for (var name in all)
37
+ __defProp(target, name, { get: all[name], enumerable: true });
38
+ };
39
+ var __copyProps = (to, from, except, desc) => {
40
+ if (from && typeof from === "object" || typeof from === "function") {
41
+ for (let key of __getOwnPropNames(from))
42
+ if (!__hasOwnProp.call(to, key) && key !== except)
43
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
44
+ }
45
+ return to;
46
+ };
47
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
48
+
49
+ // src/v2/react/index.tsx
50
+ var react_exports = {};
51
+ __export(react_exports, {
52
+ SyncProvider: () => SyncProvider,
53
+ useClaimFlow: () => useClaimFlow,
54
+ useClaimHistory: () => useClaimHistory,
55
+ useClaimTransaction: () => useClaimTransaction,
56
+ useClaimableDistributions: () => useClaimableDistributions,
57
+ useCommitClaim: () => useCommitClaim,
58
+ useSyncClient: () => useSyncClient
59
+ });
60
+ module.exports = __toCommonJS(react_exports);
61
+ var import_react = require("react");
62
+ var import_react_query = require("@tanstack/react-query");
63
+
64
+ // src/v2/core/http.ts
65
+ var FALLBACK_BASE_PATH = "/api/v2";
66
+ var _a, _b;
67
+ var ENV_BASE_PATH = typeof process !== "undefined" && (((_a = process.env) == null ? void 0 : _a.NEXT_PUBLIC_SYNC_V2_PATH) || ((_b = process.env) == null ? void 0 : _b.NEXT_PUBLIC_FRACTALS_SYNC_V2_PATH)) || void 0;
68
+ var DEFAULT_BASE_PATH = sanitizeBasePath(ENV_BASE_PATH || FALLBACK_BASE_PATH);
69
+ function resolveRequestConfig(options) {
70
+ return {
71
+ basePath: sanitizeBasePath((options == null ? void 0 : options.basePath) || DEFAULT_BASE_PATH),
72
+ fetcher: resolveFetcher(options == null ? void 0 : options.fetcher)
73
+ };
74
+ }
75
+ var SyncReactError = class extends Error {
76
+ constructor(status, code, details) {
77
+ super(code || `Sync request failed with status ${status}`);
78
+ this.status = status;
79
+ this.code = code;
80
+ this.details = details;
81
+ this.name = "SyncReactError";
82
+ }
83
+ };
84
+ async function requestJSON(config, path, init) {
85
+ var _a2;
86
+ const url = buildUrl(config.basePath, path);
87
+ const requestInit = prepareInit(init);
88
+ requestInit.credentials = (_a2 = requestInit.credentials) != null ? _a2 : isRelativeUrl(url) ? "include" : void 0;
89
+ const response = await config.fetcher(url, requestInit);
90
+ const payload = await parseResponseBody(response);
91
+ if (!response.ok) {
92
+ const errorCode = typeof payload === "object" && payload && "error" in payload ? String(payload.error) : void 0;
93
+ throw new SyncReactError(response.status, errorCode, payload);
94
+ }
95
+ return payload;
96
+ }
97
+ function resolveFetcher(custom) {
98
+ if (custom) return custom;
99
+ if (typeof fetch === "function") return fetch.bind(globalThis);
100
+ throw new Error("Global fetch is not available. Provide a fetcher via Sync configuration.");
101
+ }
102
+ function sanitizeBasePath(path) {
103
+ if (!path) return FALLBACK_BASE_PATH;
104
+ const trimmed = path.trim();
105
+ if (!trimmed || trimmed === "/") return "/";
106
+ return trimmed.replace(/\/+$/, "");
107
+ }
108
+ function buildUrl(basePath, path) {
109
+ if (path && isAbsoluteUrl(path)) return path;
110
+ const normalizedBase = sanitizeBasePath(basePath);
111
+ const suffix = path ? `/${path.replace(/^\/+/, "")}` : "";
112
+ if (!normalizedBase || normalizedBase === "/") {
113
+ return suffix ? suffix : normalizedBase;
114
+ }
115
+ return `${normalizedBase}${suffix}`;
116
+ }
117
+ function isAbsoluteUrl(path) {
118
+ return /^https?:\/\//i.test(path);
119
+ }
120
+ function isRelativeUrl(path) {
121
+ return !isAbsoluteUrl(path);
122
+ }
123
+ function prepareInit(init) {
124
+ const finalInit = __spreadValues({}, init);
125
+ if ((init == null ? void 0 : init.headers) instanceof Headers) {
126
+ const cloned = new Headers(init.headers);
127
+ if (!cloned.has("content-type")) cloned.set("content-type", "application/json");
128
+ finalInit.headers = cloned;
129
+ return finalInit;
130
+ }
131
+ const headers = new Headers(init == null ? void 0 : init.headers);
132
+ if (!headers.has("content-type")) {
133
+ headers.set("content-type", "application/json");
134
+ }
135
+ finalInit.headers = headers;
136
+ return finalInit;
137
+ }
138
+ async function parseResponseBody(response) {
139
+ if (response.status === 204) return void 0;
140
+ const text = await response.text();
141
+ if (!text) return void 0;
142
+ try {
143
+ return JSON.parse(text);
144
+ } catch (e) {
145
+ return text;
146
+ }
147
+ }
148
+
149
+ // src/v2/core/api.ts
150
+ async function buildClaimTransaction(input, options) {
151
+ const distributionId = input == null ? void 0 : input.distributionId;
152
+ if (!distributionId) {
153
+ throw new SyncReactError(400, "distribution_id_required");
154
+ }
155
+ const config = resolveRequestConfig(options);
156
+ const _a2 = input, { distributionId: _unused } = _a2, body = __objRest(_a2, ["distributionId"]);
157
+ return requestJSON(
158
+ config,
159
+ `/claims/${encodeURIComponent(distributionId)}/create-transaction`,
160
+ {
161
+ method: "POST",
162
+ body: JSON.stringify(body)
163
+ }
164
+ );
165
+ }
166
+ async function commitClaim(input, options) {
167
+ const distributionId = input == null ? void 0 : input.distributionId;
168
+ if (!distributionId) {
169
+ throw new SyncReactError(400, "distribution_id_required");
170
+ }
171
+ if (!(input == null ? void 0 : input.signedTransactionBase64)) {
172
+ throw new SyncReactError(400, "signed_transaction_required");
173
+ }
174
+ if (!(input == null ? void 0 : input.claimantPubkey)) {
175
+ throw new SyncReactError(400, "claimant_pubkey_required");
176
+ }
177
+ const config = resolveRequestConfig(options);
178
+ const _a2 = input, { distributionId: _unused } = _a2, body = __objRest(_a2, ["distributionId"]);
179
+ return requestJSON(
180
+ config,
181
+ `/claims/${encodeURIComponent(distributionId)}/commit`,
182
+ {
183
+ method: "POST",
184
+ body: JSON.stringify(body)
185
+ }
186
+ );
187
+ }
188
+ async function listClaimable(options) {
189
+ const config = resolveRequestConfig(options);
190
+ return requestJSON(config, "/claims/me", { method: "GET" });
191
+ }
192
+ async function listClaimHistory(options) {
193
+ const config = resolveRequestConfig(options);
194
+ return requestJSON(config, "/claims/history", { method: "GET" });
195
+ }
196
+
197
+ // src/v2/react/index.tsx
198
+ var SyncContext = (0, import_react.createContext)({ basePath: DEFAULT_BASE_PATH });
199
+ function SyncProvider({ basePath, fetcher, children }) {
200
+ const value = (0, import_react.useMemo)(
201
+ () => ({
202
+ basePath: sanitizeBasePath(basePath || DEFAULT_BASE_PATH),
203
+ fetcher
204
+ }),
205
+ [basePath, fetcher]
206
+ );
207
+ return (0, import_react.createElement)(SyncContext.Provider, { value }, children);
208
+ }
209
+ function useSyncClient() {
210
+ const requestConfig = useSyncRequestConfig();
211
+ return (0, import_react.useMemo)(() => {
212
+ const request = (path, init) => requestJSON(requestConfig, path, init);
213
+ return {
214
+ basePath: requestConfig.basePath,
215
+ request,
216
+ get: (path, init) => request(path, __spreadProps(__spreadValues({}, init), { method: "GET" })),
217
+ post: (path, body, init) => {
218
+ const finalInit = __spreadProps(__spreadValues({}, init), { method: ((init == null ? void 0 : init.method) || "POST").toUpperCase() });
219
+ if (body !== void 0) {
220
+ finalInit.body = typeof body === "string" ? body : JSON.stringify(body);
221
+ }
222
+ return request(path, finalInit);
223
+ }
224
+ };
225
+ }, [requestConfig]);
226
+ }
227
+ function useClaimableDistributions(options) {
228
+ const requestConfig = useSyncRequestConfig();
229
+ return (0, import_react_query.useQuery)(__spreadValues({
230
+ queryKey: ["sync", "v2", "claims", "me"],
231
+ queryFn: () => listClaimable(requestConfig)
232
+ }, options));
233
+ }
234
+ function useClaimHistory(options) {
235
+ const requestConfig = useSyncRequestConfig();
236
+ return (0, import_react_query.useQuery)(__spreadValues({
237
+ queryKey: ["sync", "v2", "claims", "history"],
238
+ queryFn: () => listClaimHistory(requestConfig)
239
+ }, options));
240
+ }
241
+ function useClaimTransaction(distributionId, options) {
242
+ const requestConfig = useSyncRequestConfig();
243
+ const id = distributionId || null;
244
+ return (0, import_react_query.useMutation)(__spreadValues({
245
+ mutationFn: async (input = {}) => {
246
+ if (!id) throw new SyncReactError(400, "distribution_id_required");
247
+ return buildClaimTransaction(__spreadProps(__spreadValues({}, input), { distributionId: id }), requestConfig);
248
+ }
249
+ }, options));
250
+ }
251
+ function useCommitClaim(distributionId, options) {
252
+ const requestConfig = useSyncRequestConfig();
253
+ const id = distributionId || null;
254
+ return (0, import_react_query.useMutation)(__spreadValues({
255
+ mutationFn: async (input) => {
256
+ if (!id) throw new SyncReactError(400, "distribution_id_required");
257
+ if (!(input == null ? void 0 : input.signedTransactionBase64)) throw new SyncReactError(400, "signed_transaction_required");
258
+ if (!(input == null ? void 0 : input.claimantPubkey)) throw new SyncReactError(400, "claimant_pubkey_required");
259
+ return commitClaim(__spreadProps(__spreadValues({}, input), { distributionId: id }), requestConfig);
260
+ }
261
+ }, options));
262
+ }
263
+ function useClaimFlow(distributionId) {
264
+ var _a2, _b2;
265
+ const buildMutation = useClaimTransaction(distributionId);
266
+ const commitMutation = useCommitClaim(distributionId);
267
+ const [latestPayload, setLatestPayload] = (0, import_react.useState)(null);
268
+ const build = (0, import_react.useCallback)(
269
+ async (input) => {
270
+ const payload = await buildMutation.mutateAsync(input != null ? input : {});
271
+ setLatestPayload(payload);
272
+ return payload;
273
+ },
274
+ [buildMutation]
275
+ );
276
+ const claim = (0, import_react.useCallback)(
277
+ async (signer, input) => {
278
+ const payload = await build(input);
279
+ const signerInput = await signer(payload);
280
+ if (!(signerInput == null ? void 0 : signerInput.signedTransactionBase64)) {
281
+ throw new SyncReactError(400, "signed_transaction_required");
282
+ }
283
+ if (!(signerInput == null ? void 0 : signerInput.claimantPubkey)) {
284
+ throw new SyncReactError(400, "claimant_pubkey_required");
285
+ }
286
+ const commit = await commitMutation.mutateAsync(signerInput);
287
+ return { payload, commit, signerInput };
288
+ },
289
+ [build, commitMutation]
290
+ );
291
+ const reset = (0, import_react.useCallback)(() => {
292
+ setLatestPayload(null);
293
+ }, []);
294
+ const state = {
295
+ latestPayload,
296
+ building: buildMutation.isPending,
297
+ claiming: commitMutation.isPending,
298
+ error: (_b2 = (_a2 = buildMutation.error) != null ? _a2 : commitMutation.error) != null ? _b2 : null
299
+ };
300
+ return { state, build, claim, reset };
301
+ }
302
+ function useSyncRequestConfig() {
303
+ const context = (0, import_react.useContext)(SyncContext);
304
+ const basePath = sanitizeBasePath((context == null ? void 0 : context.basePath) || DEFAULT_BASE_PATH);
305
+ const fetcher = resolveFetcher(context == null ? void 0 : context.fetcher);
306
+ return (0, import_react.useMemo)(() => ({ basePath, fetcher }), [basePath, fetcher]);
307
+ }
308
+ // Annotate the CommonJS export names for ESM import in node:
309
+ 0 && (module.exports = {
310
+ SyncProvider,
311
+ useClaimFlow,
312
+ useClaimHistory,
313
+ useClaimTransaction,
314
+ useClaimableDistributions,
315
+ useCommitClaim,
316
+ useSyncClient
317
+ });