datasquirel/utils/get-schema.js

104 lines
3.6 KiB
JavaScript
Raw Normal View History

2023-09-21 14:00:04 +00:00
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
2023-09-21 16:51:08 +00:00
const http = require("http");
2023-09-21 14:00:04 +00:00
const https = require("https");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* @typedef {Object} GetSchemaReturn
* @property {boolean} success - Did the function run successfully?
2024-10-19 16:45:42 +00:00
* @property {import("../package-shared/types").DSQL_DatabaseSchemaType | null} payload - Response payload
2023-09-21 14:00:04 +00:00
*/
/**
2024-10-22 17:17:59 +00:00
* # Get Schema for Database, table, or field *
* @param {import("../package-shared/types").GetSchemaAPIParam} params
2023-09-21 14:00:04 +00:00
*
* @returns { Promise<GetSchemaReturn> } - Return Object
*/
2024-10-22 17:17:59 +00:00
async function getSchema({ key, database, field, table }) {
2023-09-21 16:51:08 +00:00
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
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-22 17:17:59 +00:00
/** @type {import("../package-shared/types").GetSchemaRequestQuery} */
const queryObject = { database, field, table };
let query = Object.keys(queryObject)
// @ts-ignore
.filter((k) => queryObject[k])
// @ts-ignore
.map((k) => `${k}=${queryObject[k]}`)
.join("&");
2024-10-22 17:26:38 +00:00
console.log(queryObject);
console.log(query);
2023-09-21 16:51:08 +00:00
(scheme?.match(/^http$/i) ? http : https)
2023-09-21 14:00:04 +00:00
.request(
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: key,
},
2023-09-21 16:51:08 +00:00
port: localHostPort || 443,
hostname: localHost || "datasquirel.com",
path:
"/api/query/get-schema" +
2024-10-22 17:17:59 +00:00
(query?.match(/./) ? `?${query}` : ""),
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 () {
resolve(JSON.parse(str));
});
response.on("error", (err) => {
reject(err);
});
}
)
.end();
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
module.exports = getSchema;