@webstudio-is/http-client 0.83.0 → 0.85.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/cjs/index.js CHANGED
@@ -18,22 +18,38 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var src_exports = {};
20
20
  __export(src_exports, {
21
- loadProject: () => loadProject
21
+ getLatestBuildUsingProjectId: () => getLatestBuildUsingProjectId,
22
+ loadProjectDataById: () => loadProjectDataById
22
23
  });
23
24
  module.exports = __toCommonJS(src_exports);
24
- const loadProject = async ({
25
- apiUrl,
26
- projectId
27
- }) => {
28
- if (apiUrl === void 0) {
29
- throw new Error("Webstudio API URL is required.");
25
+ const loadProjectDataById = async (params) => {
26
+ const result = await getLatestBuildUsingProjectId(params);
27
+ if (result === null) {
28
+ throw new Error(``);
30
29
  }
31
- const baseUrl = new URL(apiUrl);
32
- const projectUrl = new URL(`/rest/project/${projectId}`, baseUrl);
33
- const projectResponse = await fetch(projectUrl);
34
- if (projectResponse.ok) {
35
- return await projectResponse.json();
30
+ const url = new URL(params.host);
31
+ url.pathname = `/rest/build/${result.buildId}`;
32
+ if (params.authToken) {
33
+ url.searchParams.append("authToken", params.authToken);
36
34
  }
37
- const message = await projectResponse.text();
35
+ const response = await fetch(url.href);
36
+ if (response.ok) {
37
+ return await response.json();
38
+ }
39
+ const message = await response.text();
40
+ throw new Error(message.slice(0, 1e3));
41
+ };
42
+ const getLatestBuildUsingProjectId = async (params) => {
43
+ const { host, projectId, authToken } = params;
44
+ const url = new URL(host);
45
+ url.pathname = `/rest/buildId/${projectId}`;
46
+ if (authToken) {
47
+ url.searchParams.append("authToken", authToken);
48
+ }
49
+ const response = await fetch(url.href);
50
+ if (response.ok) {
51
+ return await response.json();
52
+ }
53
+ const message = await response.text();
38
54
  throw new Error(message.slice(0, 1e3));
39
55
  };
package/lib/index.js CHANGED
@@ -1,19 +1,35 @@
1
- const loadProject = async ({
2
- apiUrl,
3
- projectId
4
- }) => {
5
- if (apiUrl === void 0) {
6
- throw new Error("Webstudio API URL is required.");
1
+ const loadProjectDataById = async (params) => {
2
+ const result = await getLatestBuildUsingProjectId(params);
3
+ if (result === null) {
4
+ throw new Error(``);
7
5
  }
8
- const baseUrl = new URL(apiUrl);
9
- const projectUrl = new URL(`/rest/project/${projectId}`, baseUrl);
10
- const projectResponse = await fetch(projectUrl);
11
- if (projectResponse.ok) {
12
- return await projectResponse.json();
6
+ const url = new URL(params.host);
7
+ url.pathname = `/rest/build/${result.buildId}`;
8
+ if (params.authToken) {
9
+ url.searchParams.append("authToken", params.authToken);
13
10
  }
14
- const message = await projectResponse.text();
11
+ const response = await fetch(url.href);
12
+ if (response.ok) {
13
+ return await response.json();
14
+ }
15
+ const message = await response.text();
16
+ throw new Error(message.slice(0, 1e3));
17
+ };
18
+ const getLatestBuildUsingProjectId = async (params) => {
19
+ const { host, projectId, authToken } = params;
20
+ const url = new URL(host);
21
+ url.pathname = `/rest/buildId/${projectId}`;
22
+ if (authToken) {
23
+ url.searchParams.append("authToken", authToken);
24
+ }
25
+ const response = await fetch(url.href);
26
+ if (response.ok) {
27
+ return await response.json();
28
+ }
29
+ const message = await response.text();
15
30
  throw new Error(message.slice(0, 1e3));
16
31
  };
17
32
  export {
18
- loadProject
33
+ getLatestBuildUsingProjectId,
34
+ loadProjectDataById
19
35
  };
@@ -1,5 +1,15 @@
1
1
  import type { Data } from "@webstudio-is/react-sdk";
2
- export declare const loadProject: ({ apiUrl, projectId, }: {
3
- apiUrl: string;
2
+ interface DefaultArgs {
3
+ host: string;
4
+ authToken?: string;
5
+ }
6
+ type ResourceFactory<T, K> = (params: DefaultArgs & T) => Promise<K>;
7
+ export declare const loadProjectDataById: ResourceFactory<{
4
8
  projectId: string;
5
- }) => Promise<Array<Data>>;
9
+ }, Data>;
10
+ export declare const getLatestBuildUsingProjectId: ResourceFactory<{
11
+ projectId: string;
12
+ }, {
13
+ buildId: string;
14
+ }>;
15
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstudio-is/http-client",
3
- "version": "0.83.0",
3
+ "version": "0.85.0",
4
4
  "description": "Webstudio HTTP Client",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
@@ -9,11 +9,11 @@
9
9
  "@jest/globals": "^29.6.2",
10
10
  "jest": "^29.6.2",
11
11
  "typescript": "5.1.6",
12
- "@webstudio-is/jest-config": "^1.0.6",
13
- "@webstudio-is/prisma-client": "^0.83.0",
14
- "@webstudio-is/react-sdk": "^0.83.0",
12
+ "@webstudio-is/jest-config": "^1.0.7",
13
+ "@webstudio-is/prisma-client": "^0.85.0",
14
+ "@webstudio-is/react-sdk": "^0.85.0",
15
15
  "@webstudio-is/scripts": "^0.0.0",
16
- "@webstudio-is/tsconfig": "^1.0.6"
16
+ "@webstudio-is/tsconfig": "^1.0.7"
17
17
  },
18
18
  "exports": {
19
19
  "source": "./src/index.ts",
package/src/index.test.ts CHANGED
@@ -1,44 +1,45 @@
1
1
  import { describe, test, expect } from "@jest/globals";
2
- import { loadProject } from "./index";
2
+ import { loadProjectDataById } from "./index";
3
3
 
4
4
  const existingProjectId = "675e8af3-48fa-4b18-9ebf-fd2b128865e2";
5
5
  const notPublishedProjectId = "7ec397c6-b3d0-4967-9073-9d83623fcf8e";
6
6
  const onlyHomeProjectId = "36d6c16f-04a0-45d4-ab1d-aa0ab61eb5b6";
7
7
  const morePagesProjectId = existingProjectId;
8
8
 
9
- const apiUrl = "http://localhost:3000";
9
+ const host = "http://localhost:3000";
10
10
 
11
11
  describe("getProjectDetails", () => {
12
12
  test("include pages", async () => {
13
- const response = await loadProject({
14
- apiUrl,
13
+ const response = await loadProjectDataById({
14
+ host,
15
15
  projectId: morePagesProjectId,
16
16
  });
17
+
17
18
  if (typeof response === "object") {
18
- return expect(response.length > 1).toBeTruthy();
19
+ return expect(response.pages.length).toBeTruthy();
19
20
  }
20
21
  throw new Error("Unexpected response");
21
22
  });
22
23
  test("does not include pages", async () => {
23
- const response = await loadProject({
24
- apiUrl,
24
+ const response = await loadProjectDataById({
25
+ host,
25
26
  projectId: onlyHomeProjectId,
26
27
  });
27
28
  if (typeof response === "object") {
28
- return expect(response.length === 1).toBeTruthy();
29
+ return expect(response.pages.length === 1).toBeTruthy();
29
30
  }
30
31
  throw new Error("Unexpected response");
31
32
  });
32
33
  test("loads existing project", async () => {
33
- const response = await loadProject({
34
- apiUrl,
34
+ const response = await loadProjectDataById({
35
+ host,
35
36
  projectId: existingProjectId,
36
37
  });
37
38
  expect(response).toBeTruthy();
38
39
  });
39
40
  test("loads not published project", async () => {
40
- const response = await loadProject({
41
- apiUrl,
41
+ const response = await loadProjectDataById({
42
+ host,
42
43
  projectId: notPublishedProjectId,
43
44
  });
44
45
  if (response instanceof Error) {
package/src/index.ts CHANGED
@@ -1,25 +1,53 @@
1
1
  import type { Data } from "@webstudio-is/react-sdk";
2
2
 
3
- export const loadProject = async ({
4
- apiUrl,
5
- projectId,
6
- }: {
7
- apiUrl: string;
8
- projectId: string;
9
- }): Promise<Array<Data>> => {
10
- if (apiUrl === undefined) {
11
- throw new Error("Webstudio API URL is required.");
3
+ interface DefaultArgs {
4
+ host: string;
5
+ authToken?: string;
6
+ }
7
+
8
+ type ResourceFactory<T, K> = (params: DefaultArgs & T) => Promise<K>;
9
+
10
+ export const loadProjectDataById: ResourceFactory<
11
+ { projectId: string },
12
+ Data
13
+ > = async (params) => {
14
+ const result = await getLatestBuildUsingProjectId(params);
15
+ if (result === null) {
16
+ throw new Error(``);
17
+ }
18
+
19
+ const url = new URL(params.host);
20
+ url.pathname = `/rest/build/${result.buildId}`;
21
+ if (params.authToken) {
22
+ url.searchParams.append("authToken", params.authToken);
23
+ }
24
+
25
+ const response = await fetch(url.href);
26
+
27
+ if (response.ok) {
28
+ return await response.json();
29
+ }
30
+
31
+ const message = await response.text();
32
+ throw new Error(message.slice(0, 1000));
33
+ };
34
+
35
+ export const getLatestBuildUsingProjectId: ResourceFactory<
36
+ { projectId: string },
37
+ { buildId: string }
38
+ > = async (params) => {
39
+ const { host, projectId, authToken } = params;
40
+ const url = new URL(host);
41
+ url.pathname = `/rest/buildId/${projectId}`;
42
+ if (authToken) {
43
+ url.searchParams.append("authToken", authToken);
12
44
  }
13
- const baseUrl = new URL(apiUrl);
14
- const projectUrl = new URL(`/rest/project/${projectId}`, baseUrl);
15
- const projectResponse = await fetch(projectUrl);
45
+ const response = await fetch(url.href);
16
46
 
17
- if (projectResponse.ok) {
18
- return await projectResponse.json();
47
+ if (response.ok) {
48
+ return await response.json();
19
49
  }
20
50
 
21
- // In the case where the status code is not 2xx,
22
- // we don't know what to do with the response other than just show an error message.
23
- const message = await projectResponse.text();
51
+ const message = await response.text();
24
52
  throw new Error(message.slice(0, 1000));
25
53
  };