@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,15 +1,20 @@
1
+ import {
2
+ DEFAULT_BASE_PATH,
3
+ SyncReactError,
4
+ buildDistributionTransaction,
5
+ commitDistributionSignature,
6
+ requestJSON,
7
+ resolveFetcher,
8
+ sanitizeBasePath
9
+ } from "../chunk-VXNOPXKB.mjs";
1
10
  import {
2
11
  __spreadProps,
3
12
  __spreadValues
4
- } from "../chunk-I4E63NIC.mjs";
13
+ } from "../chunk-FWCSY2DS.mjs";
5
14
 
6
15
  // src/react/index.tsx
7
- import { createContext, createElement, useContext, useMemo } from "react";
16
+ import { createContext, createElement, useCallback, useContext, useMemo, useState } from "react";
8
17
  import { useMutation, useQuery } from "@tanstack/react-query";
9
- var FALLBACK_BASE_PATH = "/api/v1/fractals-sync";
10
- var _a, _b;
11
- 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;
12
- var DEFAULT_BASE_PATH = sanitizeBasePath(ENV_BASE_PATH || FALLBACK_BASE_PATH);
13
18
  var SyncContext = createContext({ basePath: DEFAULT_BASE_PATH });
14
19
  function SyncProvider({ basePath, fetcher, children }) {
15
20
  const value = useMemo(
@@ -22,13 +27,11 @@ function SyncProvider({ basePath, fetcher, children }) {
22
27
  return createElement(SyncContext.Provider, { value }, children);
23
28
  }
24
29
  function useSyncClient() {
25
- const context = useContext(SyncContext);
26
- const basePath = sanitizeBasePath((context == null ? void 0 : context.basePath) || DEFAULT_BASE_PATH);
27
- const fetcher = resolveFetcher(context == null ? void 0 : context.fetcher);
30
+ const requestConfig = useSyncRequestConfig();
28
31
  return useMemo(() => {
29
- const request = (path, init) => requestJSON({ basePath, fetcher }, path, init);
32
+ const request = (path, init) => requestJSON(requestConfig, path, init);
30
33
  return {
31
- basePath,
34
+ basePath: requestConfig.basePath,
32
35
  request,
33
36
  get: (path, init) => request(path, __spreadProps(__spreadValues({}, init), { method: "GET" })),
34
37
  post: (path, body, init) => {
@@ -39,35 +42,111 @@ function useSyncClient() {
39
42
  return request(path, finalInit);
40
43
  }
41
44
  };
42
- }, [basePath, fetcher]);
43
- }
44
- var SyncReactError = class extends Error {
45
- constructor(status, code, details) {
46
- super(code || `Sync request failed with status ${status}`);
47
- this.status = status;
48
- this.code = code;
49
- this.details = details;
50
- this.name = "SyncReactError";
51
- }
52
- };
53
- function useDistributions(options) {
45
+ }, [requestConfig]);
46
+ }
47
+ function useDistributions(paramsOrOptions, optionsMaybe) {
48
+ var _a, _b;
54
49
  const client = useSyncClient();
50
+ const { params, options } = resolveDistributionsArgs(paramsOrOptions, optionsMaybe);
51
+ const path = buildDistributionsPath(params);
55
52
  return useQuery(__spreadValues({
56
- queryKey: ["sync", "distributions", "me"],
57
- queryFn: () => client.get("/distributions/me")
53
+ queryKey: ["sync", "distributions", "me", (_a = params == null ? void 0 : params.role) != null ? _a : "claimant", (_b = params == null ? void 0 : params.status) != null ? _b : "all"],
54
+ queryFn: () => client.get(path)
58
55
  }, options));
59
56
  }
60
57
  function useDistribution(distributionId, options) {
61
- var _a2;
58
+ var _a;
62
59
  const client = useSyncClient();
63
60
  const id = distributionId ? encodeURIComponent(distributionId) : "";
64
61
  return useQuery(__spreadProps(__spreadValues({
65
62
  queryKey: ["sync", "distribution", id],
66
63
  queryFn: () => client.get(`/distributions/me/${id}`)
67
64
  }, options), {
68
- enabled: Boolean(id) && ((_a2 = options == null ? void 0 : options.enabled) != null ? _a2 : true)
65
+ enabled: Boolean(id) && ((_a = options == null ? void 0 : options.enabled) != null ? _a : true)
69
66
  }));
70
67
  }
68
+ function useDistributionTransaction(options) {
69
+ const requestConfig = useSyncRequestConfig();
70
+ return useMutation(__spreadValues({
71
+ mutationFn: async (input) => {
72
+ if (!input) throw new SyncReactError(400, "distribution_input_required");
73
+ return buildDistributionTransaction(input, requestConfig);
74
+ }
75
+ }, options));
76
+ }
77
+ function useCommitDistribution(options) {
78
+ const requestConfig = useSyncRequestConfig();
79
+ return useMutation(__spreadValues({
80
+ mutationFn: async (input) => {
81
+ if (!(input == null ? void 0 : input.distributionId)) throw new SyncReactError(400, "distribution_id_required");
82
+ return commitDistributionSignature(input, requestConfig);
83
+ }
84
+ }, options));
85
+ }
86
+ function useDistributionFlow() {
87
+ var _a, _b;
88
+ const buildMutation = useDistributionTransaction();
89
+ const commitMutation = useCommitDistribution();
90
+ const [latestPayload, setLatestPayload] = useState(null);
91
+ const [latestInput, setLatestInput] = useState(null);
92
+ const build = useCallback(
93
+ async (input) => {
94
+ const payload = await buildMutation.mutateAsync(input);
95
+ setLatestPayload(payload);
96
+ setLatestInput(cloneDistributionInput(input));
97
+ return payload;
98
+ },
99
+ [buildMutation]
100
+ );
101
+ const doCommit = useCallback(
102
+ async (signer, input) => {
103
+ let payload = null;
104
+ if (input) {
105
+ payload = await build(input);
106
+ } else if (latestInput) {
107
+ payload = await build(latestInput);
108
+ } else if (latestPayload) {
109
+ payload = latestPayload;
110
+ }
111
+ if (!payload) {
112
+ throw new SyncReactError(400, "distribution_payload_missing");
113
+ }
114
+ if (!payload.distributionId) {
115
+ throw new SyncReactError(400, "distribution_id_required");
116
+ }
117
+ const signerInput = await signer(payload);
118
+ if (!signerInput || !signerInput.signature && !signerInput.transaction && !signerInput.signedTransactionBase64) {
119
+ throw new SyncReactError(400, "distribution_signature_required");
120
+ }
121
+ const commitPayload = __spreadProps(__spreadValues({}, signerInput), {
122
+ distributionId: payload.distributionId
123
+ });
124
+ if (!commitPayload.exportJson && payload.exportJson) {
125
+ commitPayload.exportJson = payload.exportJson;
126
+ }
127
+ const commitResult = await commitMutation.mutateAsync(commitPayload);
128
+ return { payload, commit: commitResult, signerInput };
129
+ },
130
+ [build, latestInput, latestPayload, commitMutation]
131
+ );
132
+ const reset = useCallback(() => {
133
+ setLatestPayload(null);
134
+ setLatestInput(null);
135
+ buildMutation.reset();
136
+ commitMutation.reset();
137
+ }, [buildMutation, commitMutation]);
138
+ const error = (_b = (_a = buildMutation.error) != null ? _a : commitMutation.error) != null ? _b : null;
139
+ const state = useMemo(
140
+ () => ({
141
+ latestPayload,
142
+ building: buildMutation.isPending,
143
+ committing: commitMutation.isPending,
144
+ error
145
+ }),
146
+ [latestPayload, buildMutation.isPending, commitMutation.isPending, error]
147
+ );
148
+ return { state, build, commit: doCommit, claim: doCommit, reset };
149
+ }
71
150
  function useClaimableDistributions(options) {
72
151
  const client = useSyncClient();
73
152
  return useQuery(__spreadValues({
@@ -105,78 +184,85 @@ function useCommitClaim(distributionId, options) {
105
184
  }
106
185
  }, options));
107
186
  }
108
- async function requestJSON(config, path, init) {
109
- var _a2;
110
- const url = buildUrl(config.basePath, path);
111
- const requestInit = prepareInit(init);
112
- requestInit.credentials = (_a2 = requestInit.credentials) != null ? _a2 : isRelativeUrl(url) ? "include" : void 0;
113
- const response = await config.fetcher(url, requestInit);
114
- const payload = await parseResponseBody(response);
115
- if (!response.ok) {
116
- const errorCode = typeof payload === "object" && payload && "error" in payload ? String(payload.error) : void 0;
117
- throw new SyncReactError(response.status, errorCode, payload);
118
- }
119
- return payload;
120
- }
121
- function prepareInit(init) {
122
- const finalInit = __spreadValues({}, init);
123
- if ((init == null ? void 0 : init.headers) instanceof Headers) {
124
- const cloned = new Headers(init.headers);
125
- if (!cloned.has("content-type")) cloned.set("content-type", "application/json");
126
- finalInit.headers = cloned;
127
- return finalInit;
128
- }
129
- const headers = new Headers(init == null ? void 0 : init.headers);
130
- if (!headers.has("content-type")) {
131
- headers.set("content-type", "application/json");
132
- }
133
- finalInit.headers = headers;
134
- return finalInit;
135
- }
136
- async function parseResponseBody(response) {
137
- if (response.status === 204) return void 0;
138
- const text = await response.text();
139
- if (!text) return void 0;
140
- try {
141
- return JSON.parse(text);
142
- } catch (e) {
143
- return text;
144
- }
187
+ function useClaimFlow(distributionId) {
188
+ var _a, _b;
189
+ const buildMutation = useClaimTransaction(distributionId);
190
+ const commitMutation = useCommitClaim(distributionId);
191
+ const [latestPayload, setLatestPayload] = useState(null);
192
+ const build = useCallback(
193
+ async (input) => {
194
+ const payload = await buildMutation.mutateAsync(input != null ? input : {});
195
+ setLatestPayload(payload);
196
+ return payload;
197
+ },
198
+ [buildMutation]
199
+ );
200
+ const claim = useCallback(
201
+ async (signer, input) => {
202
+ const payload = await build(input);
203
+ const signerInput = await signer(payload);
204
+ if (!signerInput || !signerInput.signature && !signerInput.transaction && !signerInput.signedTransactionBase64) {
205
+ throw new SyncReactError(400, "claim_signature_required");
206
+ }
207
+ const commit = await commitMutation.mutateAsync(signerInput);
208
+ return { payload, commit, signerInput };
209
+ },
210
+ [build, commitMutation]
211
+ );
212
+ const reset = useCallback(() => {
213
+ setLatestPayload(null);
214
+ }, []);
215
+ const state = {
216
+ latestPayload,
217
+ building: buildMutation.isPending,
218
+ claiming: commitMutation.isPending,
219
+ error: (_b = (_a = buildMutation.error) != null ? _a : commitMutation.error) != null ? _b : null
220
+ };
221
+ return { state, build, claim, reset };
145
222
  }
146
- function resolveFetcher(custom) {
147
- if (custom) return custom;
148
- if (typeof fetch === "function") return fetch.bind(globalThis);
149
- throw new Error("Global fetch is not available. Provide a fetcher via <SyncProvider fetcher={...}>.");
150
- }
151
- function sanitizeBasePath(path) {
152
- if (!path) return FALLBACK_BASE_PATH;
153
- const trimmed = path.trim();
154
- if (!trimmed || trimmed === "/") return "/";
155
- return trimmed.replace(/\/+$/, "");
156
- }
157
- function buildUrl(basePath, path) {
158
- if (path && isAbsoluteUrl(path)) return path;
159
- const normalizedBase = sanitizeBasePath(basePath);
160
- const suffix = path ? `/${path.replace(/^\/+/, "")}` : "";
161
- if (!normalizedBase || normalizedBase === "/") {
162
- return suffix ? suffix : normalizedBase;
223
+ function useSyncRequestConfig() {
224
+ const context = useContext(SyncContext);
225
+ const basePath = sanitizeBasePath((context == null ? void 0 : context.basePath) || DEFAULT_BASE_PATH);
226
+ const fetcher = resolveFetcher(context == null ? void 0 : context.fetcher);
227
+ return useMemo(() => ({ basePath, fetcher }), [basePath, fetcher]);
228
+ }
229
+ function cloneDistributionInput(input) {
230
+ return __spreadProps(__spreadValues({}, input), {
231
+ recipients: input.recipients.map((recipient) => __spreadValues({}, recipient))
232
+ });
233
+ }
234
+ function resolveDistributionsArgs(paramsOrOptions, optionsMaybe) {
235
+ if (isDistributionsParams(paramsOrOptions)) {
236
+ return { params: paramsOrOptions, options: optionsMaybe };
163
237
  }
164
- return `${normalizedBase}${suffix}`;
238
+ return { params: void 0, options: paramsOrOptions };
165
239
  }
166
- function isAbsoluteUrl(path) {
167
- return /^https?:\/\//i.test(path);
240
+ function isDistributionsParams(arg) {
241
+ if (!arg || typeof arg !== "object") return false;
242
+ return "role" in arg || "status" in arg;
168
243
  }
169
- function isRelativeUrl(path) {
170
- return !isAbsoluteUrl(path);
244
+ function buildDistributionsPath(params) {
245
+ const search = new URLSearchParams();
246
+ if ((params == null ? void 0 : params.role) && params.role !== "claimant") {
247
+ search.set("role", params.role);
248
+ }
249
+ if (params == null ? void 0 : params.status) {
250
+ search.set("status", params.status);
251
+ }
252
+ const qs = search.toString();
253
+ return qs ? `/distributions/me?${qs}` : "/distributions/me";
171
254
  }
172
255
  export {
173
256
  SyncProvider,
174
- SyncReactError,
257
+ useClaimFlow,
175
258
  useClaimHistory,
176
259
  useClaimTransaction,
177
260
  useClaimableDistributions,
178
261
  useCommitClaim,
262
+ useCommitDistribution,
179
263
  useDistribution,
264
+ useDistributionFlow,
265
+ useDistributionTransaction,
180
266
  useDistributions,
181
267
  useSyncClient
182
268
  };
@@ -1,4 +1,4 @@
1
- import { L as ListDistributionsRequest, g as ListDistributionsResponse, h as CreateDistributionRequest, i as CreateDistributionResponse, j as CommitDistributionRequest, k as CommitDistributionResponse, B as BuildClaimTransactionRequest, d as ClaimTransactionResponse, m as CommitClaimRequest, n as CommitClaimResponse, o as ClaimableResponse, a as Distribution, q as ClaimHistoryResponse, H as HealthResponse } from '../api-CQqyHbnT.mjs';
1
+ import { L as ListDistributionsRequest, g as ListDistributionsResponse, l as CreateDistributionRequest, k as DistributionTransactionPayload, o as CommitDistributionRequest, p as CommitDistributionResponse, r as BuildClaimTransactionRequest, d as ClaimTransactionResponse, t as CommitClaimRequest, u as CommitClaimResponse, v as ClaimableResponse, a as Distribution, x as ClaimHistoryResponse, H as HealthResponse } from '../api-Cl-FACri.mjs';
2
2
 
3
3
  type QueryInit = string | URLSearchParams | Record<string, string | number | boolean | null | undefined>;
4
4
  type HeaderInjector = () => Promise<HeadersInit> | HeadersInit;
@@ -15,8 +15,8 @@ declare class SyncServerClient {
15
15
  private buildQueryString;
16
16
  private withQuery;
17
17
  listDistributions(params?: ListDistributionsRequest): Promise<ListDistributionsResponse>;
18
- createDistribution(body: CreateDistributionRequest): Promise<CreateDistributionResponse>;
19
- createDistributionWithId(distributionId: string, body: CreateDistributionRequest): Promise<CreateDistributionResponse>;
18
+ createDistribution(body: CreateDistributionRequest): Promise<DistributionTransactionPayload>;
19
+ createDistributionWithId(distributionId: string, body: CreateDistributionRequest): Promise<DistributionTransactionPayload>;
20
20
  commitDistribution(distributionId: string, body: CommitDistributionRequest): Promise<CommitDistributionResponse>;
21
21
  buildClaimTransaction(body: BuildClaimTransactionRequest): Promise<ClaimTransactionResponse>;
22
22
  commitClaim(body: CommitClaimRequest): Promise<CommitClaimResponse>;
@@ -1,4 +1,4 @@
1
- import { L as ListDistributionsRequest, g as ListDistributionsResponse, h as CreateDistributionRequest, i as CreateDistributionResponse, j as CommitDistributionRequest, k as CommitDistributionResponse, B as BuildClaimTransactionRequest, d as ClaimTransactionResponse, m as CommitClaimRequest, n as CommitClaimResponse, o as ClaimableResponse, a as Distribution, q as ClaimHistoryResponse, H as HealthResponse } from '../api-CQqyHbnT.js';
1
+ import { L as ListDistributionsRequest, g as ListDistributionsResponse, l as CreateDistributionRequest, k as DistributionTransactionPayload, o as CommitDistributionRequest, p as CommitDistributionResponse, r as BuildClaimTransactionRequest, d as ClaimTransactionResponse, t as CommitClaimRequest, u as CommitClaimResponse, v as ClaimableResponse, a as Distribution, x as ClaimHistoryResponse, H as HealthResponse } from '../api-Cl-FACri.js';
2
2
 
3
3
  type QueryInit = string | URLSearchParams | Record<string, string | number | boolean | null | undefined>;
4
4
  type HeaderInjector = () => Promise<HeadersInit> | HeadersInit;
@@ -15,8 +15,8 @@ declare class SyncServerClient {
15
15
  private buildQueryString;
16
16
  private withQuery;
17
17
  listDistributions(params?: ListDistributionsRequest): Promise<ListDistributionsResponse>;
18
- createDistribution(body: CreateDistributionRequest): Promise<CreateDistributionResponse>;
19
- createDistributionWithId(distributionId: string, body: CreateDistributionRequest): Promise<CreateDistributionResponse>;
18
+ createDistribution(body: CreateDistributionRequest): Promise<DistributionTransactionPayload>;
19
+ createDistributionWithId(distributionId: string, body: CreateDistributionRequest): Promise<DistributionTransactionPayload>;
20
20
  commitDistribution(distributionId: string, body: CommitDistributionRequest): Promise<CommitDistributionResponse>;
21
21
  buildClaimTransaction(body: BuildClaimTransactionRequest): Promise<ClaimTransactionResponse>;
22
22
  commitClaim(body: CommitClaimRequest): Promise<CommitClaimResponse>;
@@ -4,8 +4,8 @@ import {
4
4
  SyncServerClient,
5
5
  createMethodHandler,
6
6
  syncRouteHandler
7
- } from "../chunk-BHPW43XZ.mjs";
8
- import "../chunk-I4E63NIC.mjs";
7
+ } from "../chunk-JZGD7RD2.mjs";
8
+ import "../chunk-FWCSY2DS.mjs";
9
9
  export {
10
10
  GET,
11
11
  POST,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fractalshq/sync",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "Fractals Sync SDK: shared types, server client, React hooks, and widgets",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",