UNPKG

4.15 kBJavaScriptView Raw
1"use strict";
2/**
3 * © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
4 * (see file: README.md)
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7Object.defineProperty(exports, "__esModule", { value: true });
8const http = require("http");
9const lodash_1 = require("lodash");
10const request = require("request");
11const url = require("url");
12const logger = require("./logger");
13class RestApiClient {
14 constructor(options) {
15 this.DEFAULT_CONFIG = {
16 protocol: 'https',
17 host: 'localhost',
18 port: '443',
19 user: 'user',
20 password: 'password',
21 base: '',
22 strictSSL: true,
23 };
24 options = lodash_1.merge(this.DEFAULT_CONFIG, options);
25 this.options = options;
26 }
27 encode() {
28 return encodeURIComponent.apply(this, arguments);
29 }
30 url(pathname, query) {
31 const options = this.options;
32 const uri = url.format({
33 query,
34 hostname: options.host,
35 pathname: options.base + pathname,
36 port: options.port,
37 protocol: options.protocol,
38 });
39 return decodeURIComponent(uri);
40 }
41 authorize(p) {
42 const options = this.options;
43 if (p.oauth) {
44 p.oauth = options.oauth;
45 return;
46 }
47 if (typeof options.user === 'string') {
48 p.auth = {
49 user: options.user,
50 pass: options.password,
51 };
52 }
53 }
54 request(method, path, params) {
55 if (typeof path === 'object') {
56 let args = Array.from(path);
57 args.unshift(method);
58 return this.request.apply(this, args);
59 }
60 let options = this.options;
61 // Cookie useful to help you bypass SSO like OKTA
62 // After logging into the browser with Jira, you can
63 // copy and paste your cookie into ~/.gh.json
64 // plugins": { "jira": { "cookie": "longcookie", ...} }
65 const cookie = options.cookie
66 ? {
67 headers: {
68 Cookie: options.cookie,
69 },
70 }
71 : {};
72 let p = Object.assign({}, cookie, { method, strictSSL: options.strictSSL, uri: this.url(path), json: true, followAllRedirects: true });
73 if (params) {
74 p = lodash_1.merge(p, params);
75 }
76 this.authorize(p);
77 let id = Math.floor(Math.random() * 10000000);
78 let begin = new Date().getTime();
79 return new Promise((resolve, reject) => {
80 logger.debug(`New request #${id} started at ${begin}:\n${method} ${p.uri}`);
81 logger.insane(p);
82 request(p, (error, response) => {
83 let end = new Date().getTime();
84 logger.debug(`End of request #${id} at ${end} (${end -
85 begin}ms) with status code: ${response && response.statusCode}`);
86 if (response) {
87 logger.insane('Response headers:');
88 logger.insane(response.headers);
89 logger.debug('Response body');
90 logger.debug(response.body);
91 }
92 if (error) {
93 reject(error);
94 return;
95 }
96 if (response.statusCode < 200 || response.statusCode > 399) {
97 reject({
98 response,
99 error: `${response.statusCode} ${http.STATUS_CODES[response.statusCode]}`,
100 code: response.statusCode,
101 msg: http.STATUS_CODES[response.statusCode],
102 });
103 return;
104 }
105 resolve(response);
106 });
107 });
108 }
109 get() {
110 return this.request('GET', arguments);
111 }
112 post() {
113 return this.request('POST', arguments);
114 }
115 put() {
116 return this.request('PUT', arguments);
117 }
118 delete() {
119 return this.request('DELETE', arguments);
120 }
121}
122module.exports = RestApiClient;
123//# sourceMappingURL=rest-api-client.js.map
\No newline at end of file