-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (98 loc) · 3.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"use strict";
const { URL } = require("url");
const { posix } = require("path");
const http = require("http");
const https = require("https");
async function makeHttpRequest(isSecure, url, login, password, method, path, body, options, doNotReadResponse) {
options = options || {};
url = isSecure
? url.startsWith("https://") ? url : "https://" + url
: url.startsWith("http://") ? url : "http://" + url;
const parsedBaseUrl = new URL(url);
const prefixPath = parsedBaseUrl.pathname;
const fullPath = posix.join(prefixPath, path);
const parsedFullUrl = new URL(fullPath, parsedBaseUrl);
const fullUrl = parsedFullUrl.toString();
options.method = method;
if ((typeof login === "string") && (typeof password === "string")) {
options.auth = login + ":" + password;
}
if (body) {
body = JSON.stringify(body);
if (!options.hasOwnProperty("headers")) {
options.headers = {};
}
options.headers["Content-Type"] = "application/json";
options.headers["Content-Length"] = Buffer.byteLength(body);
}
const protocolModule = isSecure ? https : http;
const request = protocolModule.request(fullUrl, options);
return new Promise(function(resolve, reject) {
request.on("response", function (response) {
if (!doNotReadResponse) {
const dataChunks = [];
response.on("data", (chunk) => {
dataChunks.push(chunk);
});
response.on("end", () => {
const responseBodyBuffer = Buffer.concat(dataChunks);
const responseBodyString = responseBodyBuffer.toString("utf8");
let responseBody;
let parseError = null;
try {
responseBody = JSON.parse(responseBodyString);
} catch (err) {
responseBody = responseBodyString;
parseError = err;
}
const parseSuccessfully = (parseError === null);
const isJsonResponse = response.headers.hasOwnProperty("content-type")
&& response.headers["content-type"].includes("application/json");
if (isJsonResponse && !parseSuccessfully) {
reject(parseError);
} else {
if ((response.statusCode < 200) || (response.statusCode > 299)) {
reject(new ExternalServiceError(response.statusCode, response.statusMessage, responseBody));
} else {
resolve(responseBody);
}
}
});
} else {
resolve(response);
}
});
request.on("error", (err) => reject(err));
if (body) {
request.write(body);
}
request.end();
});
}
class ExternalServiceError extends Error {
constructor(status, statusMessage, response) {
let message;
if ((typeof response === "object") && ("error" in response)) {
message = response["error"];
} else if (typeof response === "string") {
message = response;
} else {
message = JSON.stringify(response);
}
const trailerArray = [];
if (statusMessage) {
trailerArray.push(statusMessage);
}
if (status) {
trailerArray.push(status);
}
if ((message === "") && trailerArray.length !== 0) {
message = trailerArray.join(" ");
}
super(message);
this.name = this.constructor.name;
this.status = status;
this.statusMessage = statusMessage;
}
}
module.exports = makeHttpRequest;