datasquirel/utils/get.js

92 lines
3.0 KiB
JavaScript
Raw Normal View History

2023-05-03 06:16:29 +00:00
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const https = require("https");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
2023-05-18 08:22:05 +00:00
/**
* @typedef {Object} GetReturn
* @property {boolean} success - Did the function run successfully?
2023-07-02 06:06:48 +00:00
* @property {(Object[]|string)} [payload=[]] - GET request results
2023-05-18 08:22:05 +00:00
*/
2023-05-03 06:16:29 +00:00
/**
2023-07-24 13:37:08 +00:00
* Make a get request to Datasquirel API
2023-05-03 06:16:29 +00:00
* ==============================================================================
2023-05-18 08:22:05 +00:00
* @async
*
* @param {Object} params - Single object passed
* @param {string} params.key - API Key
* @param {string} params.db - Database Name
* @param {(string | Object)} params.query - SQL Query
*
2023-05-22 07:51:33 +00:00
* @returns { Promise<GetReturn> } - Return Object
2023-05-03 06:16:29 +00:00
*/
2023-07-07 19:13:13 +00:00
async function get({ key, db, query }) {
2023-05-03 06:16:29 +00:00
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
https
.request(
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/query/get?db=${db}&query=${query
.replace(/\n|\r|\n\r/g, "")
.replace(/ {2,}/g, " ")
.replace(/ /g, "+")}`,
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("error", (err) => {
reject(err);
});
}
)
.end();
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
2023-07-07 19:13:13 +00:00
}
2023-05-03 06:16:29 +00:00
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
2023-07-07 19:13:13 +00:00
module.exports = get;