datasquirel/package-shared/functions/backend/httpsRequest.js

142 lines
4.8 KiB
JavaScript
Raw Permalink Normal View History

2024-11-13 13:13:10 +00:00
// @ts-check
2023-09-21 14:00:04 +00:00
/**
* Imports
* ==============================================================================
*/
2024-12-06 10:31:24 +00:00
const https = require("https");
const http = require("http");
const { URL } = require("url");
2023-09-21 14:00:04 +00:00
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/**
* Main Function
* ==============================================================================
* @param {{
2024-12-06 10:31:24 +00:00
* scheme?: string,
2023-09-21 14:00:04 +00:00
* url?: string,
2024-12-06 10:31:24 +00:00
* method?: string,
* hostname?: string,
2023-09-21 14:00:04 +00:00
* path?: string,
2024-12-06 10:31:24 +00:00
* port?: number | string,
2023-09-21 14:00:04 +00:00
* headers?: object,
* body?: object,
* }} params - params
*/
2024-12-06 10:31:24 +00:00
module.exports = function httpsRequest({
url,
method,
hostname,
path,
headers,
body,
port,
scheme,
}) {
2023-09-21 14:00:04 +00:00
const reqPayloadString = body ? JSON.stringify(body) : null;
2024-12-06 10:31:24 +00:00
const PARSED_URL = url ? new URL(url) : null;
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 = {
2024-12-06 10:31:24 +00:00
method: method || "GET",
hostname: PARSED_URL ? PARSED_URL.hostname : hostname,
port: scheme?.match(/https/i)
? 443
: PARSED_URL
? PARSED_URL.protocol?.match(/https/i)
? 443
: PARSED_URL.port
: port
? Number(port)
: 80,
2023-09-21 14:00:04 +00:00
headers: {},
};
if (path) requestOptions.path = path;
2024-12-06 10:31:24 +00:00
// if (href) requestOptions.href = href;
2023-09-21 14:00:04 +00:00
if (headers) requestOptions.headers = headers;
if (body) {
requestOptions.headers["Content-Type"] = "application/json";
2024-12-06 10:31:24 +00:00
requestOptions.headers["Content-Length"] = reqPayloadString
? Buffer.from(reqPayloadString).length
: undefined;
2023-09-21 14:00:04 +00:00
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
return new Promise((res, rej) => {
2024-12-06 10:31:24 +00:00
const httpsRequest = (
scheme?.match(/https/i)
? https
: PARSED_URL?.protocol?.match(/https/i)
? https
: http
).request(
2023-09-21 14:00:04 +00:00
/* ====== Request Options object ====== */
2024-12-06 10:31:24 +00:00
requestOptions,
2023-09-21 14:00:04 +00:00
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
/* ====== 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);
2024-12-06 10:31:24 +00:00
rej(`HTTP response error =>, ${error.message}`);
});
response.on("close", () => {
console.log("HTTP(S) Response Closed Successfully");
2023-09-21 14:00:04 +00:00
});
}
);
if (body) httpsRequest.write(reqPayloadString);
httpsRequest.on("error", (error) => {
2024-12-06 10:31:24 +00:00
console.log("HTTPS request ERROR =>", error.message);
rej(`HTTP request error =>, ${error.message}`);
2023-09-21 14:00:04 +00:00
});
httpsRequest.end();
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
});
2024-12-06 10:31:24 +00:00
};
2023-09-21 14:00:04 +00:00
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////