2024-11-13 13:13:10 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2023-09-21 14:00:04 +00:00
|
|
|
/**
|
|
|
|
* Imports
|
|
|
|
* ==============================================================================
|
|
|
|
*/
|
2024-11-13 13:13:10 +00:00
|
|
|
const grabHostNames = require("../../../../package-shared/utils/grab-host-names");
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main Function
|
|
|
|
* ==============================================================================
|
|
|
|
* @param {{
|
|
|
|
* url?: string,
|
|
|
|
* method: string,
|
|
|
|
* hostname: string,
|
|
|
|
* path?: string,
|
|
|
|
* href?: string,
|
|
|
|
* headers?: object,
|
|
|
|
* body?: object,
|
|
|
|
* }} params - params
|
|
|
|
*/
|
|
|
|
function httpsRequest({ url, method, hostname, path, href, headers, body }) {
|
|
|
|
const reqPayloadString = body ? JSON.stringify(body) : null;
|
|
|
|
|
2024-11-13 13:13:10 +00:00
|
|
|
const { host, port, scheme } = grabHostNames();
|
2023-09-21 16:51:08 +00:00
|
|
|
|
2023-09-21 14:00:04 +00:00
|
|
|
////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
2024-11-13 13:13:10 +00:00
|
|
|
/** @type {any} */
|
2023-09-21 14:00:04 +00:00
|
|
|
let requestOptions = {
|
|
|
|
method: method,
|
2024-11-13 13:13:10 +00:00
|
|
|
hostname: host,
|
|
|
|
port,
|
2023-09-21 14:00:04 +00:00
|
|
|
headers: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (path) requestOptions.path = path;
|
|
|
|
if (href) requestOptions.href = href;
|
|
|
|
|
|
|
|
if (headers) requestOptions.headers = headers;
|
|
|
|
if (body) {
|
|
|
|
requestOptions.headers["Content-Type"] = "application/json";
|
2023-09-21 16:51:08 +00:00
|
|
|
requestOptions.headers["Content-Length"] = Buffer.from(
|
|
|
|
reqPayloadString || ""
|
|
|
|
).length;
|
2023-09-21 14:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
|
|
|
return new Promise((res, rej) => {
|
2024-11-13 13:13:10 +00:00
|
|
|
const httpsRequest = scheme.request(
|
2023-09-21 14:00:04 +00:00
|
|
|
/* ====== Request Options object ====== */
|
|
|
|
// @ts-ignore
|
|
|
|
url ? url : 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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (body) httpsRequest.write(reqPayloadString);
|
|
|
|
|
|
|
|
httpsRequest.on("error", (error) => {
|
|
|
|
console.log("HTTPS request ERROR =>", error);
|
|
|
|
});
|
|
|
|
|
|
|
|
httpsRequest.end();
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
module.exports = httpsRequest;
|