@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,138 @@
1
+ import {
2
+ DEFAULT_BASE_PATH,
3
+ SyncReactError,
4
+ buildClaimTransaction,
5
+ commitClaim,
6
+ listClaimHistory,
7
+ listClaimable,
8
+ requestJSON,
9
+ resolveFetcher,
10
+ sanitizeBasePath
11
+ } from "../../chunk-RTLKOAT7.mjs";
12
+ import {
13
+ __spreadProps,
14
+ __spreadValues
15
+ } from "../../chunk-FWCSY2DS.mjs";
16
+
17
+ // src/v2/react/index.tsx
18
+ import { createContext, createElement, useCallback, useContext, useMemo, useState } from "react";
19
+ import { useMutation, useQuery } from "@tanstack/react-query";
20
+ var SyncContext = createContext({ basePath: DEFAULT_BASE_PATH });
21
+ function SyncProvider({ basePath, fetcher, children }) {
22
+ const value = useMemo(
23
+ () => ({
24
+ basePath: sanitizeBasePath(basePath || DEFAULT_BASE_PATH),
25
+ fetcher
26
+ }),
27
+ [basePath, fetcher]
28
+ );
29
+ return createElement(SyncContext.Provider, { value }, children);
30
+ }
31
+ function useSyncClient() {
32
+ const requestConfig = useSyncRequestConfig();
33
+ return useMemo(() => {
34
+ const request = (path, init) => requestJSON(requestConfig, path, init);
35
+ return {
36
+ basePath: requestConfig.basePath,
37
+ request,
38
+ get: (path, init) => request(path, __spreadProps(__spreadValues({}, init), { method: "GET" })),
39
+ post: (path, body, init) => {
40
+ const finalInit = __spreadProps(__spreadValues({}, init), { method: ((init == null ? void 0 : init.method) || "POST").toUpperCase() });
41
+ if (body !== void 0) {
42
+ finalInit.body = typeof body === "string" ? body : JSON.stringify(body);
43
+ }
44
+ return request(path, finalInit);
45
+ }
46
+ };
47
+ }, [requestConfig]);
48
+ }
49
+ function useClaimableDistributions(options) {
50
+ const requestConfig = useSyncRequestConfig();
51
+ return useQuery(__spreadValues({
52
+ queryKey: ["sync", "v2", "claims", "me"],
53
+ queryFn: () => listClaimable(requestConfig)
54
+ }, options));
55
+ }
56
+ function useClaimHistory(options) {
57
+ const requestConfig = useSyncRequestConfig();
58
+ return useQuery(__spreadValues({
59
+ queryKey: ["sync", "v2", "claims", "history"],
60
+ queryFn: () => listClaimHistory(requestConfig)
61
+ }, options));
62
+ }
63
+ function useClaimTransaction(distributionId, options) {
64
+ const requestConfig = useSyncRequestConfig();
65
+ const id = distributionId || null;
66
+ return useMutation(__spreadValues({
67
+ mutationFn: async (input = {}) => {
68
+ if (!id) throw new SyncReactError(400, "distribution_id_required");
69
+ return buildClaimTransaction(__spreadProps(__spreadValues({}, input), { distributionId: id }), requestConfig);
70
+ }
71
+ }, options));
72
+ }
73
+ function useCommitClaim(distributionId, options) {
74
+ const requestConfig = useSyncRequestConfig();
75
+ const id = distributionId || null;
76
+ return useMutation(__spreadValues({
77
+ mutationFn: async (input) => {
78
+ if (!id) throw new SyncReactError(400, "distribution_id_required");
79
+ if (!(input == null ? void 0 : input.signedTransactionBase64)) throw new SyncReactError(400, "signed_transaction_required");
80
+ if (!(input == null ? void 0 : input.claimantPubkey)) throw new SyncReactError(400, "claimant_pubkey_required");
81
+ return commitClaim(__spreadProps(__spreadValues({}, input), { distributionId: id }), requestConfig);
82
+ }
83
+ }, options));
84
+ }
85
+ function useClaimFlow(distributionId) {
86
+ var _a, _b;
87
+ const buildMutation = useClaimTransaction(distributionId);
88
+ const commitMutation = useCommitClaim(distributionId);
89
+ const [latestPayload, setLatestPayload] = useState(null);
90
+ const build = useCallback(
91
+ async (input) => {
92
+ const payload = await buildMutation.mutateAsync(input != null ? input : {});
93
+ setLatestPayload(payload);
94
+ return payload;
95
+ },
96
+ [buildMutation]
97
+ );
98
+ const claim = useCallback(
99
+ async (signer, input) => {
100
+ const payload = await build(input);
101
+ const signerInput = await signer(payload);
102
+ if (!(signerInput == null ? void 0 : signerInput.signedTransactionBase64)) {
103
+ throw new SyncReactError(400, "signed_transaction_required");
104
+ }
105
+ if (!(signerInput == null ? void 0 : signerInput.claimantPubkey)) {
106
+ throw new SyncReactError(400, "claimant_pubkey_required");
107
+ }
108
+ const commit = await commitMutation.mutateAsync(signerInput);
109
+ return { payload, commit, signerInput };
110
+ },
111
+ [build, commitMutation]
112
+ );
113
+ const reset = useCallback(() => {
114
+ setLatestPayload(null);
115
+ }, []);
116
+ const state = {
117
+ latestPayload,
118
+ building: buildMutation.isPending,
119
+ claiming: commitMutation.isPending,
120
+ error: (_b = (_a = buildMutation.error) != null ? _a : commitMutation.error) != null ? _b : null
121
+ };
122
+ return { state, build, claim, reset };
123
+ }
124
+ function useSyncRequestConfig() {
125
+ const context = useContext(SyncContext);
126
+ const basePath = sanitizeBasePath((context == null ? void 0 : context.basePath) || DEFAULT_BASE_PATH);
127
+ const fetcher = resolveFetcher(context == null ? void 0 : context.fetcher);
128
+ return useMemo(() => ({ basePath, fetcher }), [basePath, fetcher]);
129
+ }
130
+ export {
131
+ SyncProvider,
132
+ useClaimFlow,
133
+ useClaimHistory,
134
+ useClaimTransaction,
135
+ useClaimableDistributions,
136
+ useCommitClaim,
137
+ useSyncClient
138
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fractalshq/sync",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
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",
@@ -11,6 +11,21 @@
11
11
  "import": "./dist/index.mjs",
12
12
  "require": "./dist/index.js"
13
13
  },
14
+ "./v2": {
15
+ "types": "./dist/v2/index.d.ts",
16
+ "import": "./dist/v2/index.mjs",
17
+ "require": "./dist/v2/index.js"
18
+ },
19
+ "./v2/core": {
20
+ "types": "./dist/v2/core/index.d.ts",
21
+ "import": "./dist/v2/core/index.mjs",
22
+ "require": "./dist/v2/core/index.js"
23
+ },
24
+ "./v2/react": {
25
+ "types": "./dist/v2/react/index.d.ts",
26
+ "import": "./dist/v2/react/index.mjs",
27
+ "require": "./dist/v2/react/index.js"
28
+ },
14
29
  "./core": {
15
30
  "types": "./dist/core/index.d.ts",
16
31
  "import": "./dist/core/index.mjs",
@@ -37,8 +52,8 @@
37
52
  "README.md"
38
53
  ],
39
54
  "scripts": {
40
- "build": "tsup src/index.ts src/core/index.ts src/server/index.ts src/react/index.tsx src/widgets/index.ts --format cjs,esm --dts",
41
- "dev": "tsup src/index.ts src/core/index.ts src/server/index.ts src/react/index.tsx src/widgets/index.ts --format cjs,esm --dts --watch",
55
+ "build": "tsup src/index.ts src/core/index.ts src/server/index.ts src/react/index.tsx src/widgets/index.ts src/v2/index.ts src/v2/core/index.ts src/v2/react/index.tsx --format cjs,esm --dts",
56
+ "dev": "tsup src/index.ts src/core/index.ts src/server/index.ts src/react/index.tsx src/widgets/index.ts src/v2/index.ts src/v2/core/index.ts src/v2/react/index.tsx --format cjs,esm --dts --watch",
42
57
  "test": "vitest",
43
58
  "lint": "eslint src --ext .ts,.tsx",
44
59
  "typecheck": "tsc --noEmit"