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.

Rest endpoint to load by projectId (#1863)

Andras Bacsaif849cc5b7cce
diff --git a/apps/builder/app/routes/rest.buildId.$projectId.tsx b/apps/builder/app/routes/rest.buildId.$projectId.tsx
new file mode 100644
index 0000000000..f9da3c73e7
--- /dev/null
+++ b/apps/builder/app/routes/rest.buildId.$projectId.tsx
@@ -0,0 +1,39 @@
+import { json, type LoaderArgs } from "@remix-run/node";
+import { db as projectDb } from "@webstudio-is/project/index.server";
+import { sentryException } from "~/shared/sentry";
+import { createContext } from "~/shared/context.server";
+
+export const loader = async ({
+ params,
+ request,
+}: LoaderArgs): Promise<{ buildId: string | null }> => {
+ try {
+ const projectId = params.projectId;
+
+ if (projectId === undefined) {
+ throw json("Required project id", { status: 400 });
+ }
+
+ const context = await createContext(request);
+
+ const project = await projectDb.project.loadById(projectId, context);
+ const buildId = project.latestBuild?.buildId ?? null;
+
+ return {
+ buildId,
+ };
+ } catch (error) {
+ // If a Response is thrown, we're rethrowing it for Remix to handle.
+ // https://remix.run/docs/en/v1/api/conventions#throwing-responses-in-loaders
+ if (error instanceof Response) {
+ throw error;
+ }
+
+ sentryException({ error });
+
+ // We have no idea what happened, so we'll return a 500 error.
+ throw json(error instanceof Error ? error.message : String(error), {
+ status: 500,
+ });
+ }
+};
diff --git a/packages/project/src/db/project.ts b/packages/project/src/db/project.ts
index b1002c0fde..adb17764e2 100644
--- a/packages/project/src/db/project.ts
+++ b/packages/project/src/db/project.ts
@@ -33,6 +33,10 @@ export const loadById = async (
},
});
+ if (data === null) {
+ throw new Error(`Project ${projectId} not found`);
+ }
+
return Project.parse(data);
};