bdy 1.16.21-dev → 1.16.23-dev

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.
Files changed (42) hide show
  1. package/distTs/package.json +1 -1
  2. package/distTs/src/api/client.js +305 -61
  3. package/distTs/src/command/login.js +24 -13
  4. package/distTs/src/command/logout.js +18 -0
  5. package/distTs/src/command/package/download.js +1 -3
  6. package/distTs/src/command/package/list.js +1 -3
  7. package/distTs/src/command/package/publish.js +1 -3
  8. package/distTs/src/command/pipeline/run.js +1 -3
  9. package/distTs/src/command/project/list.js +8 -5
  10. package/distTs/src/command/project/set.js +1 -3
  11. package/distTs/src/command/sandbox/cp.js +1 -3
  12. package/distTs/src/command/sandbox/create.js +7 -12
  13. package/distTs/src/command/sandbox/destroy.js +1 -3
  14. package/distTs/src/command/sandbox/endpoint/add.js +1 -3
  15. package/distTs/src/command/sandbox/endpoint/delete.js +4 -4
  16. package/distTs/src/command/sandbox/endpoint/get.js +1 -3
  17. package/distTs/src/command/sandbox/endpoint/list.js +1 -3
  18. package/distTs/src/command/sandbox/exec/command.js +1 -3
  19. package/distTs/src/command/sandbox/exec/kill.js +1 -3
  20. package/distTs/src/command/sandbox/exec/list.js +1 -3
  21. package/distTs/src/command/sandbox/exec/logs.js +1 -3
  22. package/distTs/src/command/sandbox/exec/status.js +1 -3
  23. package/distTs/src/command/sandbox/get.js +5 -4
  24. package/distTs/src/command/sandbox/list.js +4 -5
  25. package/distTs/src/command/sandbox/restart.js +3 -6
  26. package/distTs/src/command/sandbox/snapshot/create.js +3 -6
  27. package/distTs/src/command/sandbox/snapshot/delete.js +1 -3
  28. package/distTs/src/command/sandbox/snapshot/get.js +6 -4
  29. package/distTs/src/command/sandbox/snapshot/list.js +19 -12
  30. package/distTs/src/command/sandbox/start.js +3 -6
  31. package/distTs/src/command/sandbox/status.js +2 -3
  32. package/distTs/src/command/sandbox/stop.js +3 -6
  33. package/distTs/src/command/whoami.js +5 -1
  34. package/distTs/src/command/workspace/list.js +3 -5
  35. package/distTs/src/command/workspace/set.js +1 -3
  36. package/distTs/src/input.js +16 -2
  37. package/distTs/src/output.js +14 -0
  38. package/distTs/src/texts.js +8 -30
  39. package/distTs/src/tunnel/cfg.js +24 -5
  40. package/distTs/src/utils.js +83 -0
  41. package/distTs/src/visualTest/server.js +1 -0
  42. package/package.json +1 -1
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bdy",
3
3
  "preferGlobal": false,
4
- "version": "1.16.21-dev",
4
+ "version": "1.16.23-dev",
5
5
  "type": "commonjs",
6
6
  "license": "MIT",
7
7
  "scripts": {
@@ -8,23 +8,62 @@ const texts_1 = require("../texts");
8
8
  const logger_1 = __importDefault(require("../logger"));
9
9
  const utils_1 = require("../utils");
10
10
  const node_fs_1 = __importDefault(require("node:fs"));
11
+ const cfg_1 = __importDefault(require("../tunnel/cfg"));
11
12
  class ApiClient {
12
13
  client;
13
14
  token;
15
+ refreshToken;
16
+ clientId;
17
+ clientSecret;
18
+ clientToken;
14
19
  baseUrl;
15
- constructor(baseUrl, token) {
20
+ constructor(baseUrl, token, refreshToken, clientId, clientSecret, clientToken) {
16
21
  this.baseUrl = baseUrl;
17
22
  this.token = token;
23
+ this.refreshToken = refreshToken;
24
+ this.clientId = clientId;
25
+ this.clientSecret = clientSecret;
26
+ this.clientToken = clientToken;
18
27
  this.client = new undici_1.Pool(baseUrl, {
19
28
  connect: {
20
29
  rejectUnauthorized: false,
21
30
  },
22
31
  });
23
32
  }
24
- async request(method, path, body, parseResponseBody = false, rawResponseBody = false, httpUrlEncoded = false) {
25
- const headers = {};
26
- if (this.token)
33
+ async tryRefreshToken() {
34
+ try {
35
+ const data = await this.request({
36
+ method: 'POST',
37
+ path: '/oauth2/token',
38
+ body: {
39
+ refresh_token: this.refreshToken,
40
+ client_id: this.clientId,
41
+ client_secret: this.clientSecret,
42
+ grant_type: 'refresh_token',
43
+ },
44
+ parseResponseBody: true,
45
+ httpUrlEncoded: true,
46
+ tryRefreshingToken: false,
47
+ });
48
+ if (data && data.access_token && data.refresh_token) {
49
+ this.token = data.access_token;
50
+ this.refreshToken = data.refresh_token;
51
+ cfg_1.default.setApiToken(this.token);
52
+ cfg_1.default.setApiRefreshToken(this.refreshToken);
53
+ return true;
54
+ }
55
+ }
56
+ catch {
57
+ // do nothing
58
+ }
59
+ return false;
60
+ }
61
+ async request({ method = 'GET', path, query, body = null, headers = {}, parseResponseBody = false, rawResponseBody = false, httpUrlEncoded = false, tryRefreshingToken = true, }) {
62
+ if (!headers)
63
+ headers = {};
64
+ if (this.token && !headers.authorization) {
27
65
  headers.authorization = `Bearer ${this.token}`;
66
+ }
28
67
  let bodyParsed = undefined;
29
68
  if (body) {
30
69
  if (body instanceof undici_1.FormData) {
@@ -32,11 +71,15 @@ class ApiClient {
32
71
  }
33
72
  else {
34
73
  if (httpUrlEncoded) {
35
- headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8';
74
+ if (!headers['content-type']) {
75
+ headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8';
76
+ }
36
77
  bodyParsed = new URLSearchParams(body).toString();
37
78
  }
38
79
  else {
39
- headers['content-type'] = 'application/json; charset=utf-8';
80
+ if (!headers['content-type']) {
81
+ headers['content-type'] = 'application/json; charset=utf-8';
82
+ }
40
83
  bodyParsed = JSON.stringify(body);
41
84
  }
42
85
  }
@@ -44,6 +87,7 @@ class ApiClient {
44
87
  const opts = {
45
88
  method,
46
89
  path,
90
+ query,
47
91
  headers,
48
92
  body: bodyParsed,
49
93
  };
@@ -68,6 +112,24 @@ class ApiClient {
68
112
  }
69
113
  if (status === 401) {
70
114
  await responseBody.dump();
115
+ if (this.refreshToken &&
116
+ this.clientId &&
117
+ this.clientSecret &&
118
+ tryRefreshingToken) {
119
+ const success = await this.tryRefreshToken();
120
+ if (success) {
121
+ return await this.request({
122
+ method,
123
+ path,
124
+ body,
125
+ headers,
126
+ parseResponseBody,
127
+ rawResponseBody,
128
+ httpUrlEncoded,
129
+ tryRefreshingToken: false,
130
+ });
131
+ }
132
+ }
71
133
  throw new Error(texts_1.ERR_REST_API_WRONG_TOKEN);
72
134
  }
73
135
  if (status === 403) {
@@ -142,100 +204,236 @@ class ApiClient {
142
204
  }
143
205
  }
144
206
  async getPipelineRun(workspace, project, pipelineId, executionId) {
145
- return await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/projects/${encodeURIComponent(project)}/pipelines/${encodeURIComponent(pipelineId)}/executions/${encodeURIComponent(executionId)}`, null, true);
207
+ return await this.request({
208
+ method: 'GET',
209
+ path: `/workspaces/${encodeURIComponent(workspace)}/projects/${encodeURIComponent(project)}/pipelines/${encodeURIComponent(pipelineId)}/executions/${encodeURIComponent(executionId)}`,
210
+ parseResponseBody: true,
211
+ });
146
212
  }
147
213
  async getInvoker() {
148
- return await this.request('GET', '/user', null, true);
214
+ return await this.request({
215
+ method: 'GET',
216
+ path: '/user',
217
+ parseResponseBody: true,
218
+ });
149
219
  }
150
220
  async getInvokerEmails() {
151
- return await this.request('GET', '/user/emails', null, true);
221
+ return await this.request({
222
+ method: 'GET',
223
+ path: '/user/emails',
224
+ parseResponseBody: true,
225
+ });
152
226
  }
153
227
  async postPipelineRun(workspace, project, pipelineId, body) {
154
- return await this.request('POST', `/workspaces/${encodeURIComponent(workspace)}/projects/${encodeURIComponent(project)}/pipelines/${encodeURIComponent(pipelineId)}/executions`, body, true);
228
+ return await this.request({
229
+ method: 'POST',
230
+ path: `/workspaces/${encodeURIComponent(workspace)}/projects/${encodeURIComponent(project)}/pipelines/${encodeURIComponent(pipelineId)}/executions`,
231
+ body,
232
+ parseResponseBody: true,
233
+ });
155
234
  }
156
235
  // Sandbox methods
157
236
  async createSandbox(workspace, project, body) {
158
- return await this.request('POST', `/workspaces/${encodeURIComponent(workspace)}/sandboxes?project_name=${encodeURIComponent(project)}`, body, true);
237
+ return await this.request({
238
+ method: 'POST',
239
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes?project_name=${encodeURIComponent(project)}`,
240
+ body,
241
+ parseResponseBody: true,
242
+ });
159
243
  }
160
244
  async sandboxUploadFile(workspace, sandboxId, remotePath, localPath) {
161
245
  const formData = new undici_1.FormData();
162
246
  const file = await node_fs_1.default.openAsBlob(localPath);
163
247
  formData.set('file', file);
164
- return await this.request('POST', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/content/upload${remotePath}`, formData, true);
248
+ return await this.request({
249
+ method: 'POST',
250
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/content/upload${remotePath}`,
251
+ body: formData,
252
+ parseResponseBody: true,
253
+ });
165
254
  }
166
255
  async listSandboxes(workspace, project) {
167
- return await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/sandboxes?project_name=${encodeURIComponent(project)}`, null, true);
256
+ return await this.request({
257
+ method: 'GET',
258
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes?project_name=${encodeURIComponent(project)}`,
259
+ parseResponseBody: true,
260
+ });
168
261
  }
169
262
  async getSandbox(workspace, sandboxId) {
170
- return await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}`, null, true);
263
+ return await this.request({
264
+ method: 'GET',
265
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}`,
266
+ parseResponseBody: true,
267
+ });
171
268
  }
172
269
  async deleteSandbox(workspace, sandboxId) {
173
- return await this.request('DELETE', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}`, null, false);
270
+ return await this.request({
271
+ method: 'DELETE',
272
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}`,
273
+ parseResponseBody: false,
274
+ });
174
275
  }
175
276
  async updateSandbox(workspace, sandboxId, body) {
176
- return await this.request('PATCH', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}`, body, true);
277
+ return await this.request({
278
+ method: 'PATCH',
279
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}`,
280
+ body,
281
+ parseResponseBody: true,
282
+ });
177
283
  }
178
284
  async startSandbox(workspace, sandboxId) {
179
- return await this.request('POST', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/start`, {}, true);
285
+ return await this.request({
286
+ method: 'POST',
287
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/start`,
288
+ body: {},
289
+ parseResponseBody: true,
290
+ });
180
291
  }
181
292
  async stopSandbox(workspace, sandboxId) {
182
- return await this.request('POST', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/stop`, {}, true);
293
+ return await this.request({
294
+ method: 'POST',
295
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/stop`,
296
+ body: {},
297
+ parseResponseBody: true,
298
+ });
183
299
  }
184
300
  async registerApp(name, redirectUrl) {
185
- return await this.request('POST', '/auth/register', {
186
- redirect_uris: [redirectUrl],
187
- client_name: name,
188
- grant_types: ['authorization_code', 'refresh_token'],
189
- response_types: ['code'],
190
- token_endpoint_auth_method: "client_secret_basic",
191
- token_expires_in: 3600
192
- }, true);
301
+ return await this.request({
302
+ method: 'POST',
303
+ path: '/auth/register',
304
+ body: {
305
+ redirect_uris: [redirectUrl],
306
+ client_name: name,
307
+ grant_types: ['authorization_code', 'refresh_token'],
308
+ response_types: ['code'],
309
+ token_endpoint_auth_method: 'client_secret_basic',
310
+ token_expires_in: 3600,
311
+ },
312
+ parseResponseBody: true,
313
+ });
193
314
  }
194
315
  async getApp(clientId) {
195
- return await this.request('GET', `/auth/register/${encodeURIComponent(clientId)}`, null, true);
316
+ return await this.request({
317
+ method: 'GET',
318
+ path: `/auth/register/${encodeURIComponent(clientId)}`,
319
+ headers: {
320
+ authorization: `Bearer ${this.clientToken || ''}`,
321
+ },
322
+ parseResponseBody: true,
323
+ });
324
+ }
325
+ async deleteApp(clientId) {
326
+ return await this.request({
327
+ method: 'DELETE',
328
+ path: `/auth/register/${encodeURIComponent(clientId)}`,
329
+ headers: {
330
+ authorization: `Bearer ${this.clientToken || ''}`,
331
+ },
332
+ parseResponseBody: true,
333
+ });
196
334
  }
197
335
  async exchangeAppToken(code, clientId, clientSecret, redirectUrl) {
198
- return await this.request('POST', '/oauth2/token', {
199
- grant_type: 'authorization_code',
200
- code,
201
- client_id: clientId,
202
- client_secret: clientSecret,
203
- redirect_uri: redirectUrl
204
- }, true, false, true);
336
+ return await this.request({
337
+ method: 'POST',
338
+ path: '/oauth2/token',
339
+ body: {
340
+ grant_type: 'authorization_code',
341
+ code,
342
+ client_id: clientId,
343
+ client_secret: clientSecret,
344
+ redirect_uri: redirectUrl,
345
+ },
346
+ parseResponseBody: true,
347
+ httpUrlEncoded: true,
348
+ });
205
349
  }
206
350
  async restartSandbox(workspace, sandboxId) {
207
- return await this.request('POST', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/restart`, {}, true);
351
+ return await this.request({
352
+ method: 'POST',
353
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/restart`,
354
+ body: {},
355
+ parseResponseBody: true,
356
+ });
208
357
  }
209
358
  async executeSandboxCommand(workspace, sandboxId, body) {
210
- return await this.request('POST', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/commands`, body, true);
359
+ return await this.request({
360
+ method: 'POST',
361
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/commands`,
362
+ body,
363
+ parseResponseBody: true,
364
+ });
211
365
  }
212
366
  async getSandboxCommand(workspace, sandboxId, commandId) {
213
- return await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/commands/${encodeURIComponent(commandId)}`, null, true);
367
+ return await this.request({
368
+ method: 'GET',
369
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/commands/${encodeURIComponent(commandId)}`,
370
+ parseResponseBody: true,
371
+ });
214
372
  }
215
373
  async listSandboxCommands(workspace, sandboxId) {
216
- return await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/commands`, null, true);
374
+ return await this.request({
375
+ method: 'GET',
376
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/commands`,
377
+ parseResponseBody: true,
378
+ });
217
379
  }
218
380
  async streamSandboxCommandLogs(workspace, sandboxId, commandId, follow = true) {
219
381
  let q = '';
220
382
  if (follow)
221
383
  q = '?follow=true';
222
- return await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/commands/${encodeURIComponent(commandId)}/logs${q}`, null, false, true);
384
+ return await this.request({
385
+ method: 'GET',
386
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/commands/${encodeURIComponent(commandId)}/logs${q}`,
387
+ parseResponseBody: false,
388
+ rawResponseBody: true,
389
+ });
223
390
  }
224
391
  async terminateSandboxCommand(workspace, sandboxId, commandId) {
225
- return await this.request('POST', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/commands/${encodeURIComponent(commandId)}/terminate`, {}, true);
392
+ return await this.request({
393
+ method: 'POST',
394
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/commands/${encodeURIComponent(commandId)}/terminate`,
395
+ body: {},
396
+ parseResponseBody: true,
397
+ });
226
398
  }
227
- // Snapshot methods
228
399
  async listSandboxSnapshots(workspace, sandboxId) {
229
- return await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/snapshots`, null, true);
400
+ return await this.request({
401
+ method: 'GET',
402
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/snapshots`,
403
+ parseResponseBody: true,
404
+ });
405
+ }
406
+ async listProjectSnapshots(workspace, project) {
407
+ const query = {};
408
+ if (project)
409
+ query.project_name = encodeURIComponent(project);
410
+ return await this.request({
411
+ method: 'GET',
412
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/snapshots`,
413
+ query,
414
+ parseResponseBody: true
415
+ });
230
416
  }
231
417
  async createSandboxSnapshot(workspace, sandboxId, body) {
232
- return await this.request('POST', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/snapshots`, body, true);
418
+ return await this.request({
419
+ method: 'POST',
420
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/snapshots`,
421
+ body,
422
+ parseResponseBody: true,
423
+ });
233
424
  }
234
425
  async getSandboxSnapshot(workspace, sandboxId, snapshotId) {
235
- return await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/snapshots/${encodeURIComponent(snapshotId)}`, null, true);
426
+ return await this.request({
427
+ method: 'GET',
428
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/snapshots/${encodeURIComponent(snapshotId)}`,
429
+ parseResponseBody: true,
430
+ });
236
431
  }
237
432
  async deleteSandboxSnapshot(workspace, sandboxId, snapshotId) {
238
- return await this.request('DELETE', `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/snapshots/${encodeURIComponent(snapshotId)}`, null, false);
433
+ return await this.request({
434
+ method: 'DELETE',
435
+ path: `/workspaces/${encodeURIComponent(workspace)}/sandboxes/${encodeURIComponent(sandboxId)}/snapshots/${encodeURIComponent(snapshotId)}`,
436
+ });
239
437
  }
240
438
  async sandboxWaitForSnapshot(workspace, sandboxId, snapshotId, timeout = 300) {
241
439
  const end = Date.now() + timeout * 1000;
@@ -306,16 +504,28 @@ class ApiClient {
306
504
  }
307
505
  }
308
506
  async getWorkspaces() {
309
- return await this.request('GET', '/workspaces', null, true);
507
+ return await this.request({
508
+ method: 'GET',
509
+ path: '/workspaces',
510
+ parseResponseBody: true,
511
+ });
310
512
  }
311
513
  async getPackages(workspace, project) {
312
514
  let query = '';
313
515
  if (project)
314
516
  query += `?project_name=${encodeURIComponent(project)}`;
315
- return await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/packages${query}`, null, true);
517
+ return await this.request({
518
+ method: 'GET',
519
+ path: `/workspaces/${encodeURIComponent(workspace)}/packages${query}`,
520
+ parseResponseBody: true,
521
+ });
316
522
  }
317
523
  async getProjects(workspace) {
318
- return await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/projects`, null, true);
524
+ return await this.request({
525
+ method: 'GET',
526
+ path: `/workspaces/${encodeURIComponent(workspace)}/projects`,
527
+ parseResponseBody: true,
528
+ });
319
529
  }
320
530
  async getResourceByIdentifier(workspace, params) {
321
531
  let query = '';
@@ -326,10 +536,17 @@ class ApiClient {
326
536
  query += '&';
327
537
  query += encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
328
538
  });
329
- return await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/identifiers${query}`, null, true);
539
+ return await this.request({
540
+ method: 'GET',
541
+ path: `/workspaces/${encodeURIComponent(workspace)}/identifiers${query}`,
542
+ parseResponseBody: true,
543
+ });
330
544
  }
331
545
  async getPipelineByIdentifier(workspace, project, identifier) {
332
- return await this.getResourceByIdentifier(workspace, { project, pipeline: identifier });
546
+ return await this.getResourceByIdentifier(workspace, {
547
+ project,
548
+ pipeline: identifier,
549
+ });
333
550
  }
334
551
  async getPackageVersionByIdentifier(workspace, project, pkg, version) {
335
552
  const opts = {
@@ -342,27 +559,49 @@ class ApiClient {
342
559
  return await this.getResourceByIdentifier(workspace, opts);
343
560
  }
344
561
  async getPackageVersion(workspace, pkgId, versionId) {
345
- return await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/packages/${encodeURIComponent(pkgId)}/versions/${encodeURIComponent(versionId)}`, null, true);
562
+ return await this.request({
563
+ method: 'GET',
564
+ path: `/workspaces/${encodeURIComponent(workspace)}/packages/${encodeURIComponent(pkgId)}/versions/${encodeURIComponent(versionId)}`,
565
+ parseResponseBody: true,
566
+ });
346
567
  }
347
568
  async getPackageLatest(workspace, pkgId) {
348
- const res = await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/packages/${encodeURIComponent(pkgId)}/versions?page=1&per_page=1`, null, true);
569
+ const res = await this.request({
570
+ method: 'GET',
571
+ path: `/workspaces/${encodeURIComponent(workspace)}/packages/${encodeURIComponent(pkgId)}/versions?page=1&per_page=1`,
572
+ parseResponseBody: true,
573
+ });
349
574
  if (res && res.versions && res.versions.length > 0) {
350
575
  return res.versions[0];
351
576
  }
352
577
  return null;
353
578
  }
354
579
  async downloadPackageVersion(workspace, pkgId, versionId) {
355
- return await this.request('GET', `/workspaces/${encodeURIComponent(workspace)}/packages/${encodeURIComponent(pkgId)}/versions/${encodeURIComponent(versionId)}/download`, null, false, true);
580
+ return await this.request({
581
+ method: 'GET',
582
+ path: `/workspaces/${encodeURIComponent(workspace)}/packages/${encodeURIComponent(pkgId)}/versions/${encodeURIComponent(versionId)}/download`,
583
+ rawResponseBody: true,
584
+ });
356
585
  }
357
586
  async postPackageVersion(workspace, pkgId, version) {
358
- return await this.request('POST', `/workspaces/${encodeURIComponent(workspace)}/packages/${encodeURIComponent(pkgId)}/versions`, {
359
- version
360
- }, true);
587
+ return await this.request({
588
+ method: 'POST',
589
+ path: `/workspaces/${encodeURIComponent(workspace)}/packages/${encodeURIComponent(pkgId)}/versions`,
590
+ body: {
591
+ version,
592
+ },
593
+ parseResponseBody: true,
594
+ });
361
595
  }
362
596
  async postPackageVersionZip(workspace, pkgId, versionId, file) {
363
597
  const form = new undici_1.FormData();
364
598
  form.append('file', file);
365
- return await this.request('POST', `/workspaces/${encodeURIComponent(workspace)}/packages/${encodeURIComponent(pkgId)}/versions/${versionId}/upload`, form, true);
599
+ return await this.request({
600
+ method: 'POST',
601
+ path: `/workspaces/${encodeURIComponent(workspace)}/packages/${encodeURIComponent(pkgId)}/versions/${versionId}/upload`,
602
+ body: form,
603
+ parseResponseBody: true,
604
+ });
366
605
  }
367
606
  async postPackage(workspace, project, identifier) {
368
607
  const body = {
@@ -371,16 +610,21 @@ class ApiClient {
371
610
  type: 'FILE',
372
611
  scope: 'WORKSPACE',
373
612
  authorization: {
374
- type: 'BUDDY'
375
- }
613
+ type: 'BUDDY',
614
+ },
376
615
  };
377
616
  if (project) {
378
617
  body.project = {
379
- name: project
618
+ name: project,
380
619
  };
381
620
  body.scope = 'PROJECT';
382
621
  }
383
- return await this.request('POST', `/workspaces/${encodeURIComponent(workspace)}/packages`, body, true);
622
+ return await this.request({
623
+ method: 'POST',
624
+ path: `/workspaces/${encodeURIComponent(workspace)}/packages`,
625
+ body,
626
+ parseResponseBody: true,
627
+ });
384
628
  }
385
629
  }
386
630
  exports.default = ApiClient;
@@ -62,7 +62,10 @@ async function oauthServer(api, clientId, clientSecret) {
62
62
  const response = await client.exchangeAppToken(urlCode, clientId, clientSecret, OAUTH_CLIENT_APP_REDIRECT_URL);
63
63
  res.end(texts_1.ERR_LOGIN_HTTP_SUCCESS);
64
64
  s.close();
65
- resolve(response.access_token);
65
+ resolve({
66
+ token: response.access_token,
67
+ refreshToken: response.refresh_token,
68
+ });
66
69
  }
67
70
  catch {
68
71
  res.end(texts_1.ERR_LOGIN_HTTP_FAILED);
@@ -84,11 +87,14 @@ async function getOrCreateApp(api) {
84
87
  const client = new client_1.default(new URL(`https://${api}`));
85
88
  let clientId = cfg_1.default.getApiClientId();
86
89
  let clientSecret = cfg_1.default.getApiClientSecret();
87
- if (clientId && clientSecret) {
90
+ let clientToken = cfg_1.default.getApiClientToken();
91
+ if (clientId && clientSecret && clientToken) {
88
92
  let clear = false;
89
93
  try {
90
94
  const app = await client.getApp(clientId);
91
- if (!app || !app.redirect_uris || !app.redirect_uris.includes(OAUTH_CLIENT_APP_REDIRECT_URL)) {
95
+ if (!app ||
96
+ !app.redirect_uris ||
97
+ !app.redirect_uris.includes(OAUTH_CLIENT_APP_REDIRECT_URL)) {
92
98
  clear = true;
93
99
  }
94
100
  }
@@ -98,25 +104,31 @@ async function getOrCreateApp(api) {
98
104
  if (clear) {
99
105
  clientId = null;
100
106
  clientSecret = null;
107
+ clientToken = null;
101
108
  }
102
109
  }
103
- if (!clientId || !clientSecret) {
110
+ if (!clientId || !clientSecret || !clientToken) {
104
111
  const app = await client.registerApp('Buddy CLI', OAUTH_CLIENT_APP_REDIRECT_URL);
105
112
  clientId = app.client_id;
106
113
  clientSecret = app.client_secret;
107
- cfg_1.default.setApiClient(clientId, clientSecret);
114
+ clientToken = app.registration_access_token;
115
+ cfg_1.default.setApiClient(clientId, clientSecret, clientToken);
108
116
  }
109
117
  return {
110
118
  clientId,
111
119
  clientSecret,
120
+ clientToken,
112
121
  };
113
122
  }
114
123
  async function authorizeOAuth(api) {
115
- const { clientId, clientSecret } = await getOrCreateApp(api);
116
- return await oauthServer(api, clientId, clientSecret);
124
+ const { clientId, clientSecret, clientToken } = await getOrCreateApp(api);
125
+ const { token, refreshToken } = await oauthServer(api, clientId, clientSecret);
126
+ return { token, refreshToken, clientId, clientSecret, clientToken };
117
127
  }
118
- async function authorizeToken(api, token, workspace, project) {
119
- const client = new client_1.default(new URL(`https://${api}`), token);
128
+ async function authorizeToken(api, token, refreshToken, clientId, clientSecret, clientToken, workspace, project) {
129
+ cfg_1.default.setApiToken(token);
130
+ cfg_1.default.setApiRefreshToken(refreshToken);
131
+ const client = new client_1.default(new URL(`https://${api}`), token, refreshToken, clientId, clientSecret, clientToken);
120
132
  const w = await client.getWorkspaces();
121
133
  if (!w.workspaces?.length) {
122
134
  output_1.default.exitError(texts_1.ERR_LOGIN_NO_WORKSPACES);
@@ -130,7 +142,6 @@ async function authorizeToken(api, token, workspace, project) {
130
142
  else {
131
143
  workspace = await output_1.default.selectWorkspace(w.workspaces);
132
144
  }
133
- cfg_1.default.setApiToken(token);
134
145
  cfg_1.default.setWorkspace(workspace);
135
146
  const p = await client.getProjects(workspace);
136
147
  if (project) {
@@ -185,11 +196,11 @@ commandLogin.action(async (options) => {
185
196
  cfg_1.default.setBaseUrl(api);
186
197
  if (!token) {
187
198
  output_1.default.normal(texts_1.TXT_LOGIN_OAUTH);
188
- const token = await authorizeOAuth(api);
189
- await authorizeToken(api, token, workspace, project);
199
+ const { token, refreshToken, clientId, clientSecret, clientToken } = await authorizeOAuth(api);
200
+ await authorizeToken(api, token, refreshToken, clientId, clientSecret, clientToken, workspace, project);
190
201
  }
191
202
  else {
192
- await authorizeToken(api, token, workspace, project);
203
+ await authorizeToken(api, token, '', '', '', '', workspace, project);
193
204
  }
194
205
  });
195
206
  exports.default = commandLogin;
@@ -7,8 +7,26 @@ const cfg_1 = __importDefault(require("../tunnel/cfg"));
7
7
  const output_1 = __importDefault(require("../output"));
8
8
  const texts_1 = require("../texts");
9
9
  const utils_1 = require("../utils");
10
+ const client_1 = __importDefault(require("../api/client"));
11
+ const input_1 = __importDefault(require("../input"));
10
12
  const commandLogout = (0, utils_1.newCommand)('logout', texts_1.DESC_COMMAND_LOGOUT);
13
+ const tryRemovingApp = async () => {
14
+ try {
15
+ const token = cfg_1.default.getApiToken();
16
+ const refreshToken = cfg_1.default.getApiRefreshApiToken();
17
+ const clientId = cfg_1.default.getApiClientId();
18
+ const clientSecret = cfg_1.default.getApiClientSecret();
19
+ const clientToken = cfg_1.default.getApiClientToken();
20
+ const baseUrl = input_1.default.restApiBaseUrl();
21
+ const client = new client_1.default(baseUrl, token, refreshToken, clientId, clientSecret, clientToken);
22
+ await client.deleteApp(clientId);
23
+ }
24
+ catch {
25
+ // do nothing
26
+ }
27
+ };
11
28
  commandLogout.action(async () => {
29
+ await tryRemovingApp();
12
30
  cfg_1.default.clearLogin();
13
31
  output_1.default.exitSuccess(texts_1.TXT_LOGOUT_SUCCESS);
14
32
  });