@superblocksteam/sdk 0.0.23 → 1.0.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/dist/client.d.ts +15 -1
- package/dist/client.js +29 -12
- package/dist/sdk.d.ts +8 -1
- package/dist/sdk.js +9 -0
- package/package.json +2 -2
- package/src/client.ts +57 -21
- package/src/sdk.ts +21 -2
package/dist/client.d.ts
CHANGED
|
@@ -4,13 +4,27 @@ export interface UploadFile {
|
|
|
4
4
|
filename: string;
|
|
5
5
|
ContentType: string;
|
|
6
6
|
}
|
|
7
|
+
export interface ApplicationWrapper {
|
|
8
|
+
application: Record<string, any>;
|
|
9
|
+
page: Record<string, any>;
|
|
10
|
+
apis: Record<string, any>[];
|
|
11
|
+
}
|
|
7
12
|
export declare function fetchApplication({ applicationId, token, superblocksBaseUrl, viewMode, injectedHeaders, }: {
|
|
8
13
|
applicationId: string;
|
|
9
14
|
token: string;
|
|
10
15
|
superblocksBaseUrl: string;
|
|
11
16
|
viewMode: ViewMode;
|
|
12
17
|
injectedHeaders: Record<string, string>;
|
|
13
|
-
}): Promise<
|
|
18
|
+
}): Promise<ApplicationWrapper | undefined>;
|
|
19
|
+
export declare function fetchApplicationWithComponents({ applicationId, token, superblocksBaseUrl, viewMode, injectedHeaders, }: {
|
|
20
|
+
applicationId: string;
|
|
21
|
+
token: string;
|
|
22
|
+
superblocksBaseUrl: string;
|
|
23
|
+
viewMode: ViewMode;
|
|
24
|
+
injectedHeaders: Record<string, string>;
|
|
25
|
+
}): Promise<(ApplicationWrapper & {
|
|
26
|
+
componentFiles: any;
|
|
27
|
+
}) | undefined>;
|
|
14
28
|
export declare function fetchApplications(token: string, superblocksBaseUrl: string, injectedHeaders?: Record<string, string>): Promise<any>;
|
|
15
29
|
export declare function fetchApi(apiId: string, token: string, superblocksBaseUrl: string, viewMode: ViewMode): Promise<any>;
|
|
16
30
|
export declare function fetchApis(token: string, superblocksBaseUrl: string): Promise<any>;
|
package/dist/client.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fetchCurrentUser = exports.uploadComponents = exports.registerComponents = exports.fetchApis = exports.fetchApi = exports.fetchApplications = exports.fetchApplication = void 0;
|
|
3
|
+
exports.fetchCurrentUser = exports.uploadComponents = exports.registerComponents = exports.fetchApis = exports.fetchApi = exports.fetchApplications = exports.fetchApplicationWithComponents = exports.fetchApplication = void 0;
|
|
4
4
|
const fs = require("fs");
|
|
5
5
|
const util_1 = require("@superblocksteam/util");
|
|
6
|
-
const util_2 = require("@superblocksteam/util");
|
|
7
6
|
const axios_1 = require("axios");
|
|
8
7
|
const FormData = require("form-data");
|
|
9
8
|
const lodash_1 = require("lodash");
|
|
@@ -13,7 +12,7 @@ const SUPERBLOCKS_MAX_FILE_SIZE_MB = 10;
|
|
|
13
12
|
const CLI_VERSION_HEADER = "x-superblocks-cli-version";
|
|
14
13
|
const SUPERBLOCKS_URL_HEADER = "x-superblocks-url";
|
|
15
14
|
async function fetchApplication({ applicationId, token, superblocksBaseUrl, viewMode, injectedHeaders = {}, }) {
|
|
16
|
-
var _a, _b
|
|
15
|
+
var _a, _b;
|
|
17
16
|
const serverURL = new URL(`api/v2/applications/${applicationId}?viewMode=${viewMode}`, superblocksBaseUrl);
|
|
18
17
|
let serverResponse;
|
|
19
18
|
try {
|
|
@@ -29,13 +28,31 @@ async function fetchApplication({ applicationId, token, superblocksBaseUrl, view
|
|
|
29
28
|
}
|
|
30
29
|
catch (e) {
|
|
31
30
|
if (axios_1.default.isAxiosError(e) && ((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 404) {
|
|
32
|
-
throw new
|
|
31
|
+
throw new util_1.NotFoundError(`Application ${applicationId} was not found`);
|
|
33
32
|
}
|
|
34
33
|
throw new Error("Could not fetch application");
|
|
35
34
|
}
|
|
35
|
+
return (_b = serverResponse === null || serverResponse === void 0 ? void 0 : serverResponse.data) === null || _b === void 0 ? void 0 : _b.data;
|
|
36
|
+
}
|
|
37
|
+
exports.fetchApplication = fetchApplication;
|
|
38
|
+
async function fetchApplicationWithComponents({ applicationId, token, superblocksBaseUrl, viewMode, injectedHeaders = {}, }) {
|
|
39
|
+
var _a, _b, _c;
|
|
40
|
+
const applicationWrapper = await fetchApplication({
|
|
41
|
+
applicationId,
|
|
42
|
+
token,
|
|
43
|
+
superblocksBaseUrl,
|
|
44
|
+
viewMode,
|
|
45
|
+
injectedHeaders,
|
|
46
|
+
});
|
|
47
|
+
if ((0, lodash_1.isEmpty)(applicationWrapper)) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
36
50
|
// if there are no custom components, just return here without trying to initialize them using bucketeer
|
|
37
|
-
if ((0, lodash_1.isEmpty)((
|
|
38
|
-
return
|
|
51
|
+
if ((0, lodash_1.isEmpty)((_b = (_a = applicationWrapper.application) === null || _a === void 0 ? void 0 : _a.settings) === null || _b === void 0 ? void 0 : _b.registeredComponents)) {
|
|
52
|
+
return {
|
|
53
|
+
...applicationWrapper,
|
|
54
|
+
componentFiles: null,
|
|
55
|
+
};
|
|
39
56
|
}
|
|
40
57
|
// TODO(jason) update this with commit ID once we enable fetching by commit
|
|
41
58
|
const commitId = "latest";
|
|
@@ -57,13 +74,13 @@ async function fetchApplication({ applicationId, token, superblocksBaseUrl, view
|
|
|
57
74
|
return response.data;
|
|
58
75
|
}
|
|
59
76
|
catch (e) {
|
|
60
|
-
if (axios_1.default.isAxiosError(e) && ((
|
|
61
|
-
throw new
|
|
77
|
+
if (axios_1.default.isAxiosError(e) && ((_c = e.response) === null || _c === void 0 ? void 0 : _c.status) === 404) {
|
|
78
|
+
throw new util_1.NotFoundError(`Application ${applicationId} was not found`);
|
|
62
79
|
}
|
|
63
80
|
throw new Error("Could not fetch application");
|
|
64
81
|
}
|
|
65
82
|
}
|
|
66
|
-
exports.
|
|
83
|
+
exports.fetchApplicationWithComponents = fetchApplicationWithComponents;
|
|
67
84
|
async function fetchApplications(token, superblocksBaseUrl, injectedHeaders = {}) {
|
|
68
85
|
try {
|
|
69
86
|
const config = {
|
|
@@ -97,7 +114,7 @@ async function fetchApi(apiId, token, superblocksBaseUrl, viewMode) {
|
|
|
97
114
|
}
|
|
98
115
|
catch (e) {
|
|
99
116
|
if (axios_1.default.isAxiosError(e) && ((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 404) {
|
|
100
|
-
throw new
|
|
117
|
+
throw new util_1.NotFoundError(`Api ${apiId} was not found`);
|
|
101
118
|
}
|
|
102
119
|
throw new Error("Could not fetch api");
|
|
103
120
|
}
|
|
@@ -123,7 +140,7 @@ exports.fetchApis = fetchApis;
|
|
|
123
140
|
async function registerComponents(applicationId, componentConfigs, token, superblocksBaseUrl, injectedHeaders) {
|
|
124
141
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
125
142
|
try {
|
|
126
|
-
const [rootSettings] = await (0,
|
|
143
|
+
const [rootSettings] = await (0, util_1.getSuperblocksMonorepoConfigJson)(true);
|
|
127
144
|
const config = {
|
|
128
145
|
method: "put",
|
|
129
146
|
url: new URL(`${BASE_SERVER_URL}/public/application/${applicationId}/components`, superblocksBaseUrl).toString(),
|
|
@@ -190,7 +207,7 @@ You can reduce your component bundle size by uploading static assets to a separa
|
|
|
190
207
|
}
|
|
191
208
|
const formHeaders = formData.getHeaders();
|
|
192
209
|
try {
|
|
193
|
-
const [rootSettings] = await (0,
|
|
210
|
+
const [rootSettings] = await (0, util_1.getSuperblocksMonorepoConfigJson)(true);
|
|
194
211
|
const config = {
|
|
195
212
|
headers: {
|
|
196
213
|
Authorization: "Bearer " + token,
|
package/dist/sdk.d.ts
CHANGED
|
@@ -7,7 +7,14 @@ export declare class SuperblocksSdk {
|
|
|
7
7
|
applicationId: string;
|
|
8
8
|
headers?: Record<string, string>;
|
|
9
9
|
viewMode?: ViewMode;
|
|
10
|
-
}): Promise<
|
|
10
|
+
}): Promise<import("./client").ApplicationWrapper | undefined>;
|
|
11
|
+
fetchApplicationWithComponents({ applicationId, headers, viewMode, }: {
|
|
12
|
+
applicationId: string;
|
|
13
|
+
headers?: Record<string, string>;
|
|
14
|
+
viewMode?: ViewMode;
|
|
15
|
+
}): Promise<(import("./client").ApplicationWrapper & {
|
|
16
|
+
componentFiles: any;
|
|
17
|
+
}) | undefined>;
|
|
11
18
|
fetchApplications(headers?: Record<string, string>): Promise<any>;
|
|
12
19
|
fetchApi(apiId: string, viewMode?: ViewMode): Promise<any>;
|
|
13
20
|
fetchApis(): Promise<any>;
|
package/dist/sdk.js
CHANGED
|
@@ -18,6 +18,15 @@ class SuperblocksSdk {
|
|
|
18
18
|
injectedHeaders: headers,
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
+
async fetchApplicationWithComponents({ applicationId, headers = {}, viewMode = "export-live", }) {
|
|
22
|
+
return (0, client_1.fetchApplicationWithComponents)({
|
|
23
|
+
applicationId,
|
|
24
|
+
token: this.token,
|
|
25
|
+
superblocksBaseUrl: this.superblocksBaseUrl,
|
|
26
|
+
viewMode,
|
|
27
|
+
injectedHeaders: headers,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
21
30
|
async fetchApplications(headers) {
|
|
22
31
|
return (0, client_1.fetchApplications)(this.token, this.superblocksBaseUrl, headers);
|
|
23
32
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superblocksteam/sdk",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Superblocks JS SDK",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"typescript": "^5.1.3"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@superblocksteam/util": "0.0
|
|
33
|
+
"@superblocksteam/util": "1.0.0",
|
|
34
34
|
"axios": "^1.3.5"
|
|
35
35
|
},
|
|
36
36
|
"homepage": "https://www.superblocks.com",
|
package/src/client.ts
CHANGED
|
@@ -2,11 +2,12 @@ import * as fs from "fs";
|
|
|
2
2
|
import {
|
|
3
3
|
ComponentEvent,
|
|
4
4
|
getBucketeerUrlFromSuperblocksUrl,
|
|
5
|
+
getSuperblocksMonorepoConfigJson,
|
|
5
6
|
getContentType,
|
|
6
7
|
COMPONENT_EVENT_HEADER,
|
|
8
|
+
NotFoundError,
|
|
7
9
|
} from "@superblocksteam/util";
|
|
8
|
-
import {
|
|
9
|
-
import axios, { AxiosError } from "axios";
|
|
10
|
+
import axios, { AxiosError, AxiosRequestConfig } from "axios";
|
|
10
11
|
import * as FormData from "form-data";
|
|
11
12
|
import { isEmpty } from "lodash";
|
|
12
13
|
|
|
@@ -26,6 +27,14 @@ export interface UploadFile {
|
|
|
26
27
|
ContentType: string;
|
|
27
28
|
}
|
|
28
29
|
|
|
30
|
+
export interface ApplicationWrapper {
|
|
31
|
+
application: Record<string, any>;
|
|
32
|
+
page: Record<string, any>;
|
|
33
|
+
apis: Record<string, any>[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type ResponseWithMeta<T> = { responseMeta: unknown; data: T };
|
|
37
|
+
|
|
29
38
|
export async function fetchApplication({
|
|
30
39
|
applicationId,
|
|
31
40
|
token,
|
|
@@ -38,7 +47,7 @@ export async function fetchApplication({
|
|
|
38
47
|
superblocksBaseUrl: string;
|
|
39
48
|
viewMode: ViewMode;
|
|
40
49
|
injectedHeaders: Record<string, string>;
|
|
41
|
-
}) {
|
|
50
|
+
}): Promise<ApplicationWrapper | undefined> {
|
|
42
51
|
const serverURL = new URL(
|
|
43
52
|
`api/v2/applications/${applicationId}?viewMode=${viewMode}`,
|
|
44
53
|
superblocksBaseUrl
|
|
@@ -46,7 +55,7 @@ export async function fetchApplication({
|
|
|
46
55
|
let serverResponse;
|
|
47
56
|
|
|
48
57
|
try {
|
|
49
|
-
const config = {
|
|
58
|
+
const config: AxiosRequestConfig = {
|
|
50
59
|
method: "get",
|
|
51
60
|
url: serverURL.toString(),
|
|
52
61
|
headers: {
|
|
@@ -54,21 +63,48 @@ export async function fetchApplication({
|
|
|
54
63
|
...injectedHeaders,
|
|
55
64
|
},
|
|
56
65
|
};
|
|
57
|
-
serverResponse = await axios(config);
|
|
66
|
+
serverResponse = await axios<ResponseWithMeta<ApplicationWrapper>>(config);
|
|
58
67
|
} catch (e) {
|
|
59
68
|
if (axios.isAxiosError(e) && e.response?.status === 404) {
|
|
60
|
-
throw new
|
|
69
|
+
throw new NotFoundError(`Application ${applicationId} was not found`);
|
|
61
70
|
}
|
|
62
71
|
throw new Error("Could not fetch application");
|
|
63
72
|
}
|
|
64
73
|
|
|
74
|
+
return serverResponse?.data?.data;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function fetchApplicationWithComponents({
|
|
78
|
+
applicationId,
|
|
79
|
+
token,
|
|
80
|
+
superblocksBaseUrl,
|
|
81
|
+
viewMode,
|
|
82
|
+
injectedHeaders = {},
|
|
83
|
+
}: {
|
|
84
|
+
applicationId: string;
|
|
85
|
+
token: string;
|
|
86
|
+
superblocksBaseUrl: string;
|
|
87
|
+
viewMode: ViewMode;
|
|
88
|
+
injectedHeaders: Record<string, string>;
|
|
89
|
+
}): Promise<(ApplicationWrapper & { componentFiles: any }) | undefined> {
|
|
90
|
+
const applicationWrapper = await fetchApplication({
|
|
91
|
+
applicationId,
|
|
92
|
+
token,
|
|
93
|
+
superblocksBaseUrl,
|
|
94
|
+
viewMode,
|
|
95
|
+
injectedHeaders,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
if (isEmpty(applicationWrapper)) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
65
102
|
// if there are no custom components, just return here without trying to initialize them using bucketeer
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return serverResponse.data.data;
|
|
103
|
+
if (isEmpty(applicationWrapper.application?.settings?.registeredComponents)) {
|
|
104
|
+
return {
|
|
105
|
+
...applicationWrapper,
|
|
106
|
+
componentFiles: null,
|
|
107
|
+
};
|
|
72
108
|
}
|
|
73
109
|
|
|
74
110
|
// TODO(jason) update this with commit ID once we enable fetching by commit
|
|
@@ -84,7 +120,7 @@ export async function fetchApplication({
|
|
|
84
120
|
).toString();
|
|
85
121
|
|
|
86
122
|
try {
|
|
87
|
-
const config = {
|
|
123
|
+
const config: AxiosRequestConfig = {
|
|
88
124
|
method: "get",
|
|
89
125
|
url: componentFileURL,
|
|
90
126
|
headers: {
|
|
@@ -97,7 +133,7 @@ export async function fetchApplication({
|
|
|
97
133
|
return response.data;
|
|
98
134
|
} catch (e) {
|
|
99
135
|
if (axios.isAxiosError(e) && e.response?.status === 404) {
|
|
100
|
-
throw new
|
|
136
|
+
throw new NotFoundError(`Application ${applicationId} was not found`);
|
|
101
137
|
}
|
|
102
138
|
throw new Error("Could not fetch application");
|
|
103
139
|
}
|
|
@@ -109,7 +145,7 @@ export async function fetchApplications(
|
|
|
109
145
|
injectedHeaders: Record<string, string> = {}
|
|
110
146
|
) {
|
|
111
147
|
try {
|
|
112
|
-
const config = {
|
|
148
|
+
const config: AxiosRequestConfig = {
|
|
113
149
|
method: "get",
|
|
114
150
|
url: new URL(`/api/v2/applications`, superblocksBaseUrl).toString(),
|
|
115
151
|
headers: {
|
|
@@ -131,7 +167,7 @@ export async function fetchApi(
|
|
|
131
167
|
viewMode: ViewMode
|
|
132
168
|
) {
|
|
133
169
|
try {
|
|
134
|
-
const config = {
|
|
170
|
+
const config: AxiosRequestConfig = {
|
|
135
171
|
method: "get",
|
|
136
172
|
url: new URL(
|
|
137
173
|
`/api/v3/apis/${apiId}?viewMode=${viewMode}`,
|
|
@@ -145,7 +181,7 @@ export async function fetchApi(
|
|
|
145
181
|
return response.data.data;
|
|
146
182
|
} catch (e) {
|
|
147
183
|
if (axios.isAxiosError(e) && e.response?.status === 404) {
|
|
148
|
-
throw new
|
|
184
|
+
throw new NotFoundError(`Api ${apiId} was not found`);
|
|
149
185
|
}
|
|
150
186
|
throw new Error("Could not fetch api");
|
|
151
187
|
}
|
|
@@ -153,7 +189,7 @@ export async function fetchApi(
|
|
|
153
189
|
|
|
154
190
|
export async function fetchApis(token: string, superblocksBaseUrl: string) {
|
|
155
191
|
try {
|
|
156
|
-
const config = {
|
|
192
|
+
const config: AxiosRequestConfig = {
|
|
157
193
|
method: "get",
|
|
158
194
|
url: new URL(`/api/v3/apis`, superblocksBaseUrl).toString(),
|
|
159
195
|
headers: {
|
|
@@ -176,7 +212,7 @@ export async function registerComponents(
|
|
|
176
212
|
) {
|
|
177
213
|
try {
|
|
178
214
|
const [rootSettings] = await getSuperblocksMonorepoConfigJson(true);
|
|
179
|
-
const config = {
|
|
215
|
+
const config: AxiosRequestConfig = {
|
|
180
216
|
method: "put",
|
|
181
217
|
url: new URL(
|
|
182
218
|
`${BASE_SERVER_URL}/public/application/${applicationId}/components`,
|
|
@@ -280,7 +316,7 @@ You can reduce your component bundle size by uploading static assets to a separa
|
|
|
280
316
|
|
|
281
317
|
try {
|
|
282
318
|
const [rootSettings] = await getSuperblocksMonorepoConfigJson(true);
|
|
283
|
-
const config = {
|
|
319
|
+
const config: AxiosRequestConfig = {
|
|
284
320
|
headers: {
|
|
285
321
|
Authorization: "Bearer " + token,
|
|
286
322
|
[CLI_VERSION_HEADER]: rootSettings.metadata.cliVersion,
|
|
@@ -314,7 +350,7 @@ export async function fetchCurrentUser(
|
|
|
314
350
|
superblocksBaseUrl: string
|
|
315
351
|
) {
|
|
316
352
|
try {
|
|
317
|
-
const config = {
|
|
353
|
+
const config: AxiosRequestConfig = {
|
|
318
354
|
method: "get",
|
|
319
355
|
url: new URL(`/api/v1/users/me`, superblocksBaseUrl).toString(),
|
|
320
356
|
headers: {
|
package/src/sdk.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
fetchApi,
|
|
3
3
|
fetchApis,
|
|
4
|
-
fetchApplication
|
|
4
|
+
fetchApplication,
|
|
5
|
+
fetchApplicationWithComponents,
|
|
5
6
|
fetchApplications,
|
|
6
7
|
registerComponents,
|
|
7
8
|
uploadComponents,
|
|
@@ -27,7 +28,25 @@ export class SuperblocksSdk {
|
|
|
27
28
|
headers?: Record<string, string>;
|
|
28
29
|
viewMode?: ViewMode;
|
|
29
30
|
}) {
|
|
30
|
-
return
|
|
31
|
+
return fetchApplication({
|
|
32
|
+
applicationId,
|
|
33
|
+
token: this.token,
|
|
34
|
+
superblocksBaseUrl: this.superblocksBaseUrl,
|
|
35
|
+
viewMode,
|
|
36
|
+
injectedHeaders: headers,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async fetchApplicationWithComponents({
|
|
41
|
+
applicationId,
|
|
42
|
+
headers = {},
|
|
43
|
+
viewMode = "export-live",
|
|
44
|
+
}: {
|
|
45
|
+
applicationId: string;
|
|
46
|
+
headers?: Record<string, string>;
|
|
47
|
+
viewMode?: ViewMode;
|
|
48
|
+
}) {
|
|
49
|
+
return fetchApplicationWithComponents({
|
|
31
50
|
applicationId,
|
|
32
51
|
token: this.token,
|
|
33
52
|
superblocksBaseUrl: this.superblocksBaseUrl,
|