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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
simplify http-client & single rest endpoint for publishing
Andras Bacsai6cf7c3543904
diff --git a/apps/designer/app/routes/rest/css.$projectId.$pageId.tsx b/apps/designer/app/routes/rest/css.$projectId.$pageId.tsxdeleted file mode 100644index 601618562f..0000000000--- a/apps/designer/app/routes/rest/css.$projectId.$pageId.tsx+++ /dev/null@@ -1,46 +0,0 @@-import { ActionFunction, json } from "@remix-run/node";-import env from "~/env.server";-import { sentryException } from "~/shared/sentry";-import { generateCssText } from "~/shared/css-utils";-import { BuildParams } from "~/shared/router-utils";--export const loader: ActionFunction = async ({ request, params }) => {- try {- const projectId = params.projectId ?? undefined;- const pageId = params.pageId ?? undefined;-- if (projectId === undefined) {- throw json("Required project id", { status: 404 });- }-- if (pageId === undefined) {- throw json("Required page id", { status: 404 });- }- const url = new URL(request.url);- const buildParams: BuildParams = {- projectId,- mode: "published",- pathname: url.pathname,- pageId,- };- const cssText = await generateCssText(buildParams);-- return new Response(cssText, {- headers: {- "Content-Type": "text/css",- },- });- } 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 });- return {- errors: error instanceof Error ? error.message : String(error),- env,- };- }-};diff --git a/apps/designer/app/routes/rest/project.$projectId.tsx b/apps/designer/app/routes/rest/project.$projectId.tsxnew file mode 100644index 0000000000..aac108ca4b--- /dev/null+++ b/apps/designer/app/routes/rest/project.$projectId.tsx@@ -0,0 +1,79 @@+import { ActionFunction, json } from "@remix-run/node";+import env from "~/env.server";+import { db } from "@webstudio-is/project/server";+import { sentryException } from "~/shared/sentry";+import { generateCssText } from "~/shared/css-utils";+import type { Props, Tree } from "@webstudio-is/react-sdk";+import type { Page } from "@webstudio-is/project";++type PagesDetails = {+ [key: string]: {+ page: Page;+ tree: Tree | null;+ props: Props[];+ css: string;+ };+};+export const loader: ActionFunction = async ({ request, params }) => {+ try {+ const projectId = params.projectId ?? undefined;+ const url = new URL(request.url);+ const pagesDetails: PagesDetails = {};++ if (projectId === undefined) {+ throw json("Required project id", { status: 400 });+ }++ const prodBuild = await db.build.loadByProjectId(projectId, "prod");+ if (prodBuild === undefined) {+ throw json(+ "Project not found or not published yet. Please contact us to get help.",+ { status: 500 }+ );+ }+ const {+ pages: { homePage, pages },+ } = prodBuild;+ const breakpoints = await db.breakpoints.load(prodBuild.id);+ pagesDetails["/"] = {+ page: homePage,+ tree: await db.tree.loadById(homePage.treeId),+ props: await db.props.loadByTreeId(homePage.treeId),+ css: await generateCssText({+ projectId,+ mode: "published",+ pathname: url.pathname,+ pageId: homePage.id,+ }),+ };++ if (pages.length > 0) {+ for (const page of pages) {+ pagesDetails[page.path] = {+ page,+ tree: await db.tree.loadById(page.treeId),+ props: await db.props.loadByTreeId(page.treeId),+ css: await generateCssText({+ projectId,+ mode: "published",+ pathname: page.path,+ pageId: page.id,+ }),+ };+ }+ }+ return { pages: pagesDetails, breakpoints };+ } 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 });+ return {+ errors: error instanceof Error ? error.message : String(error),+ env,+ };+ }+};diff --git a/apps/designer/app/shared/css-utils/generate-css-text.ts b/apps/designer/app/shared/css-utils/generate-css-text.tsindex 36c3c88a07..a311151631 100644--- a/apps/designer/app/shared/css-utils/generate-css-text.ts+++ b/apps/designer/app/shared/css-utils/generate-css-text.ts@@ -5,7 +5,7 @@ import { loadCanvasData } from "~/shared/db";import { createCssEngine } from "@webstudio-is/css-engine";import { idAttribute } from "@webstudio-is/react-sdk";import { addGlobalRules } from "~/canvas/shared/styles";-import { BuildParams } from "../router-utils";+import type { BuildParams } from "../router-utils";export const generateCssText = async (buildParams: BuildParams) => {if (buildParams === undefined) {diff --git a/packages/http-client/package.json b/packages/http-client/package.jsonindex 3fd785de35..e5db066f60 100644--- a/packages/http-client/package.json+++ b/packages/http-client/package.json@@ -1,6 +1,6 @@{"name": "@webstudio-is/http-client",- "version": "0.3.0",+ "version": "0.3.1","description": "Webstudio HTTP Client","author": "Webstudio <[email protected]>","homepage": "https://webstudio.is",diff --git a/packages/http-client/src/index.d.ts b/packages/http-client/src/index.d.tsindex be6da10ec1..4396bfac27 100644--- a/packages/http-client/src/index.d.ts+++ b/packages/http-client/src/index.d.ts@@ -9,7 +9,7 @@ export type Page = {root: [string, string];};export type Project = {- [include in string]: {+ [string]: {page: Page;tree: BaseInstance & { errors: string };props: Array<UserProp> & { errors: string };diff --git a/packages/http-client/src/index.ts b/packages/http-client/src/index.tsindex f9f7170937..21ae3a2fa2 100644--- a/packages/http-client/src/index.ts+++ b/packages/http-client/src/index.ts@@ -12,117 +12,17 @@ export const loadProject = async ({if (apiUrl === undefined) {throw new Error("Webstudio API URL is required.");}- const urls = [];- const gatheredPages: Project = {};-const baseUrl = new URL(apiUrl);- const pagesUrl = new URL(`/rest/pages/${projectId}`, baseUrl);- const listOfPages = await (await fetch(pagesUrl)).json();- if (listOfPages === undefined || listOfPages.errors) {- throw new Error(`Project ${projectId} needs to be published first`);- }- const { pages, homePage } = listOfPages;- if (homePage) {- const { id } = homePage;- const treeUrl = new URL(`/rest/tree/${projectId}/${id}`, baseUrl);- const propsUrl = new URL(`/rest/props/${projectId}/${id}`, baseUrl);- const breakpointsUrl = new URL(`/rest/breakpoints/${projectId}`, baseUrl);- const cssUrl = new URL(`/rest/css/${projectId}/${id}`, baseUrl);- urls.push({- page: { ...homePage },- url: treeUrl,- path: "/",- type: "tree",- });- urls.push({- page: { ...homePage },- url: propsUrl,- path: "/",- type: "props",- });- urls.push({- page: { ...homePage },- url: breakpointsUrl,- path: "/",- type: "breakpoints",- });- urls.push({- page: { ...homePage },- url: cssUrl,- path: "/",- type: "css",- });- }-- if (pages && pages.length > 0) {- for (const page of pages) {- const { id, path } = page;- const treeUrl = new URL(`/rest/tree/${projectId}/${id}`, baseUrl);- const propsUrl = new URL(`/rest/props/${projectId}/${id}`, baseUrl);- const cssUrl = new URL(`/rest/css/${projectId}/${id}`, baseUrl);-- urls.push({- page,- url: treeUrl,- path,- type: "tree",- });-- urls.push({- page,- url: propsUrl,- path,- type: "props",- });-- urls.push({- page,- url: cssUrl,- path,- type: "css",- });- }- }-- if (urls.length === 0) {- throw new Error("No pages to fetch");- }- const data = await Promise.all(- urls.map(async (url) => {- const res = await fetch(url.url);- if (res.ok) {- return {- page: url.page,- path: url.path,- type: url.type,- data: url.type === "css" ? await res.text() : await res.json(),- };- }- })- );- const breakpoints = data.find(- (d) => d?.type === "breakpoints" && d.path === "/"- );-- if (Object.keys(data).length > 0) {- for (const page of data) {- if (page && page.path && page.type && page.data) {- const { path, type } = page;- gatheredPages[path] = {- ...gatheredPages[path],- page: page.page,- [type]: page.data,- breakpoints: breakpoints?.data || {},- };- }- }+ const projectUrl = new URL(`/rest/project/${projectId}`, baseUrl);+ const projectResponse = await fetch(projectUrl);+ const project = await projectResponse.json();+ if (!projectResponse.ok) {+ throw new Error(project);}- return {- ...gatheredPages,- };+ return project;} catch (error: unknown) {if (error instanceof Error) {- return error.message;+ return error;}return "Unknown error";}diff --git a/packages/react-sdk/src/db/types.ts b/packages/react-sdk/src/db/types.tsindex 9a576bfa73..af3be76205 100644--- a/packages/react-sdk/src/db/types.ts+++ b/packages/react-sdk/src/db/types.ts@@ -7,6 +7,12 @@ export type Tree = {root: Instance;};+export type Props = {+ props: any;+ id: string;+ instanceId: string;+ treeId: string;+};export type InstanceProps = Omit<DbInstanceProps, "props"> & {props: Array<UserProp>;};