@webstudio-is/http-client 0.173.0 → 0.175.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,5 +1,5 @@
1
1
  // src/index.ts
2
- var loadProjectDataById = async (params) => {
2
+ var loadProjectDataByProjectId = async (params) => {
3
3
  const result = await getLatestBuildUsingProjectId(params);
4
4
  if (result.buildId === null) {
5
5
  throw new Error(`The project is not published yet`);
@@ -19,10 +19,14 @@ var loadProjectDataById = async (params) => {
19
19
  var loadProjectDataByBuildId = async (params) => {
20
20
  const url = new URL(params.origin);
21
21
  url.pathname = `/rest/build/${params.buildId}`;
22
+ const headers = {};
23
+ if ("seviceToken" in params) {
24
+ headers.Authorization = params.seviceToken;
25
+ } else {
26
+ headers["x-auth-token"] = params.authToken;
27
+ }
22
28
  const response = await fetch(url.href, {
23
- headers: {
24
- Authorization: params.authToken
25
- }
29
+ headers
26
30
  });
27
31
  if (response.ok) {
28
32
  return await response.json();
@@ -44,8 +48,39 @@ var getLatestBuildUsingProjectId = async (params) => {
44
48
  const message = await response.text();
45
49
  throw new Error(message.slice(0, 1e3));
46
50
  };
51
+ var buildProjectDomainPrefix = "p-";
52
+ var parseBuilderUrl = (urlStr) => {
53
+ const url = new URL(urlStr);
54
+ const fragments = url.host.split(".");
55
+ 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>.*))?/;
56
+ const match = fragments[0].match(re);
57
+ const prefix = match?.groups?.prefix;
58
+ const projectId = match?.groups?.uuid;
59
+ const branch = match?.groups?.branch;
60
+ if (prefix !== buildProjectDomainPrefix) {
61
+ return {
62
+ projectId: void 0,
63
+ sourceOrigin: url.origin
64
+ };
65
+ }
66
+ if (projectId === void 0) {
67
+ return {
68
+ projectId: void 0,
69
+ sourceOrigin: url.origin
70
+ };
71
+ }
72
+ fragments[0] = fragments[0].replace(re, branch ?? "");
73
+ const sourceUrl = new URL(url.origin);
74
+ sourceUrl.protocol = "https";
75
+ sourceUrl.host = fragments.filter(Boolean).join(".");
76
+ return {
77
+ projectId,
78
+ sourceOrigin: sourceUrl.origin
79
+ };
80
+ };
47
81
  export {
48
82
  getLatestBuildUsingProjectId,
49
83
  loadProjectDataByBuildId,
50
- loadProjectDataById
84
+ loadProjectDataByProjectId,
85
+ parseBuilderUrl
51
86
  };
@@ -21,7 +21,7 @@ export type Data = {
21
21
  };
22
22
  assets: Array<Asset>;
23
23
  };
24
- export declare const loadProjectDataById: (params: {
24
+ export declare const loadProjectDataByProjectId: (params: {
25
25
  projectId: string;
26
26
  origin: string;
27
27
  authToken?: string;
@@ -29,8 +29,11 @@ export declare const loadProjectDataById: (params: {
29
29
  export declare const loadProjectDataByBuildId: (params: {
30
30
  buildId: string;
31
31
  origin: string;
32
+ } & ({
33
+ seviceToken: string;
34
+ } | {
32
35
  authToken: string;
33
- }) => Promise<Data>;
36
+ })) => Promise<Data>;
34
37
  export declare const getLatestBuildUsingProjectId: (params: {
35
38
  projectId: string;
36
39
  origin: string;
@@ -38,3 +41,10 @@ export declare const getLatestBuildUsingProjectId: (params: {
38
41
  }) => Promise<{
39
42
  buildId: string | null;
40
43
  }>;
44
+ export declare const parseBuilderUrl: (urlStr: string) => {
45
+ projectId: undefined;
46
+ sourceOrigin: string;
47
+ } | {
48
+ projectId: string;
49
+ sourceOrigin: string;
50
+ };
@@ -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.173.0",
3
+ "version": "0.175.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.173.0"
9
+ "@webstudio-is/sdk": "0.175.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
  }