@superblocksteam/sdk 0.0.22 → 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.js +47 -7
- package/package.json +6 -4
- package/src/client.ts +54 -2
package/dist/client.js
CHANGED
|
@@ -6,12 +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
14
|
const SUPERBLOCKS_URL_HEADER = "x-superblocks-url";
|
|
13
15
|
async function fetchApplication({ applicationId, token, superblocksBaseUrl, viewMode, injectedHeaders = {}, }) {
|
|
14
|
-
var _a;
|
|
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
|
+
}
|
|
15
40
|
// TODO(jason) update this with commit ID once we enable fetching by commit
|
|
16
41
|
const commitId = "latest";
|
|
17
42
|
superblocksBaseUrl = superblocksBaseUrl.replace(/\/$/, "");
|
|
@@ -32,7 +57,7 @@ async function fetchApplication({ applicationId, token, superblocksBaseUrl, view
|
|
|
32
57
|
return response.data;
|
|
33
58
|
}
|
|
34
59
|
catch (e) {
|
|
35
|
-
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) {
|
|
36
61
|
throw new Error(`Application ${applicationId} was not found`);
|
|
37
62
|
}
|
|
38
63
|
throw new Error("Could not fetch application");
|
|
@@ -122,7 +147,7 @@ async function registerComponents(applicationId, componentConfigs, token, superb
|
|
|
122
147
|
}
|
|
123
148
|
exports.registerComponents = registerComponents;
|
|
124
149
|
async function uploadComponents({ applicationId, componentConfigs, files, token, superblocksBaseUrl, }) {
|
|
125
|
-
var _a, _b, _c, _d, _e, _f;
|
|
150
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
126
151
|
superblocksBaseUrl = superblocksBaseUrl.replace(/\/$/, "");
|
|
127
152
|
const bucketeerBaseUrl = (0, util_1.getBucketeerUrlFromSuperblocksUrl)(superblocksBaseUrl);
|
|
128
153
|
// parse files to source and bundled files based on path
|
|
@@ -133,7 +158,8 @@ async function uploadComponents({ applicationId, componentConfigs, files, token,
|
|
|
133
158
|
}
|
|
134
159
|
return {
|
|
135
160
|
name: `file${ind + 1}`,
|
|
136
|
-
|
|
161
|
+
// win32 => posix filepath. Bucketeer does not accept windows style filepaths
|
|
162
|
+
filename: file.replace(/\\/g, "/"),
|
|
137
163
|
ContentType: contentType,
|
|
138
164
|
};
|
|
139
165
|
});
|
|
@@ -146,11 +172,22 @@ async function uploadComponents({ applicationId, componentConfigs, files, token,
|
|
|
146
172
|
buildFiles,
|
|
147
173
|
});
|
|
148
174
|
const postURL = new URL(`${BASE_BUCKETEER_URL}/components/upload`, bucketeerBaseUrl).toString();
|
|
175
|
+
let totalFileSizeMB = 0;
|
|
149
176
|
const formData = new FormData();
|
|
150
177
|
formData.append("format", formatJSON);
|
|
151
178
|
uploadFiles.forEach((uploadFile) => {
|
|
179
|
+
const uploadFileStats = fs.statSync(uploadFile.filename);
|
|
180
|
+
totalFileSizeMB += uploadFileStats.size / (1024 * 1024);
|
|
152
181
|
formData.append(uploadFile.name, fs.createReadStream(uploadFile.filename));
|
|
153
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
|
+
}
|
|
154
191
|
const formHeaders = formData.getHeaders();
|
|
155
192
|
try {
|
|
156
193
|
const [rootSettings] = await (0, util_2.getSuperblocksMonorepoConfigJson)(true);
|
|
@@ -167,10 +204,13 @@ async function uploadComponents({ applicationId, componentConfigs, files, token,
|
|
|
167
204
|
return response.data;
|
|
168
205
|
}
|
|
169
206
|
catch (e) {
|
|
170
|
-
if (e instanceof axios_1.AxiosError) {
|
|
171
|
-
|
|
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;
|
|
172
212
|
throw new Error(`Could not upload application components
|
|
173
|
-
${message !== null && message !== void 0 ? message : (
|
|
213
|
+
${message !== null && message !== void 0 ? message : (_g = e.response) === null || _g === void 0 ? void 0 : _g.statusText}`);
|
|
174
214
|
}
|
|
175
215
|
throw new Error(`Could not upload application components
|
|
176
216
|
${e.message}`);
|
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
|
@@ -8,10 +8,13 @@ import {
|
|
|
8
8
|
import { getSuperblocksMonorepoConfigJson } from "@superblocksteam/util";
|
|
9
9
|
import axios, { AxiosError } from "axios";
|
|
10
10
|
import * as FormData from "form-data";
|
|
11
|
+
import { isEmpty } from "lodash";
|
|
11
12
|
|
|
12
13
|
const BASE_SERVER_URL = "api/v1";
|
|
13
14
|
const BASE_BUCKETEER_URL = "api/v1";
|
|
14
15
|
|
|
16
|
+
const SUPERBLOCKS_MAX_FILE_SIZE_MB = 10;
|
|
17
|
+
|
|
15
18
|
const CLI_VERSION_HEADER = "x-superblocks-cli-version";
|
|
16
19
|
const SUPERBLOCKS_URL_HEADER = "x-superblocks-url";
|
|
17
20
|
|
|
@@ -36,6 +39,38 @@ export async function fetchApplication({
|
|
|
36
39
|
viewMode: ViewMode;
|
|
37
40
|
injectedHeaders: Record<string, string>;
|
|
38
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
|
+
|
|
39
74
|
// TODO(jason) update this with commit ID once we enable fetching by commit
|
|
40
75
|
const commitId = "latest";
|
|
41
76
|
superblocksBaseUrl = superblocksBaseUrl.replace(/\/$/, "");
|
|
@@ -200,7 +235,8 @@ export async function uploadComponents({
|
|
|
200
235
|
}
|
|
201
236
|
return {
|
|
202
237
|
name: `file${ind + 1}`,
|
|
203
|
-
|
|
238
|
+
// win32 => posix filepath. Bucketeer does not accept windows style filepaths
|
|
239
|
+
filename: file.replace(/\\/g, "/"),
|
|
204
240
|
ContentType: contentType,
|
|
205
241
|
} as UploadFile;
|
|
206
242
|
});
|
|
@@ -223,11 +259,23 @@ export async function uploadComponents({
|
|
|
223
259
|
bucketeerBaseUrl
|
|
224
260
|
).toString();
|
|
225
261
|
|
|
262
|
+
let totalFileSizeMB = 0;
|
|
226
263
|
const formData = new FormData();
|
|
227
264
|
formData.append("format", formatJSON);
|
|
228
265
|
uploadFiles.forEach((uploadFile) => {
|
|
266
|
+
const uploadFileStats = fs.statSync(uploadFile.filename);
|
|
267
|
+
totalFileSizeMB += uploadFileStats.size / (1024 * 1024);
|
|
229
268
|
formData.append(uploadFile.name, fs.createReadStream(uploadFile.filename));
|
|
230
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
|
+
}
|
|
231
279
|
const formHeaders = formData.getHeaders();
|
|
232
280
|
|
|
233
281
|
try {
|
|
@@ -246,7 +294,11 @@ export async function uploadComponents({
|
|
|
246
294
|
|
|
247
295
|
return response.data;
|
|
248
296
|
} catch (e: any) {
|
|
249
|
-
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) {
|
|
250
302
|
const message =
|
|
251
303
|
(e.response?.data?.responseMeta?.message as string) ?? e.response?.data;
|
|
252
304
|
throw new Error(`Could not upload application components
|