@webstudio-is/http-client 0.174.0 → 0.179.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.js CHANGED
@@ -1,15 +1,12 @@
1
1
  // src/index.ts
2
- var loadProjectDataById = async (params) => {
3
- const result = await getLatestBuildUsingProjectId(params);
4
- if (result.buildId === null) {
5
- throw new Error(`The project is not published yet`);
6
- }
7
- const url = new URL(params.origin);
8
- url.pathname = `/rest/build/${result.buildId}`;
9
- if (params.authToken) {
10
- url.searchParams.append("authToken", params.authToken);
11
- }
12
- const response = await fetch(url.href);
2
+ var getLatestBuildUsingProjectId = async (params) => {
3
+ const { origin, projectId, authToken } = params;
4
+ const { sourceOrigin } = parseBuilderUrl(origin);
5
+ const url = new URL(sourceOrigin);
6
+ url.pathname = `/rest/buildId/${projectId}`;
7
+ const headers = new Headers();
8
+ headers.set("x-auth-token", authToken);
9
+ const response = await fetch(url.href, { headers });
13
10
  if (response.ok) {
14
11
  return await response.json();
15
12
  }
@@ -17,12 +14,17 @@ var loadProjectDataById = async (params) => {
17
14
  throw new Error(message.slice(0, 1e3));
18
15
  };
19
16
  var loadProjectDataByBuildId = async (params) => {
20
- const url = new URL(params.origin);
17
+ const { sourceOrigin } = parseBuilderUrl(params.origin);
18
+ const url = new URL(sourceOrigin);
21
19
  url.pathname = `/rest/build/${params.buildId}`;
20
+ const headers = new Headers();
21
+ if ("seviceToken" in params) {
22
+ headers.set("Authorization", params.seviceToken);
23
+ } else {
24
+ headers.set("x-auth-token", params.authToken);
25
+ }
22
26
  const response = await fetch(url.href, {
23
- headers: {
24
- Authorization: params.authToken
25
- }
27
+ headers
26
28
  });
27
29
  if (response.ok) {
28
30
  return await response.json();
@@ -30,22 +32,49 @@ var loadProjectDataByBuildId = async (params) => {
30
32
  const message = await response.text();
31
33
  throw new Error(message.slice(0, 1e3));
32
34
  };
33
- var getLatestBuildUsingProjectId = async (params) => {
34
- const { origin, projectId, authToken } = params;
35
- const url = new URL(origin);
36
- url.pathname = `/rest/buildId/${projectId}`;
37
- if (authToken) {
38
- url.searchParams.append("authToken", authToken);
35
+ var loadProjectDataByProjectId = async (params) => {
36
+ const result = await getLatestBuildUsingProjectId(params);
37
+ if (result.buildId === null) {
38
+ throw new Error(`The project is not published yet`);
39
39
  }
40
- const response = await fetch(url.href);
41
- if (response.ok) {
42
- return await response.json();
40
+ return await loadProjectDataByBuildId({
41
+ buildId: result.buildId,
42
+ origin: params.origin,
43
+ authToken: params.authToken
44
+ });
45
+ };
46
+ var buildProjectDomainPrefix = "p-";
47
+ var parseBuilderUrl = (urlStr) => {
48
+ const url = new URL(urlStr);
49
+ const fragments = url.host.split(".");
50
+ const re = /^(?<prefix>[a-z-]+)(?<uuid>[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})(-dot-(?<branch>.*))?/;
51
+ const match = fragments[0].match(re);
52
+ const prefix = match?.groups?.prefix;
53
+ const projectId = match?.groups?.uuid;
54
+ const branch = match?.groups?.branch;
55
+ if (prefix !== buildProjectDomainPrefix) {
56
+ return {
57
+ projectId: void 0,
58
+ sourceOrigin: url.origin
59
+ };
43
60
  }
44
- const message = await response.text();
45
- throw new Error(message.slice(0, 1e3));
61
+ if (projectId === void 0) {
62
+ return {
63
+ projectId: void 0,
64
+ sourceOrigin: url.origin
65
+ };
66
+ }
67
+ fragments[0] = fragments[0].replace(re, branch ?? "");
68
+ const sourceUrl = new URL(url.origin);
69
+ sourceUrl.protocol = "https";
70
+ sourceUrl.host = fragments.filter(Boolean).join(".");
71
+ return {
72
+ projectId,
73
+ sourceOrigin: sourceUrl.origin
74
+ };
46
75
  };
47
76
  export {
48
- getLatestBuildUsingProjectId,
49
77
  loadProjectDataByBuildId,
50
- loadProjectDataById
78
+ loadProjectDataByProjectId,
79
+ parseBuilderUrl
51
80
  };
@@ -21,20 +21,23 @@ export type Data = {
21
21
  };
22
22
  assets: Array<Asset>;
23
23
  };
24
- export declare const loadProjectDataById: (params: {
25
- projectId: string;
26
- origin: string;
27
- authToken?: string;
28
- }) => Promise<Data>;
29
24
  export declare const loadProjectDataByBuildId: (params: {
30
25
  buildId: string;
31
26
  origin: string;
27
+ } & ({
28
+ seviceToken: string;
29
+ } | {
32
30
  authToken: string;
33
- }) => Promise<Data>;
34
- export declare const getLatestBuildUsingProjectId: (params: {
31
+ })) => Promise<Data>;
32
+ export declare const loadProjectDataByProjectId: (params: {
35
33
  projectId: string;
36
34
  origin: string;
37
- authToken?: string;
38
- }) => Promise<{
39
- buildId: string | null;
40
- }>;
35
+ authToken: string;
36
+ }) => Promise<Data>;
37
+ export declare const parseBuilderUrl: (urlStr: string) => {
38
+ projectId: undefined;
39
+ sourceOrigin: string;
40
+ } | {
41
+ projectId: string;
42
+ sourceOrigin: string;
43
+ };
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,16 +1,18 @@
1
1
  {
2
2
  "name": "@webstudio-is/http-client",
3
- "version": "0.174.0",
3
+ "version": "0.179.0",
4
4
  "description": "Webstudio HTTP Client",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
7
7
  "type": "module",
8
8
  "dependencies": {
9
- "@webstudio-is/sdk": "0.174.0"
9
+ "@webstudio-is/sdk": "0.179.0"
10
10
  },
11
11
  "devDependencies": {
12
+ "@jest/globals": "^29.7.0",
12
13
  "typescript": "5.5.2",
13
- "@webstudio-is/tsconfig": "1.0.7"
14
+ "@webstudio-is/tsconfig": "1.0.7",
15
+ "@webstudio-is/jest-config": "1.0.7"
14
16
  },
15
17
  "exports": {
16
18
  "webstudio": "./src/index.ts",
@@ -18,7 +20,8 @@
18
20
  "import": "./lib/index.js"
19
21
  },
20
22
  "files": [
21
- "lib/*"
23
+ "lib/*",
24
+ "!*.{test,stories}.*"
22
25
  ],
23
26
  "license": "AGPL-3.0-or-later",
24
27
  "private": false,
@@ -26,6 +29,7 @@
26
29
  "scripts": {
27
30
  "build": "rm -rf lib && esbuild src/index.ts --outdir=lib --bundle --format=esm --packages=external",
28
31
  "dts": "tsc --project tsconfig.dts.json",
29
- "typecheck": "tsc"
32
+ "typecheck": "tsc",
33
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest"
30
34
  }
31
35
  }