@webstudio-is/sdk 0.189.0 → 0.191.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.
package/lib/index.js CHANGED
@@ -168,6 +168,25 @@ var Instance = z3.object({
168
168
  children: z3.array(InstanceChild)
169
169
  });
170
170
  var Instances = z3.map(InstanceId, Instance);
171
+ var MatcherRelation = z3.union([
172
+ z3.literal("ancestor"),
173
+ z3.literal("parent"),
174
+ z3.literal("self"),
175
+ z3.literal("child"),
176
+ z3.literal("descendant")
177
+ ]);
178
+ var MatcherOperation = z3.object({
179
+ $eq: z3.string().optional(),
180
+ $neq: z3.string().optional(),
181
+ $in: z3.array(z3.string()).optional(),
182
+ $nin: z3.array(z3.string()).optional()
183
+ });
184
+ var Matcher = z3.object({
185
+ relation: MatcherRelation,
186
+ component: MatcherOperation.optional(),
187
+ tag: MatcherOperation.optional()
188
+ });
189
+ var Matchers = z3.union([Matcher, z3.array(Matcher)]);
171
190
 
172
191
  // src/schema/data-sources.ts
173
192
  import { z as z4 } from "zod";
@@ -253,15 +272,6 @@ var ResourceRequest = z5.object({
253
272
  body: z5.optional(z5.unknown())
254
273
  });
255
274
  var Resources = z5.map(ResourceId, Resource);
256
- var LOCAL_RESOURCE_PREFIX = "$resources";
257
- var isLocalResource = (pathname, resourceName) => {
258
- const segments = pathname.split("/").filter(Boolean);
259
- if (resourceName === void 0) {
260
- return segments[0] === LOCAL_RESOURCE_PREFIX;
261
- }
262
- return segments.join("/") === `${LOCAL_RESOURCE_PREFIX}/${resourceName}`;
263
- };
264
- var sitemapResourceUrl = `/${LOCAL_RESOURCE_PREFIX}/sitemap.xml`;
265
275
 
266
276
  // src/schema/props.ts
267
277
  import { z as z6 } from "zod";
@@ -901,64 +911,6 @@ var createScope = (occupiedIdentifiers = [], normalizeName = normalizeJsName, se
901
911
  };
902
912
  };
903
913
 
904
- // src/resource-loader.ts
905
- var loadResource = async (customFetch, resourceRequest) => {
906
- const { url, method, headers, body } = resourceRequest;
907
- const requestHeaders = new Headers(
908
- headers.map(({ name, value }) => [name, value])
909
- );
910
- const requestInit = {
911
- method,
912
- headers: requestHeaders
913
- };
914
- if (method !== "get" && body !== void 0) {
915
- if (typeof body === "string") {
916
- requestInit.body = body;
917
- }
918
- if (typeof body === "object") {
919
- requestInit.body = JSON.stringify(body);
920
- }
921
- }
922
- try {
923
- const response = await customFetch(url.trim(), requestInit);
924
- let data = await response.text();
925
- try {
926
- data = JSON.parse(data);
927
- } catch {
928
- }
929
- if (!response.ok) {
930
- console.error(
931
- `Failed to load resource: ${url} - ${response.status}: ${JSON.stringify(data).slice(0, 300)}`
932
- );
933
- }
934
- return {
935
- ok: response.ok,
936
- data,
937
- status: response.status,
938
- statusText: response.statusText
939
- };
940
- } catch (error) {
941
- console.error(error);
942
- const message = error.message;
943
- return {
944
- ok: false,
945
- data: void 0,
946
- status: 500,
947
- statusText: message
948
- };
949
- }
950
- };
951
- var loadResources = async (customFetch, requests) => {
952
- return Object.fromEntries(
953
- await Promise.all(
954
- Array.from(
955
- requests,
956
- async ([name, request]) => [name, await loadResource(customFetch, request)]
957
- )
958
- )
959
- );
960
- };
961
-
962
914
  // src/resources-generator.ts
963
915
  var generateResources = ({
964
916
  scope,
@@ -1271,31 +1223,6 @@ var generatePageMeta = ({
1271
1223
  `;
1272
1224
  return generated;
1273
1225
  };
1274
-
1275
- // src/form-fields.ts
1276
- var formIdFieldName = `ws--form-id`;
1277
- var formBotFieldName = `ws--form-bot`;
1278
-
1279
- // src/to-string.ts
1280
- var createJsonStringifyProxy = (target) => {
1281
- return new Proxy(target, {
1282
- get(target2, prop, receiver) {
1283
- if (prop === "toString") {
1284
- return function() {
1285
- return JSON.stringify(target2);
1286
- };
1287
- }
1288
- const value = Reflect.get(target2, prop, receiver);
1289
- if (typeof value === "object" && value !== null) {
1290
- return createJsonStringifyProxy(value);
1291
- }
1292
- return value;
1293
- }
1294
- });
1295
- };
1296
- var isPlainObject = (value) => {
1297
- return Object.prototype.toString.call(value) === "[object Object]" && (Object.getPrototypeOf(value) === null || Object.getPrototypeOf(value) === Object.prototype);
1298
- };
1299
1226
  export {
1300
1227
  Asset,
1301
1228
  Assets,
@@ -1317,6 +1244,10 @@ export {
1317
1244
  Instance,
1318
1245
  InstanceChild,
1319
1246
  Instances,
1247
+ Matcher,
1248
+ MatcherOperation,
1249
+ MatcherRelation,
1250
+ Matchers,
1320
1251
  PageName,
1321
1252
  PagePath,
1322
1253
  PageRedirect,
@@ -1339,7 +1270,6 @@ export {
1339
1270
  Templates,
1340
1271
  TextChild,
1341
1272
  WebstudioFragment,
1342
- createJsonStringifyProxy,
1343
1273
  createScope,
1344
1274
  decodeDataSourceVariable,
1345
1275
  documentTypes,
@@ -1349,8 +1279,6 @@ export {
1349
1279
  findParentFolderByChildId,
1350
1280
  findTreeInstanceIds,
1351
1281
  findTreeInstanceIdsExcludingSlotDescendants,
1352
- formBotFieldName,
1353
- formIdFieldName,
1354
1282
  generateExpression,
1355
1283
  generateObjectExpression,
1356
1284
  generatePageMeta,
@@ -1361,17 +1289,12 @@ export {
1361
1289
  getStyleDeclKey,
1362
1290
  initialBreakpoints,
1363
1291
  isLiteralExpression,
1364
- isLocalResource,
1365
1292
  isPathnamePattern,
1366
- isPlainObject,
1367
1293
  isRootFolder,
1368
1294
  lintExpression,
1369
- loadResource,
1370
- loadResources,
1371
1295
  matchPathnameParams,
1372
1296
  parseComponentName,
1373
1297
  parseObjectExpression,
1374
1298
  replaceFormActionsWithResources,
1375
- sitemapResourceUrl,
1376
1299
  transpileExpression
1377
1300
  };
package/lib/runtime.js ADDED
@@ -0,0 +1,101 @@
1
+ // src/resource-loader.ts
2
+ var LOCAL_RESOURCE_PREFIX = "$resources";
3
+ var isLocalResource = (pathname, resourceName) => {
4
+ const segments = pathname.split("/").filter(Boolean);
5
+ if (resourceName === void 0) {
6
+ return segments[0] === LOCAL_RESOURCE_PREFIX;
7
+ }
8
+ return segments.join("/") === `${LOCAL_RESOURCE_PREFIX}/${resourceName}`;
9
+ };
10
+ var sitemapResourceUrl = `/${LOCAL_RESOURCE_PREFIX}/sitemap.xml`;
11
+ var loadResource = async (customFetch, resourceRequest) => {
12
+ const { url, method, headers, body } = resourceRequest;
13
+ const requestHeaders = new Headers(
14
+ headers.map(({ name, value }) => [name, value])
15
+ );
16
+ const requestInit = {
17
+ method,
18
+ headers: requestHeaders
19
+ };
20
+ if (method !== "get" && body !== void 0) {
21
+ if (typeof body === "string") {
22
+ requestInit.body = body;
23
+ }
24
+ if (typeof body === "object") {
25
+ requestInit.body = JSON.stringify(body);
26
+ }
27
+ }
28
+ try {
29
+ const response = await customFetch(url.trim(), requestInit);
30
+ let data = await response.text();
31
+ try {
32
+ data = JSON.parse(data);
33
+ } catch {
34
+ }
35
+ if (!response.ok) {
36
+ console.error(
37
+ `Failed to load resource: ${url} - ${response.status}: ${JSON.stringify(data).slice(0, 300)}`
38
+ );
39
+ }
40
+ return {
41
+ ok: response.ok,
42
+ data,
43
+ status: response.status,
44
+ statusText: response.statusText
45
+ };
46
+ } catch (error) {
47
+ console.error(error);
48
+ const message = error.message;
49
+ return {
50
+ ok: false,
51
+ data: void 0,
52
+ status: 500,
53
+ statusText: message
54
+ };
55
+ }
56
+ };
57
+ var loadResources = async (customFetch, requests) => {
58
+ return Object.fromEntries(
59
+ await Promise.all(
60
+ Array.from(
61
+ requests,
62
+ async ([name, request]) => [name, await loadResource(customFetch, request)]
63
+ )
64
+ )
65
+ );
66
+ };
67
+
68
+ // src/to-string.ts
69
+ var createJsonStringifyProxy = (target) => {
70
+ return new Proxy(target, {
71
+ get(target2, prop, receiver) {
72
+ if (prop === "toString") {
73
+ return function() {
74
+ return JSON.stringify(target2);
75
+ };
76
+ }
77
+ const value = Reflect.get(target2, prop, receiver);
78
+ if (typeof value === "object" && value !== null) {
79
+ return createJsonStringifyProxy(value);
80
+ }
81
+ return value;
82
+ }
83
+ });
84
+ };
85
+ var isPlainObject = (value) => {
86
+ return Object.prototype.toString.call(value) === "[object Object]" && (Object.getPrototypeOf(value) === null || Object.getPrototypeOf(value) === Object.prototype);
87
+ };
88
+
89
+ // src/form-fields.ts
90
+ var formIdFieldName = `ws--form-id`;
91
+ var formBotFieldName = `ws--form-bot`;
92
+ export {
93
+ createJsonStringifyProxy,
94
+ formBotFieldName,
95
+ formIdFieldName,
96
+ isLocalResource,
97
+ isPlainObject,
98
+ loadResource,
99
+ loadResources,
100
+ sitemapResourceUrl
101
+ };
@@ -13,10 +13,7 @@ export * from "./schema/webstudio";
13
13
  export * from "./instances-utils";
14
14
  export * from "./page-utils";
15
15
  export * from "./scope";
16
- export * from "./resource-loader";
17
16
  export * from "./expression";
18
17
  export * from "./resources-generator";
19
18
  export * from "./page-meta-generator";
20
19
  export * from "./url-pattern";
21
- export * from "./form-fields";
22
- export * from "./to-string";
@@ -140,7 +140,7 @@ export declare const renderJsx: (root: JSX.Element) => {
140
140
  type ComponentProps = Record<string, unknown> & Record<`${string}:expression`, string> & {
141
141
  "ws:id"?: string;
142
142
  "ws:label"?: string;
143
- children?: ReactNode;
143
+ children?: ReactNode | ExpressionValue;
144
144
  };
145
145
  type Component = {
146
146
  displayName: string;
@@ -1,4 +1,9 @@
1
1
  import type { ResourceRequest } from "./schema/resources";
2
+ /**
3
+ * Prevents fetch cycles by prefixing local resources.
4
+ */
5
+ export declare const isLocalResource: (pathname: string, resourceName?: string) => boolean;
6
+ export declare const sitemapResourceUrl = "/$resources/sitemap.xml";
2
7
  export declare const loadResource: (customFetch: typeof fetch, resourceRequest: ResourceRequest) => Promise<{
3
8
  ok: boolean;
4
9
  data: string;
@@ -0,0 +1,3 @@
1
+ export * from "./resource-loader";
2
+ export * from "./to-string";
3
+ export * from "./form-fields";
@@ -206,3 +206,212 @@ export declare const Instances: z.ZodMap<z.ZodString, z.ZodObject<{
206
206
  label?: string | undefined;
207
207
  }>>;
208
208
  export type Instances = z.infer<typeof Instances>;
209
+ export declare const MatcherRelation: z.ZodUnion<[z.ZodLiteral<"ancestor">, z.ZodLiteral<"parent">, z.ZodLiteral<"self">, z.ZodLiteral<"child">, z.ZodLiteral<"descendant">]>;
210
+ export type MatcherRelation = z.infer<typeof MatcherRelation>;
211
+ export declare const MatcherOperation: z.ZodObject<{
212
+ $eq: z.ZodOptional<z.ZodString>;
213
+ $neq: z.ZodOptional<z.ZodString>;
214
+ $in: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
215
+ $nin: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
216
+ }, "strip", z.ZodTypeAny, {
217
+ $eq?: string | undefined;
218
+ $neq?: string | undefined;
219
+ $in?: string[] | undefined;
220
+ $nin?: string[] | undefined;
221
+ }, {
222
+ $eq?: string | undefined;
223
+ $neq?: string | undefined;
224
+ $in?: string[] | undefined;
225
+ $nin?: string[] | undefined;
226
+ }>;
227
+ export type MatcherOperation = z.infer<typeof MatcherOperation>;
228
+ export declare const Matcher: z.ZodObject<{
229
+ relation: z.ZodUnion<[z.ZodLiteral<"ancestor">, z.ZodLiteral<"parent">, z.ZodLiteral<"self">, z.ZodLiteral<"child">, z.ZodLiteral<"descendant">]>;
230
+ component: z.ZodOptional<z.ZodObject<{
231
+ $eq: z.ZodOptional<z.ZodString>;
232
+ $neq: z.ZodOptional<z.ZodString>;
233
+ $in: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
234
+ $nin: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
235
+ }, "strip", z.ZodTypeAny, {
236
+ $eq?: string | undefined;
237
+ $neq?: string | undefined;
238
+ $in?: string[] | undefined;
239
+ $nin?: string[] | undefined;
240
+ }, {
241
+ $eq?: string | undefined;
242
+ $neq?: string | undefined;
243
+ $in?: string[] | undefined;
244
+ $nin?: string[] | undefined;
245
+ }>>;
246
+ tag: z.ZodOptional<z.ZodObject<{
247
+ $eq: z.ZodOptional<z.ZodString>;
248
+ $neq: z.ZodOptional<z.ZodString>;
249
+ $in: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
250
+ $nin: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
251
+ }, "strip", z.ZodTypeAny, {
252
+ $eq?: string | undefined;
253
+ $neq?: string | undefined;
254
+ $in?: string[] | undefined;
255
+ $nin?: string[] | undefined;
256
+ }, {
257
+ $eq?: string | undefined;
258
+ $neq?: string | undefined;
259
+ $in?: string[] | undefined;
260
+ $nin?: string[] | undefined;
261
+ }>>;
262
+ }, "strip", z.ZodTypeAny, {
263
+ relation: "ancestor" | "parent" | "self" | "child" | "descendant";
264
+ component?: {
265
+ $eq?: string | undefined;
266
+ $neq?: string | undefined;
267
+ $in?: string[] | undefined;
268
+ $nin?: string[] | undefined;
269
+ } | undefined;
270
+ tag?: {
271
+ $eq?: string | undefined;
272
+ $neq?: string | undefined;
273
+ $in?: string[] | undefined;
274
+ $nin?: string[] | undefined;
275
+ } | undefined;
276
+ }, {
277
+ relation: "ancestor" | "parent" | "self" | "child" | "descendant";
278
+ component?: {
279
+ $eq?: string | undefined;
280
+ $neq?: string | undefined;
281
+ $in?: string[] | undefined;
282
+ $nin?: string[] | undefined;
283
+ } | undefined;
284
+ tag?: {
285
+ $eq?: string | undefined;
286
+ $neq?: string | undefined;
287
+ $in?: string[] | undefined;
288
+ $nin?: string[] | undefined;
289
+ } | undefined;
290
+ }>;
291
+ export type Matcher = z.infer<typeof Matcher>;
292
+ export declare const Matchers: z.ZodUnion<[z.ZodObject<{
293
+ relation: z.ZodUnion<[z.ZodLiteral<"ancestor">, z.ZodLiteral<"parent">, z.ZodLiteral<"self">, z.ZodLiteral<"child">, z.ZodLiteral<"descendant">]>;
294
+ component: z.ZodOptional<z.ZodObject<{
295
+ $eq: z.ZodOptional<z.ZodString>;
296
+ $neq: z.ZodOptional<z.ZodString>;
297
+ $in: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
298
+ $nin: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
299
+ }, "strip", z.ZodTypeAny, {
300
+ $eq?: string | undefined;
301
+ $neq?: string | undefined;
302
+ $in?: string[] | undefined;
303
+ $nin?: string[] | undefined;
304
+ }, {
305
+ $eq?: string | undefined;
306
+ $neq?: string | undefined;
307
+ $in?: string[] | undefined;
308
+ $nin?: string[] | undefined;
309
+ }>>;
310
+ tag: z.ZodOptional<z.ZodObject<{
311
+ $eq: z.ZodOptional<z.ZodString>;
312
+ $neq: z.ZodOptional<z.ZodString>;
313
+ $in: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
314
+ $nin: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
315
+ }, "strip", z.ZodTypeAny, {
316
+ $eq?: string | undefined;
317
+ $neq?: string | undefined;
318
+ $in?: string[] | undefined;
319
+ $nin?: string[] | undefined;
320
+ }, {
321
+ $eq?: string | undefined;
322
+ $neq?: string | undefined;
323
+ $in?: string[] | undefined;
324
+ $nin?: string[] | undefined;
325
+ }>>;
326
+ }, "strip", z.ZodTypeAny, {
327
+ relation: "ancestor" | "parent" | "self" | "child" | "descendant";
328
+ component?: {
329
+ $eq?: string | undefined;
330
+ $neq?: string | undefined;
331
+ $in?: string[] | undefined;
332
+ $nin?: string[] | undefined;
333
+ } | undefined;
334
+ tag?: {
335
+ $eq?: string | undefined;
336
+ $neq?: string | undefined;
337
+ $in?: string[] | undefined;
338
+ $nin?: string[] | undefined;
339
+ } | undefined;
340
+ }, {
341
+ relation: "ancestor" | "parent" | "self" | "child" | "descendant";
342
+ component?: {
343
+ $eq?: string | undefined;
344
+ $neq?: string | undefined;
345
+ $in?: string[] | undefined;
346
+ $nin?: string[] | undefined;
347
+ } | undefined;
348
+ tag?: {
349
+ $eq?: string | undefined;
350
+ $neq?: string | undefined;
351
+ $in?: string[] | undefined;
352
+ $nin?: string[] | undefined;
353
+ } | undefined;
354
+ }>, z.ZodArray<z.ZodObject<{
355
+ relation: z.ZodUnion<[z.ZodLiteral<"ancestor">, z.ZodLiteral<"parent">, z.ZodLiteral<"self">, z.ZodLiteral<"child">, z.ZodLiteral<"descendant">]>;
356
+ component: z.ZodOptional<z.ZodObject<{
357
+ $eq: z.ZodOptional<z.ZodString>;
358
+ $neq: z.ZodOptional<z.ZodString>;
359
+ $in: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
360
+ $nin: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
361
+ }, "strip", z.ZodTypeAny, {
362
+ $eq?: string | undefined;
363
+ $neq?: string | undefined;
364
+ $in?: string[] | undefined;
365
+ $nin?: string[] | undefined;
366
+ }, {
367
+ $eq?: string | undefined;
368
+ $neq?: string | undefined;
369
+ $in?: string[] | undefined;
370
+ $nin?: string[] | undefined;
371
+ }>>;
372
+ tag: z.ZodOptional<z.ZodObject<{
373
+ $eq: z.ZodOptional<z.ZodString>;
374
+ $neq: z.ZodOptional<z.ZodString>;
375
+ $in: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
376
+ $nin: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
377
+ }, "strip", z.ZodTypeAny, {
378
+ $eq?: string | undefined;
379
+ $neq?: string | undefined;
380
+ $in?: string[] | undefined;
381
+ $nin?: string[] | undefined;
382
+ }, {
383
+ $eq?: string | undefined;
384
+ $neq?: string | undefined;
385
+ $in?: string[] | undefined;
386
+ $nin?: string[] | undefined;
387
+ }>>;
388
+ }, "strip", z.ZodTypeAny, {
389
+ relation: "ancestor" | "parent" | "self" | "child" | "descendant";
390
+ component?: {
391
+ $eq?: string | undefined;
392
+ $neq?: string | undefined;
393
+ $in?: string[] | undefined;
394
+ $nin?: string[] | undefined;
395
+ } | undefined;
396
+ tag?: {
397
+ $eq?: string | undefined;
398
+ $neq?: string | undefined;
399
+ $in?: string[] | undefined;
400
+ $nin?: string[] | undefined;
401
+ } | undefined;
402
+ }, {
403
+ relation: "ancestor" | "parent" | "self" | "child" | "descendant";
404
+ component?: {
405
+ $eq?: string | undefined;
406
+ $neq?: string | undefined;
407
+ $in?: string[] | undefined;
408
+ $nin?: string[] | undefined;
409
+ } | undefined;
410
+ tag?: {
411
+ $eq?: string | undefined;
412
+ $neq?: string | undefined;
413
+ $in?: string[] | undefined;
414
+ $nin?: string[] | undefined;
415
+ } | undefined;
416
+ }>, "many">]>;
417
+ export type Matchers = z.infer<typeof Matchers>;
@@ -119,8 +119,3 @@ export declare const Resources: z.ZodMap<z.ZodString, z.ZodObject<{
119
119
  body?: string | undefined;
120
120
  }>>;
121
121
  export type Resources = z.infer<typeof Resources>;
122
- /**
123
- * Prevents fetch cycles by prefixing local resources.
124
- */
125
- export declare const isLocalResource: (pathname: string, resourceName?: string) => boolean;
126
- export declare const sitemapResourceUrl = "/$resources/sitemap.xml";