Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

add rest/{props,tree}/$projectId/$pageId

Roman Pominovc4b6fa9bd11f
diff --git a/apps/designer/app/routes/rest/patch.ts b/apps/designer/app/routes/rest/patch.ts
index b3f05de21c..ca7fb4ddf2 100644
--- a/apps/designer/app/routes/rest/patch.ts
+++ b/apps/designer/app/routes/rest/patch.ts
@@ -6,7 +6,7 @@ import { type Tree } from "@webstudio-is/react-sdk";
type PatchData = {
transactions: Array<SyncItem>;
- treeId: Tree["id"];
+ treeId: Tree["id"]; // @todo: it would be more correct to pass Page["id"] here
buildId: Build["id"];
};
diff --git a/apps/designer/app/routes/rest/props.$projectId.$pageId.ts b/apps/designer/app/routes/rest/props.$projectId.$pageId.ts
new file mode 100644
index 0000000000..a25330073c
--- /dev/null
+++ b/apps/designer/app/routes/rest/props.$projectId.$pageId.ts
@@ -0,0 +1,44 @@
+import { type LoaderFunction } from "@remix-run/node";
+import { type InstanceProps } from "@webstudio-is/react-sdk";
+import { db } from "@webstudio-is/project/index.server";
+import { utils } from "@webstudio-is/project";
+
+type ErrorData = {
+ errors: string;
+};
+
+export const loader: LoaderFunction = async ({
+ params,
+}): Promise<Array<InstanceProps> | ErrorData> => {
+ try {
+ if (params.projectId === undefined) {
+ throw new Error(`Project ID required`);
+ }
+ if (params.pageId === undefined) {
+ throw new Error(`Page ID required`);
+ }
+
+ const prodBuild = await db.build.loadByProjectId(params.projectId, "prod");
+
+ if (prodBuild === undefined) {
+ throw new Error(
+ `Project ${params.projectId} needs to be published first`
+ );
+ }
+
+ const page = utils.pages.findById(prodBuild.pages, params.pageId);
+
+ if (page === undefined) {
+ throw new Error(`Page ${params.pageId} not found`);
+ }
+
+ return await db.props.loadByTreeId(page.treeId);
+ } catch (error) {
+ if (error instanceof Error) {
+ return {
+ errors: error.message,
+ };
+ }
+ }
+ return { errors: "Unexpected error" };
+};
diff --git a/apps/designer/app/routes/rest/props.$projectId.ts b/apps/designer/app/routes/rest/props.$projectId.ts
index cc240b1b06..bcb478d834 100644
--- a/apps/designer/app/routes/rest/props.$projectId.ts
+++ b/apps/designer/app/routes/rest/props.$projectId.ts
@@ -21,7 +21,6 @@ export const loader: LoaderFunction = async ({
);
}
- // @todo: use a correct page rather than homePage
return await db.props.loadByTreeId(prodBuild.pages.homePage.treeId);
} catch (error) {
if (error instanceof Error) {
diff --git a/apps/designer/app/routes/rest/tree.$projectId.$pageId.ts b/apps/designer/app/routes/rest/tree.$projectId.$pageId.ts
new file mode 100644
index 0000000000..615b65de22
--- /dev/null
+++ b/apps/designer/app/routes/rest/tree.$projectId.$pageId.ts
@@ -0,0 +1,44 @@
+import { type LoaderFunction } from "@remix-run/node";
+import type { Tree } from "@webstudio-is/react-sdk";
+import { db } from "@webstudio-is/project/index.server";
+import { utils } from "@webstudio-is/project";
+
+export type ErrorData = {
+ errors: string;
+};
+
+export const loader: LoaderFunction = async ({
+ params,
+}): Promise<Tree | null | ErrorData> => {
+ try {
+ if (params.projectId === undefined) {
+ throw new Error(`Project ID required`);
+ }
+ if (params.pageId === undefined) {
+ throw new Error(`Page ID required`);
+ }
+
+ const prodBuild = await db.build.loadByProjectId(params.projectId, "prod");
+
+ if (prodBuild === undefined) {
+ throw new Error(
+ `Project ${params.projectId} needs to be published first`
+ );
+ }
+
+ const page = utils.pages.findById(prodBuild.pages, params.pageId);
+
+ if (page === undefined) {
+ throw new Error(`Page ${params.pageId} not found`);
+ }
+
+ return await db.tree.loadById(page.treeId);
+ } catch (error) {
+ if (error instanceof Error) {
+ return {
+ errors: error.message,
+ };
+ }
+ }
+ return { errors: "Unexpected error" };
+};
diff --git a/packages/project/src/index.ts b/packages/project/src/index.ts
index 0f425bcc05..aaa1997bbc 100644
--- a/packages/project/src/index.ts
+++ b/packages/project/src/index.ts
@@ -1,5 +1,11 @@
import * as breakpointsUtils from "./shared/breakpoints";
import * as treeUtils from "./shared/tree-utils";
+import * as pagesUtils from "./shared/pages";
+
+export const utils = {
+ breakpoints: breakpointsUtils,
+ tree: treeUtils,
+ pages: pagesUtils,
+} as const;
-export const utils = { breakpoints: breakpointsUtils, tree: treeUtils };
export * from "./db/types";
diff --git a/packages/project/src/shared/pages/find-by-id.ts b/packages/project/src/shared/pages/find-by-id.ts
new file mode 100644
index 0000000000..23d2ae152a
--- /dev/null
+++ b/packages/project/src/shared/pages/find-by-id.ts
@@ -0,0 +1,8 @@
+import { type Pages, type Page } from "../../db/types";
+
+export const findById = (pages: Pages, pageId: string): Page | undefined => {
+ if (pages.homePage.id === pageId) {
+ return pages.homePage;
+ }
+ return pages.pages.find((page) => page.id === pageId);
+};
diff --git a/packages/project/src/shared/pages/index.ts b/packages/project/src/shared/pages/index.ts
new file mode 100644
index 0000000000..41138b8354
--- /dev/null
+++ b/packages/project/src/shared/pages/index.ts
@@ -0,0 +1 @@
+export * from "./find-by-id";