@webstudio-is/sdk 0.125.0 → 0.127.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
@@ -35,14 +35,14 @@ var MIN_TITLE_LENGTH = 2;
35
35
  var PageId = z2.string();
36
36
  var FolderId = z2.string();
37
37
  var FolderName = z2.string().refine((value) => value.trim() !== "", "Can't be empty");
38
- var FolderSlug = z2.string().refine((slug) => slug !== "", "Can't be empty").refine((slug) => slug.includes("/") === false, "Can't contain a /").refine(
38
+ var Slug = z2.string().refine(
39
39
  (path) => /^[-a-z0-9]*$/.test(path),
40
40
  "Only a-z, 0-9 and - are allowed"
41
41
  );
42
42
  var Folder = z2.object({
43
43
  id: FolderId,
44
44
  name: FolderName,
45
- slug: z2.string(),
45
+ slug: Slug,
46
46
  children: z2.array(z2.union([FolderId, PageId]))
47
47
  });
48
48
  var PageName = z2.string().refine((value) => value.trim() !== "", "Can't be empty");
@@ -127,12 +127,13 @@ var ExpressionChild = z3.object({
127
127
  type: z3.literal("expression"),
128
128
  value: z3.string()
129
129
  });
130
+ var InstanceChild = z3.union([IdChild, TextChild, ExpressionChild]);
130
131
  var Instance = z3.object({
131
132
  type: z3.literal("instance"),
132
133
  id: InstanceId,
133
134
  component: z3.string(),
134
135
  label: z3.string().optional(),
135
- children: z3.array(z3.union([IdChild, TextChild, ExpressionChild]))
136
+ children: z3.array(InstanceChild)
136
137
  });
137
138
  var Instances = z3.map(InstanceId, Instance);
138
139
 
@@ -363,6 +364,21 @@ var Deployment = z11.object({
363
364
  projectDomain: z11.string()
364
365
  });
365
366
 
367
+ // src/schema/webstudio.ts
368
+ import { z as z12 } from "zod";
369
+ var WebstudioFragment = z12.object({
370
+ children: z12.array(InstanceChild),
371
+ instances: z12.array(Instance),
372
+ assets: z12.array(Asset),
373
+ dataSources: z12.array(DataSource),
374
+ resources: z12.array(Resource),
375
+ props: z12.array(Prop),
376
+ breakpoints: z12.array(Breakpoint),
377
+ styleSourceSelections: z12.array(StyleSourceSelection),
378
+ styleSources: z12.array(StyleSource),
379
+ styles: z12.array(StyleDecl)
380
+ });
381
+
366
382
  // src/instances-utils.ts
367
383
  var traverseInstances = (instances, instanceId, callback) => {
368
384
  const instance = instances.get(instanceId);
@@ -408,6 +424,54 @@ var parseComponentName = (componentName) => {
408
424
  return [namespace, name];
409
425
  };
410
426
 
427
+ // src/page-utils.ts
428
+ var ROOT_FOLDER_ID = "root";
429
+ var isRoot = (folder) => folder.id === ROOT_FOLDER_ID;
430
+ var findPageByIdOrPath = (idOrPath, pages) => {
431
+ if (idOrPath === "" || idOrPath === "/" || idOrPath === pages.homePage.id) {
432
+ return pages.homePage;
433
+ }
434
+ return pages.pages.find(
435
+ (page) => page.id === idOrPath || getPagePath(page.id, pages) === idOrPath
436
+ );
437
+ };
438
+ var findParentFolderByChildId = (id, folders) => {
439
+ for (const folder of folders) {
440
+ if (folder.children.includes(id)) {
441
+ return folder;
442
+ }
443
+ }
444
+ };
445
+ var getPagePath = (id, pages) => {
446
+ const foldersMap = /* @__PURE__ */ new Map();
447
+ const childParentMap = /* @__PURE__ */ new Map();
448
+ for (const folder of pages.folders) {
449
+ foldersMap.set(folder.id, folder);
450
+ for (const childId of folder.children) {
451
+ childParentMap.set(childId, folder.id);
452
+ }
453
+ }
454
+ const paths = [];
455
+ let currentId = id;
456
+ const allPages = [pages.homePage, ...pages.pages];
457
+ for (const page of allPages) {
458
+ if (page.id === id) {
459
+ paths.push(page.path);
460
+ currentId = childParentMap.get(page.id);
461
+ break;
462
+ }
463
+ }
464
+ while (currentId) {
465
+ const folder = foldersMap.get(currentId);
466
+ if (folder === void 0) {
467
+ break;
468
+ }
469
+ paths.push(folder.slug);
470
+ currentId = childParentMap.get(currentId);
471
+ }
472
+ return paths.reverse().join("/").replace(/\/+/g, "/");
473
+ };
474
+
411
475
  // src/scope.ts
412
476
  var normalizeName = (name) => {
413
477
  name = name.replaceAll(/[^\w$]/g, "");
@@ -458,19 +522,28 @@ var loadResource = async (resourceData) => {
458
522
  if (method !== "get" && body !== void 0) {
459
523
  requestInit.body = body;
460
524
  }
461
- const response = await fetch(url, requestInit);
462
- let data;
463
- if (response.ok && // accept json by default and when specified explicitly
464
- (requestHeaders.has("accept") === false || requestHeaders.get("accept") === "application/json")) {
465
- data = await response.json();
466
- } else {
467
- data = await response.text();
525
+ try {
526
+ const response = await fetch(url, requestInit);
527
+ let data;
528
+ if (response.ok && // accept json by default and when specified explicitly
529
+ (requestHeaders.has("accept") === false || requestHeaders.get("accept") === "application/json")) {
530
+ data = await response.json();
531
+ } else {
532
+ data = await response.text();
533
+ }
534
+ return {
535
+ data,
536
+ status: response.status,
537
+ statusText: response.statusText
538
+ };
539
+ } catch (error) {
540
+ const message = error.message;
541
+ return {
542
+ data: void 0,
543
+ status: 500,
544
+ statusText: message
545
+ };
468
546
  }
469
- return {
470
- data,
471
- status: response.status,
472
- statusText: response.statusText
473
- };
474
547
  };
475
548
  export {
476
549
  Asset,
@@ -484,13 +557,13 @@ export {
484
557
  ExpressionChild,
485
558
  Folder,
486
559
  FolderName,
487
- FolderSlug,
488
560
  FontAsset,
489
561
  HomePagePath,
490
562
  IdChild,
491
563
  ImageAsset,
492
564
  ImageMeta,
493
565
  Instance,
566
+ InstanceChild,
494
567
  Instances,
495
568
  PageName,
496
569
  PagePath,
@@ -498,6 +571,7 @@ export {
498
571
  Pages,
499
572
  Prop,
500
573
  Props,
574
+ ROOT_FOLDER_ID,
501
575
  Resource,
502
576
  Resources,
503
577
  StyleDecl,
@@ -507,11 +581,16 @@ export {
507
581
  StyleSources,
508
582
  Styles,
509
583
  TextChild,
584
+ WebstudioFragment,
510
585
  createScope,
586
+ findPageByIdOrPath,
587
+ findParentFolderByChildId,
511
588
  findTreeInstanceIds,
512
589
  findTreeInstanceIdsExcludingSlotDescendants,
590
+ getPagePath,
513
591
  getStyleDeclKey,
514
592
  initialBreakpoints,
593
+ isRoot,
515
594
  loadResource,
516
595
  parseComponentName
517
596
  };
@@ -9,6 +9,8 @@ export * from "./schema/style-sources";
9
9
  export * from "./schema/style-source-selections";
10
10
  export * from "./schema/styles";
11
11
  export * from "./schema/deployment";
12
+ export * from "./schema/webstudio";
12
13
  export * from "./instances-utils";
14
+ export * from "./page-utils";
13
15
  export * from "./scope";
14
16
  export * from "./resource-loader";
@@ -0,0 +1,18 @@
1
+ import type { Folder, Page, Pages } from "./schema/pages";
2
+ export declare const ROOT_FOLDER_ID = "root";
3
+ /**
4
+ * Returns true if folder is the root folder.
5
+ */
6
+ export declare const isRoot: (folder: Folder) => boolean;
7
+ /**
8
+ * Find a page by id or path.
9
+ */
10
+ export declare const findPageByIdOrPath: (idOrPath: string, pages: Pages) => Page | undefined;
11
+ /**
12
+ * Find a folder that has has that id in the children.
13
+ */
14
+ export declare const findParentFolderByChildId: (id: Folder["id"] | Page["id"], folders: Array<Folder>) => Folder | undefined;
15
+ /**
16
+ * Get a path from all folder slugs from root to the current folder or page.
17
+ */
18
+ export declare const getPagePath: (id: Folder["id"] | Page["id"], pages: Pages) => string;
@@ -0,0 +1 @@
1
+ export {};
@@ -32,6 +32,34 @@ export declare const ExpressionChild: z.ZodObject<{
32
32
  type: "expression";
33
33
  }>;
34
34
  export type ExpressionChild = z.infer<typeof ExpressionChild>;
35
+ export declare const InstanceChild: z.ZodUnion<[z.ZodObject<{
36
+ type: z.ZodLiteral<"id">;
37
+ value: z.ZodString;
38
+ }, "strip", z.ZodTypeAny, {
39
+ value: string;
40
+ type: "id";
41
+ }, {
42
+ value: string;
43
+ type: "id";
44
+ }>, z.ZodObject<{
45
+ type: z.ZodLiteral<"text">;
46
+ value: z.ZodString;
47
+ }, "strip", z.ZodTypeAny, {
48
+ value: string;
49
+ type: "text";
50
+ }, {
51
+ value: string;
52
+ type: "text";
53
+ }>, z.ZodObject<{
54
+ type: z.ZodLiteral<"expression">;
55
+ value: z.ZodString;
56
+ }, "strip", z.ZodTypeAny, {
57
+ value: string;
58
+ type: "expression";
59
+ }, {
60
+ value: string;
61
+ type: "expression";
62
+ }>]>;
35
63
  export declare const Instance: z.ZodObject<{
36
64
  type: z.ZodLiteral<"instance">;
37
65
  id: z.ZodString;
@@ -1,10 +1,9 @@
1
1
  import { z } from "zod";
2
2
  export declare const FolderName: z.ZodEffects<z.ZodString, string, string>;
3
- export declare const FolderSlug: z.ZodEffects<z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, string, string>, string, string>;
4
3
  export declare const Folder: z.ZodObject<{
5
4
  id: z.ZodString;
6
5
  name: z.ZodEffects<z.ZodString, string, string>;
7
- slug: z.ZodString;
6
+ slug: z.ZodEffects<z.ZodString, string, string>;
8
7
  children: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodString]>, "many">;
9
8
  }, "strip", z.ZodTypeAny, {
10
9
  name: string;
@@ -323,7 +322,7 @@ export declare const Pages: z.ZodObject<{
323
322
  folders: z.ZodEffects<z.ZodArray<z.ZodObject<{
324
323
  id: z.ZodString;
325
324
  name: z.ZodEffects<z.ZodString, string, string>;
326
- slug: z.ZodString;
325
+ slug: z.ZodEffects<z.ZodString, string, string>;
327
326
  children: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodString]>, "many">;
328
327
  }, "strip", z.ZodTypeAny, {
329
328
  name: string;