@superblocksteam/sdk 1.2.0 → 1.3.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 CHANGED
@@ -1,10 +1,5 @@
1
+ import { ComponentEvent, LocalGitRepoState, SuperblocksResourceType } from "@superblocksteam/util";
1
2
  export type ViewMode = "export-deployed" | "export-latest" | "export-live";
2
- export declare class BranchNotCheckedOutError extends Error {
3
- constructor(message: string);
4
- }
5
- export declare class CommitAlreadyExistsError extends Error {
6
- constructor(message: string);
7
- }
8
3
  export interface UploadFile {
9
4
  name: string;
10
5
  filename: string;
@@ -28,7 +23,7 @@ export type Branch = {
28
23
  };
29
24
  export declare function fetchApplication({ applicationId, branch, token, superblocksBaseUrl, viewMode, injectedHeaders, }: {
30
25
  applicationId: string;
31
- branch: string;
26
+ branch?: string;
32
27
  token: string;
33
28
  superblocksBaseUrl: string;
34
29
  viewMode: ViewMode;
@@ -53,6 +48,9 @@ export declare function fetchApplicationWithComponents({ applicationId, branch,
53
48
  export declare function fetchApplications(token: string, superblocksBaseUrl: string, injectedHeaders?: Record<string, string>): Promise<any>;
54
49
  export declare function fetchApi(apiId: string, token: string, superblocksBaseUrl: string, viewMode: ViewMode): Promise<any>;
55
50
  export declare function fetchApis(token: string, superblocksBaseUrl: string): Promise<any>;
51
+ export declare function validateGitSetup(resourceId: string, resourceType: Exclude<SuperblocksResourceType, "BACKEND">, event: ComponentEvent, localGitRepoState: LocalGitRepoState, token: string, superblocksBaseUrl: string, injectedHeaders?: Record<string, string>): Promise<{
52
+ branchName: string | null;
53
+ }>;
56
54
  export declare function registerComponents(applicationId: string, componentConfigs: Record<string, any>, token: string, superblocksBaseUrl: string, branch: string | null, injectedHeaders?: Record<string, string>): Promise<any>;
57
55
  export declare function uploadComponents({ applicationId, componentConfigs, files, token, superblocksBaseUrl, branch, }: {
58
56
  applicationId: string;
package/dist/client.js CHANGED
@@ -1,33 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.pushApplication = exports.fetchCurrentUser = exports.uploadComponents = exports.registerComponents = exports.fetchApis = exports.fetchApi = exports.fetchApplications = exports.fetchApplicationWithComponents = exports.fetchApplicationBranches = exports.fetchApplication = exports.CommitAlreadyExistsError = exports.BranchNotCheckedOutError = void 0;
3
+ exports.pushApplication = exports.fetchCurrentUser = exports.uploadComponents = exports.registerComponents = exports.validateGitSetup = exports.fetchApis = exports.fetchApi = exports.fetchApplications = exports.fetchApplicationWithComponents = exports.fetchApplicationBranches = exports.fetchApplication = void 0;
4
4
  const fs = require("fs");
5
5
  const util_1 = require("@superblocksteam/util");
6
6
  const axios_1 = require("axios");
7
7
  const FormData = require("form-data");
8
8
  const lodash_1 = require("lodash");
9
+ const errors_1 = require("./errors");
9
10
  const BASE_SERVER_URL = "api/v1";
10
11
  const BASE_BUCKETEER_URL = "api/v1";
11
12
  const SUPERBLOCKS_MAX_FILE_SIZE_MB = 10;
12
13
  const CLI_VERSION_HEADER = "x-superblocks-cli-version";
13
14
  const SUPERBLOCKS_URL_HEADER = "x-superblocks-url";
14
- class BranchNotCheckedOutError extends Error {
15
- constructor(message) {
16
- super(message);
17
- this.name = "BranchNotCheckedOutError";
18
- }
19
- }
20
- exports.BranchNotCheckedOutError = BranchNotCheckedOutError;
21
- class CommitAlreadyExistsError extends Error {
22
- constructor(message) {
23
- super(message);
24
- this.name = "CommitAlreadyExistsError";
25
- }
26
- }
27
- exports.CommitAlreadyExistsError = CommitAlreadyExistsError;
28
15
  async function fetchApplication({ applicationId, branch, token, superblocksBaseUrl, viewMode, injectedHeaders = {}, }) {
29
16
  var _a, _b;
30
- const serverURL = new URL(`api/v2/applications/${applicationId}/branches/${branch}?viewMode=${viewMode}`, superblocksBaseUrl);
17
+ const serverURL = branch
18
+ ? new URL(`api/v2/applications/${applicationId}/branches/${branch}?viewMode=${viewMode}`, superblocksBaseUrl)
19
+ : new URL(`api/v2/applications/${applicationId}?viewMode=${viewMode}`, superblocksBaseUrl);
31
20
  let serverResponse;
32
21
  try {
33
22
  const config = {
@@ -98,7 +87,8 @@ async function fetchApplicationWithComponents({ applicationId, branch, token, su
98
87
  superblocksBaseUrl = superblocksBaseUrl.replace(/\/$/, "");
99
88
  const bucketeerBaseUrl = (0, util_1.getBucketeerUrlFromSuperblocksUrl)(superblocksBaseUrl);
100
89
  // fetch files from bucketeer
101
- const componentFileURL = new URL(`${BASE_BUCKETEER_URL}/components/${applicationId}?commit=${commitId}&viewMode=${viewMode}`, bucketeerBaseUrl).toString();
90
+ const branchPath = branch ? `/branches/${encodeURIComponent(branch)}` : "";
91
+ const componentFileURL = new URL(`${BASE_BUCKETEER_URL}/components/${applicationId}${branchPath}?commit=${commitId}&viewMode=${viewMode}`, bucketeerBaseUrl).toString();
102
92
  try {
103
93
  const config = {
104
94
  method: "get",
@@ -176,6 +166,61 @@ async function fetchApis(token, superblocksBaseUrl) {
176
166
  }
177
167
  }
178
168
  exports.fetchApis = fetchApis;
169
+ async function validateGitSetup(resourceId,
170
+ // TODO: change the type to `SuperblocksResourceType` once we support other resource types
171
+ resourceType, event, localGitRepoState, token, superblocksBaseUrl, injectedHeaders) {
172
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
173
+ let resourceName;
174
+ try {
175
+ const [rootSettings] = await (0, util_1.getSuperblocksMonorepoConfigJson)(true);
176
+ const requestBody = {
177
+ event,
178
+ gitState: localGitRepoState,
179
+ };
180
+ let path;
181
+ switch (resourceType) {
182
+ case "APPLICATION": {
183
+ path = `/public/application/${resourceId}/validate-git-setup`;
184
+ break;
185
+ }
186
+ default:
187
+ (0, util_1.unreachable)(resourceType);
188
+ }
189
+ const config = {
190
+ method: "post",
191
+ url: new URL(`${BASE_SERVER_URL}/${path}`, superblocksBaseUrl).toString(),
192
+ headers: {
193
+ Authorization: "Bearer " + token,
194
+ [CLI_VERSION_HEADER]: rootSettings.metadata.cliVersion,
195
+ ...injectedHeaders,
196
+ },
197
+ data: requestBody,
198
+ };
199
+ const response = await (0, axios_1.default)(config);
200
+ const { validationResult } = response.data.data;
201
+ if (validationResult.errors.length > 0) {
202
+ if (validationResult.resourceName) {
203
+ resourceName = validationResult.resourceName;
204
+ }
205
+ throw new Error(validationResult.errors.join("\n"));
206
+ }
207
+ return {
208
+ branchName: ((_a = validationResult.branchName) !== null && _a !== void 0 ? _a : null),
209
+ };
210
+ }
211
+ catch (e) {
212
+ let message;
213
+ if (e instanceof axios_1.AxiosError) {
214
+ message =
215
+ (_k = (_j = (_g = (_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 : JSON.stringify((_f = e.response) === null || _f === void 0 ? void 0 : _f.data)) !== null && _g !== void 0 ? _g : (_h = e.response) === null || _h === void 0 ? void 0 : _h.statusText) !== null && _j !== void 0 ? _j : e === null || e === void 0 ? void 0 : e.message) !== null && _k !== void 0 ? _k : `${e}`;
216
+ }
217
+ else {
218
+ message = `${(e === null || e === void 0 ? void 0 : e.message) ? e === null || e === void 0 ? void 0 : e.message : e}`;
219
+ }
220
+ throw new Error(`Could not validate your git setup against Superblocks for the ${resourceType.toLowerCase()} ${resourceName !== null && resourceName !== void 0 ? resourceName : resourceId}${message ? "\n" + message : ""}`);
221
+ }
222
+ }
223
+ exports.validateGitSetup = validateGitSetup;
179
224
  async function registerComponents(applicationId, componentConfigs, token, superblocksBaseUrl, branch, injectedHeaders) {
180
225
  var _a, _b, _c, _d, _e, _f, _g;
181
226
  try {
@@ -324,10 +369,10 @@ async function pushApplication({ applicationId, token, superblocksBaseUrl, appli
324
369
  if (axios_1.default.isAxiosError(e)) {
325
370
  const respCode = (_d = (_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.status) !== null && _d !== void 0 ? _d : (_e = e.response) === null || _e === void 0 ? void 0 : _e.status;
326
371
  if (respCode === 405) {
327
- throw new BranchNotCheckedOutError(`Branch ${branch} is not checked out`);
372
+ throw new errors_1.BranchNotCheckedOutError(`Branch ${branch} is not checked out`);
328
373
  }
329
374
  else if (respCode === 409) {
330
- throw new CommitAlreadyExistsError(`Commit ${applicationConfig.commitId} already exists`);
375
+ throw new errors_1.CommitAlreadyExistsError(`Commit ${applicationConfig.commitId} already exists`);
331
376
  }
332
377
  const message = (_l = (_j = (_h = (_g = (_f = e.response) === null || _f === void 0 ? void 0 : _f.data) === null || _g === void 0 ? void 0 : _g.responseMeta) === null || _h === void 0 ? void 0 : _h.message) !== null && _j !== void 0 ? _j : JSON.stringify((_k = e.response) === null || _k === void 0 ? void 0 : _k.data)) !== null && _l !== void 0 ? _l : (_m = e.response) === null || _m === void 0 ? void 0 : _m.statusText;
333
378
  throw new Error(`Could not push application ${message ? "\n" + message : ""}`);
@@ -0,0 +1,6 @@
1
+ export declare class BranchNotCheckedOutError extends Error {
2
+ constructor(message: string);
3
+ }
4
+ export declare class CommitAlreadyExistsError extends Error {
5
+ constructor(message: string);
6
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CommitAlreadyExistsError = exports.BranchNotCheckedOutError = void 0;
4
+ class BranchNotCheckedOutError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = "BranchNotCheckedOutError";
8
+ }
9
+ }
10
+ exports.BranchNotCheckedOutError = BranchNotCheckedOutError;
11
+ class CommitAlreadyExistsError extends Error {
12
+ constructor(message) {
13
+ super(message);
14
+ this.name = "CommitAlreadyExistsError";
15
+ }
16
+ }
17
+ exports.CommitAlreadyExistsError = CommitAlreadyExistsError;
package/dist/sdk.d.ts CHANGED
@@ -1,11 +1,13 @@
1
+ import { ComponentEvent, LocalGitRepoState, SuperblocksResourceType } from "@superblocksteam/util";
1
2
  import { ViewMode, PushApplicationWithCommitConfig } from "./client";
3
+ export * from "./errors";
2
4
  export declare class SuperblocksSdk {
3
5
  token: string;
4
6
  superblocksBaseUrl: string;
5
7
  constructor(accessToken: string, superblocksBaseUrl: string);
6
8
  fetchApplication({ applicationId, branch, headers, viewMode, }: {
7
9
  applicationId: string;
8
- branch: string;
10
+ branch?: string;
9
11
  headers?: Record<string, string>;
10
12
  viewMode?: ViewMode;
11
13
  }): Promise<import("./client").ApplicationWrapper | undefined>;
@@ -21,6 +23,9 @@ export declare class SuperblocksSdk {
21
23
  fetchApplications(headers?: Record<string, string>): Promise<any>;
22
24
  fetchApi(apiId: string, viewMode?: ViewMode): Promise<any>;
23
25
  fetchApis(): Promise<any>;
26
+ validateGitSetup(resourceId: string, resourceType: Exclude<SuperblocksResourceType, "BACKEND">, event: ComponentEvent, localGitRepoState: LocalGitRepoState, injectedHeaders?: Record<string, string>): Promise<{
27
+ branchName: string | null;
28
+ }>;
24
29
  registerComponents(applicationId: string, componentConfigs: Record<string, any>, branch: string | null, injectedHeaders?: Record<string, string>): Promise<any>;
25
30
  uploadComponents(applicationId: string, componentConfigs: Record<string, any>, files: string[], branch: string | null): Promise<any>;
26
31
  fetchCurrentUser(): Promise<any>;
package/dist/sdk.js CHANGED
@@ -1,7 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SuperblocksSdk = void 0;
4
+ const tslib_1 = require("tslib");
4
5
  const client_1 = require("./client");
6
+ // Exporting here instead of index.ts because only sdk.ts is exposed outside in package.json
7
+ tslib_1.__exportStar(require("./errors"), exports);
5
8
  class SuperblocksSdk {
6
9
  constructor(accessToken, superblocksBaseUrl) {
7
10
  this.token = "";
@@ -46,6 +49,11 @@ class SuperblocksSdk {
46
49
  async fetchApis() {
47
50
  return (0, client_1.fetchApis)(this.token, this.superblocksBaseUrl);
48
51
  }
52
+ async validateGitSetup(resourceId,
53
+ // TODO: change the type to `SuperblocksResourceType` once we support other resource types
54
+ resourceType, event, localGitRepoState, injectedHeaders) {
55
+ return (0, client_1.validateGitSetup)(resourceId, resourceType, event, localGitRepoState, this.token, this.superblocksBaseUrl, injectedHeaders);
56
+ }
49
57
  async registerComponents(applicationId, componentConfigs, branch, injectedHeaders) {
50
58
  return (0, client_1.registerComponents)(applicationId, componentConfigs, this.token, this.superblocksBaseUrl, branch, injectedHeaders);
51
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superblocksteam/sdk",
3
- "version": "1.2.0",
3
+ "version": "1.3.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": "1.2.0",
33
+ "@superblocksteam/util": "1.3.0",
34
34
  "axios": "^1.3.5"
35
35
  },
36
36
  "homepage": "https://www.superblocks.com",
package/src/client.ts CHANGED
@@ -6,10 +6,15 @@ import {
6
6
  getContentType,
7
7
  COMPONENT_EVENT_HEADER,
8
8
  NotFoundError,
9
+ LocalGitRepoState,
10
+ ValidateGitSetupRequestBody,
11
+ SuperblocksResourceType,
12
+ unreachable,
9
13
  } from "@superblocksteam/util";
10
14
  import axios, { AxiosError, AxiosRequestConfig } from "axios";
11
15
  import * as FormData from "form-data";
12
16
  import { isEmpty } from "lodash";
17
+ import { BranchNotCheckedOutError, CommitAlreadyExistsError } from "./errors";
13
18
 
14
19
  const BASE_SERVER_URL = "api/v1";
15
20
  const BASE_BUCKETEER_URL = "api/v1";
@@ -21,20 +26,6 @@ const SUPERBLOCKS_URL_HEADER = "x-superblocks-url";
21
26
 
22
27
  export type ViewMode = "export-deployed" | "export-latest" | "export-live";
23
28
 
24
- export class BranchNotCheckedOutError extends Error {
25
- constructor(message: string) {
26
- super(message);
27
- this.name = "BranchNotCheckedOutError";
28
- }
29
- }
30
-
31
- export class CommitAlreadyExistsError extends Error {
32
- constructor(message: string) {
33
- super(message);
34
- this.name = "CommitAlreadyExistsError";
35
- }
36
- }
37
-
38
29
  export interface UploadFile {
39
30
  name: string;
40
31
  filename: string;
@@ -72,16 +63,22 @@ export async function fetchApplication({
72
63
  injectedHeaders = {},
73
64
  }: {
74
65
  applicationId: string;
75
- branch: string;
66
+ branch?: string;
76
67
  token: string;
77
68
  superblocksBaseUrl: string;
78
69
  viewMode: ViewMode;
79
70
  injectedHeaders: Record<string, string>;
80
71
  }): Promise<ApplicationWrapper | undefined> {
81
- const serverURL = new URL(
82
- `api/v2/applications/${applicationId}/branches/${branch}?viewMode=${viewMode}`,
83
- superblocksBaseUrl
84
- );
72
+ const serverURL = branch
73
+ ? new URL(
74
+ `api/v2/applications/${applicationId}/branches/${branch}?viewMode=${viewMode}`,
75
+ superblocksBaseUrl
76
+ )
77
+ : new URL(
78
+ `api/v2/applications/${applicationId}?viewMode=${viewMode}`,
79
+ superblocksBaseUrl
80
+ );
81
+
85
82
  let serverResponse;
86
83
 
87
84
  try {
@@ -184,8 +181,9 @@ export async function fetchApplicationWithComponents({
184
181
  getBucketeerUrlFromSuperblocksUrl(superblocksBaseUrl);
185
182
 
186
183
  // fetch files from bucketeer
184
+ const branchPath = branch ? `/branches/${encodeURIComponent(branch)}` : "";
187
185
  const componentFileURL = new URL(
188
- `${BASE_BUCKETEER_URL}/components/${applicationId}?commit=${commitId}&viewMode=${viewMode}`,
186
+ `${BASE_BUCKETEER_URL}/components/${applicationId}${branchPath}?commit=${commitId}&viewMode=${viewMode}`,
189
187
  bucketeerBaseUrl
190
188
  ).toString();
191
189
 
@@ -273,6 +271,73 @@ export async function fetchApis(token: string, superblocksBaseUrl: string) {
273
271
  }
274
272
  }
275
273
 
274
+ export async function validateGitSetup(
275
+ resourceId: string,
276
+ // TODO: change the type to `SuperblocksResourceType` once we support other resource types
277
+ resourceType: Exclude<SuperblocksResourceType, "BACKEND">,
278
+ event: ComponentEvent,
279
+ localGitRepoState: LocalGitRepoState,
280
+ token: string,
281
+ superblocksBaseUrl: string,
282
+ injectedHeaders?: Record<string, string>
283
+ ) {
284
+ let resourceName: string | undefined;
285
+ try {
286
+ const [rootSettings] = await getSuperblocksMonorepoConfigJson(true);
287
+ const requestBody: ValidateGitSetupRequestBody = {
288
+ event,
289
+ gitState: localGitRepoState,
290
+ };
291
+ let path: string;
292
+ switch (resourceType) {
293
+ case "APPLICATION": {
294
+ path = `/public/application/${resourceId}/validate-git-setup`;
295
+ break;
296
+ }
297
+ default:
298
+ unreachable(resourceType);
299
+ }
300
+ const config: AxiosRequestConfig = {
301
+ method: "post",
302
+ url: new URL(`${BASE_SERVER_URL}/${path}`, superblocksBaseUrl).toString(),
303
+ headers: {
304
+ Authorization: "Bearer " + token,
305
+ [CLI_VERSION_HEADER]: rootSettings.metadata.cliVersion,
306
+ ...injectedHeaders,
307
+ },
308
+ data: requestBody,
309
+ };
310
+ const response = await axios(config);
311
+ const { validationResult } = response.data.data;
312
+ if (validationResult.errors.length > 0) {
313
+ if (validationResult.resourceName) {
314
+ resourceName = validationResult.resourceName;
315
+ }
316
+ throw new Error(validationResult.errors.join("\n"));
317
+ }
318
+ return {
319
+ branchName: (validationResult.branchName ?? null) as string | null,
320
+ };
321
+ } catch (e: any) {
322
+ let message: string;
323
+ if (e instanceof AxiosError) {
324
+ message =
325
+ (e.response?.data?.responseMeta?.message as string) ??
326
+ JSON.stringify(e.response?.data) ??
327
+ e.response?.statusText ??
328
+ e?.message ??
329
+ `${e}`;
330
+ } else {
331
+ message = `${e?.message ? e?.message : e}`;
332
+ }
333
+ throw new Error(
334
+ `Could not validate your git setup against Superblocks for the ${resourceType.toLowerCase()} ${
335
+ resourceName ?? resourceId
336
+ }${message ? "\n" + message : ""}`
337
+ );
338
+ }
339
+ }
340
+
276
341
  export async function registerComponents(
277
342
  applicationId: string,
278
343
  componentConfigs: Record<string, any>,
package/src/errors.ts ADDED
@@ -0,0 +1,13 @@
1
+ export class BranchNotCheckedOutError extends Error {
2
+ constructor(message: string) {
3
+ super(message);
4
+ this.name = "BranchNotCheckedOutError";
5
+ }
6
+ }
7
+
8
+ export class CommitAlreadyExistsError extends Error {
9
+ constructor(message: string) {
10
+ super(message);
11
+ this.name = "CommitAlreadyExistsError";
12
+ }
13
+ }
package/src/sdk.ts CHANGED
@@ -1,3 +1,8 @@
1
+ import {
2
+ ComponentEvent,
3
+ LocalGitRepoState,
4
+ SuperblocksResourceType,
5
+ } from "@superblocksteam/util";
1
6
  import {
2
7
  fetchApi,
3
8
  fetchApis,
@@ -11,8 +16,12 @@ import {
11
16
  pushApplication,
12
17
  PushApplicationWithCommitConfig,
13
18
  fetchApplicationBranches,
19
+ validateGitSetup,
14
20
  } from "./client";
15
21
 
22
+ // Exporting here instead of index.ts because only sdk.ts is exposed outside in package.json
23
+ export * from "./errors";
24
+
16
25
  export class SuperblocksSdk {
17
26
  token = "";
18
27
  superblocksBaseUrl = "";
@@ -29,7 +38,7 @@ export class SuperblocksSdk {
29
38
  viewMode = "export-live",
30
39
  }: {
31
40
  applicationId: string;
32
- branch: string;
41
+ branch?: string;
33
42
  headers?: Record<string, string>;
34
43
  viewMode?: ViewMode;
35
44
  }) {
@@ -85,6 +94,25 @@ export class SuperblocksSdk {
85
94
  return fetchApis(this.token, this.superblocksBaseUrl);
86
95
  }
87
96
 
97
+ async validateGitSetup(
98
+ resourceId: string,
99
+ // TODO: change the type to `SuperblocksResourceType` once we support other resource types
100
+ resourceType: Exclude<SuperblocksResourceType, "BACKEND">,
101
+ event: ComponentEvent,
102
+ localGitRepoState: LocalGitRepoState,
103
+ injectedHeaders?: Record<string, string>
104
+ ) {
105
+ return validateGitSetup(
106
+ resourceId,
107
+ resourceType,
108
+ event,
109
+ localGitRepoState,
110
+ this.token,
111
+ this.superblocksBaseUrl,
112
+ injectedHeaders
113
+ );
114
+ }
115
+
88
116
  async registerComponents(
89
117
  applicationId: string,
90
118
  componentConfigs: Record<string, any>,