116 lines
3.7 KiB
JavaScript
116 lines
3.7 KiB
JavaScript
import path from "path";
|
|
import fs from "fs";
|
|
import grabHostNames from "../utils/grab-host-names";
|
|
import apiGet from "../functions/api/query/get";
|
|
import serializeQuery from "../utils/serialize-query";
|
|
import apiGetGrabQueryAndValues from "../utils/grab-query-and-values";
|
|
import debugLog from "../utils/logging/debug-log";
|
|
/**
|
|
* # Make a get request to Datasquirel API
|
|
*/
|
|
export default async function get({ key, db, query, queryValues, tableName, user_id, debug, forceLocal, }) {
|
|
const grabedHostNames = grabHostNames();
|
|
const { host, port, scheme } = grabedHostNames;
|
|
function debugFn(log, label) {
|
|
debugLog({ log, addTime: true, title: "apiGet", label });
|
|
}
|
|
/**
|
|
* Check for local DB settings
|
|
*
|
|
* @description Look for local db settings in `.env` file and by pass the http request if available
|
|
*/
|
|
const { DSQL_DB_NAME } = process.env;
|
|
if ((DSQL_DB_NAME === null || DSQL_DB_NAME === void 0 ? void 0 : DSQL_DB_NAME.match(/./)) && global.DSQL_USE_LOCAL) {
|
|
let dbSchema;
|
|
try {
|
|
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json");
|
|
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
|
|
}
|
|
catch (error) { }
|
|
if (debug) {
|
|
debugFn("Running Locally ...");
|
|
}
|
|
return await apiGet({
|
|
dbFullName: DSQL_DB_NAME,
|
|
query,
|
|
queryValues,
|
|
tableName,
|
|
dbSchema,
|
|
debug,
|
|
forceLocal,
|
|
});
|
|
}
|
|
/**
|
|
* Make https request
|
|
*
|
|
* @description make a request to datasquirel.com
|
|
*/
|
|
const httpResponse = await new Promise((resolve, reject) => {
|
|
const queryAndValues = apiGetGrabQueryAndValues({
|
|
query,
|
|
values: queryValues,
|
|
});
|
|
const queryObject = {
|
|
db: process.env.DSQL_API_DB_NAME || String(db),
|
|
query: queryAndValues.query,
|
|
queryValues: queryAndValues.valuesString,
|
|
tableName,
|
|
debug,
|
|
};
|
|
if (debug) {
|
|
debugFn(queryObject, "queryObject");
|
|
}
|
|
const queryString = serializeQuery(Object.assign({}, queryObject));
|
|
if (debug) {
|
|
debugFn(queryString, "queryString");
|
|
}
|
|
let path = `/api/query/${user_id || grabedHostNames.user_id}/get${queryString}`;
|
|
if (debug) {
|
|
debugFn(path, "path");
|
|
}
|
|
const requestObject = {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: key ||
|
|
process.env.DSQL_READ_ONLY_API_KEY ||
|
|
process.env.DSQL_FULL_ACCESS_API_KEY ||
|
|
process.env.DSQL_API_KEY,
|
|
},
|
|
port,
|
|
hostname: host,
|
|
path,
|
|
};
|
|
scheme
|
|
.request(requestObject,
|
|
/**
|
|
* Callback Function
|
|
*
|
|
* @description https request callback
|
|
*/
|
|
(response) => {
|
|
var str = "";
|
|
response.on("data", function (chunk) {
|
|
str += chunk;
|
|
});
|
|
response.on("end", function () {
|
|
try {
|
|
resolve(JSON.parse(str));
|
|
}
|
|
catch ( /** @type {any} */error) {
|
|
reject({
|
|
error: error.message,
|
|
result: str,
|
|
});
|
|
}
|
|
});
|
|
response.on("error", (err) => {
|
|
console.log("DSQL get Error,", err.message);
|
|
resolve(null);
|
|
});
|
|
})
|
|
.end();
|
|
});
|
|
return httpResponse;
|
|
}
|