@puckeditor/cloud-client 0.7.0-canary.75c0f12c → 0.7.0

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,358 @@
1
+ import {
2
+ chat,
3
+ createAttachments,
4
+ deleteAttachment,
5
+ endpoints,
6
+ findRouteInRegistry,
7
+ generate,
8
+ getApiKey,
9
+ getAttachment,
10
+ handlePuckRequest,
11
+ tool
12
+ } from "./chunk-4OIQ53M4.mjs";
13
+ import {
14
+ init_react_import
15
+ } from "./chunk-O6DC5HI2.mjs";
16
+
17
+ // src/experimental.ts
18
+ init_react_import();
19
+
20
+ // src/api/pages/routes.ts
21
+ init_react_import();
22
+
23
+ // src/api/pages/lib/request-context.ts
24
+ init_react_import();
25
+
26
+ // src/api/pages/lib/utils.ts
27
+ init_react_import();
28
+ var DEFAULT_HOST = "https://cloud.puckeditor.com/api";
29
+ var normalizeHost = (host) => host.replace(/\/+$/, "");
30
+ var normalizeMaybeString = (value) => {
31
+ if (typeof value !== "string") {
32
+ return null;
33
+ }
34
+ const trimmed = value.trim();
35
+ return trimmed.length > 0 ? trimmed : null;
36
+ };
37
+ var buildQueryString = ({
38
+ path,
39
+ versionId
40
+ }) => {
41
+ const searchParams = new URLSearchParams();
42
+ if (path) {
43
+ searchParams.set("path", path);
44
+ }
45
+ if (versionId) {
46
+ searchParams.set("versionId", versionId);
47
+ }
48
+ const queryString = searchParams.toString();
49
+ return queryString ? `?${queryString}` : "";
50
+ };
51
+ var getErrorText = async (res) => {
52
+ try {
53
+ const body = await res.json();
54
+ return body?.error ?? "Unknown reason";
55
+ } catch {
56
+ return "Unknown reason";
57
+ }
58
+ };
59
+
60
+ // src/api/pages/lib/request-context.ts
61
+ var getSiteId = (context, options) => normalizeMaybeString(context.headers.get("x-site-id")) ?? normalizeMaybeString(options.pages?.siteId) ?? normalizeMaybeString(process.env.PUCK_SITE_ID);
62
+ var getRequiredSiteId = (context, options) => {
63
+ const siteId = getSiteId(context, options);
64
+ if (!siteId) {
65
+ throw new Error(
66
+ "Missing site-id. Set PUCK_SITE_ID, provide pages.siteId, or send x-site-id."
67
+ );
68
+ }
69
+ return siteId;
70
+ };
71
+ var getToken = (context) => normalizeMaybeString(context.headers.get("x-one-time-token"));
72
+
73
+ // src/api/pages/lib/request-cloud-page.ts
74
+ init_react_import();
75
+ var internalError = (error) => Response.json(
76
+ {
77
+ status: 500,
78
+ error: error instanceof Error ? error.message : "An unexpected error occurred"
79
+ },
80
+ { status: 500 }
81
+ );
82
+ var createNewResponse = async (response) => {
83
+ const body = response.status === 204 || response.status === 304 ? null : await response.arrayBuffer();
84
+ const contentType = response.headers.get("content-type");
85
+ const headers = new Headers();
86
+ if (contentType) {
87
+ headers.set("content-type", contentType);
88
+ }
89
+ return new Response(body, {
90
+ headers,
91
+ status: response.status,
92
+ statusText: response.statusText
93
+ });
94
+ };
95
+ var requestCloudPage = async (route, {
96
+ body,
97
+ method = "GET",
98
+ path,
99
+ siteId,
100
+ token,
101
+ versionId
102
+ }, options = {}) => {
103
+ const apiKey = options.apiKey ?? getApiKey();
104
+ const host = normalizeHost(options.host ?? DEFAULT_HOST);
105
+ if (!apiKey && !token) {
106
+ throw new Error(
107
+ "No Puck API key specified. Set the PUCK_API_KEY environment variable, or provide one to the function"
108
+ );
109
+ }
110
+ return fetch(`${host}${route}${buildQueryString({ path, versionId })}`, {
111
+ method,
112
+ headers: {
113
+ "x-site-id": siteId,
114
+ ...token ? { "x-one-time-token": token } : {},
115
+ ...apiKey && !token ? { "x-api-key": apiKey } : {},
116
+ ...body ? { "content-type": "application/json" } : {}
117
+ },
118
+ ...body ? { body: JSON.stringify(body) } : {}
119
+ });
120
+ };
121
+ var proxyCloudPage = async (route, request, options) => {
122
+ try {
123
+ const response = await requestCloudPage(route, request, options);
124
+ return createNewResponse(response);
125
+ } catch (error) {
126
+ return internalError(error);
127
+ }
128
+ };
129
+
130
+ // src/api/pages/routes.ts
131
+ var getPagesForSite = async (context, options = {}) => {
132
+ const siteId = getRequiredSiteId(context, options);
133
+ return proxyCloudPage(
134
+ "/pages",
135
+ {
136
+ path: normalizeMaybeString(context.query.path),
137
+ siteId,
138
+ token: getToken(context),
139
+ versionId: normalizeMaybeString(context.query.versionId)
140
+ },
141
+ options
142
+ );
143
+ };
144
+ var getPage = async (context, options = {}) => {
145
+ const siteId = getRequiredSiteId(context, options);
146
+ const id = normalizeMaybeString(context.params.id);
147
+ if (!id) {
148
+ return Response.json(
149
+ { status: 400, error: "Missing page id" },
150
+ { status: 400 }
151
+ );
152
+ }
153
+ return proxyCloudPage(
154
+ `/pages/${id}`,
155
+ {
156
+ siteId,
157
+ token: getToken(context),
158
+ versionId: normalizeMaybeString(context.query.versionId)
159
+ },
160
+ options
161
+ );
162
+ };
163
+ var createPage = async (context, options = {}) => {
164
+ const siteId = getRequiredSiteId(context, options);
165
+ return proxyCloudPage(
166
+ "/pages",
167
+ {
168
+ body: context.body,
169
+ method: "POST",
170
+ siteId,
171
+ token: getToken(context)
172
+ },
173
+ options
174
+ );
175
+ };
176
+ var updatePage = async (context, options = {}) => {
177
+ const siteId = getRequiredSiteId(context, options);
178
+ const id = normalizeMaybeString(context.params.id);
179
+ if (!id) {
180
+ return Response.json(
181
+ { status: 400, error: "Missing page id" },
182
+ { status: 400 }
183
+ );
184
+ }
185
+ return proxyCloudPage(
186
+ `/pages/${id}`,
187
+ {
188
+ body: context.body,
189
+ method: "POST",
190
+ siteId,
191
+ token: getToken(context)
192
+ },
193
+ options
194
+ );
195
+ };
196
+ var deletePage = async (context, options = {}) => {
197
+ const siteId = getRequiredSiteId(context, options);
198
+ const id = normalizeMaybeString(context.params.id);
199
+ if (!id) {
200
+ return Response.json(
201
+ { status: 400, error: "Missing page id" },
202
+ { status: 400 }
203
+ );
204
+ }
205
+ return proxyCloudPage(
206
+ `/pages/${id}`,
207
+ {
208
+ method: "DELETE",
209
+ siteId,
210
+ token: getToken(context)
211
+ },
212
+ options
213
+ );
214
+ };
215
+
216
+ // src/api/sites/verify-puck-host.ts
217
+ init_react_import();
218
+ var verifyPuckHost = async ({
219
+ host,
220
+ siteId,
221
+ token
222
+ }) => {
223
+ const resolvedSiteId = normalizeMaybeString(
224
+ siteId || process.env.PUCK_SITE_ID
225
+ );
226
+ const resolvedToken = normalizeMaybeString(token);
227
+ if (!resolvedSiteId || !resolvedToken) {
228
+ return false;
229
+ }
230
+ const res = await fetch(
231
+ `${normalizeHost(host ?? DEFAULT_HOST)}/sites/verify-host`,
232
+ {
233
+ method: "POST",
234
+ headers: {
235
+ "content-type": "application/json"
236
+ },
237
+ body: JSON.stringify({
238
+ siteId: resolvedSiteId,
239
+ token: resolvedToken
240
+ })
241
+ }
242
+ );
243
+ if (res.status === 400 || res.status === 401) {
244
+ return false;
245
+ }
246
+ if (!res.ok) {
247
+ throw new Error(
248
+ `Puck ${res.status} (${res.statusText}): ${await getErrorText(res)}`
249
+ );
250
+ }
251
+ let body;
252
+ try {
253
+ body = await res.json();
254
+ } catch {
255
+ return false;
256
+ }
257
+ return body.ok === true;
258
+ };
259
+
260
+ // src/api/pages.ts
261
+ init_react_import();
262
+
263
+ // src/api/pages/get-published-page-data.ts
264
+ init_react_import();
265
+ var getPublishedPageData = async ({
266
+ apiKey,
267
+ host,
268
+ path,
269
+ siteId
270
+ }) => {
271
+ const resolvedSiteId = normalizeMaybeString(siteId) ?? normalizeMaybeString(process.env.PUCK_SITE_ID);
272
+ if (!resolvedSiteId) {
273
+ throw new Error(
274
+ "Missing site-id. Set PUCK_SITE_ID or provide siteId to getPage."
275
+ );
276
+ }
277
+ const res = await requestCloudPage(
278
+ "/pages/published",
279
+ {
280
+ path: normalizeMaybeString(path),
281
+ siteId: resolvedSiteId
282
+ },
283
+ {
284
+ ...host ? { host } : {},
285
+ ...apiKey ? { apiKey } : {}
286
+ }
287
+ );
288
+ if (res.status === 404) {
289
+ return null;
290
+ }
291
+ if (!res.ok) {
292
+ throw new Error(
293
+ `Puck ${res.status} (${res.statusText}): ${await getErrorText(res)}`
294
+ );
295
+ }
296
+ let body;
297
+ try {
298
+ body = await res.json();
299
+ } catch {
300
+ return null;
301
+ }
302
+ return body.page?.versions?.at(0)?.data ?? null;
303
+ };
304
+
305
+ // src/experimental.ts
306
+ var routeRegistry = [
307
+ {
308
+ pattern: "/api/puck/chat/attachments/:attachmentId",
309
+ methods: {
310
+ DELETE: deleteAttachment,
311
+ GET: getAttachment
312
+ }
313
+ },
314
+ {
315
+ pattern: "/api/puck/chat/attachments",
316
+ methods: {
317
+ POST: createAttachments
318
+ }
319
+ },
320
+ {
321
+ pattern: "/api/puck/chat",
322
+ methods: {
323
+ POST: ({ body }, options) => chat(body, options)
324
+ }
325
+ },
326
+ {
327
+ pattern: "/api/puck/pages/published",
328
+ methods: {}
329
+ },
330
+ {
331
+ pattern: "/api/puck/pages/:id",
332
+ methods: {
333
+ DELETE: deletePage,
334
+ GET: getPage,
335
+ POST: updatePage
336
+ }
337
+ },
338
+ {
339
+ pattern: "/api/puck/pages",
340
+ methods: {
341
+ GET: getPagesForSite,
342
+ POST: createPage
343
+ }
344
+ }
345
+ ];
346
+ var findRoute = (pathname, method) => findRouteInRegistry(routeRegistry, pathname, method);
347
+ async function puckHandler(request, options = {}) {
348
+ return handlePuckRequest(request, options, findRoute);
349
+ }
350
+ export {
351
+ chat,
352
+ endpoints,
353
+ generate,
354
+ getPublishedPageData as getPage,
355
+ puckHandler,
356
+ tool,
357
+ verifyPuckHost
358
+ };
package/dist/index.d.mts CHANGED
@@ -27,6 +27,20 @@ type DataFinish = {
27
27
  tokenUsage: TokenUsage;
28
28
  };
29
29
 
30
+ type OnFinishResult = DataFinish;
31
+ type OnFinishCallback = (result: OnFinishResult) => void;
32
+ type UserToolRegistry = Record<string, UserTool>;
33
+ type PuckAiOptions = {
34
+ context?: string;
35
+ tools?: UserToolRegistry;
36
+ onFinish?: OnFinishCallback;
37
+ };
38
+ type PuckCloudOptions = {
39
+ ai?: PuckAiOptions;
40
+ host?: string;
41
+ apiKey?: string;
42
+ };
43
+
30
44
  declare const tool: <INPUT extends z.ZodTypeAny, OUTPUT extends z.ZodTypeAny>(t: UserTool<INPUT, OUTPUT>) => UserTool<INPUT, OUTPUT>;
31
45
 
32
46
  type ChatParams = {
@@ -42,30 +56,16 @@ type GenerateParams = {
42
56
  config: Config;
43
57
  pageData?: Data;
44
58
  context?: string;
45
- collectionId?: string;
46
59
  tools?: UserToolRegistry;
47
60
  host?: string;
48
61
  apiKey?: string;
49
62
  onFinish?: OnFinishCallback;
50
63
  };
51
- declare function generate({ prompt, config, pageData, context, collectionId, tools, apiKey, host, onFinish, }: GenerateParams): Promise<null>;
64
+ declare function generate({ prompt, config, pageData, context, tools, apiKey, host, onFinish, }: GenerateParams): Promise<null>;
52
65
 
53
- declare function puckHandler(request: Request, options: PuckCloudOptions): Promise<Response>;
66
+ declare function puckHandler(request: Request, options?: PuckCloudOptions): Promise<Response>;
54
67
 
55
68
  declare const endpoints: readonly ["chat"];
56
69
  type Endpoint = (typeof endpoints)[number];
57
- type OnFinishResult = DataFinish;
58
- type OnFinishCallback = (result: OnFinishResult) => void;
59
- type PuckCloudOptions = {
60
- ai?: {
61
- context?: string;
62
- collectionId?: string;
63
- tools?: UserToolRegistry;
64
- onFinish?: OnFinishCallback;
65
- };
66
- host?: string;
67
- apiKey?: string;
68
- };
69
- type UserToolRegistry = Record<string, UserTool>;
70
70
 
71
71
  export { type ChatParams, type Endpoint, type GenerateParams, type OnFinishCallback, type OnFinishResult, type PuckCloudOptions, type UserTool, type UserToolRegistry, chat, endpoints, generate, puckHandler, tool };
package/dist/index.d.ts CHANGED
@@ -27,6 +27,20 @@ type DataFinish = {
27
27
  tokenUsage: TokenUsage;
28
28
  };
29
29
 
30
+ type OnFinishResult = DataFinish;
31
+ type OnFinishCallback = (result: OnFinishResult) => void;
32
+ type UserToolRegistry = Record<string, UserTool>;
33
+ type PuckAiOptions = {
34
+ context?: string;
35
+ tools?: UserToolRegistry;
36
+ onFinish?: OnFinishCallback;
37
+ };
38
+ type PuckCloudOptions = {
39
+ ai?: PuckAiOptions;
40
+ host?: string;
41
+ apiKey?: string;
42
+ };
43
+
30
44
  declare const tool: <INPUT extends z.ZodTypeAny, OUTPUT extends z.ZodTypeAny>(t: UserTool<INPUT, OUTPUT>) => UserTool<INPUT, OUTPUT>;
31
45
 
32
46
  type ChatParams = {
@@ -42,30 +56,16 @@ type GenerateParams = {
42
56
  config: Config;
43
57
  pageData?: Data;
44
58
  context?: string;
45
- collectionId?: string;
46
59
  tools?: UserToolRegistry;
47
60
  host?: string;
48
61
  apiKey?: string;
49
62
  onFinish?: OnFinishCallback;
50
63
  };
51
- declare function generate({ prompt, config, pageData, context, collectionId, tools, apiKey, host, onFinish, }: GenerateParams): Promise<null>;
64
+ declare function generate({ prompt, config, pageData, context, tools, apiKey, host, onFinish, }: GenerateParams): Promise<null>;
52
65
 
53
- declare function puckHandler(request: Request, options: PuckCloudOptions): Promise<Response>;
66
+ declare function puckHandler(request: Request, options?: PuckCloudOptions): Promise<Response>;
54
67
 
55
68
  declare const endpoints: readonly ["chat"];
56
69
  type Endpoint = (typeof endpoints)[number];
57
- type OnFinishResult = DataFinish;
58
- type OnFinishCallback = (result: OnFinishResult) => void;
59
- type PuckCloudOptions = {
60
- ai?: {
61
- context?: string;
62
- collectionId?: string;
63
- tools?: UserToolRegistry;
64
- onFinish?: OnFinishCallback;
65
- };
66
- host?: string;
67
- apiKey?: string;
68
- };
69
- type UserToolRegistry = Record<string, UserTool>;
70
70
 
71
71
  export { type ChatParams, type Endpoint, type GenerateParams, type OnFinishCallback, type OnFinishResult, type PuckCloudOptions, type UserTool, type UserToolRegistry, chat, endpoints, generate, puckHandler, tool };