mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-07 04:23:40 +09:00
fix: type errors
This commit is contained in:
1 parent
07e81efeef
commit
aa3aea369e
3 files changed
+106
-103
No files matched your search
Vendored
-2
@@ -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];
|
||||
|
||||
@@ -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`
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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(),
|
||||
};
|
||||
}
|
||||
})
|
||||
);
|
||||
const breakpoints = data.find(
|
||||
(d) => d?.type === "breakpoints" && d.path === "/"
|
||||
);
|
||||
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);
|
||||
|
||||
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 || {},
|
||||
};
|
||||
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 || {},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
...gatheredPages,
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
}
|
||||
return "Unknown error";
|
||||
}
|
||||
return {
|
||||
...gatheredPages,
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user