@superblocksteam/sdk 0.0.17 → 0.0.19

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,68 @@ 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
+ superblocksBaseUrl = superblocksBaseUrl.replace(/\/$/, "");
119
+ const bucketeerBaseUrl = (0, util_1.getBucketeerUrlFromSuperblocksUrl)(superblocksBaseUrl);
120
+ // parse files to source and bundled files based on path
121
+ const uploadFiles = files.map((file, ind) => {
122
+ let contentType = (0, util_1.getContentType)(file);
123
+ if (contentType.length === 0) {
124
+ contentType = "application/octet-stream"; // default case when we don't know the encoding
125
+ }
126
+ return {
127
+ name: `file${ind + 1}`,
128
+ filename: file,
129
+ ContentType: contentType,
130
+ };
131
+ });
132
+ const buildFiles = uploadFiles.filter((uploadFile) => uploadFile.filename.startsWith("dist/"));
133
+ const srcFiles = uploadFiles.filter((uploadFile) => !uploadFile.filename.startsWith("dist/"));
134
+ const formatJSON = JSON.stringify({
135
+ applicationId,
136
+ registeredComponents: componentConfigs,
137
+ srcFiles,
138
+ buildFiles,
139
+ });
140
+ const postURL = new URL(`${BASE_BUCKETEER_URL}/components/upload`, bucketeerBaseUrl).toString();
141
+ const formData = new FormData();
142
+ formData.append("format", formatJSON);
143
+ uploadFiles.forEach((uploadFile) => {
144
+ formData.append(uploadFile.name, fs.createReadStream(uploadFile.filename));
145
+ });
146
+ const formHeaders = formData.getHeaders();
102
147
  try {
148
+ const [rootSettings] = await (0, util_2.getSuperblocksMonorepoConfigJson)(true);
103
149
  const config = {
104
- method: "post",
105
- url: new URL(`${BASE_SERVER_URL}/public/application/${applicationId}/publish`, superblocksBaseUrl).toString(),
106
150
  headers: {
107
151
  Authorization: "Bearer " + token,
152
+ [CLI_VERSION_HEADER]: rootSettings.metadata.cliVersion,
153
+ "x-superblocks-url": superblocksBaseUrl,
154
+ ...formHeaders,
108
155
  },
109
- data: { components: componentConfigs, files },
110
156
  };
111
- const response = await (0, axios_1.default)(config);
157
+ const response = await axios_1.default.post(postURL, formData, config);
112
158
  return response.data;
113
159
  }
114
160
  catch (e) {
115
- throw new Error("Could not upload application components");
161
+ if (e instanceof axios_1.AxiosError) {
162
+ 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;
163
+ throw new Error(`Could not upload application components
164
+ ${message}`);
165
+ }
166
+ throw new Error(`Could not upload application components
167
+ ${e.message}`);
116
168
  }
117
169
  }
118
170
  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.19",
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,99 @@ 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
+ superblocksBaseUrl = superblocksBaseUrl.replace(/\/$/, "");
157
+ const bucketeerBaseUrl =
158
+ getBucketeerUrlFromSuperblocksUrl(superblocksBaseUrl);
159
+ // parse files to source and bundled files based on path
160
+ const uploadFiles: UploadFile[] = files.map((file, ind) => {
161
+ let contentType: string = getContentType(file);
162
+ if (contentType.length === 0) {
163
+ contentType = "application/octet-stream"; // default case when we don't know the encoding
164
+ }
165
+ return {
166
+ name: `file${ind + 1}`,
167
+ filename: file,
168
+ ContentType: contentType,
169
+ } as UploadFile;
170
+ });
171
+
172
+ const buildFiles: UploadFile[] = uploadFiles.filter((uploadFile) =>
173
+ uploadFile.filename.startsWith("dist/")
174
+ );
175
+ const srcFiles: UploadFile[] = uploadFiles.filter(
176
+ (uploadFile) => !uploadFile.filename.startsWith("dist/")
177
+ );
178
+
179
+ const formatJSON = JSON.stringify({
180
+ applicationId,
181
+ registeredComponents: componentConfigs,
182
+ srcFiles,
183
+ buildFiles,
184
+ });
185
+ const postURL = new URL(
186
+ `${BASE_BUCKETEER_URL}/components/upload`,
187
+ bucketeerBaseUrl
188
+ ).toString();
189
+
190
+ const formData = new FormData();
191
+ formData.append("format", formatJSON);
192
+ uploadFiles.forEach((uploadFile) => {
193
+ formData.append(uploadFile.name, fs.createReadStream(uploadFile.filename));
194
+ });
195
+ const formHeaders = formData.getHeaders();
196
+
126
197
  try {
198
+ const [rootSettings] = await getSuperblocksMonorepoConfigJson(true);
127
199
  const config = {
128
- method: "post",
129
- url: new URL(
130
- `${BASE_SERVER_URL}/public/application/${applicationId}/publish`,
131
- superblocksBaseUrl
132
- ).toString(),
133
200
  headers: {
134
201
  Authorization: "Bearer " + token,
202
+ [CLI_VERSION_HEADER]: rootSettings.metadata.cliVersion,
203
+ "x-superblocks-url": superblocksBaseUrl,
204
+ ...formHeaders,
135
205
  },
136
- data: { components: componentConfigs, files },
137
206
  };
138
- const response = await axios(config);
207
+
208
+ const response = await axios.post(postURL, formData, config);
209
+
139
210
  return response.data;
140
- } catch (e) {
141
- throw new Error("Could not upload application components");
211
+ } catch (e: any) {
212
+ if (e instanceof AxiosError) {
213
+ const message = e.response?.data?.responseMeta?.message as string;
214
+ throw new Error(`Could not upload application components
215
+ ${message}`);
216
+ }
217
+ throw new Error(`Could not upload application components
218
+ ${e.message as string}`);
142
219
  }
143
220
  }
144
221
 
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() {