UNPKG

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