app.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AppClient = exports.formatName = void 0;
  4. const guards_1 = require("../guards");
  5. const color_1 = require("./color");
  6. const http_1 = require("./http");
  7. function formatName(app) {
  8. if (app.org) {
  9. return `${(0, color_1.weak)(`${app.org.name} / `)}${app.name}`;
  10. }
  11. return app.name;
  12. }
  13. exports.formatName = formatName;
  14. class AppClient extends http_1.ResourceClient {
  15. constructor(token, e) {
  16. super();
  17. this.token = token;
  18. this.e = e;
  19. }
  20. async load(id) {
  21. const { req } = await this.e.client.make('GET', `/apps/${id}`);
  22. this.applyAuthentication(req, this.token);
  23. const res = await this.e.client.do(req);
  24. if (!(0, guards_1.isAppResponse)(res)) {
  25. throw (0, http_1.createFatalAPIFormat)(req, res);
  26. }
  27. return res.data;
  28. }
  29. async create(details) {
  30. const { req } = await this.e.client.make('POST', '/apps');
  31. this.applyAuthentication(req, this.token);
  32. req.send(details);
  33. const res = await this.e.client.do(req);
  34. if (!(0, guards_1.isAppResponse)(res)) {
  35. throw (0, http_1.createFatalAPIFormat)(req, res);
  36. }
  37. return res.data;
  38. }
  39. paginate(args = {}, orgId) {
  40. return this.e.client.paginate({
  41. reqgen: async () => {
  42. const { req } = await this.e.client.make('GET', '/apps');
  43. this.applyAuthentication(req, this.token);
  44. if (orgId) {
  45. req.send({ org_id: orgId });
  46. }
  47. return { req };
  48. },
  49. guard: guards_1.isAppsResponse,
  50. ...args,
  51. });
  52. }
  53. async createAssociation(id, association) {
  54. const { req } = await this.e.client.make('POST', `/apps/${id}/repository`);
  55. req
  56. .set('Authorization', `Bearer ${this.token}`)
  57. .send({
  58. repository_id: association.repoId,
  59. type: association.type,
  60. branches: association.branches,
  61. });
  62. const res = await this.e.client.do(req);
  63. if (!(0, guards_1.isAppAssociationResponse)(res)) {
  64. throw (0, http_1.createFatalAPIFormat)(req, res);
  65. }
  66. return res.data;
  67. }
  68. async deleteAssociation(id) {
  69. const { req } = await this.e.client.make('DELETE', `/apps/${id}/repository`);
  70. req
  71. .set('Authorization', `Bearer ${this.token}`)
  72. .send({});
  73. await req;
  74. }
  75. }
  76. exports.AppClient = AppClient;