@superblocksteam/sdk 0.0.21 → 0.0.23
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/dist/client.d.ts +4 -3
- package/dist/client.js +58 -12
- package/dist/sdk.d.ts +7 -3
- package/dist/sdk.js +6 -5
- package/package.json +6 -4
- package/src/client.ts +70 -6
- package/src/sdk.ts +18 -10
package/dist/client.d.ts
CHANGED
|
@@ -4,16 +4,17 @@ export interface UploadFile {
|
|
|
4
4
|
filename: string;
|
|
5
5
|
ContentType: string;
|
|
6
6
|
}
|
|
7
|
-
export declare function fetchApplication({ applicationId, token, superblocksBaseUrl, viewMode, }: {
|
|
7
|
+
export declare function fetchApplication({ applicationId, token, superblocksBaseUrl, viewMode, injectedHeaders, }: {
|
|
8
8
|
applicationId: string;
|
|
9
9
|
token: string;
|
|
10
10
|
superblocksBaseUrl: string;
|
|
11
11
|
viewMode: ViewMode;
|
|
12
|
+
injectedHeaders: Record<string, string>;
|
|
12
13
|
}): Promise<any>;
|
|
13
|
-
export declare function fetchApplications(token: string, superblocksBaseUrl: string): Promise<any>;
|
|
14
|
+
export declare function fetchApplications(token: string, superblocksBaseUrl: string, injectedHeaders?: Record<string, string>): Promise<any>;
|
|
14
15
|
export declare function fetchApi(apiId: string, token: string, superblocksBaseUrl: string, viewMode: ViewMode): Promise<any>;
|
|
15
16
|
export declare function fetchApis(token: string, superblocksBaseUrl: string): Promise<any>;
|
|
16
|
-
export declare function registerComponents(applicationId: string, componentConfigs: Record<string, any>, token: string, superblocksBaseUrl: string): Promise<any>;
|
|
17
|
+
export declare function registerComponents(applicationId: string, componentConfigs: Record<string, any>, token: string, superblocksBaseUrl: string, injectedHeaders?: Record<string, string>): Promise<any>;
|
|
17
18
|
export declare function uploadComponents({ applicationId, componentConfigs, files, token, superblocksBaseUrl, }: {
|
|
18
19
|
applicationId: string;
|
|
19
20
|
componentConfigs: Record<string, any>;
|
package/dist/client.js
CHANGED
|
@@ -6,11 +6,37 @@ const util_1 = require("@superblocksteam/util");
|
|
|
6
6
|
const util_2 = require("@superblocksteam/util");
|
|
7
7
|
const axios_1 = require("axios");
|
|
8
8
|
const FormData = require("form-data");
|
|
9
|
+
const lodash_1 = require("lodash");
|
|
9
10
|
const BASE_SERVER_URL = "api/v1";
|
|
10
11
|
const BASE_BUCKETEER_URL = "api/v1";
|
|
12
|
+
const SUPERBLOCKS_MAX_FILE_SIZE_MB = 10;
|
|
11
13
|
const CLI_VERSION_HEADER = "x-superblocks-cli-version";
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
const SUPERBLOCKS_URL_HEADER = "x-superblocks-url";
|
|
15
|
+
async function fetchApplication({ applicationId, token, superblocksBaseUrl, viewMode, injectedHeaders = {}, }) {
|
|
16
|
+
var _a, _b, _c, _d, _e, _f;
|
|
17
|
+
const serverURL = new URL(`api/v2/applications/${applicationId}?viewMode=${viewMode}`, superblocksBaseUrl);
|
|
18
|
+
let serverResponse;
|
|
19
|
+
try {
|
|
20
|
+
const config = {
|
|
21
|
+
method: "get",
|
|
22
|
+
url: serverURL.toString(),
|
|
23
|
+
headers: {
|
|
24
|
+
Authorization: "Bearer " + token,
|
|
25
|
+
...injectedHeaders,
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
serverResponse = await (0, axios_1.default)(config);
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
if (axios_1.default.isAxiosError(e) && ((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 404) {
|
|
32
|
+
throw new Error(`Application ${applicationId} was not found`);
|
|
33
|
+
}
|
|
34
|
+
throw new Error("Could not fetch application");
|
|
35
|
+
}
|
|
36
|
+
// if there are no custom components, just return here without trying to initialize them using bucketeer
|
|
37
|
+
if ((0, lodash_1.isEmpty)((_e = (_d = (_c = (_b = serverResponse === null || serverResponse === void 0 ? void 0 : serverResponse.data) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.application) === null || _d === void 0 ? void 0 : _d.settings) === null || _e === void 0 ? void 0 : _e.registeredComponents)) {
|
|
38
|
+
return serverResponse.data.data;
|
|
39
|
+
}
|
|
14
40
|
// TODO(jason) update this with commit ID once we enable fetching by commit
|
|
15
41
|
const commitId = "latest";
|
|
16
42
|
superblocksBaseUrl = superblocksBaseUrl.replace(/\/$/, "");
|
|
@@ -23,27 +49,29 @@ async function fetchApplication({ applicationId, token, superblocksBaseUrl, view
|
|
|
23
49
|
url: componentFileURL,
|
|
24
50
|
headers: {
|
|
25
51
|
Authorization: "Bearer " + token,
|
|
26
|
-
|
|
52
|
+
[SUPERBLOCKS_URL_HEADER]: superblocksBaseUrl,
|
|
53
|
+
...injectedHeaders,
|
|
27
54
|
},
|
|
28
55
|
};
|
|
29
56
|
const response = await (0, axios_1.default)(config);
|
|
30
57
|
return response.data;
|
|
31
58
|
}
|
|
32
59
|
catch (e) {
|
|
33
|
-
if (axios_1.default.isAxiosError(e) && ((
|
|
60
|
+
if (axios_1.default.isAxiosError(e) && ((_f = e.response) === null || _f === void 0 ? void 0 : _f.status) === 404) {
|
|
34
61
|
throw new Error(`Application ${applicationId} was not found`);
|
|
35
62
|
}
|
|
36
63
|
throw new Error("Could not fetch application");
|
|
37
64
|
}
|
|
38
65
|
}
|
|
39
66
|
exports.fetchApplication = fetchApplication;
|
|
40
|
-
async function fetchApplications(token, superblocksBaseUrl) {
|
|
67
|
+
async function fetchApplications(token, superblocksBaseUrl, injectedHeaders = {}) {
|
|
41
68
|
try {
|
|
42
69
|
const config = {
|
|
43
70
|
method: "get",
|
|
44
71
|
url: new URL(`/api/v2/applications`, superblocksBaseUrl).toString(),
|
|
45
72
|
headers: {
|
|
46
73
|
Authorization: "Bearer " + token,
|
|
74
|
+
...injectedHeaders,
|
|
47
75
|
},
|
|
48
76
|
};
|
|
49
77
|
const response = await (0, axios_1.default)(config);
|
|
@@ -92,7 +120,7 @@ async function fetchApis(token, superblocksBaseUrl) {
|
|
|
92
120
|
}
|
|
93
121
|
}
|
|
94
122
|
exports.fetchApis = fetchApis;
|
|
95
|
-
async function registerComponents(applicationId, componentConfigs, token, superblocksBaseUrl) {
|
|
123
|
+
async function registerComponents(applicationId, componentConfigs, token, superblocksBaseUrl, injectedHeaders) {
|
|
96
124
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
97
125
|
try {
|
|
98
126
|
const [rootSettings] = await (0, util_2.getSuperblocksMonorepoConfigJson)(true);
|
|
@@ -102,6 +130,7 @@ async function registerComponents(applicationId, componentConfigs, token, superb
|
|
|
102
130
|
headers: {
|
|
103
131
|
Authorization: "Bearer " + token,
|
|
104
132
|
[CLI_VERSION_HEADER]: rootSettings.metadata.cliVersion,
|
|
133
|
+
...injectedHeaders,
|
|
105
134
|
},
|
|
106
135
|
data: { components: componentConfigs },
|
|
107
136
|
};
|
|
@@ -118,7 +147,7 @@ async function registerComponents(applicationId, componentConfigs, token, superb
|
|
|
118
147
|
}
|
|
119
148
|
exports.registerComponents = registerComponents;
|
|
120
149
|
async function uploadComponents({ applicationId, componentConfigs, files, token, superblocksBaseUrl, }) {
|
|
121
|
-
var _a, _b, _c, _d, _e, _f;
|
|
150
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
122
151
|
superblocksBaseUrl = superblocksBaseUrl.replace(/\/$/, "");
|
|
123
152
|
const bucketeerBaseUrl = (0, util_1.getBucketeerUrlFromSuperblocksUrl)(superblocksBaseUrl);
|
|
124
153
|
// parse files to source and bundled files based on path
|
|
@@ -129,7 +158,8 @@ async function uploadComponents({ applicationId, componentConfigs, files, token,
|
|
|
129
158
|
}
|
|
130
159
|
return {
|
|
131
160
|
name: `file${ind + 1}`,
|
|
132
|
-
|
|
161
|
+
// win32 => posix filepath. Bucketeer does not accept windows style filepaths
|
|
162
|
+
filename: file.replace(/\\/g, "/"),
|
|
133
163
|
ContentType: contentType,
|
|
134
164
|
};
|
|
135
165
|
});
|
|
@@ -142,11 +172,22 @@ async function uploadComponents({ applicationId, componentConfigs, files, token,
|
|
|
142
172
|
buildFiles,
|
|
143
173
|
});
|
|
144
174
|
const postURL = new URL(`${BASE_BUCKETEER_URL}/components/upload`, bucketeerBaseUrl).toString();
|
|
175
|
+
let totalFileSizeMB = 0;
|
|
145
176
|
const formData = new FormData();
|
|
146
177
|
formData.append("format", formatJSON);
|
|
147
178
|
uploadFiles.forEach((uploadFile) => {
|
|
179
|
+
const uploadFileStats = fs.statSync(uploadFile.filename);
|
|
180
|
+
totalFileSizeMB += uploadFileStats.size / (1024 * 1024);
|
|
148
181
|
formData.append(uploadFile.name, fs.createReadStream(uploadFile.filename));
|
|
149
182
|
});
|
|
183
|
+
if (totalFileSizeMB > SUPERBLOCKS_MAX_FILE_SIZE_MB) {
|
|
184
|
+
throw new Error(`upload size exceeded maximum allowed size.
|
|
185
|
+
|
|
186
|
+
Current Size: ${totalFileSizeMB.toFixed(2)} MB
|
|
187
|
+
Max Allowed Size: ${SUPERBLOCKS_MAX_FILE_SIZE_MB.toFixed(0)} MB
|
|
188
|
+
|
|
189
|
+
You can reduce your component bundle size by uploading static assets to a separate service.`);
|
|
190
|
+
}
|
|
150
191
|
const formHeaders = formData.getHeaders();
|
|
151
192
|
try {
|
|
152
193
|
const [rootSettings] = await (0, util_2.getSuperblocksMonorepoConfigJson)(true);
|
|
@@ -154,7 +195,8 @@ async function uploadComponents({ applicationId, componentConfigs, files, token,
|
|
|
154
195
|
headers: {
|
|
155
196
|
Authorization: "Bearer " + token,
|
|
156
197
|
[CLI_VERSION_HEADER]: rootSettings.metadata.cliVersion,
|
|
157
|
-
|
|
198
|
+
[util_1.COMPONENT_EVENT_HEADER]: util_1.ComponentEvent.UPLOAD,
|
|
199
|
+
[SUPERBLOCKS_URL_HEADER]: superblocksBaseUrl,
|
|
158
200
|
...formHeaders,
|
|
159
201
|
},
|
|
160
202
|
};
|
|
@@ -162,10 +204,13 @@ async function uploadComponents({ applicationId, componentConfigs, files, token,
|
|
|
162
204
|
return response.data;
|
|
163
205
|
}
|
|
164
206
|
catch (e) {
|
|
165
|
-
if (e instanceof axios_1.AxiosError) {
|
|
166
|
-
|
|
207
|
+
if (e instanceof axios_1.AxiosError && ((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 413) {
|
|
208
|
+
throw new Error(`Could not upload application components, file size limit exceeded`);
|
|
209
|
+
}
|
|
210
|
+
else if (e instanceof axios_1.AxiosError) {
|
|
211
|
+
const message = (_e = (_d = (_c = (_b = e.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.responseMeta) === null || _d === void 0 ? void 0 : _d.message) !== null && _e !== void 0 ? _e : (_f = e.response) === null || _f === void 0 ? void 0 : _f.data;
|
|
167
212
|
throw new Error(`Could not upload application components
|
|
168
|
-
${message !== null && message !== void 0 ? message : (
|
|
213
|
+
${message !== null && message !== void 0 ? message : (_g = e.response) === null || _g === void 0 ? void 0 : _g.statusText}`);
|
|
169
214
|
}
|
|
170
215
|
throw new Error(`Could not upload application components
|
|
171
216
|
${e.message}`);
|
|
@@ -179,6 +224,7 @@ async function fetchCurrentUser(token, superblocksBaseUrl) {
|
|
|
179
224
|
url: new URL(`/api/v1/users/me`, superblocksBaseUrl).toString(),
|
|
180
225
|
headers: {
|
|
181
226
|
Authorization: "Bearer " + token,
|
|
227
|
+
[util_1.COMPONENT_EVENT_HEADER]: util_1.ComponentEvent.LOGIN,
|
|
182
228
|
},
|
|
183
229
|
};
|
|
184
230
|
const response = await (0, axios_1.default)(config);
|
package/dist/sdk.d.ts
CHANGED
|
@@ -3,11 +3,15 @@ export declare class SuperblocksSdk {
|
|
|
3
3
|
token: string;
|
|
4
4
|
superblocksBaseUrl: string;
|
|
5
5
|
constructor(accessToken: string, superblocksBaseUrl: string);
|
|
6
|
-
fetchApplication(applicationId
|
|
7
|
-
|
|
6
|
+
fetchApplication({ applicationId, headers, viewMode, }: {
|
|
7
|
+
applicationId: string;
|
|
8
|
+
headers?: Record<string, string>;
|
|
9
|
+
viewMode?: ViewMode;
|
|
10
|
+
}): Promise<any>;
|
|
11
|
+
fetchApplications(headers?: Record<string, string>): Promise<any>;
|
|
8
12
|
fetchApi(apiId: string, viewMode?: ViewMode): Promise<any>;
|
|
9
13
|
fetchApis(): Promise<any>;
|
|
10
|
-
registerComponents(applicationId: string, componentConfigs: Record<string, any>): Promise<any>;
|
|
14
|
+
registerComponents(applicationId: string, componentConfigs: Record<string, any>, injectedHeaders?: Record<string, string>): Promise<any>;
|
|
11
15
|
uploadComponents(applicationId: string, componentConfigs: Record<string, any>, files: string[]): Promise<any>;
|
|
12
16
|
fetchCurrentUser(): Promise<any>;
|
|
13
17
|
}
|
package/dist/sdk.js
CHANGED
|
@@ -9,16 +9,17 @@ class SuperblocksSdk {
|
|
|
9
9
|
this.token = accessToken;
|
|
10
10
|
this.superblocksBaseUrl = superblocksBaseUrl;
|
|
11
11
|
}
|
|
12
|
-
async fetchApplication(applicationId, viewMode = "export-live") {
|
|
12
|
+
async fetchApplication({ applicationId, headers = {}, viewMode = "export-live", }) {
|
|
13
13
|
return (0, client_1.fetchApplication)({
|
|
14
14
|
applicationId,
|
|
15
15
|
token: this.token,
|
|
16
16
|
superblocksBaseUrl: this.superblocksBaseUrl,
|
|
17
17
|
viewMode,
|
|
18
|
+
injectedHeaders: headers,
|
|
18
19
|
});
|
|
19
20
|
}
|
|
20
|
-
async fetchApplications() {
|
|
21
|
-
return (0, client_1.fetchApplications)(this.token, this.superblocksBaseUrl);
|
|
21
|
+
async fetchApplications(headers) {
|
|
22
|
+
return (0, client_1.fetchApplications)(this.token, this.superblocksBaseUrl, headers);
|
|
22
23
|
}
|
|
23
24
|
async fetchApi(apiId, viewMode = "export-live") {
|
|
24
25
|
return (0, client_1.fetchApi)(apiId, this.token, this.superblocksBaseUrl, viewMode);
|
|
@@ -26,8 +27,8 @@ class SuperblocksSdk {
|
|
|
26
27
|
async fetchApis() {
|
|
27
28
|
return (0, client_1.fetchApis)(this.token, this.superblocksBaseUrl);
|
|
28
29
|
}
|
|
29
|
-
async registerComponents(applicationId, componentConfigs) {
|
|
30
|
-
return (0, client_1.registerComponents)(applicationId, componentConfigs, this.token, this.superblocksBaseUrl);
|
|
30
|
+
async registerComponents(applicationId, componentConfigs, injectedHeaders) {
|
|
31
|
+
return (0, client_1.registerComponents)(applicationId, componentConfigs, this.token, this.superblocksBaseUrl, injectedHeaders);
|
|
31
32
|
}
|
|
32
33
|
async uploadComponents(applicationId, componentConfigs, files) {
|
|
33
34
|
return (0, client_1.uploadComponents)({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superblocksteam/sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.23",
|
|
4
4
|
"description": "Superblocks JS SDK",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"clean": "npm run clean:build && rm -rf node_modules",
|
|
18
18
|
"clean:build": "tsc --build --clean && rm -rf dist tsconfig.tsbuildinfo",
|
|
19
19
|
"lint": "eslint . --ext .ts --config .eslintrc.json",
|
|
20
|
-
"lint:fix": "eslint . --ext .ts --config .eslintrc.json --fix"
|
|
20
|
+
"lint:fix": "eslint . --ext .ts --config .eslintrc.json --fix",
|
|
21
|
+
"typecheck": "tsc --noEmit"
|
|
21
22
|
},
|
|
22
23
|
"license": "Superblocks Community Software License",
|
|
23
24
|
"devDependencies": {
|
|
@@ -29,11 +30,12 @@
|
|
|
29
30
|
"typescript": "^5.1.3"
|
|
30
31
|
},
|
|
31
32
|
"dependencies": {
|
|
32
|
-
"@superblocksteam/util": "
|
|
33
|
+
"@superblocksteam/util": "0.0.23",
|
|
33
34
|
"axios": "^1.3.5"
|
|
34
35
|
},
|
|
35
36
|
"homepage": "https://www.superblocks.com",
|
|
36
37
|
"engines": {
|
|
37
|
-
"node": ">=
|
|
38
|
+
"node": ">=16.0.0",
|
|
39
|
+
"npm": ">=8.0.0"
|
|
38
40
|
}
|
|
39
41
|
}
|
package/src/client.ts
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
import * as fs from "fs";
|
|
2
2
|
import {
|
|
3
|
+
ComponentEvent,
|
|
3
4
|
getBucketeerUrlFromSuperblocksUrl,
|
|
4
5
|
getContentType,
|
|
6
|
+
COMPONENT_EVENT_HEADER,
|
|
5
7
|
} from "@superblocksteam/util";
|
|
6
8
|
import { getSuperblocksMonorepoConfigJson } from "@superblocksteam/util";
|
|
7
9
|
import axios, { AxiosError } from "axios";
|
|
8
10
|
import * as FormData from "form-data";
|
|
11
|
+
import { isEmpty } from "lodash";
|
|
9
12
|
|
|
10
13
|
const BASE_SERVER_URL = "api/v1";
|
|
11
14
|
const BASE_BUCKETEER_URL = "api/v1";
|
|
12
15
|
|
|
16
|
+
const SUPERBLOCKS_MAX_FILE_SIZE_MB = 10;
|
|
17
|
+
|
|
13
18
|
const CLI_VERSION_HEADER = "x-superblocks-cli-version";
|
|
19
|
+
const SUPERBLOCKS_URL_HEADER = "x-superblocks-url";
|
|
14
20
|
|
|
15
21
|
export type ViewMode = "export-deployed" | "export-latest" | "export-live";
|
|
16
22
|
|
|
@@ -25,12 +31,46 @@ export async function fetchApplication({
|
|
|
25
31
|
token,
|
|
26
32
|
superblocksBaseUrl,
|
|
27
33
|
viewMode,
|
|
34
|
+
injectedHeaders = {},
|
|
28
35
|
}: {
|
|
29
36
|
applicationId: string;
|
|
30
37
|
token: string;
|
|
31
38
|
superblocksBaseUrl: string;
|
|
32
39
|
viewMode: ViewMode;
|
|
40
|
+
injectedHeaders: Record<string, string>;
|
|
33
41
|
}) {
|
|
42
|
+
const serverURL = new URL(
|
|
43
|
+
`api/v2/applications/${applicationId}?viewMode=${viewMode}`,
|
|
44
|
+
superblocksBaseUrl
|
|
45
|
+
);
|
|
46
|
+
let serverResponse;
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const config = {
|
|
50
|
+
method: "get",
|
|
51
|
+
url: serverURL.toString(),
|
|
52
|
+
headers: {
|
|
53
|
+
Authorization: "Bearer " + token,
|
|
54
|
+
...injectedHeaders,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
serverResponse = await axios(config);
|
|
58
|
+
} catch (e) {
|
|
59
|
+
if (axios.isAxiosError(e) && e.response?.status === 404) {
|
|
60
|
+
throw new Error(`Application ${applicationId} was not found`);
|
|
61
|
+
}
|
|
62
|
+
throw new Error("Could not fetch application");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// if there are no custom components, just return here without trying to initialize them using bucketeer
|
|
66
|
+
if (
|
|
67
|
+
isEmpty(
|
|
68
|
+
serverResponse?.data?.data?.application?.settings?.registeredComponents
|
|
69
|
+
)
|
|
70
|
+
) {
|
|
71
|
+
return serverResponse.data.data;
|
|
72
|
+
}
|
|
73
|
+
|
|
34
74
|
// TODO(jason) update this with commit ID once we enable fetching by commit
|
|
35
75
|
const commitId = "latest";
|
|
36
76
|
superblocksBaseUrl = superblocksBaseUrl.replace(/\/$/, "");
|
|
@@ -49,7 +89,8 @@ export async function fetchApplication({
|
|
|
49
89
|
url: componentFileURL,
|
|
50
90
|
headers: {
|
|
51
91
|
Authorization: "Bearer " + token,
|
|
52
|
-
|
|
92
|
+
[SUPERBLOCKS_URL_HEADER]: superblocksBaseUrl,
|
|
93
|
+
...injectedHeaders,
|
|
53
94
|
},
|
|
54
95
|
};
|
|
55
96
|
const response = await axios(config);
|
|
@@ -64,7 +105,8 @@ export async function fetchApplication({
|
|
|
64
105
|
|
|
65
106
|
export async function fetchApplications(
|
|
66
107
|
token: string,
|
|
67
|
-
superblocksBaseUrl: string
|
|
108
|
+
superblocksBaseUrl: string,
|
|
109
|
+
injectedHeaders: Record<string, string> = {}
|
|
68
110
|
) {
|
|
69
111
|
try {
|
|
70
112
|
const config = {
|
|
@@ -72,6 +114,7 @@ export async function fetchApplications(
|
|
|
72
114
|
url: new URL(`/api/v2/applications`, superblocksBaseUrl).toString(),
|
|
73
115
|
headers: {
|
|
74
116
|
Authorization: "Bearer " + token,
|
|
117
|
+
...injectedHeaders,
|
|
75
118
|
},
|
|
76
119
|
};
|
|
77
120
|
const response = await axios(config);
|
|
@@ -128,7 +171,8 @@ export async function registerComponents(
|
|
|
128
171
|
applicationId: string,
|
|
129
172
|
componentConfigs: Record<string, any>,
|
|
130
173
|
token: string,
|
|
131
|
-
superblocksBaseUrl: string
|
|
174
|
+
superblocksBaseUrl: string,
|
|
175
|
+
injectedHeaders?: Record<string, string>
|
|
132
176
|
) {
|
|
133
177
|
try {
|
|
134
178
|
const [rootSettings] = await getSuperblocksMonorepoConfigJson(true);
|
|
@@ -141,6 +185,7 @@ export async function registerComponents(
|
|
|
141
185
|
headers: {
|
|
142
186
|
Authorization: "Bearer " + token,
|
|
143
187
|
[CLI_VERSION_HEADER]: rootSettings.metadata.cliVersion,
|
|
188
|
+
...injectedHeaders,
|
|
144
189
|
},
|
|
145
190
|
data: { components: componentConfigs },
|
|
146
191
|
};
|
|
@@ -190,7 +235,8 @@ export async function uploadComponents({
|
|
|
190
235
|
}
|
|
191
236
|
return {
|
|
192
237
|
name: `file${ind + 1}`,
|
|
193
|
-
|
|
238
|
+
// win32 => posix filepath. Bucketeer does not accept windows style filepaths
|
|
239
|
+
filename: file.replace(/\\/g, "/"),
|
|
194
240
|
ContentType: contentType,
|
|
195
241
|
} as UploadFile;
|
|
196
242
|
});
|
|
@@ -213,11 +259,23 @@ export async function uploadComponents({
|
|
|
213
259
|
bucketeerBaseUrl
|
|
214
260
|
).toString();
|
|
215
261
|
|
|
262
|
+
let totalFileSizeMB = 0;
|
|
216
263
|
const formData = new FormData();
|
|
217
264
|
formData.append("format", formatJSON);
|
|
218
265
|
uploadFiles.forEach((uploadFile) => {
|
|
266
|
+
const uploadFileStats = fs.statSync(uploadFile.filename);
|
|
267
|
+
totalFileSizeMB += uploadFileStats.size / (1024 * 1024);
|
|
219
268
|
formData.append(uploadFile.name, fs.createReadStream(uploadFile.filename));
|
|
220
269
|
});
|
|
270
|
+
|
|
271
|
+
if (totalFileSizeMB > SUPERBLOCKS_MAX_FILE_SIZE_MB) {
|
|
272
|
+
throw new Error(`upload size exceeded maximum allowed size.
|
|
273
|
+
|
|
274
|
+
Current Size: ${totalFileSizeMB.toFixed(2)} MB
|
|
275
|
+
Max Allowed Size: ${SUPERBLOCKS_MAX_FILE_SIZE_MB.toFixed(0)} MB
|
|
276
|
+
|
|
277
|
+
You can reduce your component bundle size by uploading static assets to a separate service.`);
|
|
278
|
+
}
|
|
221
279
|
const formHeaders = formData.getHeaders();
|
|
222
280
|
|
|
223
281
|
try {
|
|
@@ -226,7 +284,8 @@ export async function uploadComponents({
|
|
|
226
284
|
headers: {
|
|
227
285
|
Authorization: "Bearer " + token,
|
|
228
286
|
[CLI_VERSION_HEADER]: rootSettings.metadata.cliVersion,
|
|
229
|
-
|
|
287
|
+
[COMPONENT_EVENT_HEADER]: ComponentEvent.UPLOAD,
|
|
288
|
+
[SUPERBLOCKS_URL_HEADER]: superblocksBaseUrl,
|
|
230
289
|
...formHeaders,
|
|
231
290
|
},
|
|
232
291
|
};
|
|
@@ -235,7 +294,11 @@ export async function uploadComponents({
|
|
|
235
294
|
|
|
236
295
|
return response.data;
|
|
237
296
|
} catch (e: any) {
|
|
238
|
-
if (e instanceof AxiosError) {
|
|
297
|
+
if (e instanceof AxiosError && e.response?.status === 413) {
|
|
298
|
+
throw new Error(
|
|
299
|
+
`Could not upload application components, file size limit exceeded`
|
|
300
|
+
);
|
|
301
|
+
} else if (e instanceof AxiosError) {
|
|
239
302
|
const message =
|
|
240
303
|
(e.response?.data?.responseMeta?.message as string) ?? e.response?.data;
|
|
241
304
|
throw new Error(`Could not upload application components
|
|
@@ -256,6 +319,7 @@ export async function fetchCurrentUser(
|
|
|
256
319
|
url: new URL(`/api/v1/users/me`, superblocksBaseUrl).toString(),
|
|
257
320
|
headers: {
|
|
258
321
|
Authorization: "Bearer " + token,
|
|
322
|
+
[COMPONENT_EVENT_HEADER]: ComponentEvent.LOGIN,
|
|
259
323
|
},
|
|
260
324
|
};
|
|
261
325
|
const response = await axios(config);
|
package/src/sdk.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
fetchApi,
|
|
3
3
|
fetchApis,
|
|
4
|
-
fetchApplication,
|
|
4
|
+
fetchApplication as fetchApplicationClient,
|
|
5
5
|
fetchApplications,
|
|
6
6
|
registerComponents,
|
|
7
7
|
uploadComponents,
|
|
@@ -18,20 +18,26 @@ export class SuperblocksSdk {
|
|
|
18
18
|
this.superblocksBaseUrl = superblocksBaseUrl;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
async fetchApplication(
|
|
22
|
-
applicationId
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
async fetchApplication({
|
|
22
|
+
applicationId,
|
|
23
|
+
headers = {},
|
|
24
|
+
viewMode = "export-live",
|
|
25
|
+
}: {
|
|
26
|
+
applicationId: string;
|
|
27
|
+
headers?: Record<string, string>;
|
|
28
|
+
viewMode?: ViewMode;
|
|
29
|
+
}) {
|
|
30
|
+
return fetchApplicationClient({
|
|
26
31
|
applicationId,
|
|
27
32
|
token: this.token,
|
|
28
33
|
superblocksBaseUrl: this.superblocksBaseUrl,
|
|
29
34
|
viewMode,
|
|
35
|
+
injectedHeaders: headers,
|
|
30
36
|
});
|
|
31
37
|
}
|
|
32
38
|
|
|
33
|
-
async fetchApplications() {
|
|
34
|
-
return fetchApplications(this.token, this.superblocksBaseUrl);
|
|
39
|
+
async fetchApplications(headers?: Record<string, string>) {
|
|
40
|
+
return fetchApplications(this.token, this.superblocksBaseUrl, headers);
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
async fetchApi(apiId: string, viewMode: ViewMode = "export-live") {
|
|
@@ -44,13 +50,15 @@ export class SuperblocksSdk {
|
|
|
44
50
|
|
|
45
51
|
async registerComponents(
|
|
46
52
|
applicationId: string,
|
|
47
|
-
componentConfigs: Record<string, any
|
|
53
|
+
componentConfigs: Record<string, any>,
|
|
54
|
+
injectedHeaders?: Record<string, string>
|
|
48
55
|
) {
|
|
49
56
|
return registerComponents(
|
|
50
57
|
applicationId,
|
|
51
58
|
componentConfigs,
|
|
52
59
|
this.token,
|
|
53
|
-
this.superblocksBaseUrl
|
|
60
|
+
this.superblocksBaseUrl,
|
|
61
|
+
injectedHeaders
|
|
54
62
|
);
|
|
55
63
|
}
|
|
56
64
|
|