@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.
- package/dist/api-C0dGK-PK.d.mts +203 -0
- package/dist/api-C0dGK-PK.d.ts +203 -0
- package/dist/api-Cl-FACri.d.mts +204 -0
- package/dist/api-Cl-FACri.d.ts +204 -0
- package/dist/chunk-FWCSY2DS.mjs +37 -0
- package/dist/chunk-JZGD7RD2.mjs +498 -0
- package/dist/chunk-VXNOPXKB.mjs +249 -0
- package/dist/core/index.d.mts +19 -2
- package/dist/core/index.d.ts +19 -2
- package/dist/core/index.js +266 -2
- package/dist/core/index.mjs +38 -4
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +270 -23
- package/dist/index.mjs +44 -10
- package/dist/react/index.d.mts +34 -10
- package/dist/react/index.d.ts +34 -10
- package/dist/react/index.js +301 -77
- package/dist/react/index.mjs +137 -88
- package/dist/server/index.d.mts +3 -3
- package/dist/server/index.d.ts +3 -3
- package/dist/server/index.mjs +2 -2
- package/package.json +1 -1
package/dist/react/index.mjs
CHANGED
|
@@ -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-
|
|
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
|
|
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(
|
|
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
|
-
}, [
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
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(
|
|
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
|
|
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) && ((
|
|
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
|
|
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: (
|
|
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
|
-
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
|
183
|
-
if (
|
|
184
|
-
|
|
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
|
|
238
|
+
return { params: void 0, options: paramsOrOptions };
|
|
201
239
|
}
|
|
202
|
-
function
|
|
203
|
-
|
|
240
|
+
function isDistributionsParams(arg) {
|
|
241
|
+
if (!arg || typeof arg !== "object") return false;
|
|
242
|
+
return "role" in arg || "status" in arg;
|
|
204
243
|
}
|
|
205
|
-
function
|
|
206
|
-
|
|
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
|
};
|
package/dist/server/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { L as ListDistributionsRequest, g as ListDistributionsResponse,
|
|
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<
|
|
19
|
-
createDistributionWithId(distributionId: string, body: CreateDistributionRequest): Promise<
|
|
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>;
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { L as ListDistributionsRequest, g as ListDistributionsResponse,
|
|
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<
|
|
19
|
-
createDistributionWithId(distributionId: string, body: CreateDistributionRequest): Promise<
|
|
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>;
|
package/dist/server/index.mjs
CHANGED