datasquirel/utils/get.ts

153 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-09-21 14:00:04 +00:00
// @ts-check
2025-01-10 19:10:28 +00:00
import https from "node:https";
import path from "path";
import fs from "fs";
import grabHostNames from "../package-shared/utils/grab-host-names";
import apiGet from "../package-shared/functions/api/query/get";
import serializeQuery from "../package-shared/utils/serialize-query";
import { GetReturn } from "../package-shared/types";
type Param = {
key?: string;
db?: string;
query: string;
queryValues?: string[];
tableName?: string;
useLocal?: boolean;
user_id?: string | number;
};
2023-09-21 14:00:04 +00:00
/**
2025-01-10 19:10:28 +00:00
* # Make a get request to Datasquirel API
2023-09-21 14:00:04 +00:00
*/
2025-01-10 19:10:28 +00:00
export default async function get({
key,
db,
query,
queryValues,
tableName,
useLocal,
user_id,
2025-01-10 19:10:28 +00:00
}: Param): Promise<GetReturn> {
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} */
2025-01-10 19:10:28 +00:00
let dbSchema:
| import("../package-shared/types").DSQL_DatabaseSchemaType
| undefined;
2023-09-21 14:00:04 +00:00
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} */
2025-01-10 19:10:28 +00:00
const queryObject: import("../package-shared/types").GetReqQueryObject =
{
db: process.env.DSQL_API_DB_NAME || String(db),
query: String(
query.replace(/\n|\r|\n\r/g, "").replace(/ {2,}/g, " ")
),
queryValues: queryValues
? JSON.stringify(queryValues)
: undefined,
tableName,
};
2024-10-18 04:49:04 +00:00
2025-01-01 08:01:39 +00:00
const queryString = serializeQuery({ ...queryObject });
2024-10-18 04:49:04 +00:00
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} */
2025-01-10 19:10:28 +00:00
const requestObject: https.RequestOptions = {
2024-07-13 10:53:35 +00:00
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,
2025-01-01 08:01:39 +00:00
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 {
2025-01-10 19:10:28 +00:00
resolve(JSON.parse(str) as GetReturn);
} catch (/** @type {any} */ error: any) {
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);
2025-01-10 19:10:28 +00:00
resolve(null);
2023-09-21 14:00:04 +00:00
});
}
)
.end();
});
2025-01-10 19:10:28 +00:00
return httpResponse as GetReturn;
2023-09-21 14:00:04 +00:00
}