@webstudio-is/http-client 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -1,7 +1,22 @@
1
- import type { Includes, Project } from "./index.d";
2
- export declare const loadProject: ({ apiUrl, projectId, include, }: {
1
+ import type { BaseInstance, UserProp } from "@webstudio-is/react-sdk";
2
+ import type { Breakpoint } from "@webstudio-is/css-data";
3
+ export declare type Page = {
4
+ id: string;
5
+ root: [string, string];
6
+ };
7
+ export declare type Project = {
8
+ pages: {
9
+ [path: string]: {
10
+ page: Page;
11
+ tree: BaseInstance;
12
+ props: Array<UserProp> | [];
13
+ breakpoints: Array<Breakpoint> | null;
14
+ css: string;
15
+ };
16
+ };
17
+ };
18
+ export declare const loadProject: ({ apiUrl, projectId, }: {
3
19
  apiUrl: string;
4
20
  projectId: string;
5
- include?: Includes<boolean> | undefined;
6
- }) => Promise<Project>;
21
+ }) => Promise<Project | string>;
7
22
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAQ,MAAM,WAAW,CAAC;AAEzD,eAAO,MAAM,WAAW;YAKd,MAAM;eACH,MAAM;;MAEf,QAAQ,OAAO,CAoElB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACtE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEzD,oBAAY,IAAI,GAAG;IACjB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACxB,CAAC;AACF,oBAAY,OAAO,GAAG;IACpB,KAAK,EAAE;QACL,CAAC,IAAI,EAAE,MAAM,GAAG;YACd,IAAI,EAAE,IAAI,CAAC;YACX,IAAI,EAAE,YAAY,CAAC;YACnB,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC;YAC5B,WAAW,EAAE,KAAK,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;YACtC,GAAG,EAAE,MAAM,CAAC;SACb,CAAC;KACH,CAAC;CACH,CAAC;AAEF,eAAO,MAAM,WAAW;YAId,MAAM;eACH,MAAM;MACf,QAAQ,OAAO,GAAG,MAAM,CAmB3B,CAAC"}
package/lib/index.js CHANGED
@@ -5,67 +5,25 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.loadProject = void 0;
7
7
  const isomorphic_fetch_1 = __importDefault(require("isomorphic-fetch"));
8
- const loadProject = async ({ apiUrl, projectId, include = { tree: true, props: true, breakpoints: true, pages: false }, }) => {
9
- if (apiUrl === undefined) {
10
- throw new Error("Webstudio API URL is required.");
11
- }
12
- const urls = [];
13
- const pages = {};
14
- const baseUrl = new URL(apiUrl);
15
- const treeUrl = new URL(`/rest/tree/${projectId}`, baseUrl);
16
- const propsUrl = new URL(`/rest/props/${projectId}`, baseUrl);
17
- const breakpointsUrl = new URL(`/rest/breakpoints/${projectId}`, baseUrl);
18
- const pagesUrl = new URL(`/rest/pages/${projectId}`, baseUrl);
19
- const [tree, props, breakpoints, listOfPages] = await Promise.all([
20
- include.tree && (await (0, isomorphic_fetch_1.default)(treeUrl)).json(),
21
- include.props && (await (0, isomorphic_fetch_1.default)(propsUrl)).json(),
22
- include.breakpoints && (await (0, isomorphic_fetch_1.default)(breakpointsUrl)).json(),
23
- include.pages && (await (0, isomorphic_fetch_1.default)(pagesUrl)).json(),
24
- ]);
25
- if (include.pages && listOfPages.pages && listOfPages.pages.length > 0) {
26
- for (const page of listOfPages.pages) {
27
- urls.push({
28
- url: new URL(`/rest/tree/${projectId}/${page.id}`, baseUrl),
29
- path: page.path,
30
- type: "tree",
31
- });
32
- urls.push({
33
- url: new URL(`/rest/props/${projectId}/${page.id}`, baseUrl),
34
- path: page.path,
35
- type: "props",
36
- });
8
+ const loadProject = async ({ apiUrl, projectId, }) => {
9
+ try {
10
+ if (apiUrl === undefined) {
11
+ throw new Error("Webstudio API URL is required.");
37
12
  }
38
- if (urls.length === 0) {
39
- return {
40
- tree,
41
- props,
42
- breakpoints,
43
- pages,
44
- };
13
+ const baseUrl = new URL(apiUrl);
14
+ const projectUrl = new URL(`/rest/project/${projectId}`, baseUrl);
15
+ const projectResponse = await (0, isomorphic_fetch_1.default)(projectUrl);
16
+ const project = await projectResponse.json();
17
+ if (!projectResponse.ok) {
18
+ throw new Error(project);
45
19
  }
46
- const pagesData = await Promise.all(urls.map(async (url) => {
47
- const res = await (0, isomorphic_fetch_1.default)(url.url);
48
- if (res.ok) {
49
- return { path: url.path, type: url.type, data: await res.json() };
50
- }
51
- }));
52
- if (Object.keys(pagesData).length > 0) {
53
- for (const page of pagesData) {
54
- if (page && page.path && page.type && page.data) {
55
- pages[page.path] = {
56
- ...pages[page.path],
57
- [page.type]: page.data,
58
- breakpoints,
59
- };
60
- }
61
- }
20
+ return project;
21
+ }
22
+ catch (error) {
23
+ if (error instanceof Error) {
24
+ return error.message;
62
25
  }
26
+ throw error;
63
27
  }
64
- return {
65
- tree,
66
- props,
67
- breakpoints,
68
- pages: Object.keys(pages).length > 0 ? pages : null,
69
- };
70
28
  };
71
29
  exports.loadProject = loadProject;
package/lib/index.test.js CHANGED
@@ -1,38 +1,50 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const index_1 = require("./index");
4
- const existingProjectId = "398d3918-9b00-4a43-8ab6-de4e151ae98a";
4
+ const existingProjectId = "675e8af3-48fa-4b18-9ebf-fd2b128865e2";
5
5
  const notPublishedProjectId = "7ec397c6-b3d0-4967-9073-9d83623fcf8e";
6
+ const onlyHomeProjectId = "36d6c16f-04a0-45d4-ab1d-aa0ab61eb5b6";
7
+ const morePagesProjectId = existingProjectId;
6
8
  const apiUrl = "http://localhost:3000";
7
9
  describe("getProjectDetails", () => {
8
10
  test("include pages", async () => {
9
11
  const response = await (0, index_1.loadProject)({
10
12
  apiUrl,
11
- projectId: existingProjectId,
12
- include: { tree: true, props: true, breakpoints: true, pages: true },
13
+ projectId: morePagesProjectId,
13
14
  });
14
- expect(response.pages).toBeTruthy();
15
+ if (response instanceof Error) {
16
+ throw response;
17
+ }
18
+ if (typeof response === "object") {
19
+ return expect(Object.keys(response.pages).length > 1).toBeTruthy();
20
+ }
21
+ throw new Error("Unexpected response");
15
22
  });
16
23
  test("does not include pages", async () => {
17
24
  const response = await (0, index_1.loadProject)({
18
25
  apiUrl,
19
- projectId: notPublishedProjectId,
20
- include: { tree: true, props: true, breakpoints: true, pages: true },
26
+ projectId: onlyHomeProjectId,
21
27
  });
22
- expect(response.pages).toBeNull();
28
+ if (response instanceof Error) {
29
+ throw response;
30
+ }
31
+ if (typeof response === "object") {
32
+ return expect(Object.keys(response.pages).length === 1).toBeTruthy();
33
+ }
34
+ throw new Error("Unexpected response");
23
35
  });
24
36
  test("loads existing project", async () => {
25
37
  const response = await (0, index_1.loadProject)({
26
38
  apiUrl,
27
39
  projectId: existingProjectId,
28
40
  });
29
- expect(response.tree.id).toBeTruthy();
41
+ expect(response).toBeTruthy();
30
42
  });
31
43
  test("loads not published project", async () => {
32
44
  const response = await (0, index_1.loadProject)({
33
45
  apiUrl,
34
46
  projectId: notPublishedProjectId,
35
47
  });
36
- expect(response.tree.errors).toBe(`Project ${notPublishedProjectId} needs to be published first`);
48
+ expect(response.toString()).toBe(`Project ${notPublishedProjectId} not found or not published yet. Please contact us to get help.`);
37
49
  });
38
50
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/http-client",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Webstudio HTTP Client",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",