datasquirel/utils/post.js

151 lines
5.1 KiB
JavaScript
Raw Normal View History

2023-08-08 14:55:27 +00:00
// @ts-check
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-07-02 06:06:48 +00:00
/**
* @typedef {object} PostDataPayload
2023-07-24 13:37:08 +00:00
* @property {"insert" | "update" | "delete"} action - The target action to take
2023-07-02 06:06:48 +00:00
* @property {string} table - Table name(slug) eg "blog_posts"
2023-07-27 04:22:02 +00:00
* @property {object} [data] - Table insert payload object => This must have keys that match
2023-07-02 06:06:48 +00:00
* table fields
2023-07-27 04:17:37 +00:00
* @property {string?} [identifierColumnName] - Table identifier field name => eg. "id" OR "email"
* @property {string?} [identifierValue] - Corresponding value of the selected field name => This
* checks identifies a the target row for "update" or "delete". Not needed for "insert"
* @property {string?} [duplicateColumnName] - Duplicate column name to check for
* @property {string?} [duplicateColumnValue] - Duplicate column value to match. If no "update" param
2023-07-15 14:06:32 +00:00
* provided, function will return null
2023-07-27 04:17:37 +00:00
* @property {boolean?} [update] - Should the "insert" action update the existing entry if indeed
2023-07-15 14:06:32 +00:00
* the entry with "duplicateColumnValue" exists?
2023-07-02 06:06:48 +00:00
*/
2023-05-03 06:16:29 +00:00
/**
2023-07-24 13:37:08 +00:00
* Make a post 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
2023-07-15 14:06:32 +00:00
* @param {string} params.key - FULL ACCESS API Key
2023-05-18 08:22:05 +00:00
* @param {string} params.database - Database Name
2023-07-27 04:08:15 +00:00
* @param {PostDataPayload | string} params.query - SQL query String or Request Object
2023-07-27 09:53:18 +00:00
* @param {any[]} [params.queryValues] - Query Values if using "?" placeholders
2023-08-08 14:55:27 +00:00
* @param {string} [params.tableName] - Name of the table to query
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-08-08 14:55:27 +00:00
async function post({ key, query, queryValues, database, tableName }) {
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-22 10:17:04 +00:00
const reqPayloadString = JSON.stringify({
2023-05-14 09:03:32 +00:00
query,
2023-07-27 09:53:18 +00:00
queryValues,
2023-05-14 09:03:32 +00:00
database,
2023-08-08 15:17:37 +00:00
tableName: tableName ? tableName : null,
2023-05-22 10:04:04 +00:00
}).replace(/\n|\r|\n\r/gm, "");
2023-05-06 12:18:44 +00:00
2023-05-22 10:17:04 +00:00
try {
JSON.parse(reqPayloadString);
} catch (error) {
console.log(error);
console.log(reqPayloadString);
return {
success: false,
payload: null,
error: "Query object is invalid. Please Check query data values",
};
}
const reqPayload = reqPayloadString;
2023-05-06 12:18:44 +00:00
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
2023-05-22 10:43:42 +00:00
"Content-Length": Buffer.from(reqPayload).length,
2023-05-06 12:18:44 +00:00
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-08-08 15:20:28 +00:00
console.log(error);
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,
2023-08-08 15:20:28 +00:00
error: error,
2023-05-22 08:41:53 +00:00
});
}
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);
2023-05-22 10:37:14 +00:00
httpsRequest.on("error", (error) => {
2023-08-08 15:20:28 +00:00
console.log("HTTPS request ERROR =>", error);
2023-05-22 10:37:14 +00:00
});
2023-05-06 12:18:44 +00:00
httpsRequest.end();
2023-05-03 06:16:29 +00:00
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
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 = post;