@sybil-studio-devs/sdk 0.1.0 → 0.1.1
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/chunk-IN53AFMQ.mjs +348 -0
- package/dist/client-BPFcAPV8.d.mts +428 -0
- package/dist/client-BPFcAPV8.d.ts +428 -0
- package/dist/index.d.mts +4 -394
- package/dist/index.d.ts +4 -394
- package/dist/index.js +100 -7
- package/dist/index.mjs +7 -252
- package/dist/react/index.d.mts +44 -0
- package/dist/react/index.d.ts +44 -0
- package/dist/react/index.js +542 -0
- package/dist/react/index.mjs +174 -0
- package/package.json +18 -5
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/react/index.ts
|
|
21
|
+
var react_exports = {};
|
|
22
|
+
__export(react_exports, {
|
|
23
|
+
SybilPages: () => SybilPages,
|
|
24
|
+
useSybilPages: () => useSybilPages
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(react_exports);
|
|
27
|
+
|
|
28
|
+
// src/react/SybilPages.tsx
|
|
29
|
+
var import_react = require("react");
|
|
30
|
+
|
|
31
|
+
// src/client.ts
|
|
32
|
+
var DEFAULT_BASE_URL = "https://app.sybil.studio/api/sdk/v1";
|
|
33
|
+
var DEFAULT_TIMEOUT = 3e4;
|
|
34
|
+
var SybilSDK = class {
|
|
35
|
+
constructor(config) {
|
|
36
|
+
if (typeof config === "string") {
|
|
37
|
+
this.apiKey = config;
|
|
38
|
+
this.baseUrl = DEFAULT_BASE_URL;
|
|
39
|
+
this.timeout = DEFAULT_TIMEOUT;
|
|
40
|
+
} else {
|
|
41
|
+
this.apiKey = config.apiKey;
|
|
42
|
+
this.baseUrl = config.baseUrl || DEFAULT_BASE_URL;
|
|
43
|
+
this.timeout = config.timeout || DEFAULT_TIMEOUT;
|
|
44
|
+
}
|
|
45
|
+
if (!this.apiKey) {
|
|
46
|
+
throw new Error("API key is required");
|
|
47
|
+
}
|
|
48
|
+
if (!this.apiKey.startsWith("sk_live_") && !this.apiKey.startsWith("sk_test_")) {
|
|
49
|
+
throw new Error("Invalid API key format. Must start with sk_live_ or sk_test_");
|
|
50
|
+
}
|
|
51
|
+
this.pages = new PagesAPI(this);
|
|
52
|
+
this.documents = new DocumentsAPI(this);
|
|
53
|
+
this.youtube = new YouTubeAPI(this);
|
|
54
|
+
this.chat = new ChatAPI(this);
|
|
55
|
+
this.analyze = new AnalyzeAPI(this);
|
|
56
|
+
}
|
|
57
|
+
async request(endpoint, options = {}) {
|
|
58
|
+
const url = `${this.baseUrl}${endpoint}`;
|
|
59
|
+
const headers = {
|
|
60
|
+
"x-api-key": this.apiKey,
|
|
61
|
+
...options.headers
|
|
62
|
+
};
|
|
63
|
+
if (options.body && typeof options.body === "string") {
|
|
64
|
+
headers["Content-Type"] = "application/json";
|
|
65
|
+
}
|
|
66
|
+
const controller = new AbortController();
|
|
67
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
68
|
+
try {
|
|
69
|
+
const response = await fetch(url, {
|
|
70
|
+
...options,
|
|
71
|
+
headers,
|
|
72
|
+
signal: controller.signal
|
|
73
|
+
});
|
|
74
|
+
clearTimeout(timeoutId);
|
|
75
|
+
if (!response.ok) {
|
|
76
|
+
const errorData = await response.json().catch(() => ({}));
|
|
77
|
+
const message = errorData.error?.message || `Request failed with status ${response.status}`;
|
|
78
|
+
throw new SybilError(message, response.status, errorData.error?.code);
|
|
79
|
+
}
|
|
80
|
+
return response.json();
|
|
81
|
+
} catch (error) {
|
|
82
|
+
clearTimeout(timeoutId);
|
|
83
|
+
if (error instanceof SybilError) {
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
87
|
+
throw new SybilError("Request timeout", 408, "TIMEOUT");
|
|
88
|
+
}
|
|
89
|
+
throw new SybilError(
|
|
90
|
+
error instanceof Error ? error.message : "Unknown error",
|
|
91
|
+
500,
|
|
92
|
+
"UNKNOWN"
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
var SybilError = class extends Error {
|
|
98
|
+
constructor(message, status, code) {
|
|
99
|
+
super(message);
|
|
100
|
+
this.name = "SybilError";
|
|
101
|
+
this.status = status;
|
|
102
|
+
this.code = code;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
var PagesAPI = class {
|
|
106
|
+
constructor(client) {
|
|
107
|
+
this.client = client;
|
|
108
|
+
}
|
|
109
|
+
async list(params = {}) {
|
|
110
|
+
const searchParams = new URLSearchParams();
|
|
111
|
+
if (params.limit) searchParams.set("limit", params.limit.toString());
|
|
112
|
+
if (params.offset) searchParams.set("offset", params.offset.toString());
|
|
113
|
+
if (params.parent_id) searchParams.set("parent_id", params.parent_id);
|
|
114
|
+
if (params.is_published !== void 0) searchParams.set("is_published", params.is_published.toString());
|
|
115
|
+
const query = searchParams.toString();
|
|
116
|
+
return this.client.request(`/pages${query ? `?${query}` : ""}`);
|
|
117
|
+
}
|
|
118
|
+
async get(pageId) {
|
|
119
|
+
return this.client.request(`/pages/${pageId}`);
|
|
120
|
+
}
|
|
121
|
+
async create(params = {}) {
|
|
122
|
+
return this.client.request("/pages", {
|
|
123
|
+
method: "POST",
|
|
124
|
+
body: JSON.stringify(params)
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
async update(pageId, params) {
|
|
128
|
+
return this.client.request(`/pages/${pageId}`, {
|
|
129
|
+
method: "PUT",
|
|
130
|
+
body: JSON.stringify(params)
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
async delete(pageId) {
|
|
134
|
+
return this.client.request(`/pages/${pageId}`, {
|
|
135
|
+
method: "DELETE"
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
async upload(file, fileName) {
|
|
139
|
+
const formData = new FormData();
|
|
140
|
+
formData.append("file", file, fileName);
|
|
141
|
+
return this.client.request("/pages/upload", {
|
|
142
|
+
method: "POST",
|
|
143
|
+
body: formData
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
async deleteFile(filePath) {
|
|
147
|
+
return this.client.request("/pages/upload", {
|
|
148
|
+
method: "DELETE",
|
|
149
|
+
body: JSON.stringify({ filePath })
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
var DocumentsAPI = class {
|
|
154
|
+
constructor(client) {
|
|
155
|
+
this.client = client;
|
|
156
|
+
}
|
|
157
|
+
async list(params = {}) {
|
|
158
|
+
const searchParams = new URLSearchParams();
|
|
159
|
+
if (params.limit) searchParams.set("limit", params.limit.toString());
|
|
160
|
+
if (params.offset) searchParams.set("offset", params.offset.toString());
|
|
161
|
+
if (params.status) searchParams.set("status", params.status);
|
|
162
|
+
if (params.fileType) searchParams.set("fileType", params.fileType);
|
|
163
|
+
if (params.search) searchParams.set("search", params.search);
|
|
164
|
+
const query = searchParams.toString();
|
|
165
|
+
return this.client.request(`/documents${query ? `?${query}` : ""}`);
|
|
166
|
+
}
|
|
167
|
+
async get(documentId, params = {}) {
|
|
168
|
+
const searchParams = new URLSearchParams();
|
|
169
|
+
if (params.includeContent) searchParams.set("includeContent", "true");
|
|
170
|
+
if (params.includeInsights === false) searchParams.set("includeInsights", "false");
|
|
171
|
+
if (params.includeMetadata === false) searchParams.set("includeMetadata", "false");
|
|
172
|
+
const query = searchParams.toString();
|
|
173
|
+
return this.client.request(`/documents/${documentId}${query ? `?${query}` : ""}`);
|
|
174
|
+
}
|
|
175
|
+
async process(file, options = {}) {
|
|
176
|
+
const formData = new FormData();
|
|
177
|
+
formData.append("file", file, options.fileName);
|
|
178
|
+
if (options.metadata) {
|
|
179
|
+
formData.append("metadata", JSON.stringify(options.metadata));
|
|
180
|
+
}
|
|
181
|
+
if (options.collectionId) {
|
|
182
|
+
formData.append("collectionId", options.collectionId);
|
|
183
|
+
}
|
|
184
|
+
return this.client.request("/documents", {
|
|
185
|
+
method: "POST",
|
|
186
|
+
body: formData
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
async status(documentId) {
|
|
190
|
+
return this.client.request(`/documents/${documentId}/status`);
|
|
191
|
+
}
|
|
192
|
+
async insights(documentId, type) {
|
|
193
|
+
const query = type ? `?type=${type}` : "";
|
|
194
|
+
return this.client.request(`/documents/${documentId}/insights${query}`);
|
|
195
|
+
}
|
|
196
|
+
async delete(documentId) {
|
|
197
|
+
return this.client.request(`/documents/${documentId}`, {
|
|
198
|
+
method: "DELETE"
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
async waitForProcessing(documentId, options = {}) {
|
|
202
|
+
const maxAttempts = options.maxAttempts || 60;
|
|
203
|
+
const interval = options.interval || 3e3;
|
|
204
|
+
for (let i = 0; i < maxAttempts; i++) {
|
|
205
|
+
const status = await this.status(documentId);
|
|
206
|
+
if (status.status === "processed" || status.status === "completed") {
|
|
207
|
+
const { document: document2 } = await this.get(documentId);
|
|
208
|
+
return document2;
|
|
209
|
+
}
|
|
210
|
+
if (status.status === "failed") {
|
|
211
|
+
throw new SybilError(status.error?.message || "Processing failed", 500, "PROCESSING_FAILED");
|
|
212
|
+
}
|
|
213
|
+
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
214
|
+
}
|
|
215
|
+
throw new SybilError("Processing timeout", 408, "TIMEOUT");
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
var YouTubeAPI = class {
|
|
219
|
+
constructor(client) {
|
|
220
|
+
this.client = client;
|
|
221
|
+
}
|
|
222
|
+
async list(params = {}) {
|
|
223
|
+
const searchParams = new URLSearchParams();
|
|
224
|
+
if (params.limit) searchParams.set("limit", params.limit.toString());
|
|
225
|
+
if (params.offset) searchParams.set("offset", params.offset.toString());
|
|
226
|
+
const query = searchParams.toString();
|
|
227
|
+
return this.client.request(`/youtube${query ? `?${query}` : ""}`);
|
|
228
|
+
}
|
|
229
|
+
async process(url, options) {
|
|
230
|
+
return this.client.request("/youtube", {
|
|
231
|
+
method: "POST",
|
|
232
|
+
body: JSON.stringify({
|
|
233
|
+
url,
|
|
234
|
+
collectionId: options?.collectionId,
|
|
235
|
+
force: options?.force
|
|
236
|
+
})
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
async get(videoId, params = {}) {
|
|
240
|
+
const searchParams = new URLSearchParams();
|
|
241
|
+
if (params.includeTranscript) searchParams.set("includeTranscript", "true");
|
|
242
|
+
if (params.includeChunks) searchParams.set("includeChunks", "true");
|
|
243
|
+
if (params.includeInsights === false) searchParams.set("includeInsights", "false");
|
|
244
|
+
const query = searchParams.toString();
|
|
245
|
+
return this.client.request(`/youtube/${videoId}${query ? `?${query}` : ""}`);
|
|
246
|
+
}
|
|
247
|
+
async delete(videoId) {
|
|
248
|
+
return this.client.request(`/youtube/${videoId}`, {
|
|
249
|
+
method: "DELETE"
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
var ChatAPI = class {
|
|
254
|
+
constructor(client) {
|
|
255
|
+
this.client = client;
|
|
256
|
+
}
|
|
257
|
+
async send(paramsOrDocId, query) {
|
|
258
|
+
const params = typeof paramsOrDocId === "string" ? { documentId: paramsOrDocId, query } : paramsOrDocId;
|
|
259
|
+
return this.client.request("/chat", {
|
|
260
|
+
method: "POST",
|
|
261
|
+
body: JSON.stringify(params)
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
async history(documentId, limit) {
|
|
265
|
+
const searchParams = new URLSearchParams();
|
|
266
|
+
searchParams.set("documentId", documentId);
|
|
267
|
+
if (limit) searchParams.set("limit", limit.toString());
|
|
268
|
+
return this.client.request(`/chat?${searchParams.toString()}`);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
var AnalyzeAPI = class {
|
|
272
|
+
constructor(client) {
|
|
273
|
+
this.client = client;
|
|
274
|
+
}
|
|
275
|
+
async run(paramsOrDocId, forceRefresh) {
|
|
276
|
+
const params = typeof paramsOrDocId === "string" ? { documentId: paramsOrDocId, forceRefresh } : paramsOrDocId;
|
|
277
|
+
return this.client.request("/analyze", {
|
|
278
|
+
method: "POST",
|
|
279
|
+
body: JSON.stringify(params)
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
async get(documentId) {
|
|
283
|
+
return this.client.request(`/analyze?documentId=${documentId}`);
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
function embedPage(options) {
|
|
287
|
+
const {
|
|
288
|
+
pageId,
|
|
289
|
+
apiKey,
|
|
290
|
+
container,
|
|
291
|
+
baseUrl = "https://app.sybil.studio",
|
|
292
|
+
theme = "dark",
|
|
293
|
+
readOnly = false,
|
|
294
|
+
toolbar = true,
|
|
295
|
+
minHeight = "400px",
|
|
296
|
+
maxHeight,
|
|
297
|
+
onReady,
|
|
298
|
+
onChange,
|
|
299
|
+
onSave,
|
|
300
|
+
onError
|
|
301
|
+
} = options;
|
|
302
|
+
const containerEl = typeof container === "string" ? document.querySelector(container) : container;
|
|
303
|
+
if (!containerEl) {
|
|
304
|
+
throw new Error("Container element not found");
|
|
305
|
+
}
|
|
306
|
+
const params = new URLSearchParams({
|
|
307
|
+
apiKey,
|
|
308
|
+
readOnly: readOnly.toString(),
|
|
309
|
+
toolbar: toolbar.toString(),
|
|
310
|
+
minHeight
|
|
311
|
+
});
|
|
312
|
+
if (typeof theme === "string") {
|
|
313
|
+
params.set("theme", theme);
|
|
314
|
+
} else {
|
|
315
|
+
if (theme.background) params.set("bg", theme.background);
|
|
316
|
+
if (theme.backgroundSecondary) params.set("bgSecondary", theme.backgroundSecondary);
|
|
317
|
+
if (theme.text) params.set("textColor", theme.text);
|
|
318
|
+
if (theme.textSecondary) params.set("textSecondary", theme.textSecondary);
|
|
319
|
+
if (theme.border) params.set("borderColor", theme.border);
|
|
320
|
+
if (theme.accent) params.set("accent", theme.accent);
|
|
321
|
+
}
|
|
322
|
+
const iframe = document.createElement("iframe");
|
|
323
|
+
iframe.src = `${baseUrl}/embed/pages/${pageId}?${params.toString()}`;
|
|
324
|
+
iframe.style.width = "100%";
|
|
325
|
+
iframe.style.minHeight = minHeight;
|
|
326
|
+
if (maxHeight) iframe.style.maxHeight = maxHeight;
|
|
327
|
+
iframe.style.border = "none";
|
|
328
|
+
iframe.style.borderRadius = "8px";
|
|
329
|
+
iframe.allow = "clipboard-write";
|
|
330
|
+
const handleMessage = (event) => {
|
|
331
|
+
if (event.origin !== new URL(baseUrl).origin) return;
|
|
332
|
+
switch (event.data?.type) {
|
|
333
|
+
case "sybil:page:ready":
|
|
334
|
+
onReady?.();
|
|
335
|
+
break;
|
|
336
|
+
case "sybil:page:changed":
|
|
337
|
+
onChange?.(event.data.blocks);
|
|
338
|
+
break;
|
|
339
|
+
case "sybil:page:saved":
|
|
340
|
+
onSave?.();
|
|
341
|
+
break;
|
|
342
|
+
case "sybil:page:error":
|
|
343
|
+
onError?.(new Error(event.data.error));
|
|
344
|
+
break;
|
|
345
|
+
case "sybil:content":
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
window.addEventListener("message", handleMessage);
|
|
350
|
+
containerEl.appendChild(iframe);
|
|
351
|
+
return {
|
|
352
|
+
getContent: () => {
|
|
353
|
+
return new Promise((resolve) => {
|
|
354
|
+
const handler = (event) => {
|
|
355
|
+
if (event.data?.type === "sybil:content") {
|
|
356
|
+
window.removeEventListener("message", handler);
|
|
357
|
+
resolve(event.data.blocks);
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
window.addEventListener("message", handler);
|
|
361
|
+
iframe.contentWindow?.postMessage({ type: "sybil:getContent" }, baseUrl);
|
|
362
|
+
});
|
|
363
|
+
},
|
|
364
|
+
setContent: (blocks) => {
|
|
365
|
+
iframe.contentWindow?.postMessage({ type: "sybil:setContent", blocks }, baseUrl);
|
|
366
|
+
},
|
|
367
|
+
destroy: () => {
|
|
368
|
+
window.removeEventListener("message", handleMessage);
|
|
369
|
+
iframe.remove();
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// src/react/SybilPages.tsx
|
|
375
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
376
|
+
var SybilPages = (0, import_react.forwardRef)(function SybilPages2({
|
|
377
|
+
apiKey,
|
|
378
|
+
pageId,
|
|
379
|
+
baseUrl,
|
|
380
|
+
theme = "dark",
|
|
381
|
+
readOnly = false,
|
|
382
|
+
toolbar = true,
|
|
383
|
+
minHeight = "400px",
|
|
384
|
+
maxHeight,
|
|
385
|
+
className,
|
|
386
|
+
style,
|
|
387
|
+
onReady,
|
|
388
|
+
onChange,
|
|
389
|
+
onSave,
|
|
390
|
+
onError
|
|
391
|
+
}, ref) {
|
|
392
|
+
const containerRef = (0, import_react.useRef)(null);
|
|
393
|
+
const instanceRef = (0, import_react.useRef)(null);
|
|
394
|
+
(0, import_react.useImperativeHandle)(ref, () => ({
|
|
395
|
+
getContent: async () => {
|
|
396
|
+
if (!instanceRef.current) return [];
|
|
397
|
+
return instanceRef.current.getContent();
|
|
398
|
+
},
|
|
399
|
+
setContent: (blocks) => {
|
|
400
|
+
instanceRef.current?.setContent(blocks);
|
|
401
|
+
}
|
|
402
|
+
}));
|
|
403
|
+
(0, import_react.useEffect)(() => {
|
|
404
|
+
if (!containerRef.current || !pageId || !apiKey) return;
|
|
405
|
+
instanceRef.current?.destroy();
|
|
406
|
+
instanceRef.current = embedPage({
|
|
407
|
+
pageId,
|
|
408
|
+
apiKey,
|
|
409
|
+
container: containerRef.current,
|
|
410
|
+
baseUrl,
|
|
411
|
+
theme,
|
|
412
|
+
readOnly,
|
|
413
|
+
toolbar,
|
|
414
|
+
minHeight,
|
|
415
|
+
maxHeight,
|
|
416
|
+
onReady,
|
|
417
|
+
onChange,
|
|
418
|
+
onSave,
|
|
419
|
+
onError
|
|
420
|
+
});
|
|
421
|
+
return () => {
|
|
422
|
+
instanceRef.current?.destroy();
|
|
423
|
+
instanceRef.current = null;
|
|
424
|
+
};
|
|
425
|
+
}, [pageId, apiKey, baseUrl, theme, readOnly, toolbar, minHeight, maxHeight]);
|
|
426
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
427
|
+
"div",
|
|
428
|
+
{
|
|
429
|
+
ref: containerRef,
|
|
430
|
+
className,
|
|
431
|
+
style: {
|
|
432
|
+
minHeight,
|
|
433
|
+
maxHeight,
|
|
434
|
+
...style
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
);
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
// src/react/hooks.ts
|
|
441
|
+
var import_react2 = require("react");
|
|
442
|
+
function useSybilPages(options) {
|
|
443
|
+
const { apiKey, baseUrl } = options;
|
|
444
|
+
const [page, setPage] = (0, import_react2.useState)(null);
|
|
445
|
+
const [loading, setLoading] = (0, import_react2.useState)(false);
|
|
446
|
+
const [saving, setSaving] = (0, import_react2.useState)(false);
|
|
447
|
+
const [error, setError] = (0, import_react2.useState)(null);
|
|
448
|
+
const sdkRef = (0, import_react2.useRef)(null);
|
|
449
|
+
const pendingBlocksRef = (0, import_react2.useRef)(null);
|
|
450
|
+
(0, import_react2.useEffect)(() => {
|
|
451
|
+
sdkRef.current = new SybilSDK({
|
|
452
|
+
apiKey,
|
|
453
|
+
baseUrl: baseUrl ? `${baseUrl}/api/sdk/v1` : void 0
|
|
454
|
+
});
|
|
455
|
+
}, [apiKey, baseUrl]);
|
|
456
|
+
const fetchPage = (0, import_react2.useCallback)(async (pageId) => {
|
|
457
|
+
if (!sdkRef.current) return;
|
|
458
|
+
setLoading(true);
|
|
459
|
+
setError(null);
|
|
460
|
+
try {
|
|
461
|
+
const { page: fetchedPage } = await sdkRef.current.pages.get(pageId);
|
|
462
|
+
setPage(fetchedPage);
|
|
463
|
+
} catch (err) {
|
|
464
|
+
setError(err instanceof Error ? err : new Error("Failed to fetch page"));
|
|
465
|
+
} finally {
|
|
466
|
+
setLoading(false);
|
|
467
|
+
}
|
|
468
|
+
}, []);
|
|
469
|
+
const createPage = (0, import_react2.useCallback)(async (params = {}) => {
|
|
470
|
+
if (!sdkRef.current) throw new Error("SDK not initialized");
|
|
471
|
+
setLoading(true);
|
|
472
|
+
setError(null);
|
|
473
|
+
try {
|
|
474
|
+
const { page: newPage } = await sdkRef.current.pages.create(params);
|
|
475
|
+
setPage(newPage);
|
|
476
|
+
return newPage;
|
|
477
|
+
} catch (err) {
|
|
478
|
+
const error2 = err instanceof Error ? err : new Error("Failed to create page");
|
|
479
|
+
setError(error2);
|
|
480
|
+
throw error2;
|
|
481
|
+
} finally {
|
|
482
|
+
setLoading(false);
|
|
483
|
+
}
|
|
484
|
+
}, []);
|
|
485
|
+
const updatePage = (0, import_react2.useCallback)(async (pageId, params) => {
|
|
486
|
+
if (!sdkRef.current) throw new Error("SDK not initialized");
|
|
487
|
+
setSaving(true);
|
|
488
|
+
setError(null);
|
|
489
|
+
try {
|
|
490
|
+
const { page: updatedPage } = await sdkRef.current.pages.update(pageId, params);
|
|
491
|
+
setPage(updatedPage);
|
|
492
|
+
return updatedPage;
|
|
493
|
+
} catch (err) {
|
|
494
|
+
const error2 = err instanceof Error ? err : new Error("Failed to update page");
|
|
495
|
+
setError(error2);
|
|
496
|
+
throw error2;
|
|
497
|
+
} finally {
|
|
498
|
+
setSaving(false);
|
|
499
|
+
}
|
|
500
|
+
}, []);
|
|
501
|
+
const deletePage = (0, import_react2.useCallback)(async (pageId) => {
|
|
502
|
+
if (!sdkRef.current) throw new Error("SDK not initialized");
|
|
503
|
+
setLoading(true);
|
|
504
|
+
setError(null);
|
|
505
|
+
try {
|
|
506
|
+
await sdkRef.current.pages.delete(pageId);
|
|
507
|
+
setPage(null);
|
|
508
|
+
} catch (err) {
|
|
509
|
+
const error2 = err instanceof Error ? err : new Error("Failed to delete page");
|
|
510
|
+
setError(error2);
|
|
511
|
+
throw error2;
|
|
512
|
+
} finally {
|
|
513
|
+
setLoading(false);
|
|
514
|
+
}
|
|
515
|
+
}, []);
|
|
516
|
+
const setBlocks = (0, import_react2.useCallback)((blocks) => {
|
|
517
|
+
pendingBlocksRef.current = blocks;
|
|
518
|
+
setPage((prev) => prev ? { ...prev, blocks } : null);
|
|
519
|
+
}, []);
|
|
520
|
+
const save = (0, import_react2.useCallback)(async () => {
|
|
521
|
+
if (!page || !sdkRef.current || !pendingBlocksRef.current) return;
|
|
522
|
+
await updatePage(page.id, { blocks: pendingBlocksRef.current });
|
|
523
|
+
pendingBlocksRef.current = null;
|
|
524
|
+
}, [page, updatePage]);
|
|
525
|
+
return {
|
|
526
|
+
page,
|
|
527
|
+
loading,
|
|
528
|
+
saving,
|
|
529
|
+
error,
|
|
530
|
+
fetchPage,
|
|
531
|
+
createPage,
|
|
532
|
+
updatePage,
|
|
533
|
+
deletePage,
|
|
534
|
+
setBlocks,
|
|
535
|
+
save
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
539
|
+
0 && (module.exports = {
|
|
540
|
+
SybilPages,
|
|
541
|
+
useSybilPages
|
|
542
|
+
});
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SybilSDK,
|
|
3
|
+
embedPage
|
|
4
|
+
} from "../chunk-IN53AFMQ.mjs";
|
|
5
|
+
|
|
6
|
+
// src/react/SybilPages.tsx
|
|
7
|
+
import { useEffect, useRef, forwardRef, useImperativeHandle } from "react";
|
|
8
|
+
import { jsx } from "react/jsx-runtime";
|
|
9
|
+
var SybilPages = forwardRef(function SybilPages2({
|
|
10
|
+
apiKey,
|
|
11
|
+
pageId,
|
|
12
|
+
baseUrl,
|
|
13
|
+
theme = "dark",
|
|
14
|
+
readOnly = false,
|
|
15
|
+
toolbar = true,
|
|
16
|
+
minHeight = "400px",
|
|
17
|
+
maxHeight,
|
|
18
|
+
className,
|
|
19
|
+
style,
|
|
20
|
+
onReady,
|
|
21
|
+
onChange,
|
|
22
|
+
onSave,
|
|
23
|
+
onError
|
|
24
|
+
}, ref) {
|
|
25
|
+
const containerRef = useRef(null);
|
|
26
|
+
const instanceRef = useRef(null);
|
|
27
|
+
useImperativeHandle(ref, () => ({
|
|
28
|
+
getContent: async () => {
|
|
29
|
+
if (!instanceRef.current) return [];
|
|
30
|
+
return instanceRef.current.getContent();
|
|
31
|
+
},
|
|
32
|
+
setContent: (blocks) => {
|
|
33
|
+
instanceRef.current?.setContent(blocks);
|
|
34
|
+
}
|
|
35
|
+
}));
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
if (!containerRef.current || !pageId || !apiKey) return;
|
|
38
|
+
instanceRef.current?.destroy();
|
|
39
|
+
instanceRef.current = embedPage({
|
|
40
|
+
pageId,
|
|
41
|
+
apiKey,
|
|
42
|
+
container: containerRef.current,
|
|
43
|
+
baseUrl,
|
|
44
|
+
theme,
|
|
45
|
+
readOnly,
|
|
46
|
+
toolbar,
|
|
47
|
+
minHeight,
|
|
48
|
+
maxHeight,
|
|
49
|
+
onReady,
|
|
50
|
+
onChange,
|
|
51
|
+
onSave,
|
|
52
|
+
onError
|
|
53
|
+
});
|
|
54
|
+
return () => {
|
|
55
|
+
instanceRef.current?.destroy();
|
|
56
|
+
instanceRef.current = null;
|
|
57
|
+
};
|
|
58
|
+
}, [pageId, apiKey, baseUrl, theme, readOnly, toolbar, minHeight, maxHeight]);
|
|
59
|
+
return /* @__PURE__ */ jsx(
|
|
60
|
+
"div",
|
|
61
|
+
{
|
|
62
|
+
ref: containerRef,
|
|
63
|
+
className,
|
|
64
|
+
style: {
|
|
65
|
+
minHeight,
|
|
66
|
+
maxHeight,
|
|
67
|
+
...style
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// src/react/hooks.ts
|
|
74
|
+
import { useState, useCallback as useCallback2, useRef as useRef2, useEffect as useEffect2 } from "react";
|
|
75
|
+
function useSybilPages(options) {
|
|
76
|
+
const { apiKey, baseUrl } = options;
|
|
77
|
+
const [page, setPage] = useState(null);
|
|
78
|
+
const [loading, setLoading] = useState(false);
|
|
79
|
+
const [saving, setSaving] = useState(false);
|
|
80
|
+
const [error, setError] = useState(null);
|
|
81
|
+
const sdkRef = useRef2(null);
|
|
82
|
+
const pendingBlocksRef = useRef2(null);
|
|
83
|
+
useEffect2(() => {
|
|
84
|
+
sdkRef.current = new SybilSDK({
|
|
85
|
+
apiKey,
|
|
86
|
+
baseUrl: baseUrl ? `${baseUrl}/api/sdk/v1` : void 0
|
|
87
|
+
});
|
|
88
|
+
}, [apiKey, baseUrl]);
|
|
89
|
+
const fetchPage = useCallback2(async (pageId) => {
|
|
90
|
+
if (!sdkRef.current) return;
|
|
91
|
+
setLoading(true);
|
|
92
|
+
setError(null);
|
|
93
|
+
try {
|
|
94
|
+
const { page: fetchedPage } = await sdkRef.current.pages.get(pageId);
|
|
95
|
+
setPage(fetchedPage);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
setError(err instanceof Error ? err : new Error("Failed to fetch page"));
|
|
98
|
+
} finally {
|
|
99
|
+
setLoading(false);
|
|
100
|
+
}
|
|
101
|
+
}, []);
|
|
102
|
+
const createPage = useCallback2(async (params = {}) => {
|
|
103
|
+
if (!sdkRef.current) throw new Error("SDK not initialized");
|
|
104
|
+
setLoading(true);
|
|
105
|
+
setError(null);
|
|
106
|
+
try {
|
|
107
|
+
const { page: newPage } = await sdkRef.current.pages.create(params);
|
|
108
|
+
setPage(newPage);
|
|
109
|
+
return newPage;
|
|
110
|
+
} catch (err) {
|
|
111
|
+
const error2 = err instanceof Error ? err : new Error("Failed to create page");
|
|
112
|
+
setError(error2);
|
|
113
|
+
throw error2;
|
|
114
|
+
} finally {
|
|
115
|
+
setLoading(false);
|
|
116
|
+
}
|
|
117
|
+
}, []);
|
|
118
|
+
const updatePage = useCallback2(async (pageId, params) => {
|
|
119
|
+
if (!sdkRef.current) throw new Error("SDK not initialized");
|
|
120
|
+
setSaving(true);
|
|
121
|
+
setError(null);
|
|
122
|
+
try {
|
|
123
|
+
const { page: updatedPage } = await sdkRef.current.pages.update(pageId, params);
|
|
124
|
+
setPage(updatedPage);
|
|
125
|
+
return updatedPage;
|
|
126
|
+
} catch (err) {
|
|
127
|
+
const error2 = err instanceof Error ? err : new Error("Failed to update page");
|
|
128
|
+
setError(error2);
|
|
129
|
+
throw error2;
|
|
130
|
+
} finally {
|
|
131
|
+
setSaving(false);
|
|
132
|
+
}
|
|
133
|
+
}, []);
|
|
134
|
+
const deletePage = useCallback2(async (pageId) => {
|
|
135
|
+
if (!sdkRef.current) throw new Error("SDK not initialized");
|
|
136
|
+
setLoading(true);
|
|
137
|
+
setError(null);
|
|
138
|
+
try {
|
|
139
|
+
await sdkRef.current.pages.delete(pageId);
|
|
140
|
+
setPage(null);
|
|
141
|
+
} catch (err) {
|
|
142
|
+
const error2 = err instanceof Error ? err : new Error("Failed to delete page");
|
|
143
|
+
setError(error2);
|
|
144
|
+
throw error2;
|
|
145
|
+
} finally {
|
|
146
|
+
setLoading(false);
|
|
147
|
+
}
|
|
148
|
+
}, []);
|
|
149
|
+
const setBlocks = useCallback2((blocks) => {
|
|
150
|
+
pendingBlocksRef.current = blocks;
|
|
151
|
+
setPage((prev) => prev ? { ...prev, blocks } : null);
|
|
152
|
+
}, []);
|
|
153
|
+
const save = useCallback2(async () => {
|
|
154
|
+
if (!page || !sdkRef.current || !pendingBlocksRef.current) return;
|
|
155
|
+
await updatePage(page.id, { blocks: pendingBlocksRef.current });
|
|
156
|
+
pendingBlocksRef.current = null;
|
|
157
|
+
}, [page, updatePage]);
|
|
158
|
+
return {
|
|
159
|
+
page,
|
|
160
|
+
loading,
|
|
161
|
+
saving,
|
|
162
|
+
error,
|
|
163
|
+
fetchPage,
|
|
164
|
+
createPage,
|
|
165
|
+
updatePage,
|
|
166
|
+
deletePage,
|
|
167
|
+
setBlocks,
|
|
168
|
+
save
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
export {
|
|
172
|
+
SybilPages,
|
|
173
|
+
useSybilPages
|
|
174
|
+
};
|