@superblocksteam/sdk 1.2.1 → 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,3 +1,4 @@
1
+ import { ComponentEvent, LocalGitRepoState, SuperblocksResourceType } from "@superblocksteam/util";
1
2
  export type ViewMode = "export-deployed" | "export-latest" | "export-live";
2
3
  export interface UploadFile {
3
4
  name: string;
@@ -22,7 +23,7 @@ export type Branch = {
22
23
  };
23
24
  export declare function fetchApplication({ applicationId, branch, token, superblocksBaseUrl, viewMode, injectedHeaders, }: {
24
25
  applicationId: string;
25
- branch: string;
26
+ branch?: string;
26
27
  token: string;
27
28
  superblocksBaseUrl: string;
28
29
  viewMode: ViewMode;
@@ -47,6 +48,9 @@ export declare function fetchApplicationWithComponents({ applicationId, branch,
47
48
  export declare function fetchApplications(token: string, superblocksBaseUrl: string, injectedHeaders?: Record<string, string>): Promise<any>;
48
49
  export declare function fetchApi(apiId: string, token: string, superblocksBaseUrl: string, viewMode: ViewMode): Promise<any>;
49
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
+ }>;
50
54
  export declare function registerComponents(applicationId: string, componentConfigs: Record<string, any>, token: string, superblocksBaseUrl: string, branch: string | null, injectedHeaders?: Record<string, string>): Promise<any>;
51
55
  export declare function uploadComponents({ applicationId, componentConfigs, files, token, superblocksBaseUrl, branch, }: {
52
56
  applicationId: string;
package/dist/client.js CHANGED
@@ -1,6 +1,6 @@
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 = 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");
@@ -14,7 +14,9 @@ const CLI_VERSION_HEADER = "x-superblocks-cli-version";
14
14
  const SUPERBLOCKS_URL_HEADER = "x-superblocks-url";
15
15
  async function fetchApplication({ applicationId, branch, token, superblocksBaseUrl, viewMode, injectedHeaders = {}, }) {
16
16
  var _a, _b;
17
- 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);
18
20
  let serverResponse;
19
21
  try {
20
22
  const config = {
@@ -85,7 +87,8 @@ async function fetchApplicationWithComponents({ applicationId, branch, token, su
85
87
  superblocksBaseUrl = superblocksBaseUrl.replace(/\/$/, "");
86
88
  const bucketeerBaseUrl = (0, util_1.getBucketeerUrlFromSuperblocksUrl)(superblocksBaseUrl);
87
89
  // fetch files from bucketeer
88
- 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();
89
92
  try {
90
93
  const config = {
91
94
  method: "get",
@@ -163,6 +166,61 @@ async function fetchApis(token, superblocksBaseUrl) {
163
166
  }
164
167
  }
165
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;
166
224
  async function registerComponents(applicationId, componentConfigs, token, superblocksBaseUrl, branch, injectedHeaders) {
167
225
  var _a, _b, _c, _d, _e, _f, _g;
168
226
  try {
package/dist/sdk.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { ComponentEvent, LocalGitRepoState, SuperblocksResourceType } from "@superblocksteam/util";
1
2
  import { ViewMode, PushApplicationWithCommitConfig } from "./client";
2
3
  export * from "./errors";
3
4
  export declare class SuperblocksSdk {
@@ -6,7 +7,7 @@ export declare class SuperblocksSdk {
6
7
  constructor(accessToken: string, superblocksBaseUrl: string);
7
8
  fetchApplication({ applicationId, branch, headers, viewMode, }: {
8
9
  applicationId: string;
9
- branch: string;
10
+ branch?: string;
10
11
  headers?: Record<string, string>;
11
12
  viewMode?: ViewMode;
12
13
  }): Promise<import("./client").ApplicationWrapper | undefined>;
@@ -22,6 +23,9 @@ export declare class SuperblocksSdk {
22
23
  fetchApplications(headers?: Record<string, string>): Promise<any>;
23
24
  fetchApi(apiId: string, viewMode?: ViewMode): Promise<any>;
24
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
+ }>;
25
29
  registerComponents(applicationId: string, componentConfigs: Record<string, any>, branch: string | null, injectedHeaders?: Record<string, string>): Promise<any>;
26
30
  uploadComponents(applicationId: string, componentConfigs: Record<string, any>, files: string[], branch: string | null): Promise<any>;
27
31
  fetchCurrentUser(): Promise<any>;
package/dist/sdk.js CHANGED
@@ -49,6 +49,11 @@ class SuperblocksSdk {
49
49
  async fetchApis() {
50
50
  return (0, client_1.fetchApis)(this.token, this.superblocksBaseUrl);
51
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
+ }
52
57
  async registerComponents(applicationId, componentConfigs, branch, injectedHeaders) {
53
58
  return (0, client_1.registerComponents)(applicationId, componentConfigs, this.token, this.superblocksBaseUrl, branch, injectedHeaders);
54
59
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superblocksteam/sdk",
3
- "version": "1.2.1",
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.1",
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,6 +6,10 @@ 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";
@@ -59,16 +63,22 @@ export async function fetchApplication({
59
63
  injectedHeaders = {},
60
64
  }: {
61
65
  applicationId: string;
62
- branch: string;
66
+ branch?: string;
63
67
  token: string;
64
68
  superblocksBaseUrl: string;
65
69
  viewMode: ViewMode;
66
70
  injectedHeaders: Record<string, string>;
67
71
  }): Promise<ApplicationWrapper | undefined> {
68
- const serverURL = new URL(
69
- `api/v2/applications/${applicationId}/branches/${branch}?viewMode=${viewMode}`,
70
- superblocksBaseUrl
71
- );
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
+
72
82
  let serverResponse;
73
83
 
74
84
  try {
@@ -171,8 +181,9 @@ export async function fetchApplicationWithComponents({
171
181
  getBucketeerUrlFromSuperblocksUrl(superblocksBaseUrl);
172
182
 
173
183
  // fetch files from bucketeer
184
+ const branchPath = branch ? `/branches/${encodeURIComponent(branch)}` : "";
174
185
  const componentFileURL = new URL(
175
- `${BASE_BUCKETEER_URL}/components/${applicationId}?commit=${commitId}&viewMode=${viewMode}`,
186
+ `${BASE_BUCKETEER_URL}/components/${applicationId}${branchPath}?commit=${commitId}&viewMode=${viewMode}`,
176
187
  bucketeerBaseUrl
177
188
  ).toString();
178
189
 
@@ -260,6 +271,73 @@ export async function fetchApis(token: string, superblocksBaseUrl: string) {
260
271
  }
261
272
  }
262
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
+
263
341
  export async function registerComponents(
264
342
  applicationId: string,
265
343
  componentConfigs: Record<string, any>,
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,6 +16,7 @@ import {
11
16
  pushApplication,
12
17
  PushApplicationWithCommitConfig,
13
18
  fetchApplicationBranches,
19
+ validateGitSetup,
14
20
  } from "./client";
15
21
 
16
22
  // Exporting here instead of index.ts because only sdk.ts is exposed outside in package.json
@@ -32,7 +38,7 @@ export class SuperblocksSdk {
32
38
  viewMode = "export-live",
33
39
  }: {
34
40
  applicationId: string;
35
- branch: string;
41
+ branch?: string;
36
42
  headers?: Record<string, string>;
37
43
  viewMode?: ViewMode;
38
44
  }) {
@@ -88,6 +94,25 @@ export class SuperblocksSdk {
88
94
  return fetchApis(this.token, this.superblocksBaseUrl);
89
95
  }
90
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
+
91
116
  async registerComponents(
92
117
  applicationId: string,
93
118
  componentConfigs: Record<string, any>,