@superblocksteam/sdk 1.5.0 → 1.5.2

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.js CHANGED
@@ -18,7 +18,6 @@ const SUPERBLOCKS_MAX_FILE_SIZE_MB = 10;
18
18
  const CLI_VERSION_HEADER = "x-superblocks-cli-version";
19
19
  const SUPERBLOCKS_URL_HEADER = "x-superblocks-url";
20
20
  async function fetchApplication({ cliVersion, applicationId, branch, token, superblocksBaseUrl, viewMode, injectedHeaders = {}, }) {
21
- var _a, _b;
22
21
  const serverURL = branch
23
22
  ? new URL(`${BASE_SERVER_PUBLIC_API_URL}/applications/${applicationId}/branches/${encodeURIComponent(branch)}?viewMode=${viewMode}`, superblocksBaseUrl)
24
23
  : new URL(`${BASE_SERVER_PUBLIC_API_URL}/applications/${applicationId}?viewMode=${viewMode}`, superblocksBaseUrl);
@@ -36,16 +35,15 @@ async function fetchApplication({ cliVersion, applicationId, branch, token, supe
36
35
  serverResponse = await (0, axios_1.default)(config);
37
36
  }
38
37
  catch (e) {
39
- if (axios_1.default.isAxiosError(e) && ((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 404) {
38
+ if (axios_1.default.isAxiosError(e) && e.response?.status === 404) {
40
39
  throw new util_1.NotFoundError(`Application ${applicationId} was not found`);
41
40
  }
42
41
  throw new Error("Could not fetch application");
43
42
  }
44
- return (_b = serverResponse === null || serverResponse === void 0 ? void 0 : serverResponse.data) === null || _b === void 0 ? void 0 : _b.data;
43
+ return serverResponse?.data?.data;
45
44
  }
46
45
  exports.fetchApplication = fetchApplication;
47
46
  async function fetchApplicationBranches({ cliVersion, applicationId, token, superblocksBaseUrl, injectedHeaders = {}, }) {
48
- var _a, _b;
49
47
  const serverURL = new URL(`public/applications/${applicationId}/branches`, superblocksBaseUrl);
50
48
  let serverResponse;
51
49
  try {
@@ -61,16 +59,15 @@ async function fetchApplicationBranches({ cliVersion, applicationId, token, supe
61
59
  serverResponse = await (0, axios_1.default)(config);
62
60
  }
63
61
  catch (e) {
64
- if (axios_1.default.isAxiosError(e) && ((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 404) {
62
+ if (axios_1.default.isAxiosError(e) && e.response?.status === 404) {
65
63
  throw new util_1.NotFoundError(`Application ${applicationId} was not found`);
66
64
  }
67
65
  throw new Error(`Could not fetch application branches: ${e.message}`);
68
66
  }
69
- return (_b = serverResponse === null || serverResponse === void 0 ? void 0 : serverResponse.data) === null || _b === void 0 ? void 0 : _b.data;
67
+ return serverResponse?.data?.data;
70
68
  }
71
69
  exports.fetchApplicationBranches = fetchApplicationBranches;
72
70
  async function fetchApplicationWithComponents({ cliVersion, applicationId, branch, token, superblocksBaseUrl, viewMode, injectedHeaders = {}, }) {
73
- var _a, _b, _c;
74
71
  const applicationWrapper = await fetchApplication({
75
72
  cliVersion,
76
73
  applicationId,
@@ -84,7 +81,7 @@ async function fetchApplicationWithComponents({ cliVersion, applicationId, branc
84
81
  return;
85
82
  }
86
83
  // if there are no custom components, just return here without trying to initialize them using bucketeer
87
- if ((0, lodash_1.isEmpty)((_b = (_a = applicationWrapper.application) === null || _a === void 0 ? void 0 : _a.settings) === null || _b === void 0 ? void 0 : _b.registeredComponents)) {
84
+ if ((0, lodash_1.isEmpty)(applicationWrapper.application?.settings?.registeredComponents)) {
88
85
  return {
89
86
  ...applicationWrapper,
90
87
  componentFiles: null,
@@ -111,7 +108,7 @@ async function fetchApplicationWithComponents({ cliVersion, applicationId, branc
111
108
  return response.data;
112
109
  }
113
110
  catch (e) {
114
- if (axios_1.default.isAxiosError(e) && ((_c = e.response) === null || _c === void 0 ? void 0 : _c.status) === 404) {
111
+ if (axios_1.default.isAxiosError(e) && e.response?.status === 404) {
115
112
  throw new util_1.NotFoundError(`Application ${applicationId} was not found`);
116
113
  }
117
114
  throw new Error("Could not fetch application");
@@ -119,7 +116,6 @@ async function fetchApplicationWithComponents({ cliVersion, applicationId, branc
119
116
  }
120
117
  exports.fetchApplicationWithComponents = fetchApplicationWithComponents;
121
118
  async function fetchApplications(cliVersion, token, superblocksBaseUrl, injectedHeaders = {}) {
122
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
123
119
  try {
124
120
  const config = {
125
121
  method: "get",
@@ -137,17 +133,20 @@ async function fetchApplications(cliVersion, token, superblocksBaseUrl, injected
137
133
  let message;
138
134
  if (e instanceof axios_1.AxiosError) {
139
135
  message =
140
- (_j = (_h = (_f = (_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.message) !== null && _d !== void 0 ? _d : JSON.stringify((_e = e.response) === null || _e === void 0 ? void 0 : _e.data)) !== null && _f !== void 0 ? _f : (_g = e.response) === null || _g === void 0 ? void 0 : _g.statusText) !== null && _h !== void 0 ? _h : e === null || e === void 0 ? void 0 : e.message) !== null && _j !== void 0 ? _j : `${e}`;
136
+ e.response?.data?.responseMeta?.message ??
137
+ JSON.stringify(e.response?.data) ??
138
+ e.response?.statusText ??
139
+ e?.message ??
140
+ `${e}`;
141
141
  }
142
142
  else {
143
- message = `${(e === null || e === void 0 ? void 0 : e.message) ? e === null || e === void 0 ? void 0 : e.message : e}`;
143
+ message = `${e?.message ? e?.message : e}`;
144
144
  }
145
145
  throw new Error(`Could not fetch applications: ${message}`);
146
146
  }
147
147
  }
148
148
  exports.fetchApplications = fetchApplications;
149
149
  async function fetchApi(cliVersion, apiId, token, superblocksBaseUrl, viewMode, branch) {
150
- var _a;
151
150
  try {
152
151
  const serverURL = branch
153
152
  ? new URL(`${BASE_SERVER_PUBLIC_API_URL}/apis/${apiId}/branches/${encodeURIComponent(branch)}?viewMode=${viewMode}`, superblocksBaseUrl)
@@ -164,7 +163,7 @@ async function fetchApi(cliVersion, apiId, token, superblocksBaseUrl, viewMode,
164
163
  return response.data.data;
165
164
  }
166
165
  catch (e) {
167
- if (axios_1.default.isAxiosError(e) && ((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 404) {
166
+ if (axios_1.default.isAxiosError(e) && e.response?.status === 404) {
168
167
  throw new util_1.NotFoundError(`Api ${apiId} was not found`);
169
168
  }
170
169
  throw new Error("Could not fetch api");
@@ -190,7 +189,6 @@ async function fetchApis(cliVersion, token, superblocksBaseUrl) {
190
189
  }
191
190
  exports.fetchApis = fetchApis;
192
191
  async function validateGitSetup(cliVersion, resourceId, resourceType, event, localGitRepoState, token, superblocksBaseUrl, injectedHeaders) {
193
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
194
192
  let resourceName;
195
193
  try {
196
194
  const requestBody = {
@@ -229,32 +227,37 @@ async function validateGitSetup(cliVersion, resourceId, resourceType, event, loc
229
227
  throw new Error(validationResult.errors.join("\n"));
230
228
  }
231
229
  return {
232
- branchName: ((_a = validationResult.branchName) !== null && _a !== void 0 ? _a : null),
230
+ branchName: (validationResult.branchName ?? null),
233
231
  };
234
232
  }
235
233
  catch (e) {
236
234
  let message;
237
235
  if (e instanceof axios_1.AxiosError) {
238
236
  message =
239
- (_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}`;
237
+ e.response?.data?.responseMeta?.message ??
238
+ JSON.stringify(e.response?.data) ??
239
+ e.response?.statusText ??
240
+ e?.message ??
241
+ `${e}`;
240
242
  }
241
243
  else {
242
- message = `${(e === null || e === void 0 ? void 0 : e.message) ? e === null || e === void 0 ? void 0 : e.message : e}`;
244
+ message = `${e?.message ? e?.message : e}`;
243
245
  }
244
- 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 : ""}`);
246
+ throw new Error(`Could not validate your git setup against Superblocks for the ${resourceType.toLowerCase()} ${resourceName ?? resourceId}${message ? "\n" + message : ""}`);
245
247
  }
246
248
  }
247
249
  exports.validateGitSetup = validateGitSetup;
248
250
  async function registerComponents(cliVersion, applicationId, componentConfigs, token, superblocksBaseUrl, branch, injectedHeaders) {
249
- var _a, _b, _c, _d, _e, _f, _g;
250
251
  try {
251
252
  const branchPath = branch ? `/branches/${encodeURIComponent(branch)}` : "";
252
253
  const userMe = await fetchCurrentUser(cliVersion, token, superblocksBaseUrl);
253
254
  const organization = userMe.organizations[0];
254
- if (organization.agentType == types_1.AgentType.ONPREMISE && (0, flag_1.signingEnabled)(userMe.flagBootstrap)) {
255
- const agentUrls = (0, utils_1.getAgentUrls)(userMe.agents, organization.agentType);
255
+ if (organization.agentType == types_1.AgentType.ONPREMISE &&
256
+ (0, flag_1.signingEnabled)(userMe.flagBootstrap)) {
257
+ const profile = process.env.SUPERBLOCKS_PROFILE;
258
+ const agentUrl = await (0, utils_1.getAgentUrl)(userMe.agents, organization.agentType, profile);
256
259
  const socket = await (0, socket_1.connectToISocketRPCServer)({
257
- agentUrls,
260
+ agentUrl,
258
261
  superblocksBaseUrl,
259
262
  token,
260
263
  });
@@ -285,7 +288,9 @@ async function registerComponents(cliVersion, applicationId, componentConfigs, t
285
288
  }
286
289
  catch (e) {
287
290
  if (e instanceof axios_1.AxiosError) {
288
- const message = (_f = (_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.message) !== null && _d !== void 0 ? _d : JSON.stringify((_e = e.response) === null || _e === void 0 ? void 0 : _e.data)) !== null && _f !== void 0 ? _f : (_g = e.response) === null || _g === void 0 ? void 0 : _g.statusText;
291
+ const message = e.response?.data?.responseMeta?.message ??
292
+ JSON.stringify(e.response?.data) ??
293
+ e.response?.statusText;
289
294
  throw new Error(`Could not register application components ${message ? "\n" + message : ""}`);
290
295
  }
291
296
  throw new Error(`Could not register application components ${e.message ? "\n" + e.message : ""}`);
@@ -293,7 +298,6 @@ async function registerComponents(cliVersion, applicationId, componentConfigs, t
293
298
  }
294
299
  exports.registerComponents = registerComponents;
295
300
  async function uploadComponents({ cliVersion, applicationId, componentConfigs, files, token, superblocksBaseUrl, branch, }) {
296
- var _a, _b, _c, _d, _e, _f;
297
301
  superblocksBaseUrl = superblocksBaseUrl.replace(/\/$/, "");
298
302
  const bucketeerBaseUrl = (0, util_1.getBucketeerUrlFromSuperblocksUrl)(superblocksBaseUrl);
299
303
  // parse files to source and bundled files based on path
@@ -349,20 +353,21 @@ You can reduce your component bundle size by uploading static assets to a separa
349
353
  const uploadResponse = await axios_1.default.post(postURL, formData, config);
350
354
  const userMe = await fetchCurrentUser(cliVersion, token, superblocksBaseUrl);
351
355
  const organization = userMe.organizations[0];
352
- const signingRequired = organization.agentType == types_1.AgentType.ONPREMISE && (0, flag_1.signingEnabled)(userMe.flagBootstrap);
353
- let agentUrls = [];
356
+ let agentUrl;
357
+ const signingRequired = organization.agentType == types_1.AgentType.ONPREMISE &&
358
+ (0, flag_1.signingEnabled)(userMe.flagBootstrap);
354
359
  if (signingRequired) {
355
- agentUrls = (0, utils_1.getAgentUrls)(userMe.agents, organization.agentType);
360
+ agentUrl = await (0, utils_1.getAgentUrl)(userMe.agents, organization.agentType);
356
361
  }
357
362
  const socket = await (0, socket_1.connectToISocketRPCServer)({
358
- agentUrls,
363
+ agentUrl,
359
364
  superblocksBaseUrl,
360
365
  token,
361
366
  });
362
367
  try {
363
368
  await socket.call.v1.public.application.component.update({
364
369
  applicationId,
365
- branchName: branch !== null && branch !== void 0 ? branch : undefined,
370
+ branchName: branch ?? undefined,
366
371
  srcFiles: srcFiles.map((file) => file.filename),
367
372
  buildFiles: buildFiles.map((file) => file.filename),
368
373
  registeredComponents: componentConfigs,
@@ -376,15 +381,15 @@ You can reduce your component bundle size by uploading static assets to a separa
376
381
  }
377
382
  }
378
383
  catch (e) {
379
- if (e instanceof axios_1.AxiosError && ((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) === 413) {
384
+ if (e instanceof axios_1.AxiosError && e.response?.status === 413) {
380
385
  throw new Error(`Could not upload application components, file size limit exceeded`);
381
386
  }
382
387
  else {
383
388
  let message;
384
389
  if (e instanceof axios_1.AxiosError) {
385
- message = (((_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) ||
386
- ((_e = e.response) === null || _e === void 0 ? void 0 : _e.data) ||
387
- ((_f = e.response) === null || _f === void 0 ? void 0 : _f.statusText));
390
+ message = (e.response?.data?.responseMeta?.message ||
391
+ e.response?.data ||
392
+ e.response?.statusText);
388
393
  }
389
394
  else {
390
395
  message = e.message;
@@ -398,7 +403,6 @@ You can reduce your component bundle size by uploading static assets to a separa
398
403
  }
399
404
  exports.uploadComponents = uploadComponents;
400
405
  async function fetchCurrentUser(cliVersion, token, superblocksBaseUrl) {
401
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
402
406
  try {
403
407
  const config = {
404
408
  method: "get",
@@ -416,26 +420,37 @@ async function fetchCurrentUser(cliVersion, token, superblocksBaseUrl) {
416
420
  let message;
417
421
  if (e instanceof axios_1.AxiosError) {
418
422
  message =
419
- (_j = (_h = (_f = (_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.message) !== null && _d !== void 0 ? _d : JSON.stringify((_e = e.response) === null || _e === void 0 ? void 0 : _e.data)) !== null && _f !== void 0 ? _f : (_g = e.response) === null || _g === void 0 ? void 0 : _g.statusText) !== null && _h !== void 0 ? _h : e === null || e === void 0 ? void 0 : e.message) !== null && _j !== void 0 ? _j : `${e}`;
423
+ e.response?.data?.responseMeta?.message ??
424
+ JSON.stringify(e.response?.data) ??
425
+ e.response?.statusText ??
426
+ e?.message ??
427
+ `${e}`;
420
428
  }
421
429
  else {
422
- message = `${(e === null || e === void 0 ? void 0 : e.message) ? e === null || e === void 0 ? void 0 : e.message : e}`;
430
+ message = `${e?.message ? e?.message : e}`;
423
431
  }
424
432
  throw new Error(`Could not fetch current user: ${message}`);
425
433
  }
426
434
  }
427
435
  exports.fetchCurrentUser = fetchCurrentUser;
428
436
  async function pushApplication({ cliVersion, applicationId, token, superblocksBaseUrl, applicationConfig, branch, injectedHeaders = {}, }) {
429
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
430
- const serverURL = new URL(`${BASE_SERVER_PUBLIC_API_URL}/applications/${applicationId}/branches/${encodeURIComponent(branch)}/push`, superblocksBaseUrl);
431
- let serverResponse;
437
+ const handleHttpError = (status) => {
438
+ if (status === 405) {
439
+ throw new errors_1.BranchNotCheckedOutError(`Branch ${branch} is not checked out`);
440
+ }
441
+ else if (status === 409) {
442
+ throw new errors_1.CommitAlreadyExistsError(`Commit ${applicationConfig.commitId} already exists`);
443
+ }
444
+ };
432
445
  try {
433
446
  const userMe = await fetchCurrentUser(cliVersion, token, superblocksBaseUrl);
434
447
  const organization = userMe.organizations[0];
435
- if (organization.agentType == types_1.AgentType.ONPREMISE && (0, flag_1.signingEnabled)(userMe.flagBootstrap)) {
436
- const agentUrls = (0, utils_1.getAgentUrls)(userMe.agents, organization.agentType);
448
+ if (organization.agentType == types_1.AgentType.ONPREMISE &&
449
+ (0, flag_1.signingEnabled)(userMe.flagBootstrap)) {
450
+ const profile = process.env.SUPERBLOCKS_PROFILE;
451
+ const agentUrl = await (0, utils_1.getAgentUrl)(userMe.agents, organization.agentType, profile);
437
452
  const socket = await (0, socket_1.connectToISocketRPCServer)({
438
- agentUrls,
453
+ agentUrl,
439
454
  superblocksBaseUrl,
440
455
  token,
441
456
  });
@@ -449,9 +464,16 @@ async function pushApplication({ cliVersion, applicationId, token, superblocksBa
449
464
  page: applicationConfig.page,
450
465
  });
451
466
  socket.close();
467
+ handleHttpError(resp.responseMeta.status);
468
+ if (resp.responseMeta.status !== 200) {
469
+ // Get the raw error message from the server and throw it. The outer try-catch block will wrap it in a nicer error message
470
+ const message = resp?.responseMeta?.message ?? JSON.stringify(resp?.data);
471
+ throw new Error(message);
472
+ }
452
473
  return resp.data;
453
474
  }
454
475
  else {
476
+ const serverURL = new URL(`${BASE_SERVER_PUBLIC_API_URL}/applications/${applicationId}/branches/${encodeURIComponent(branch)}/push`, superblocksBaseUrl);
455
477
  const config = {
456
478
  method: "post",
457
479
  url: serverURL.toString(),
@@ -462,37 +484,49 @@ async function pushApplication({ cliVersion, applicationId, token, superblocksBa
462
484
  },
463
485
  data: applicationConfig,
464
486
  };
465
- serverResponse = await (0, axios_1.default)(config);
487
+ try {
488
+ const serverResponse = await (0, axios_1.default)(config);
489
+ return serverResponse?.data?.data;
490
+ }
491
+ catch (e) {
492
+ if (!axios_1.default.isAxiosError(e)) {
493
+ throw e;
494
+ }
495
+ handleHttpError(e.response?.data?.responseMeta?.status ?? e.response?.status);
496
+ const message = e.response?.data?.responseMeta?.message ??
497
+ JSON.stringify(e.response?.data) ??
498
+ e.response?.statusText;
499
+ throw new Error(message);
500
+ }
466
501
  }
467
502
  }
468
503
  catch (e) {
469
- if (axios_1.default.isAxiosError(e)) {
470
- 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;
471
- if (respCode === 405) {
472
- throw new errors_1.BranchNotCheckedOutError(`Branch ${branch} is not checked out`);
473
- }
474
- else if (respCode === 409) {
475
- throw new errors_1.CommitAlreadyExistsError(`Commit ${applicationConfig.commitId} already exists`);
476
- }
477
- 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;
478
- throw new Error(`Could not push application ${message ? "\n" + message : ""}`);
504
+ if (e instanceof errors_1.BranchNotCheckedOutError ||
505
+ e instanceof errors_1.CommitAlreadyExistsError) {
506
+ throw e;
479
507
  }
480
508
  throw new Error(`Could not push application ${e.message ? "\n" + e.message : ""}`);
481
509
  }
482
- return (_o = serverResponse === null || serverResponse === void 0 ? void 0 : serverResponse.data) === null || _o === void 0 ? void 0 : _o.data;
483
510
  }
484
511
  exports.pushApplication = pushApplication;
485
512
  async function pushApi({ cliVersion, apiId, token, superblocksBaseUrl, apiConfig, branch, injectedHeaders = {}, }) {
486
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
513
+ const handleHttpError = (status) => {
514
+ if (status === 405) {
515
+ throw new errors_1.BranchNotCheckedOutError(`Branch ${branch} is not checked out`);
516
+ }
517
+ else if (status === 409) {
518
+ throw new errors_1.CommitAlreadyExistsError(`Commit ${apiConfig.commitId} already exists`);
519
+ }
520
+ };
487
521
  const serverURL = new URL(`${BASE_SERVER_PUBLIC_API_URL}/apis/${apiId}/branches/${encodeURIComponent(branch)}/push`, superblocksBaseUrl);
488
- let serverResponse;
489
522
  try {
490
523
  const userMe = await fetchCurrentUser(cliVersion, token, superblocksBaseUrl);
491
524
  const organization = userMe.organizations[0];
492
- if (organization.agentType == types_1.AgentType.ONPREMISE && (0, flag_1.signingEnabled)(userMe.flagBootstrap)) {
493
- const agentUrls = (0, utils_1.getAgentUrls)(userMe.agents, organization.agentType);
525
+ if (organization.agentType == types_1.AgentType.ONPREMISE &&
526
+ (0, flag_1.signingEnabled)(userMe.flagBootstrap)) {
527
+ const agentUrl = await (0, utils_1.getAgentUrl)(userMe.agents, organization.agentType);
494
528
  const socket = await (0, socket_1.connectToISocketRPCServer)({
495
- agentUrls,
529
+ agentUrl,
496
530
  superblocksBaseUrl,
497
531
  token,
498
532
  });
@@ -504,6 +538,12 @@ async function pushApi({ cliVersion, apiId, token, superblocksBaseUrl, apiConfig
504
538
  commitMessage: apiConfig.commitMessage,
505
539
  });
506
540
  socket.close();
541
+ handleHttpError(resp.responseMeta.status);
542
+ if (resp.responseMeta.status !== 200) {
543
+ // Get the raw error message from the server and throw it. The outer try-catch block will wrap it in a nicer error message
544
+ const message = resp?.responseMeta?.message ?? JSON.stringify(resp?.data);
545
+ throw new Error(message);
546
+ }
507
547
  return resp.data;
508
548
  }
509
549
  else {
@@ -517,22 +557,28 @@ async function pushApi({ cliVersion, apiId, token, superblocksBaseUrl, apiConfig
517
557
  },
518
558
  data: apiConfig,
519
559
  };
520
- serverResponse = await (0, axios_1.default)(config);
560
+ try {
561
+ const serverResponse = await (0, axios_1.default)(config);
562
+ return serverResponse?.data?.data;
563
+ }
564
+ catch (e) {
565
+ if (!axios_1.default.isAxiosError(e)) {
566
+ throw e;
567
+ }
568
+ handleHttpError(e.response?.data?.responseMeta?.status);
569
+ const message = e.response?.data?.responseMeta?.message ??
570
+ JSON.stringify(e.response?.data) ??
571
+ e.response?.statusText;
572
+ throw new Error(message);
573
+ }
521
574
  }
522
575
  }
523
576
  catch (e) {
524
- if (axios_1.default.isAxiosError(e)) {
525
- if (((_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) === 405) {
526
- throw new errors_1.BranchNotCheckedOutError(`Branch ${branch} is not checked out`);
527
- }
528
- else if (((_f = (_e = (_d = e.response) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.responseMeta) === null || _f === void 0 ? void 0 : _f.status) === 409) {
529
- throw new errors_1.CommitAlreadyExistsError(`Commit ${apiConfig.commitId} already exists`);
530
- }
531
- const message = (_m = (_k = (_j = (_h = (_g = e.response) === null || _g === void 0 ? void 0 : _g.data) === null || _h === void 0 ? void 0 : _h.responseMeta) === null || _j === void 0 ? void 0 : _j.message) !== null && _k !== void 0 ? _k : JSON.stringify((_l = e.response) === null || _l === void 0 ? void 0 : _l.data)) !== null && _m !== void 0 ? _m : (_o = e.response) === null || _o === void 0 ? void 0 : _o.statusText;
532
- throw new Error(`Could not push api ${message ? "\n" + message : ""}`);
577
+ if (e instanceof errors_1.BranchNotCheckedOutError ||
578
+ e instanceof errors_1.CommitAlreadyExistsError) {
579
+ throw e;
533
580
  }
534
581
  throw new Error(`Could not push api ${e.message ? "\n" + e.message : ""}`);
535
582
  }
536
- return (_p = serverResponse === null || serverResponse === void 0 ? void 0 : serverResponse.data) === null || _p === void 0 ? void 0 : _p.data;
537
583
  }
538
584
  exports.pushApi = pushApi;
package/dist/flag.js CHANGED
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.signingEnabled = void 0;
4
4
  const signingEnabled = (flags) => {
5
- var _a;
6
- return (_a = flags["ui.enable-resource-signing"]) !== null && _a !== void 0 ? _a : false;
5
+ return flags["ui.enable-resource-signing"] ?? false;
7
6
  };
8
7
  exports.signingEnabled = signingEnabled;
@@ -38,6 +38,7 @@ type ResponseDto<T> = {
38
38
  export type ResponseMeta = {
39
39
  status: number;
40
40
  success: boolean;
41
+ message?: string;
41
42
  error?: APIResponseError;
42
43
  };
43
44
  type APIResponseError = {
@@ -98,8 +99,8 @@ export interface ServerMethods {
98
99
  };
99
100
  };
100
101
  }
101
- export declare function createRequestHandlers({ agentUrls, token, }: {
102
+ export declare function createRequestHandlers({ agentUrl, token, }: {
102
103
  token: string;
103
- agentUrls: string[];
104
+ agentUrl?: string;
104
105
  }): MethodHandlers<ClientMethods, ServerMethods, unknown>;
105
106
  export {};
@@ -2,14 +2,17 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createRequestHandlers = void 0;
4
4
  const signing_1 = require("./signing");
5
- function createRequestHandlers({ agentUrls, token, }) {
5
+ function createRequestHandlers({ agentUrl, token, }) {
6
6
  const requestHandlers = {
7
7
  v1: {
8
8
  signing: {
9
9
  signApplication: [
10
10
  async ({ branchName, toSign, }) => {
11
+ if (!agentUrl) {
12
+ throw new Error("Agent url not specified. This shouldn't happen.");
13
+ }
11
14
  const signature = await (0, signing_1.signResource)({
12
- agentUrls,
15
+ agentUrl,
13
16
  token: token,
14
17
  branchName,
15
18
  resource: {
@@ -25,8 +28,11 @@ function createRequestHandlers({ agentUrls, token, }) {
25
28
  async ({ branchName, toSign, }) => {
26
29
  const signatures = [];
27
30
  for (const { apiPb } of toSign) {
31
+ if (!agentUrl) {
32
+ throw new Error("Agent url not specified. This shouldn't happen.");
33
+ }
28
34
  const signature = await (0, signing_1.signResource)({
29
- agentUrls,
35
+ agentUrl,
30
36
  token: token,
31
37
  branchName,
32
38
  resource: { api: apiPb },
@@ -39,8 +45,11 @@ function createRequestHandlers({ agentUrls, token, }) {
39
45
  verifyApplication: [
40
46
  async ({ branchName, toVerify, }) => {
41
47
  try {
48
+ if (!agentUrl) {
49
+ throw new Error("Agent url not specified. This shouldn't happen.");
50
+ }
42
51
  await (0, signing_1.verifyResources)({
43
- agentUrls,
52
+ agentUrl,
44
53
  token,
45
54
  branchName,
46
55
  resources: [
@@ -62,8 +71,11 @@ function createRequestHandlers({ agentUrls, token, }) {
62
71
  verifyApi: [
63
72
  async ({ branchName, toVerify, }) => {
64
73
  try {
74
+ if (!agentUrl) {
75
+ throw new Error("Agent url not specified. This shouldn't happen.");
76
+ }
65
77
  await (0, signing_1.verifyResources)({
66
- agentUrls,
78
+ agentUrl,
67
79
  token,
68
80
  branchName,
69
81
  resources: toVerify.map(({ apiPb }) => ({
@@ -2,10 +2,10 @@
2
2
  import WebSocket from "isomorphic-ws";
3
3
  import { ServerMethods } from "./handlers";
4
4
  export type StdISocketRPCClient = ISocketClient<ServerMethods>;
5
- export declare function connectToISocketRPCServer({ superblocksBaseUrl, agentUrls, token, }: {
5
+ export declare function connectToISocketRPCServer({ superblocksBaseUrl, agentUrl, token, }: {
6
6
  superblocksBaseUrl: string;
7
- agentUrls: string[];
8
7
  token: string;
8
+ agentUrl?: string;
9
9
  }): Promise<StdISocketRPCClient>;
10
10
  export declare function connectISocket<CallableMethods, ImplementedMethods, RequestContext = never>(wsUrl: string, authorization: string | undefined, requestHandlers: MethodHandlers<ImplementedMethods, CallableMethods, RequestContext>): Promise<ISocketClient<CallableMethods>>;
11
11
  export declare function connectWebSocket(wsUrl: string): Promise<WebSocket>;
@@ -5,9 +5,9 @@ const tslib_1 = require("tslib");
5
5
  const isomorphic_ws_1 = tslib_1.__importDefault(require("isomorphic-ws"));
6
6
  const handlers_1 = require("./handlers");
7
7
  const socket_1 = require("./socket");
8
- async function connectToISocketRPCServer({ superblocksBaseUrl, agentUrls, token, }) {
8
+ async function connectToISocketRPCServer({ superblocksBaseUrl, agentUrl, token, }) {
9
9
  const requestHandlers = (0, handlers_1.createRequestHandlers)({
10
- agentUrls,
10
+ agentUrl,
11
11
  token,
12
12
  });
13
13
  const authorization = `Bearer ${token}`;
@@ -1,13 +1,13 @@
1
1
  import { ApiResource, GenericResource, Signature } from "../types";
2
- export declare function signResource({ token, branchName, resource, agentUrls, }: {
2
+ export declare function signResource({ token, branchName, resource, agentUrl, }: {
3
3
  token: string;
4
4
  branchName: string;
5
5
  resource: ApiResource | GenericResource;
6
- agentUrls: string[];
6
+ agentUrl: string;
7
7
  }): Promise<Signature>;
8
- export declare function verifyResources({ agentUrls, token, branchName, resources, }: {
8
+ export declare function verifyResources({ agentUrl, token, branchName, resources, }: {
9
9
  resources: Array<GenericResource | ApiResource>;
10
10
  token: string;
11
11
  branchName: string;
12
- agentUrls: string[];
12
+ agentUrl: string;
13
13
  }): Promise<void>;