@fractalshq/sync 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
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({
@@ -106,7 +185,7 @@ function useCommitClaim(distributionId, options) {
106
185
  }, options));
107
186
  }
108
187
  function useClaimFlow(distributionId) {
109
- var _a2, _b2;
188
+ var _a, _b;
110
189
  const buildMutation = useClaimTransaction(distributionId);
111
190
  const commitMutation = useCommitClaim(distributionId);
112
191
  const [latestPayload, setLatestPayload] = useState(null);
@@ -137,83 +216,53 @@ function useClaimFlow(distributionId) {
137
216
  latestPayload,
138
217
  building: buildMutation.isPending,
139
218
  claiming: commitMutation.isPending,
140
- error: (_b2 = (_a2 = buildMutation.error) != null ? _a2 : commitMutation.error) != null ? _b2 : null
219
+ error: (_b = (_a = buildMutation.error) != null ? _a : commitMutation.error) != null ? _b : null
141
220
  };
142
221
  return { state, build, claim, reset };
143
222
  }
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 prepareInit(init) {
158
- const finalInit = __spreadValues({}, init);
159
- if ((init == null ? void 0 : init.headers) instanceof Headers) {
160
- const cloned = new Headers(init.headers);
161
- if (!cloned.has("content-type")) cloned.set("content-type", "application/json");
162
- finalInit.headers = cloned;
163
- return finalInit;
164
- }
165
- const headers = new Headers(init == null ? void 0 : init.headers);
166
- if (!headers.has("content-type")) {
167
- headers.set("content-type", "application/json");
168
- }
169
- finalInit.headers = headers;
170
- return finalInit;
171
- }
172
- async function parseResponseBody(response) {
173
- if (response.status === 204) return void 0;
174
- const text = await response.text();
175
- if (!text) return void 0;
176
- try {
177
- return JSON.parse(text);
178
- } catch (e) {
179
- return text;
180
- }
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
+ });
181
233
  }
182
- function resolveFetcher(custom) {
183
- if (custom) return custom;
184
- if (typeof fetch === "function") return fetch.bind(globalThis);
185
- throw new Error("Global fetch is not available. Provide a fetcher via <SyncProvider fetcher={...}>.");
186
- }
187
- function sanitizeBasePath(path) {
188
- if (!path) return FALLBACK_BASE_PATH;
189
- const trimmed = path.trim();
190
- if (!trimmed || trimmed === "/") return "/";
191
- return trimmed.replace(/\/+$/, "");
192
- }
193
- function buildUrl(basePath, path) {
194
- if (path && isAbsoluteUrl(path)) return path;
195
- const normalizedBase = sanitizeBasePath(basePath);
196
- const suffix = path ? `/${path.replace(/^\/+/, "")}` : "";
197
- if (!normalizedBase || normalizedBase === "/") {
198
- return suffix ? suffix : normalizedBase;
234
+ function resolveDistributionsArgs(paramsOrOptions, optionsMaybe) {
235
+ if (isDistributionsParams(paramsOrOptions)) {
236
+ return { params: paramsOrOptions, options: optionsMaybe };
199
237
  }
200
- return `${normalizedBase}${suffix}`;
238
+ return { params: void 0, options: paramsOrOptions };
201
239
  }
202
- function isAbsoluteUrl(path) {
203
- 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;
204
243
  }
205
- function isRelativeUrl(path) {
206
- 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";
207
254
  }
208
255
  export {
209
256
  SyncProvider,
210
- SyncReactError,
211
257
  useClaimFlow,
212
258
  useClaimHistory,
213
259
  useClaimTransaction,
214
260
  useClaimableDistributions,
215
261
  useCommitClaim,
262
+ useCommitDistribution,
216
263
  useDistribution,
264
+ useDistributionFlow,
265
+ useDistributionTransaction,
217
266
  useDistributions,
218
267
  useSyncClient
219
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-C0dGK-PK.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-C0dGK-PK.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.4",
3
+ "version": "0.0.6",
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",