@superblocksteam/sdk 0.0.17 → 0.0.18

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 CHANGED
@@ -1,8 +1,19 @@
1
1
  export type ViewMode = "export-deployed" | "export-latest" | "export-live";
2
+ export interface UploadFile {
3
+ name: string;
4
+ filename: string;
5
+ ContentType: string;
6
+ }
2
7
  export declare function fetchApplication(applicationId: string, token: string, superblocksBaseUrl: string, viewMode: ViewMode): Promise<any>;
3
8
  export declare function fetchApplications(token: string, superblocksBaseUrl: string): Promise<any>;
4
9
  export declare function fetchApi(apiId: string, token: string, superblocksBaseUrl: string, viewMode: ViewMode): Promise<any>;
5
10
  export declare function fetchApis(token: string, superblocksBaseUrl: string): Promise<any>;
6
11
  export declare function registerComponents(applicationId: string, componentConfigs: Record<string, any>, token: string, superblocksBaseUrl: string): Promise<any>;
7
- export declare function uploadComponents(applicationId: string, componentConfigs: Record<string, any>, files: string[], token: string, superblocksBaseUrl: string): Promise<any>;
12
+ export declare function uploadComponents({ applicationId, componentConfigs, files, token, superblocksBaseUrl, }: {
13
+ applicationId: string;
14
+ componentConfigs: Record<string, any>;
15
+ files: string[];
16
+ token: string;
17
+ superblocksBaseUrl: string;
18
+ }): Promise<any>;
8
19
  export declare function fetchCurrentUser(token: string, superblocksBaseUrl: string): Promise<any>;
package/dist/client.js CHANGED
@@ -1,8 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fetchCurrentUser = exports.uploadComponents = exports.registerComponents = exports.fetchApis = exports.fetchApi = exports.fetchApplications = exports.fetchApplication = void 0;
4
+ const fs = require("fs");
5
+ const util_1 = require("@superblocksteam/util");
6
+ const util_2 = require("@superblocksteam/util");
4
7
  const axios_1 = require("axios");
8
+ const FormData = require("form-data");
5
9
  const BASE_SERVER_URL = "api/v1";
10
+ const BASE_BUCKETEER_URL = "api/v1";
11
+ const CLI_VERSION_HEADER = "X-Superblocks-CLI-Version";
6
12
  async function fetchApplication(applicationId, token, superblocksBaseUrl, viewMode) {
7
13
  var _a;
8
14
  const url = `/api/v2/applications/${applicationId}?viewMode=${viewMode}`;
@@ -81,12 +87,15 @@ async function fetchApis(token, superblocksBaseUrl) {
81
87
  }
82
88
  exports.fetchApis = fetchApis;
83
89
  async function registerComponents(applicationId, componentConfigs, token, superblocksBaseUrl) {
90
+ var _a, _b, _c;
84
91
  try {
92
+ const [rootSettings] = await (0, util_2.getSuperblocksMonorepoConfigJson)(true);
85
93
  const config = {
86
94
  method: "put",
87
95
  url: new URL(`${BASE_SERVER_URL}/public/application/${applicationId}/components`, superblocksBaseUrl).toString(),
88
96
  headers: {
89
97
  Authorization: "Bearer " + token,
98
+ [CLI_VERSION_HEADER]: rootSettings.metadata.cliVersion,
90
99
  },
91
100
  data: { components: componentConfigs },
92
101
  };
@@ -94,25 +103,67 @@ async function registerComponents(applicationId, componentConfigs, token, superb
94
103
  return response.data;
95
104
  }
96
105
  catch (e) {
97
- throw new Error("Could not register application components");
106
+ if (e instanceof axios_1.AxiosError) {
107
+ const message = (_c = (_b = (_a = e.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.responseMeta) === null || _c === void 0 ? void 0 : _c.message;
108
+ throw new Error(`Could not register application components
109
+ ${message !== null && message !== void 0 ? message : ""}`);
110
+ }
111
+ throw new Error(`Could not register application components
112
+ ${e.message}`);
98
113
  }
99
114
  }
100
115
  exports.registerComponents = registerComponents;
101
- async function uploadComponents(applicationId, componentConfigs, files, token, superblocksBaseUrl) {
116
+ async function uploadComponents({ applicationId, componentConfigs, files, token, superblocksBaseUrl, }) {
117
+ var _a, _b, _c;
118
+ const bucketeerBaseUrl = (0, util_1.getBucketeerUrlFromSuperblocksUrl)(superblocksBaseUrl);
119
+ // parse files to source and bundled files based on path
120
+ const uploadFiles = files.map((file, ind) => {
121
+ let contentType = (0, util_1.getContentType)(file);
122
+ if (contentType.length === 0) {
123
+ contentType = "application/octet-stream"; // default case when we don't know the encoding
124
+ }
125
+ return {
126
+ name: `file${ind + 1}`,
127
+ filename: file,
128
+ ContentType: contentType,
129
+ };
130
+ });
131
+ const buildFiles = uploadFiles.filter((uploadFile) => uploadFile.filename.startsWith("dist/"));
132
+ const srcFiles = uploadFiles.filter((uploadFile) => !uploadFile.filename.startsWith("dist/"));
133
+ const formatJSON = JSON.stringify({
134
+ applicationId,
135
+ registeredComponents: componentConfigs,
136
+ srcFiles,
137
+ buildFiles,
138
+ });
139
+ const postURL = new URL(`${BASE_BUCKETEER_URL}/components/upload`, bucketeerBaseUrl).toString();
140
+ const formData = new FormData();
141
+ formData.append("format", formatJSON);
142
+ uploadFiles.forEach((uploadFile) => {
143
+ formData.append(uploadFile.name, fs.createReadStream(uploadFile.filename));
144
+ });
145
+ const formHeaders = formData.getHeaders();
102
146
  try {
147
+ const [rootSettings] = await (0, util_2.getSuperblocksMonorepoConfigJson)(true);
103
148
  const config = {
104
- method: "post",
105
- url: new URL(`${BASE_SERVER_URL}/public/application/${applicationId}/publish`, superblocksBaseUrl).toString(),
106
149
  headers: {
107
150
  Authorization: "Bearer " + token,
151
+ [CLI_VERSION_HEADER]: rootSettings.metadata.cliVersion,
152
+ "x-superblocks-url": superblocksBaseUrl,
153
+ ...formHeaders,
108
154
  },
109
- data: { components: componentConfigs, files },
110
155
  };
111
- const response = await (0, axios_1.default)(config);
156
+ const response = await axios_1.default.post(postURL, formData, config);
112
157
  return response.data;
113
158
  }
114
159
  catch (e) {
115
- throw new Error("Could not upload application components");
160
+ if (e instanceof axios_1.AxiosError) {
161
+ const message = (_c = (_b = (_a = e.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.responseMeta) === null || _c === void 0 ? void 0 : _c.message;
162
+ throw new Error(`Could not upload application components
163
+ ${message}`);
164
+ }
165
+ throw new Error(`Could not upload application components
166
+ ${e.message}`);
116
167
  }
117
168
  }
118
169
  exports.uploadComponents = uploadComponents;
package/dist/sdk.js CHANGED
@@ -25,7 +25,13 @@ class SuperblocksSdk {
25
25
  return (0, client_1.registerComponents)(applicationId, componentConfigs, this.token, this.superblocksBaseUrl);
26
26
  }
27
27
  async uploadComponents(applicationId, componentConfigs, files) {
28
- return (0, client_1.uploadComponents)(applicationId, componentConfigs, files, this.token, this.superblocksBaseUrl);
28
+ return (0, client_1.uploadComponents)({
29
+ applicationId,
30
+ componentConfigs,
31
+ files,
32
+ token: this.token,
33
+ superblocksBaseUrl: this.superblocksBaseUrl,
34
+ });
29
35
  }
30
36
  async fetchCurrentUser() {
31
37
  return (0, client_1.fetchCurrentUser)(this.token, this.superblocksBaseUrl);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superblocksteam/sdk",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "description": "Superblocks JS SDK",
5
5
  "main": "./dist/index.js",
6
6
  "exports": {
@@ -29,6 +29,7 @@
29
29
  "typescript": "^5.1.3"
30
30
  },
31
31
  "dependencies": {
32
+ "@superblocksteam/util": "*",
32
33
  "axios": "^1.3.5"
33
34
  },
34
35
  "homepage": "https://www.superblocks.com",
package/src/client.ts CHANGED
@@ -1,9 +1,25 @@
1
- import axios from "axios";
1
+ import * as fs from "fs";
2
+ import {
3
+ getBucketeerUrlFromSuperblocksUrl,
4
+ getContentType,
5
+ } from "@superblocksteam/util";
6
+ import { getSuperblocksMonorepoConfigJson } from "@superblocksteam/util";
7
+ import axios, { AxiosError } from "axios";
8
+ import * as FormData from "form-data";
2
9
 
3
10
  const BASE_SERVER_URL = "api/v1";
11
+ const BASE_BUCKETEER_URL = "api/v1";
12
+
13
+ const CLI_VERSION_HEADER = "X-Superblocks-CLI-Version";
4
14
 
5
15
  export type ViewMode = "export-deployed" | "export-latest" | "export-live";
6
16
 
17
+ export interface UploadFile {
18
+ name: string;
19
+ filename: string;
20
+ ContentType: string;
21
+ }
22
+
7
23
  export async function fetchApplication(
8
24
  applicationId: string,
9
25
  token: string,
@@ -98,6 +114,7 @@ export async function registerComponents(
98
114
  superblocksBaseUrl: string
99
115
  ) {
100
116
  try {
117
+ const [rootSettings] = await getSuperblocksMonorepoConfigJson(true);
101
118
  const config = {
102
119
  method: "put",
103
120
  url: new URL(
@@ -106,39 +123,98 @@ export async function registerComponents(
106
123
  ).toString(),
107
124
  headers: {
108
125
  Authorization: "Bearer " + token,
126
+ [CLI_VERSION_HEADER]: rootSettings.metadata.cliVersion,
109
127
  },
110
128
  data: { components: componentConfigs },
111
129
  };
112
130
  const response = await axios(config);
113
131
  return response.data;
114
- } catch (e) {
115
- throw new Error("Could not register application components");
132
+ } catch (e: any) {
133
+ if (e instanceof AxiosError) {
134
+ const message = e.response?.data?.responseMeta?.message as string;
135
+ throw new Error(`Could not register application components
136
+ ${message ?? ""}`);
137
+ }
138
+ throw new Error(`Could not register application components
139
+ ${e.message as string}`);
116
140
  }
117
141
  }
118
142
 
119
- export async function uploadComponents(
120
- applicationId: string,
121
- componentConfigs: Record<string, any>,
122
- files: string[],
123
- token: string,
124
- superblocksBaseUrl: string
125
- ) {
143
+ export async function uploadComponents({
144
+ applicationId,
145
+ componentConfigs,
146
+ files,
147
+ token,
148
+ superblocksBaseUrl,
149
+ }: {
150
+ applicationId: string;
151
+ componentConfigs: Record<string, any>;
152
+ files: string[];
153
+ token: string;
154
+ superblocksBaseUrl: string;
155
+ }) {
156
+ const bucketeerBaseUrl =
157
+ getBucketeerUrlFromSuperblocksUrl(superblocksBaseUrl);
158
+ // parse files to source and bundled files based on path
159
+ const uploadFiles: UploadFile[] = files.map((file, ind) => {
160
+ let contentType: string = getContentType(file);
161
+ if (contentType.length === 0) {
162
+ contentType = "application/octet-stream"; // default case when we don't know the encoding
163
+ }
164
+ return {
165
+ name: `file${ind + 1}`,
166
+ filename: file,
167
+ ContentType: contentType,
168
+ } as UploadFile;
169
+ });
170
+
171
+ const buildFiles: UploadFile[] = uploadFiles.filter((uploadFile) =>
172
+ uploadFile.filename.startsWith("dist/")
173
+ );
174
+ const srcFiles: UploadFile[] = uploadFiles.filter(
175
+ (uploadFile) => !uploadFile.filename.startsWith("dist/")
176
+ );
177
+
178
+ const formatJSON = JSON.stringify({
179
+ applicationId,
180
+ registeredComponents: componentConfigs,
181
+ srcFiles,
182
+ buildFiles,
183
+ });
184
+ const postURL = new URL(
185
+ `${BASE_BUCKETEER_URL}/components/upload`,
186
+ bucketeerBaseUrl
187
+ ).toString();
188
+
189
+ const formData = new FormData();
190
+ formData.append("format", formatJSON);
191
+ uploadFiles.forEach((uploadFile) => {
192
+ formData.append(uploadFile.name, fs.createReadStream(uploadFile.filename));
193
+ });
194
+ const formHeaders = formData.getHeaders();
195
+
126
196
  try {
197
+ const [rootSettings] = await getSuperblocksMonorepoConfigJson(true);
127
198
  const config = {
128
- method: "post",
129
- url: new URL(
130
- `${BASE_SERVER_URL}/public/application/${applicationId}/publish`,
131
- superblocksBaseUrl
132
- ).toString(),
133
199
  headers: {
134
200
  Authorization: "Bearer " + token,
201
+ [CLI_VERSION_HEADER]: rootSettings.metadata.cliVersion,
202
+ "x-superblocks-url": superblocksBaseUrl,
203
+ ...formHeaders,
135
204
  },
136
- data: { components: componentConfigs, files },
137
205
  };
138
- const response = await axios(config);
206
+
207
+ const response = await axios.post(postURL, formData, config);
208
+
139
209
  return response.data;
140
- } catch (e) {
141
- throw new Error("Could not upload application components");
210
+ } catch (e: any) {
211
+ if (e instanceof AxiosError) {
212
+ const message = e.response?.data?.responseMeta?.message as string;
213
+ throw new Error(`Could not upload application components
214
+ ${message}`);
215
+ }
216
+ throw new Error(`Could not upload application components
217
+ ${e.message as string}`);
142
218
  }
143
219
  }
144
220
 
package/src/sdk.ts CHANGED
@@ -59,13 +59,13 @@ export class SuperblocksSdk {
59
59
  componentConfigs: Record<string, any>,
60
60
  files: string[]
61
61
  ) {
62
- return uploadComponents(
62
+ return uploadComponents({
63
63
  applicationId,
64
64
  componentConfigs,
65
65
  files,
66
- this.token,
67
- this.superblocksBaseUrl
68
- );
66
+ token: this.token,
67
+ superblocksBaseUrl: this.superblocksBaseUrl,
68
+ });
69
69
  }
70
70
 
71
71
  async fetchCurrentUser() {