UNPKG

2.56 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.AppClient = exports.formatName = void 0;
4const guards_1 = require("../guards");
5const color_1 = require("./color");
6const http_1 = require("./http");
7function formatName(app) {
8 if (app.org) {
9 return `${color_1.weak(`${app.org.name} / `)}${app.name}`;
10 }
11 return app.name;
12}
13exports.formatName = formatName;
14class 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 (!guards_1.isAppResponse(res)) {
25 throw 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 (!guards_1.isAppResponse(res)) {
35 throw 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 (!guards_1.isAppAssociationResponse(res)) {
64 throw 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}
76exports.AppClient = AppClient;