@webstudio-is/http-client 0.1.1 → 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 +19 -4
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +19 -17
- package/lib/index.test.js +31 -3
- package/package.json +12 -4
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,22 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
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
|
-
|
|
6
|
-
}) => Promise<Project>;
|
|
21
|
+
}) => Promise<Project | string>;
|
|
7
22
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -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;
|
|
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,23 +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,
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const loadProject = async ({ apiUrl, projectId, }) => {
|
|
9
|
+
try {
|
|
10
|
+
if (apiUrl === undefined) {
|
|
11
|
+
throw new Error("Webstudio API URL is required.");
|
|
12
|
+
}
|
|
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);
|
|
19
|
+
}
|
|
20
|
+
return project;
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
if (error instanceof Error) {
|
|
24
|
+
return error.message;
|
|
25
|
+
}
|
|
26
|
+
throw error;
|
|
11
27
|
}
|
|
12
|
-
const baseUrl = new URL(`${apiUrl}`);
|
|
13
|
-
const treeUrl = new URL(`/rest/tree/${projectId}`, baseUrl);
|
|
14
|
-
const propsUrl = new URL(`/rest/props/${projectId}`, baseUrl);
|
|
15
|
-
const breakpointsUrl = new URL(`/rest/breakpoints/${projectId}`, baseUrl);
|
|
16
|
-
const [tree, props, breakpoints] = await Promise.all([
|
|
17
|
-
include.tree && (await (0, isomorphic_fetch_1.default)(treeUrl)).json(),
|
|
18
|
-
include.props && (await (0, isomorphic_fetch_1.default)(propsUrl)).json(),
|
|
19
|
-
include.breakpoints && (await (0, isomorphic_fetch_1.default)(breakpointsUrl)).json(),
|
|
20
|
-
]);
|
|
21
|
-
return {
|
|
22
|
-
tree,
|
|
23
|
-
props,
|
|
24
|
-
breakpoints,
|
|
25
|
-
};
|
|
26
28
|
};
|
|
27
29
|
exports.loadProject = loadProject;
|
package/lib/index.test.js
CHANGED
|
@@ -1,22 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const index_1 = require("./index");
|
|
4
|
-
const existingProjectId = "
|
|
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", () => {
|
|
10
|
+
test("include pages", async () => {
|
|
11
|
+
const response = await (0, index_1.loadProject)({
|
|
12
|
+
apiUrl,
|
|
13
|
+
projectId: morePagesProjectId,
|
|
14
|
+
});
|
|
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");
|
|
22
|
+
});
|
|
23
|
+
test("does not include pages", async () => {
|
|
24
|
+
const response = await (0, index_1.loadProject)({
|
|
25
|
+
apiUrl,
|
|
26
|
+
projectId: onlyHomeProjectId,
|
|
27
|
+
});
|
|
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");
|
|
35
|
+
});
|
|
8
36
|
test("loads existing project", async () => {
|
|
9
37
|
const response = await (0, index_1.loadProject)({
|
|
10
38
|
apiUrl,
|
|
11
39
|
projectId: existingProjectId,
|
|
12
40
|
});
|
|
13
|
-
expect(response
|
|
41
|
+
expect(response).toBeTruthy();
|
|
14
42
|
});
|
|
15
43
|
test("loads not published project", async () => {
|
|
16
44
|
const response = await (0, index_1.loadProject)({
|
|
17
45
|
apiUrl,
|
|
18
46
|
projectId: notPublishedProjectId,
|
|
19
47
|
});
|
|
20
|
-
expect(response.
|
|
48
|
+
expect(response.toString()).toBe(`Project ${notPublishedProjectId} not found or not published yet. Please contact us to get help.`);
|
|
21
49
|
});
|
|
22
50
|
});
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webstudio-is/http-client",
|
|
3
|
-
"version": "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",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"dev": "tsup
|
|
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
|
}
|