@webstudio-is/http-client 0.1.0 → 0.2.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,7 @@
1
1
  import type { Includes, Project } from "./index.d";
2
- export declare const loadProject: ({ projectId, host, include, }: {
2
+ export declare const loadProject: ({ apiUrl, projectId, include, }: {
3
+ apiUrl: string;
3
4
  projectId: string;
4
- host?: string | undefined;
5
5
  include?: Includes<boolean> | undefined;
6
6
  }) => Promise<Project>;
7
7
  //# 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,EAAE,MAAM,WAAW,CAAC;AAInD,eAAO,MAAM,WAAW;eAKX,MAAM;;;MAGf,QAAQ,OAAO,CAgBlB,CAAC"}
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"}
package/lib/index.js CHANGED
@@ -5,21 +5,67 @@ 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 protocol = process.env.NODE_ENV === "production" ? "https" : "http";
9
- const loadProject = async ({ projectId, host = process.env.DESIGNER_HOST || "localhost:3000", include = { tree: true, props: true, breakpoints: true }, }) => {
10
- const baseUrl = new URL("/", `${protocol}://${host}`);
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);
11
15
  const treeUrl = new URL(`/rest/tree/${projectId}`, baseUrl);
12
16
  const propsUrl = new URL(`/rest/props/${projectId}`, baseUrl);
13
17
  const breakpointsUrl = new URL(`/rest/breakpoints/${projectId}`, baseUrl);
14
- const [tree, props, breakpoints] = await Promise.all([
18
+ const pagesUrl = new URL(`/rest/pages/${projectId}`, baseUrl);
19
+ const [tree, props, breakpoints, listOfPages] = await Promise.all([
15
20
  include.tree && (await (0, isomorphic_fetch_1.default)(treeUrl)).json(),
16
21
  include.props && (await (0, isomorphic_fetch_1.default)(propsUrl)).json(),
17
22
  include.breakpoints && (await (0, isomorphic_fetch_1.default)(breakpointsUrl)).json(),
23
+ include.pages && (await (0, isomorphic_fetch_1.default)(pagesUrl)).json(),
18
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
+ });
37
+ }
38
+ if (urls.length === 0) {
39
+ return {
40
+ tree,
41
+ props,
42
+ breakpoints,
43
+ pages,
44
+ };
45
+ }
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
+ }
62
+ }
63
+ }
19
64
  return {
20
65
  tree,
21
66
  props,
22
67
  breakpoints,
68
+ pages: Object.keys(pages).length > 0 ? pages : null,
23
69
  };
24
70
  };
25
71
  exports.loadProject = loadProject;
package/lib/index.test.js CHANGED
@@ -1,24 +1,38 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const index_1 = require("./index");
4
- const existingProject = "ab3619d3-831f-46f8-abb6-609558cffd99";
5
- const notPublishedProject = "7ec397c6-b3d0-4967-9073-9d83623fcf8e";
6
- const nonExistingProject = "1";
4
+ const existingProjectId = "398d3918-9b00-4a43-8ab6-de4e151ae98a";
5
+ const notPublishedProjectId = "7ec397c6-b3d0-4967-9073-9d83623fcf8e";
6
+ const apiUrl = "http://localhost:3000";
7
7
  describe("getProjectDetails", () => {
8
- test("loads existing project", async () => {
9
- const response = await (0, index_1.loadProject)({ projectId: existingProject });
10
- expect(response.tree.id).toBeTruthy();
8
+ test("include pages", async () => {
9
+ const response = await (0, index_1.loadProject)({
10
+ apiUrl,
11
+ projectId: existingProjectId,
12
+ include: { tree: true, props: true, breakpoints: true, pages: true },
13
+ });
14
+ expect(response.pages).toBeTruthy();
11
15
  });
12
- test("loads non-existing project", async () => {
16
+ test("does not include pages", async () => {
13
17
  const response = await (0, index_1.loadProject)({
14
- projectId: nonExistingProject,
18
+ apiUrl,
19
+ projectId: notPublishedProjectId,
20
+ include: { tree: true, props: true, breakpoints: true, pages: true },
15
21
  });
16
- expect(response.tree.errors).toBe("Project required");
22
+ expect(response.pages).toBeNull();
23
+ });
24
+ test("loads existing project", async () => {
25
+ const response = await (0, index_1.loadProject)({
26
+ apiUrl,
27
+ projectId: existingProjectId,
28
+ });
29
+ expect(response.tree.id).toBeTruthy();
17
30
  });
18
31
  test("loads not published project", async () => {
19
32
  const response = await (0, index_1.loadProject)({
20
- projectId: notPublishedProject,
33
+ apiUrl,
34
+ projectId: notPublishedProjectId,
21
35
  });
22
- expect(response.tree.errors).toBe("Site needs to be published, production tree ID is null.");
36
+ expect(response.tree.errors).toBe(`Project ${notPublishedProjectId} needs to be published first`);
23
37
  });
24
38
  });
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@webstudio-is/http-client",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Webstudio HTTP Client",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
7
7
  "scripts": {
8
- "dev": "tsup src/index.ts --format esm,cjs --watch --out-dir=lib",
8
+ "dev": "tsup --watch",
9
9
  "test": "jest",
10
- "build": "rm -fr lib && tsc",
10
+ "build": "rm -fr lib tsconfig.tsbuildinfo && tsc",
11
11
  "typecheck": "tsc --noEmit",
12
12
  "lint": "eslint ./src --ext .ts,.tsx --max-warnings 0",
13
13
  "checks": "yarn typecheck && yarn lint"
@@ -32,5 +32,13 @@
32
32
  "!*.test.*"
33
33
  ],
34
34
  "license": "MIT",
35
- "private": false
35
+ "private": false,
36
+ "sideEffects": false,
37
+ "tsup": {
38
+ "entry": [
39
+ "src/index.ts"
40
+ ],
41
+ "format": "cjs",
42
+ "outDir": "lib"
43
+ }
36
44
  }