datasquirel/users/add-user.js

104 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-05-06 11:14:09 +00:00
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const https = require("https");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
2023-05-18 11:43:52 +00:00
/**
2023-05-18 11:51:11 +00:00
* @typedef {object} FunctionReturn
2023-05-18 11:43:52 +00:00
* @property {boolean} success - Did the function run successfully?
* @property {(Object[]|string)} [payload=[]] - Payload
*/
2023-05-06 11:14:09 +00:00
/**
2023-07-24 21:34:01 +00:00
* @typedef {object} UserDataPayload
* @property {!string} first_name - First Name *Required
* @property {!string} last_name - Last Name *Required
* @property {!string} email - Email *Required
* @property {!string} password - Password *Required
* @property {string?} username - Username (Optional)
*/
/**
* Add User to Database
2023-05-06 11:14:09 +00:00
* ==============================================================================
2023-05-18 11:43:52 +00:00
* @async
*
2023-07-24 21:34:01 +00:00
* @param {object} props - Single object passed
* @param {string} props.key - FULL ACCESS API Key
* @param {string} props.database - Database Name
* @param {UserDataPayload} props.payload - User Data Payload
2023-05-18 11:43:52 +00:00
*
2023-05-22 07:51:33 +00:00
* @returns { Promise<FunctionReturn> }
2023-05-06 11:14:09 +00:00
*/
2023-07-07 19:13:13 +00:00
async function addUser({ key, payload, database }) {
2023-05-06 11:14:09 +00:00
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
2023-05-06 12:18:44 +00:00
const reqPayload = JSON.stringify({
payload,
database,
});
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
2023-05-22 10:52:27 +00:00
"Content-Length": Buffer.from(reqPayload).length,
2023-05-06 12:18:44 +00:00
Authorization: key,
2023-05-06 11:14:09 +00:00
},
2023-05-06 12:18:44 +00:00
port: 443,
hostname: "datasquirel.com",
path: `/api/user/add-user`,
},
2023-05-06 11:14:09 +00:00
2023-05-06 12:18:44 +00:00
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
2023-05-06 11:14:09 +00:00
2023-05-06 12:18:44 +00:00
response.on("data", function (chunk) {
str += chunk;
});
2023-05-06 11:14:09 +00:00
2023-05-06 12:18:44 +00:00
response.on("end", function () {
resolve(JSON.parse(str));
});
2023-05-06 11:14:09 +00:00
2023-05-06 12:18:44 +00:00
response.on("error", (err) => {
reject(err);
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.end();
2023-05-06 11:14:09 +00:00
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
2023-07-07 19:13:13 +00:00
}
2023-05-06 11:14:09 +00:00
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
2023-07-07 19:13:13 +00:00
module.exports = addUser;