@webstudio-is/http-client 0.1.1 → 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.
@@ -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;AAEnD,eAAO,MAAM,WAAW;YAKd,MAAM;eACH,MAAM;;MAEf,QAAQ,OAAO,CAmBlB,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,23 +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 loadProject = async ({ apiUrl, projectId, include = { tree: true, props: true, breakpoints: true }, }) => {
8
+ const loadProject = async ({ apiUrl, projectId, include = { tree: true, props: true, breakpoints: true, pages: false }, }) => {
9
9
  if (apiUrl === undefined) {
10
10
  throw new Error("Webstudio API URL is required.");
11
11
  }
12
- const baseUrl = new URL(`${apiUrl}`);
12
+ const urls = [];
13
+ const pages = {};
14
+ const baseUrl = new URL(apiUrl);
13
15
  const treeUrl = new URL(`/rest/tree/${projectId}`, baseUrl);
14
16
  const propsUrl = new URL(`/rest/props/${projectId}`, baseUrl);
15
17
  const breakpointsUrl = new URL(`/rest/breakpoints/${projectId}`, baseUrl);
16
- 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([
17
20
  include.tree && (await (0, isomorphic_fetch_1.default)(treeUrl)).json(),
18
21
  include.props && (await (0, isomorphic_fetch_1.default)(propsUrl)).json(),
19
22
  include.breakpoints && (await (0, isomorphic_fetch_1.default)(breakpointsUrl)).json(),
23
+ include.pages && (await (0, isomorphic_fetch_1.default)(pagesUrl)).json(),
20
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
+ }
21
64
  return {
22
65
  tree,
23
66
  props,
24
67
  breakpoints,
68
+ pages: Object.keys(pages).length > 0 ? pages : null,
25
69
  };
26
70
  };
27
71
  exports.loadProject = loadProject;
package/lib/index.test.js CHANGED
@@ -1,10 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const index_1 = require("./index");
4
- const existingProjectId = "ab3619d3-831f-46f8-abb6-609558cffd99";
4
+ const existingProjectId = "398d3918-9b00-4a43-8ab6-de4e151ae98a";
5
5
  const notPublishedProjectId = "7ec397c6-b3d0-4967-9073-9d83623fcf8e";
6
6
  const apiUrl = "http://localhost:3000";
7
7
  describe("getProjectDetails", () => {
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();
15
+ });
16
+ test("does not include pages", async () => {
17
+ const response = await (0, index_1.loadProject)({
18
+ apiUrl,
19
+ projectId: notPublishedProjectId,
20
+ include: { tree: true, props: true, breakpoints: true, pages: true },
21
+ });
22
+ expect(response.pages).toBeNull();
23
+ });
8
24
  test("loads existing project", async () => {
9
25
  const response = await (0, index_1.loadProject)({
10
26
  apiUrl,
@@ -17,6 +33,6 @@ describe("getProjectDetails", () => {
17
33
  apiUrl,
18
34
  projectId: notPublishedProjectId,
19
35
  });
20
- expect(response.tree.errors).toBe("Project 7ec397c6-b3d0-4967-9073-9d83623fcf8e needs to be published first");
36
+ expect(response.tree.errors).toBe(`Project ${notPublishedProjectId} needs to be published first`);
21
37
  });
22
38
  });
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@webstudio-is/http-client",
3
- "version": "0.1.1",
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
  }