53 lines
1.7 KiB
JavaScript
53 lines
1.7 KiB
JavaScript
import grabHostNames from "../utils/grab-host-names";
|
|
/**
|
|
* # Get Schema for Database, table, or field *
|
|
*/
|
|
export default async function getSchema({ key, database, field, table, user_id, env, }) {
|
|
const grabedHostNames = grabHostNames({ env });
|
|
const { host, port, scheme } = grabedHostNames;
|
|
/**
|
|
* Make https request
|
|
*
|
|
* @description make a request to datasquirel.com
|
|
*/
|
|
const httpResponse = await new Promise((resolve, reject) => {
|
|
const queryObject = { database, field, table };
|
|
let query = Object.keys(queryObject)
|
|
.filter((k) => queryObject[k])
|
|
.map((k) => `${k}=${queryObject[k]}`)
|
|
.join("&");
|
|
scheme
|
|
.request({
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: key ||
|
|
process.env.DSQL_FULL_ACCESS_API_KEY ||
|
|
process.env.DSQL_API_KEY,
|
|
},
|
|
port,
|
|
hostname: host,
|
|
path: `/api/query/${user_id || grabedHostNames.user_id}/get-schema` + ((query === null || query === void 0 ? void 0 : query.match(/./)) ? `?${query}` : ""),
|
|
},
|
|
/**
|
|
* 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) => {
|
|
resolve(null);
|
|
});
|
|
})
|
|
.end();
|
|
});
|
|
return httpResponse;
|
|
}
|