133 lines
5.3 KiB
JavaScript
133 lines
5.3 KiB
JavaScript
"use strict";
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.default = get;
|
|
const path_1 = __importDefault(require("path"));
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const grab_host_names_1 = __importDefault(require("../utils/grab-host-names"));
|
|
const get_1 = __importDefault(require("../functions/api/query/get"));
|
|
const serialize_query_1 = __importDefault(require("../utils/serialize-query"));
|
|
const grab_query_and_values_1 = __importDefault(require("../utils/grab-query-and-values"));
|
|
const debug_log_1 = __importDefault(require("../utils/logging/debug-log"));
|
|
/**
|
|
* # Make a get request to Datasquirel API
|
|
*/
|
|
function get(_a) {
|
|
return __awaiter(this, arguments, void 0, function* ({ key, db, query, queryValues, tableName, user_id, debug, forceLocal, }) {
|
|
const grabedHostNames = (0, grab_host_names_1.default)();
|
|
const { host, port, scheme } = grabedHostNames;
|
|
function debugFn(log, label) {
|
|
(0, debug_log_1.default)({ 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_1.default.resolve(process.cwd(), "dsql.schema.json");
|
|
dbSchema = JSON.parse(fs_1.default.readFileSync(localDbSchemaPath, "utf8"));
|
|
}
|
|
catch (error) { }
|
|
if (debug) {
|
|
debugFn("Running Locally ...");
|
|
}
|
|
return yield (0, get_1.default)({
|
|
dbFullName: DSQL_DB_NAME,
|
|
query,
|
|
queryValues,
|
|
tableName,
|
|
dbSchema,
|
|
debug,
|
|
forceLocal,
|
|
});
|
|
}
|
|
/**
|
|
* Make https request
|
|
*
|
|
* @description make a request to datasquirel.com
|
|
*/
|
|
const httpResponse = yield new Promise((resolve, reject) => {
|
|
const queryAndValues = (0, grab_query_and_values_1.default)({
|
|
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 = (0, serialize_query_1.default)(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;
|
|
});
|
|
}
|