datasquirel/utils/get.js

165 lines
5.2 KiB
JavaScript
Raw Permalink Normal View History

2023-09-21 14:00:04 +00:00
// @ts-check
2024-07-13 11:19:48 +00:00
const https = require("node:https");
2023-09-21 14:00:04 +00:00
const path = require("path");
const fs = require("fs");
2024-11-13 13:13:10 +00:00
const grabHostNames = require("../package-shared/utils/grab-host-names");
2024-12-06 10:31:24 +00:00
const apiGet = require("../package-shared/functions/api/query/get");
2024-12-10 14:20:48 +00:00
const serializeQuery = require("../package-shared/utils/serialize-query");
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-11-18 09:25:28 +00:00
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
2024-12-08 08:58:57 +00:00
* @param {string | number} [params.user_id] - User ID
2023-09-21 14:00:04 +00:00
*
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,
useLocal,
user_id,
}) {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
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(/./) &&
2024-11-18 09:25:28 +00:00
DSQL_DB_NAME?.match(/./) &&
useLocal
2023-09-21 16:51:08 +00:00
) {
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) {}
2024-12-06 10:31:24 +00:00
return await apiGet({
dbFullName: DSQL_DB_NAME,
query,
queryValues,
tableName,
dbSchema,
2024-12-06 11:55:03 +00:00
useLocal,
2023-09-21 14:00:04 +00:00
});
}
/**
* 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 = {
2024-11-15 14:37:53 +00:00
db: process.env.DSQL_API_DB_NAME || String(db),
2024-10-18 04:49:04 +00:00
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/${
user_id || grabedHostNames.user_id
}/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",
2024-11-15 14:37:53 +00:00
Authorization:
key ||
process.env.DSQL_READ_ONLY_API_KEY ||
process.env.DSQL_FULL_ACCESS_API_KEY ||
process.env.DSQL_API_KEY,
2024-07-13 10:53:35 +00:00
},
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-12-08 08:58:57 +00:00
console.log("requestObject", requestObject);
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;