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
fix: type errors
Andras Bacsaiaa3aea369ec3
diff --git a/packages/http-client/src/index.d.ts b/packages/http-client/src/index.d.tsindex 8a14becff7..be6da10ec1 100644--- a/packages/http-client/src/index.d.ts+++ b/packages/http-client/src/index.d.ts@@ -4,8 +4,6 @@ import type {UserProp,} from "@webstudio-is/react-sdk";-export type IncludeTypes = "tree" | "props" | "breakpoints";-export type Includes<T> = { [include in IncludeTypes]: T };export type Page = {id: string;root: [string, string];diff --git a/packages/http-client/src/index.test.ts b/packages/http-client/src/index.test.tsindex d449c19690..20c4c526e8 100644--- a/packages/http-client/src/index.test.ts+++ b/packages/http-client/src/index.test.ts@@ -2,38 +2,38 @@ import { loadProject } from "./index";const existingProjectId = "40c7a865-0bc7-4012-9318-6c4c665425de";const notPublishedProjectId = "7ec397c6-b3d0-4967-9073-9d83623fcf8e";+const onlyHomeProjectId = "1e60b0be-afba-4c8d-9956-5a5b7963e4dc";+const morePagesProjectId = "40c7a865-0bc7-4012-9318-6c4c665425de";const apiUrl = "http://localhost:3000";describe("getProjectDetails", () => {test("include pages", async () => {const response = await loadProject({apiUrl,- projectId: existingProjectId,- include: { tree: true, props: true, breakpoints: true },+ projectId: morePagesProjectId,});- expect(response.pages).toBeTruthy();+ expect(Object.keys(response).length > 1).toBeTruthy();});test("does not include pages", async () => {const response = await loadProject({apiUrl,- projectId: notPublishedProjectId,- include: { tree: true, props: true, breakpoints: true },+ projectId: onlyHomeProjectId,});- expect(response.pages).toBeNull();+ expect(Object.keys(response).length === 1).toBeTruthy();});test("loads existing project", async () => {const response = await loadProject({apiUrl,projectId: existingProjectId,});- expect(response.tree.id).toBeTruthy();+ expect(response).toBeTruthy();});test("loads not published project", async () => {const response = await loadProject({apiUrl,projectId: notPublishedProjectId,});- expect(response.tree.errors).toBe(+ expect(response).toBe(`Project ${notPublishedProjectId} needs to be published first`);});diff --git a/packages/http-client/src/index.ts b/packages/http-client/src/index.tsindex 4ae944e962..f9f7170937 100644--- a/packages/http-client/src/index.ts+++ b/packages/http-client/src/index.ts@@ -1,124 +1,129 @@import fetch from "isomorphic-fetch";-import type { Includes, Project, Page } from "./index.d";+import type { Project } from "./index.d";export const loadProject = async ({apiUrl,projectId,- include = { tree: true, props: true, breakpoints: true },}: {apiUrl: string;projectId: string;- include?: Includes<boolean>;-}): Promise<Project> => {- if (apiUrl === undefined) {- throw new Error("Webstudio API URL is required.");- }- const urls = [];- const gatheredPages: Project = {};- const baseUrl = new URL(apiUrl);- // Get all pages- const pagesUrl = new URL(`/rest/pages/${projectId}`, baseUrl);- const listOfPages = await (await fetch(pagesUrl)).json();- if (listOfPages === undefined) {- throw new Error("Project not found");- }- 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",- });- }+}): Promise<Project | string> => {+ try {+ if (apiUrl === undefined) {+ throw new Error("Webstudio API URL is required.");+ }+ const urls = [];+ const gatheredPages: Project = {};- if (pages && pages.length > 0) {- for (const page of pages) {- const { id, path } = page;+ 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,+ page: { ...homePage },url: treeUrl,- path,+ path: "/",type: "tree",});-urls.push({- page,+ page: { ...homePage },url: propsUrl,- path,+ path: "/",type: "props",});-urls.push({- page,+ page: { ...homePage },+ url: breakpointsUrl,+ path: "/",+ type: "breakpoints",+ });+ urls.push({+ page: { ...homePage },url: cssUrl,- path,+ 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(),- };+ 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",+ });}- })- );- const breakpoints = data.find(- (d) => d?.type === "breakpoints" && d.path === "/"- );+ }++ 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 || {},- };+ 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 || {},+ };+ }}}+ return {+ ...gatheredPages,+ };+ } catch (error: unknown) {+ if (error instanceof Error) {+ return error.message;+ }+ return "Unknown error";}- return {- ...gatheredPages,- };};