2023-05-03 06:16:29 +00:00
|
|
|
/**
|
|
|
|
* ==============================================================================
|
|
|
|
* Imports
|
|
|
|
* ==============================================================================
|
|
|
|
*/
|
|
|
|
const https = require("https");
|
|
|
|
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
|
2023-05-18 08:22:05 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} PostReturn
|
|
|
|
* @property {boolean} success - Did the function run successfully?
|
|
|
|
* @property {(Object[]|string)} [payload=[]] - The Y Coordinate
|
|
|
|
*/
|
|
|
|
|
2023-05-03 06:16:29 +00:00
|
|
|
/**
|
|
|
|
* ==============================================================================
|
|
|
|
* Main Function
|
|
|
|
* ==============================================================================
|
2023-05-18 08:22:05 +00:00
|
|
|
* @async
|
|
|
|
*
|
|
|
|
* @param {Object} params - Single object passed
|
|
|
|
* @param {string} params.key - API Key
|
|
|
|
* @param {string} params.database - Database Name
|
2023-05-18 11:51:11 +00:00
|
|
|
* @param {({
|
|
|
|
* action: [string="insert"],
|
|
|
|
* table: string,
|
|
|
|
* identifierColumnName: string,
|
|
|
|
* identifierValue: (string|number),
|
|
|
|
* data: object,
|
|
|
|
* } | string)} params.query - SQL query String or Request Object
|
2023-05-18 08:22:05 +00:00
|
|
|
*
|
2023-05-22 07:51:33 +00:00
|
|
|
* @returns { Promise<PostReturn> } - Return Object
|
2023-05-03 06:16:29 +00:00
|
|
|
*/
|
2023-05-14 09:03:32 +00:00
|
|
|
module.exports = async function ({ key, query, database }) {
|
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) => {
|
2023-05-14 09:03:32 +00:00
|
|
|
const reqPayload = JSON.stringify({
|
|
|
|
query,
|
|
|
|
database,
|
|
|
|
});
|
2023-05-06 12:18:44 +00:00
|
|
|
|
2023-05-22 09:07:47 +00:00
|
|
|
console.log(reqPayload);
|
|
|
|
|
2023-05-06 12:18:44 +00:00
|
|
|
const httpsRequest = https.request(
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Content-Length": reqPayload.length,
|
|
|
|
Authorization: key,
|
2023-05-03 06:16:29 +00:00
|
|
|
},
|
2023-05-06 12:18:44 +00:00
|
|
|
port: 443,
|
|
|
|
hostname: "datasquirel.com",
|
|
|
|
path: `/api/query/post`,
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback Function
|
|
|
|
*
|
|
|
|
* @description https request callback
|
|
|
|
*/
|
|
|
|
(response) => {
|
|
|
|
var str = "";
|
2023-05-03 06:16:29 +00:00
|
|
|
|
2023-05-06 12:18:44 +00:00
|
|
|
response.on("data", function (chunk) {
|
|
|
|
str += chunk;
|
|
|
|
});
|
2023-05-03 06:16:29 +00:00
|
|
|
|
2023-05-06 12:18:44 +00:00
|
|
|
response.on("end", function () {
|
2023-05-22 08:41:53 +00:00
|
|
|
try {
|
|
|
|
resolve(JSON.parse(str));
|
|
|
|
} catch (error) {
|
2023-05-22 08:54:35 +00:00
|
|
|
console.log(error.message);
|
2023-05-22 08:58:00 +00:00
|
|
|
console.log("Fetched Payload =>", str);
|
2023-05-22 08:54:35 +00:00
|
|
|
|
2023-05-22 08:41:53 +00:00
|
|
|
resolve({
|
|
|
|
success: false,
|
|
|
|
payload: null,
|
|
|
|
error: error.message,
|
|
|
|
});
|
|
|
|
}
|
2023-05-06 12:18:44 +00:00
|
|
|
});
|
2023-05-03 06:16:29 +00:00
|
|
|
|
2023-05-06 12:18:44 +00:00
|
|
|
response.on("error", (err) => {
|
2023-05-22 08:41:53 +00:00
|
|
|
resolve({
|
|
|
|
success: false,
|
|
|
|
payload: null,
|
|
|
|
error: err.message,
|
|
|
|
});
|
2023-05-06 12:18:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2023-05-03 06:16:29 +00:00
|
|
|
|
2023-05-06 12:18:44 +00:00
|
|
|
httpsRequest.write(reqPayload);
|
|
|
|
httpsRequest.end();
|
2023-05-03 06:16:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
|
|
|
|
return httpResponse;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|
|
|
|
/** ********************************************** */
|