89 lines
3.5 KiB
JavaScript
89 lines
3.5 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.default = httpsRequest;
|
|
const https_1 = __importDefault(require("https"));
|
|
const http_1 = __importDefault(require("http"));
|
|
const url_1 = require("url");
|
|
/**
|
|
* # Make Https Request
|
|
*/
|
|
function httpsRequest({ url, method, hostname, path, headers, body, port, scheme, }) {
|
|
var _a;
|
|
const reqPayloadString = body ? JSON.stringify(body) : null;
|
|
const PARSED_URL = url ? new url_1.URL(url) : null;
|
|
////////////////////////////////////////////////
|
|
////////////////////////////////////////////////
|
|
////////////////////////////////////////////////
|
|
/** @type {any} */
|
|
let requestOptions = {
|
|
method: method || "GET",
|
|
hostname: PARSED_URL ? PARSED_URL.hostname : hostname,
|
|
port: (scheme === null || scheme === void 0 ? void 0 : scheme.match(/https/i))
|
|
? 443
|
|
: PARSED_URL
|
|
? ((_a = PARSED_URL.protocol) === null || _a === void 0 ? void 0 : _a.match(/https/i))
|
|
? 443
|
|
: PARSED_URL.port
|
|
: port
|
|
? Number(port)
|
|
: 80,
|
|
headers: {},
|
|
};
|
|
if (path)
|
|
requestOptions.path = path;
|
|
// if (href) requestOptions.href = href;
|
|
if (headers)
|
|
requestOptions.headers = headers;
|
|
if (body) {
|
|
requestOptions.headers["Content-Type"] = "application/json";
|
|
requestOptions.headers["Content-Length"] = reqPayloadString
|
|
? Buffer.from(reqPayloadString).length
|
|
: undefined;
|
|
}
|
|
////////////////////////////////////////////////
|
|
////////////////////////////////////////////////
|
|
////////////////////////////////////////////////
|
|
return new Promise((res, rej) => {
|
|
var _a;
|
|
const httpsRequest = ((scheme === null || scheme === void 0 ? void 0 : scheme.match(/https/i))
|
|
? https_1.default
|
|
: ((_a = PARSED_URL === null || PARSED_URL === void 0 ? void 0 : PARSED_URL.protocol) === null || _a === void 0 ? void 0 : _a.match(/https/i))
|
|
? https_1.default
|
|
: http_1.default).request(
|
|
/* ====== Request Options object ====== */
|
|
requestOptions,
|
|
////////////////////////////////////////////////
|
|
////////////////////////////////////////////////
|
|
////////////////////////////////////////////////
|
|
/* ====== Callback function ====== */
|
|
(response) => {
|
|
var str = "";
|
|
// ## another chunk of data has been received, so append it to `str`
|
|
response.on("data", function (chunk) {
|
|
str += chunk;
|
|
});
|
|
// ## the whole response has been received, so we just print it out here
|
|
response.on("end", function () {
|
|
res(str);
|
|
});
|
|
response.on("error", (error) => {
|
|
console.log("HTTP response error =>", error.message);
|
|
rej(`HTTP response error =>, ${error.message}`);
|
|
});
|
|
response.on("close", () => {
|
|
console.log("HTTP(S) Response Closed Successfully");
|
|
});
|
|
});
|
|
if (body)
|
|
httpsRequest.write(reqPayloadString);
|
|
httpsRequest.on("error", (error) => {
|
|
console.log("HTTPS request ERROR =>", error.message);
|
|
rej(`HTTP request error =>, ${error.message}`);
|
|
});
|
|
httpsRequest.end();
|
|
});
|
|
}
|