@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.
- 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 +38 -4
- package/dist/react/index.d.mts +53 -10
- package/dist/react/index.d.ts +53 -10
- package/dist/react/index.js +339 -77
- package/dist/react/index.mjs +173 -87
- 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
|
-
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
|
|
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({
|
|
@@ -105,78 +184,85 @@ function useCommitClaim(distributionId, options) {
|
|
|
105
184
|
}
|
|
106
185
|
}, options));
|
|
107
186
|
}
|
|
108
|
-
|
|
109
|
-
var
|
|
110
|
-
const
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
function
|
|
158
|
-
if (
|
|
159
|
-
|
|
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
|
|
238
|
+
return { params: void 0, options: paramsOrOptions };
|
|
165
239
|
}
|
|
166
|
-
function
|
|
167
|
-
|
|
240
|
+
function isDistributionsParams(arg) {
|
|
241
|
+
if (!arg || typeof arg !== "object") return false;
|
|
242
|
+
return "role" in arg || "status" in arg;
|
|
168
243
|
}
|
|
169
|
-
function
|
|
170
|
-
|
|
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
|
-
|
|
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
|
};
|
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-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<
|
|
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-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<
|
|
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