datasquirel/utils/get.js

158 lines
5.3 KiB
JavaScript
Raw Normal View History

2023-09-21 14:00:04 +00:00
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
2024-07-13 11:19:48 +00:00
const http = require("node:http");
const https = require("node:https");
2023-09-21 14:00:04 +00:00
const path = require("path");
const fs = require("fs");
const localGet = require("../engine/query/get");
2024-10-18 04:49:04 +00:00
const serializeQuery = require("./functions/serialize-query");
2024-11-13 13:13:10 +00:00
const grabHostNames = require("../package-shared/utils/grab-host-names");
2023-09-21 14:00:04 +00:00
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* Make a get request to Datasquirel API
* ==============================================================================
* @async
*
* @param {Object} params - Single object passed
* @param {string} [params.key] - API Key
* @param {string} [params.db] - Database Name
* @param {string} params.query - SQL Query
* @param {string[]} [params.queryValues] - An array of query values if using "?" placeholders
* @param {string} [params.tableName] - Name of the table to query
*
2024-10-19 16:45:42 +00:00
* @returns { Promise<import("../package-shared/types").GetReturn> } - Return Object
2023-09-21 14:00:04 +00:00
*/
async function get({ key, db, query, queryValues, tableName }) {
2024-11-13 13:13:10 +00:00
const { host, port, scheme } = grabHostNames();
2023-09-21 16:51:08 +00:00
2023-09-21 14:00:04 +00:00
/**
* Check for local DB settings
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
2024-11-06 09:30:00 +00:00
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
process.env;
2023-09-21 14:00:04 +00:00
2023-09-21 16:51:08 +00:00
if (
2024-11-06 06:26:23 +00:00
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
2024-11-06 09:30:00 +00:00
DSQL_DB_PASSWORD?.match(/./) &&
2023-09-21 16:51:08 +00:00
DSQL_DB_NAME?.match(/./)
) {
2024-10-19 16:45:42 +00:00
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
2023-09-21 14:00:04 +00:00
let dbSchema;
try {
2023-09-21 16:51:08 +00:00
const localDbSchemaPath = path.resolve(
process.cwd(),
"dsql.schema.json"
);
2023-09-21 14:00:04 +00:00
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
return await localGet({
dbSchema: dbSchema,
options: {
query: query,
queryValues: queryValues,
tableName: tableName,
},
});
}
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
const httpResponse = await new Promise((resolve, reject) => {
2024-10-19 16:45:42 +00:00
/** @type {import("../package-shared/types").GetReqQueryObject} */
2024-10-18 04:49:04 +00:00
const queryObject = {
db: String(db),
query: String(
query
.replace(/\n|\r|\n\r/g, "")
.replace(/ {2,}/g, " ")
.replace(/ /g, "+")
),
queryValues: queryValues ? JSON.stringify(queryValues) : undefined,
tableName,
};
const queryString = serializeQuery({ query: queryObject });
let path = `/api/query/get${queryString}`;
2023-09-21 14:00:04 +00:00
2024-07-13 10:53:35 +00:00
/** @type {https.RequestOptions} */
const requestObject = {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: key,
},
2024-11-13 13:13:10 +00:00
port,
hostname: host,
2024-07-13 11:19:48 +00:00
path: encodeURI(path),
2024-07-13 10:53:35 +00:00
};
2024-11-13 13:13:10 +00:00
scheme
2023-09-21 14:00:04 +00:00
.request(
2024-07-13 10:53:35 +00:00
requestObject,
2023-09-21 14:00:04 +00:00
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
2024-07-13 11:01:15 +00:00
try {
resolve(JSON.parse(str));
2024-07-24 07:18:04 +00:00
} catch (/** @type {any} */ error) {
2024-07-13 11:01:15 +00:00
reject({
error: error.message,
result: str,
});
}
2023-09-21 14:00:04 +00:00
});
response.on("error", (err) => {
2024-07-13 10:50:15 +00:00
console.log("DSQL get Error,", err.message);
2023-09-21 14:00:04 +00:00
reject(err);
});
}
)
.end();
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
module.exports = get;