From bb669d3abeb25a994e0ccd318fbb1c8acf1c90a3 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 16 Nov 2022 10:48:21 +0100 Subject: [PATCH 1/5] feat: able to query pages with loadProject --- packages/http-client/src/index.d.ts | 8 +++- packages/http-client/src/index.test.ts | 20 +++++++++- packages/http-client/src/index.ts | 55 ++++++++++++++++++++++++-- 3 files changed, 76 insertions(+), 7 deletions(-) diff --git a/packages/http-client/src/index.d.ts b/packages/http-client/src/index.d.ts index 178b0828c3..2d7e6c1b4f 100644 --- a/packages/http-client/src/index.d.ts +++ b/packages/http-client/src/index.d.ts @@ -4,13 +4,17 @@ import type { UserProp, } from "@webstudio-is/react-sdk"; -export type IncludeTypes = "tree" | "props" | "breakpoints"; +export type IncludeTypes = "tree" | "props" | "breakpoints" | "pages"; export type Includes = { [include in IncludeTypes]: T }; - +export type Page = { + id: string; + root: [string, string]; +}; export type Project = { tree: BaseInstance & { errors: string }; props: Array & { errors: string }; breakpoints: Array & { errors: string; }; + pages: Record | null; }; diff --git a/packages/http-client/src/index.test.ts b/packages/http-client/src/index.test.ts index 4e1065e9f9..fdcf40efc9 100644 --- a/packages/http-client/src/index.test.ts +++ b/packages/http-client/src/index.test.ts @@ -1,10 +1,26 @@ import { loadProject } from "./index"; -const existingProjectId = "ab3619d3-831f-46f8-abb6-609558cffd99"; +const existingProjectId = "398d3918-9b00-4a43-8ab6-de4e151ae98a"; const notPublishedProjectId = "7ec397c6-b3d0-4967-9073-9d83623fcf8e"; 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, pages: true }, + }); + expect(response.pages).toBeTruthy(); + }); + test("does not include pages", async () => { + const response = await loadProject({ + apiUrl, + projectId: notPublishedProjectId, + include: { tree: true, props: true, breakpoints: true, pages: true }, + }); + expect(response.pages).toBeNull(); + }); test("loads existing project", async () => { const response = await loadProject({ apiUrl, @@ -18,7 +34,7 @@ describe("getProjectDetails", () => { projectId: notPublishedProjectId, }); expect(response.tree.errors).toBe( - "Project 7ec397c6-b3d0-4967-9073-9d83623fcf8e needs to be published first" + `Project ${notPublishedProjectId} needs to be published first` ); }); }); diff --git a/packages/http-client/src/index.ts b/packages/http-client/src/index.ts index 6dec39ec30..e88c6dee33 100644 --- a/packages/http-client/src/index.ts +++ b/packages/http-client/src/index.ts @@ -4,7 +4,7 @@ import type { Includes, Project } from "./index.d"; export const loadProject = async ({ apiUrl, projectId, - include = { tree: true, props: true, breakpoints: true }, + include = { tree: true, props: true, breakpoints: true, pages: false }, }: { apiUrl: string; projectId: string; @@ -13,19 +13,68 @@ export const loadProject = async ({ if (apiUrl === undefined) { throw new Error("Webstudio API URL is required."); } - const baseUrl = new URL(`${apiUrl}`); + const urls = []; + let pages: Record | null = {}; + + const baseUrl = new URL(apiUrl); const treeUrl = new URL(`/rest/tree/${projectId}`, baseUrl); const propsUrl = new URL(`/rest/props/${projectId}`, baseUrl); const breakpointsUrl = new URL(`/rest/breakpoints/${projectId}`, baseUrl); + const pagesUrl = new URL(`/rest/pages/${projectId}`, baseUrl); - const [tree, props, breakpoints] = await Promise.all([ + const [tree, props, breakpoints, listOfPages] = await Promise.all([ include.tree && (await fetch(treeUrl)).json(), include.props && (await fetch(propsUrl)).json(), include.breakpoints && (await fetch(breakpointsUrl)).json(), + include.pages && (await fetch(pagesUrl)).json(), ]); + + if (include.pages && listOfPages.pages && listOfPages.pages.length > 0) { + for (const page of listOfPages.pages) { + urls.push({ + url: new URL(`/rest/tree/${projectId}/${page.id}`, baseUrl), + path: page.path, + type: "tree", + }); + urls.push({ + url: new URL(`/rest/props/${projectId}/${page.id}`, baseUrl), + path: page.path, + type: "props", + }); + } + if (urls.length === 0) { + return { + tree, + props, + breakpoints, + pages, + }; + } + + const pagesData = await Promise.all([ + ...urls.map(async (url) => { + const res = await fetch(url.url); + if (res.ok) { + return { path: url.path, type: url.type, data: await res.json() }; + } + }), + ]); + if (Object.keys(pagesData).length > 0) { + for (const page of pagesData) { + if (page && page.path && page.type && page.data) { + pages[page.path] = { + ...(pages[page.path] as Record), + [page.type]: page.data, + breakpoints, + }; + } + } + } + } return { tree, props, breakpoints, + pages: Object.keys(pages).length > 0 ? pages : null, }; }; From 89aad1c74d6ed933c6511778fa30a4802990a375 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 16 Nov 2022 11:16:43 +0100 Subject: [PATCH 2/5] fix checks --- packages/http-client/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/http-client/src/index.ts b/packages/http-client/src/index.ts index e88c6dee33..76de1ca3f6 100644 --- a/packages/http-client/src/index.ts +++ b/packages/http-client/src/index.ts @@ -14,7 +14,7 @@ export const loadProject = async ({ throw new Error("Webstudio API URL is required."); } const urls = []; - let pages: Record | null = {}; + const pages: Record | null = {}; const baseUrl = new URL(apiUrl); const treeUrl = new URL(`/rest/tree/${projectId}`, baseUrl); From 006df5b9fba12141b5bb9a7986490c92f68b3e32 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 16 Nov 2022 11:37:50 +0100 Subject: [PATCH 3/5] version++ --- packages/http-client/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/http-client/package.json b/packages/http-client/package.json index 38bf1cc554..31ecf5644d 100644 --- a/packages/http-client/package.json +++ b/packages/http-client/package.json @@ -1,6 +1,6 @@ { "name": "@webstudio-is/http-client", - "version": "0.1.1", + "version": "0.2.0", "description": "Webstudio HTTP Client", "author": "Webstudio ", "homepage": "https://webstudio.is", From 0f026619760503ca3b666a825894431773891aaa Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 17 Nov 2022 10:55:58 +0100 Subject: [PATCH 4/5] Update packages/http-client/src/index.ts Co-authored-by: Oleg Isonen --- packages/http-client/src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/http-client/src/index.ts b/packages/http-client/src/index.ts index 76de1ca3f6..d2ac5f4151 100644 --- a/packages/http-client/src/index.ts +++ b/packages/http-client/src/index.ts @@ -51,8 +51,8 @@ export const loadProject = async ({ }; } - const pagesData = await Promise.all([ - ...urls.map(async (url) => { + const pagesData = await Promise.all( + urls.map(async (url) => { const res = await fetch(url.url); if (res.ok) { return { path: url.path, type: url.type, data: await res.json() }; From 1048ab3aa9d0d3391488ad31dc35ea89f902c281 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Thu, 17 Nov 2022 10:57:49 +0100 Subject: [PATCH 5/5] fix issues --- packages/http-client/src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/http-client/src/index.ts b/packages/http-client/src/index.ts index d2ac5f4151..f757b20303 100644 --- a/packages/http-client/src/index.ts +++ b/packages/http-client/src/index.ts @@ -1,5 +1,5 @@ import fetch from "isomorphic-fetch"; -import type { Includes, Project } from "./index.d"; +import type { Includes, Project, Page } from "./index.d"; export const loadProject = async ({ apiUrl, @@ -57,13 +57,13 @@ export const loadProject = async ({ if (res.ok) { return { path: url.path, type: url.type, data: await res.json() }; } - }), - ]); + }) + ); if (Object.keys(pagesData).length > 0) { for (const page of pagesData) { if (page && page.path && page.type && page.data) { pages[page.path] = { - ...(pages[page.path] as Record), + ...(pages[page.path] as Record), [page.type]: page.data, breakpoints, };