@superblocksteam/sdk 1.13.1 → 1.14.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,6 +1,6 @@
1
1
  import { ComponentEvent, LocalGitRepoState, SuperblocksResourceType } from "@superblocksteam/util";
2
2
  import { StdISocketRPCClient } from "./socket";
3
- import { Api, ApiWithPb, Page, RemoteCommitDto, UserMeDto, ViewMode } from "./types";
3
+ import { Api, ApiWithPb, Page, RemoteCommitDto, UserMeDto, ViewMode, DeploymentDto } from "./types";
4
4
  export interface UploadFile {
5
5
  name: string;
6
6
  filename: string;
@@ -151,3 +151,5 @@ export declare function fetchApiCommits({ cliVersion, applicationId, branch, tok
151
151
  limit?: number;
152
152
  offset?: number;
153
153
  }): Promise<GetCommitsResponseBody>;
154
+ export declare function deployApplication(cliVersion: string, applicationId: string, token: string, superblocksBaseUrl: string, commitId?: string): Promise<DeploymentDto>;
155
+ export declare function deployApi(cliVersion: string, apiId: string, token: string, superblocksBaseUrl: string, commitId?: string): Promise<DeploymentDto>;
package/dist/client.js CHANGED
@@ -51,6 +51,8 @@ exports.pushApplication = pushApplication;
51
51
  exports.pushApi = pushApi;
52
52
  exports.fetchApplicationCommits = fetchApplicationCommits;
53
53
  exports.fetchApiCommits = fetchApiCommits;
54
+ exports.deployApplication = deployApplication;
55
+ exports.deployApi = deployApi;
54
56
  const fs = __importStar(require("fs"));
55
57
  const util_1 = require("@superblocksteam/util");
56
58
  const axios_1 = __importStar(require("axios"));
@@ -69,6 +71,11 @@ const BASE_SERVER_API_URL_V3 = "api/v3";
69
71
  const SUPERBLOCKS_MAX_FILE_SIZE_MB = 10;
70
72
  const CLI_VERSION_HEADER = "x-superblocks-cli-version";
71
73
  const SUPERBLOCKS_URL_HEADER = "x-superblocks-url";
74
+ var ResourceType;
75
+ (function (ResourceType) {
76
+ ResourceType["APPLICATION"] = "APPLICATION";
77
+ ResourceType["BACKEND"] = "BACKEND";
78
+ })(ResourceType || (ResourceType = {}));
72
79
  async function fetchApplication({ cliVersion, applicationId, branch, token, superblocksBaseUrl, viewMode, commitId, skipSigningVerification = false, injectedHeaders = {}, }) {
73
80
  if (commitId && viewMode !== "export-commit") {
74
81
  throw new Error(`If commitId ${commitId} is provided, viewMode cannot be ${viewMode}`);
@@ -292,11 +299,11 @@ async function validateGitSetup(cliVersion, resourceId, resourceType, event, loc
292
299
  };
293
300
  let path;
294
301
  switch (resourceType) {
295
- case "APPLICATION": {
302
+ case ResourceType.APPLICATION: {
296
303
  path = `/application/${resourceId}/validate-git-setup`;
297
304
  break;
298
305
  }
299
- case "BACKEND": {
306
+ case ResourceType.BACKEND: {
300
307
  path = `/api/${resourceId}/validate-git-setup`;
301
308
  break;
302
309
  }
@@ -724,3 +731,58 @@ async function fetchApiCommits({ cliVersion, applicationId, branch, token, super
724
731
  throw new Error("Could not fetch application");
725
732
  }
726
733
  }
734
+ async function deployApplication(cliVersion, applicationId, token, superblocksBaseUrl, commitId) {
735
+ return deployResource(cliVersion, ResourceType.APPLICATION, applicationId, token, superblocksBaseUrl, commitId);
736
+ }
737
+ async function deployApi(cliVersion, apiId, token, superblocksBaseUrl, commitId) {
738
+ return deployResource(cliVersion, ResourceType.BACKEND, apiId, token, superblocksBaseUrl, commitId);
739
+ }
740
+ async function deployResource(cliVersion, resourceType, resourceId, token, superblocksBaseUrl, commitId) {
741
+ let apiBaseUrl;
742
+ try {
743
+ if (resourceType === ResourceType.BACKEND) {
744
+ apiBaseUrl = `${BASE_SERVER_PUBLIC_API_URL_V1}/api/${resourceId}/deploy`;
745
+ }
746
+ else if (resourceType === ResourceType.APPLICATION) {
747
+ apiBaseUrl = `${BASE_SERVER_PUBLIC_API_URL_V1}/application/${resourceId}/deploy`;
748
+ }
749
+ else {
750
+ throw new Error(`Unsupported resource type: ${resourceType}`);
751
+ }
752
+ const config = {
753
+ method: "post",
754
+ url: new URL(apiBaseUrl, superblocksBaseUrl).toString(),
755
+ headers: {
756
+ Authorization: "Bearer " + token,
757
+ [CLI_VERSION_HEADER]: cliVersion,
758
+ },
759
+ data: {
760
+ commitId: commitId ?? "HEAD",
761
+ },
762
+ };
763
+ const serverResponse = await (0, axios_1.default)(config);
764
+ return serverResponse?.data?.data;
765
+ }
766
+ catch (e) {
767
+ let message;
768
+ if (axios_1.default.isAxiosError(e)) {
769
+ if (e.response?.status === 404) {
770
+ throw new util_1.NotFoundError(`${e.response?.data?.responseMeta?.message}`);
771
+ }
772
+ else if (e.response?.status === 403) {
773
+ throw new util_1.ForbiddenError(`${e.response?.data?.responseMeta?.message}`);
774
+ }
775
+ else if (e.response?.status === 400) {
776
+ throw new util_1.BadRequestError(`${e.response?.data?.responseMeta?.message}`);
777
+ }
778
+ else {
779
+ message =
780
+ e.response?.data?.responseMeta?.message ??
781
+ JSON.stringify(e.response?.data) ??
782
+ e.response?.statusText;
783
+ throw new Error(`${message}`);
784
+ }
785
+ }
786
+ throw new Error(`${e.message}`);
787
+ }
788
+ }
package/dist/sdk.d.ts CHANGED
@@ -75,4 +75,6 @@ export declare class SuperblocksSdk {
75
75
  } | {
76
76
  updated: Date;
77
77
  } | undefined>;
78
+ deployApplication(applicationId: string, commitId?: string): Promise<import("./types").DeploymentDto>;
79
+ deployApi(apiId: string, commitId?: string): Promise<import("./types").DeploymentDto>;
78
80
  }
package/dist/sdk.js CHANGED
@@ -144,5 +144,11 @@ class SuperblocksSdk {
144
144
  injectedHeaders: headers,
145
145
  });
146
146
  }
147
+ async deployApplication(applicationId, commitId) {
148
+ return (0, client_1.deployApplication)(this.cliVersion, applicationId, this.token, this.superblocksBaseUrl, commitId);
149
+ }
150
+ async deployApi(apiId, commitId) {
151
+ return (0, client_1.deployApi)(this.cliVersion, apiId, this.token, this.superblocksBaseUrl, commitId);
152
+ }
147
153
  }
148
154
  exports.SuperblocksSdk = SuperblocksSdk;
@@ -201,4 +201,17 @@ export interface RemoteCommitDto {
201
201
  branchName: string;
202
202
  repositoryId: string;
203
203
  }
204
+ export interface DeploymentDto {
205
+ commitId: string;
206
+ previousCommitId?: string | null;
207
+ entityId: string;
208
+ deployer: {
209
+ id: string;
210
+ email: string;
211
+ name: string;
212
+ };
213
+ deploymentId: string;
214
+ deployMessage: string;
215
+ deploymentTime: number;
216
+ }
204
217
  export type ViewMode = "export-deployed" | "export-latest" | "export-live" | "export-commit";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superblocksteam/sdk",
3
- "version": "1.13.1",
3
+ "version": "1.14.0",
4
4
  "description": "Superblocks JS SDK",
5
5
  "main": "./dist/index.js",
6
6
  "exports": {
@@ -29,7 +29,7 @@
29
29
  "form-data": "^4.0.0",
30
30
  "lodash": "^4.17.21",
31
31
  "ws": "^8.17.0",
32
- "@superblocksteam/util": "1.13.1"
32
+ "@superblocksteam/util": "1.14.0"
33
33
  },
34
34
  "homepage": "https://www.superblocks.com",
35
35
  "engines": {
package/src/client.ts CHANGED
@@ -2,11 +2,13 @@ import * as fs from "fs";
2
2
  import {
3
3
  COMPONENT_EVENT_HEADER,
4
4
  ComponentEvent,
5
+ ForbiddenError,
5
6
  getBucketeerUrlFromSuperblocksUrl,
6
7
  getContentType,
7
8
  LocalGitRepoState,
8
9
  NotFoundError,
9
10
  SuperblocksResourceType,
11
+ BadRequestError,
10
12
  unreachable,
11
13
  ValidateGitSetupRequestBody,
12
14
  } from "@superblocksteam/util";
@@ -28,6 +30,7 @@ import {
28
30
  RemoteCommitDto,
29
31
  UserMeDto,
30
32
  ViewMode,
33
+ DeploymentDto,
31
34
  } from "./types";
32
35
  import { getAgentUrl } from "./utils";
33
36
 
@@ -112,6 +115,11 @@ export interface GetCommitsResponseBody {
112
115
  commits: CommitDto[];
113
116
  }
114
117
 
118
+ enum ResourceType {
119
+ APPLICATION = "APPLICATION",
120
+ BACKEND = "BACKEND",
121
+ }
122
+
115
123
  export async function fetchApplication({
116
124
  cliVersion,
117
125
  applicationId,
@@ -481,16 +489,16 @@ export async function validateGitSetup(
481
489
  };
482
490
  let path: string;
483
491
  switch (resourceType) {
484
- case "APPLICATION": {
492
+ case ResourceType.APPLICATION: {
485
493
  path = `/application/${resourceId}/validate-git-setup`;
486
494
  break;
487
495
  }
488
- case "BACKEND": {
496
+ case ResourceType.BACKEND: {
489
497
  path = `/api/${resourceId}/validate-git-setup`;
490
498
  break;
491
499
  }
492
500
  default:
493
- unreachable(resourceType);
501
+ unreachable(resourceType as never);
494
502
  }
495
503
  const config: AxiosRequestConfig = {
496
504
  method: "post",
@@ -1120,3 +1128,90 @@ export async function fetchApiCommits({
1120
1128
  throw new Error("Could not fetch application");
1121
1129
  }
1122
1130
  }
1131
+
1132
+ export async function deployApplication(
1133
+ cliVersion: string,
1134
+ applicationId: string,
1135
+ token: string,
1136
+ superblocksBaseUrl: string,
1137
+ commitId?: string,
1138
+ ): Promise<DeploymentDto> {
1139
+ return deployResource(
1140
+ cliVersion,
1141
+ ResourceType.APPLICATION,
1142
+ applicationId,
1143
+ token,
1144
+ superblocksBaseUrl,
1145
+ commitId,
1146
+ );
1147
+ }
1148
+
1149
+ export async function deployApi(
1150
+ cliVersion: string,
1151
+ apiId: string,
1152
+ token: string,
1153
+ superblocksBaseUrl: string,
1154
+ commitId?: string,
1155
+ ): Promise<DeploymentDto> {
1156
+ return deployResource(
1157
+ cliVersion,
1158
+ ResourceType.BACKEND,
1159
+ apiId,
1160
+ token,
1161
+ superblocksBaseUrl,
1162
+ commitId,
1163
+ );
1164
+ }
1165
+
1166
+ async function deployResource(
1167
+ cliVersion: string,
1168
+ resourceType: ResourceType,
1169
+ resourceId: string,
1170
+ token: string,
1171
+ superblocksBaseUrl: string,
1172
+ commitId?: string,
1173
+ ): Promise<DeploymentDto> {
1174
+ let apiBaseUrl;
1175
+
1176
+ try {
1177
+ if (resourceType === ResourceType.BACKEND) {
1178
+ apiBaseUrl = `${BASE_SERVER_PUBLIC_API_URL_V1}/api/${resourceId}/deploy`;
1179
+ } else if (resourceType === ResourceType.APPLICATION) {
1180
+ apiBaseUrl = `${BASE_SERVER_PUBLIC_API_URL_V1}/application/${resourceId}/deploy`;
1181
+ } else {
1182
+ throw new Error(`Unsupported resource type: ${resourceType}`);
1183
+ }
1184
+
1185
+ const config: AxiosRequestConfig = {
1186
+ method: "post",
1187
+ url: new URL(apiBaseUrl, superblocksBaseUrl).toString(),
1188
+ headers: {
1189
+ Authorization: "Bearer " + token,
1190
+ [CLI_VERSION_HEADER]: cliVersion,
1191
+ },
1192
+ data: {
1193
+ commitId: commitId ?? "HEAD",
1194
+ },
1195
+ };
1196
+ const serverResponse = await axios(config);
1197
+ return serverResponse?.data?.data;
1198
+ } catch (e: any) {
1199
+ let message: string;
1200
+ if (axios.isAxiosError(e)) {
1201
+ if (e.response?.status === 404) {
1202
+ throw new NotFoundError(`${e.response?.data?.responseMeta?.message}`);
1203
+ } else if (e.response?.status === 403) {
1204
+ throw new ForbiddenError(`${e.response?.data?.responseMeta?.message}`);
1205
+ } else if (e.response?.status === 400) {
1206
+ throw new BadRequestError(`${e.response?.data?.responseMeta?.message}`);
1207
+ } else {
1208
+ message =
1209
+ (e.response?.data?.responseMeta?.message as string) ??
1210
+ JSON.stringify(e.response?.data) ??
1211
+ e.response?.statusText;
1212
+ throw new Error(`${message}`);
1213
+ }
1214
+ }
1215
+ throw new Error(`${e.message}`);
1216
+ }
1217
+ }
package/src/sdk.ts CHANGED
@@ -20,6 +20,8 @@ import {
20
20
  registerComponents,
21
21
  uploadComponents,
22
22
  validateGitSetup,
23
+ deployApplication,
24
+ deployApi,
23
25
  } from "./client";
24
26
  import { FeatureFlags } from "./flag";
25
27
  import { UserMeDto, ViewMode } from "./types";
@@ -305,4 +307,24 @@ export class SuperblocksSdk {
305
307
  injectedHeaders: headers,
306
308
  });
307
309
  }
310
+
311
+ async deployApplication(applicationId: string, commitId?: string) {
312
+ return deployApplication(
313
+ this.cliVersion,
314
+ applicationId,
315
+ this.token,
316
+ this.superblocksBaseUrl,
317
+ commitId,
318
+ );
319
+ }
320
+
321
+ async deployApi(apiId: string, commitId?: string) {
322
+ return deployApi(
323
+ this.cliVersion,
324
+ apiId,
325
+ this.token,
326
+ this.superblocksBaseUrl,
327
+ commitId,
328
+ );
329
+ }
308
330
  }
@@ -244,6 +244,20 @@ export interface RemoteCommitDto {
244
244
  repositoryId: string;
245
245
  }
246
246
 
247
+ export interface DeploymentDto {
248
+ commitId: string;
249
+ previousCommitId?: string | null;
250
+ entityId: string;
251
+ deployer: {
252
+ id: string;
253
+ email: string;
254
+ name: string;
255
+ };
256
+ deploymentId: string;
257
+ deployMessage: string;
258
+ deploymentTime: number;
259
+ }
260
+
247
261
  export type ViewMode =
248
262
  | "export-deployed"
249
263
  | "export-latest"