This commit is contained in:
Benjamin Toby
2025-08-13 11:16:28 +01:00
parent a1e56bb1b0
commit 700a704abd
24 changed files with 400 additions and 260 deletions
+13
View File
@@ -0,0 +1,13 @@
import { APIConnectionOptions, APIResponseObject, DSQL_DatabaseSchemaType, DSQL_FieldSchemaType, DSQL_TableSchemaType } from "../../types";
import { GrabHostNamesReturn } from "../../utils/grab-host-names";
type Params = {
dbName: string;
tableName?: string;
fieldName?: string;
apiKey?: string;
apiConnectionConfig?: APIConnectionOptions;
grabbedHostnames?: GrabHostNamesReturn;
useDefault?: boolean;
};
export default function <T extends DSQL_DatabaseSchemaType | DSQL_TableSchemaType | DSQL_FieldSchemaType = DSQL_DatabaseSchemaType>({ dbName, tableName, apiKey, useDefault, fieldName, apiConnectionConfig, grabbedHostnames, }: Params): Promise<APIResponseObject<T>>;
export {};
+33
View File
@@ -0,0 +1,33 @@
"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 = default_1;
const path_1 = __importDefault(require("path"));
const query_dsql_api_1 = __importDefault(require("../../functions/api/query-dsql-api"));
const grab_api_base_path_1 = __importDefault(require("../../utils/grab-api-base-path"));
function default_1(_a) {
return __awaiter(this, arguments, void 0, function* ({ dbName, tableName, apiKey, useDefault, fieldName, apiConnectionConfig, grabbedHostnames, }) {
const basePath = (0, grab_api_base_path_1.default)({ paradigm: "schema" });
const finalPath = path_1.default.join(basePath, dbName, tableName || "", fieldName || "");
const GET_RES = yield (0, query_dsql_api_1.default)({
method: "GET",
path: finalPath,
apiKey,
useDefault,
apiConnectionConfig,
grabbedHostnames,
});
return GET_RES;
});
}
+5
View File
@@ -0,0 +1,5 @@
import get from "./get";
declare const schema: {
get: typeof get;
};
export default schema;
+10
View File
@@ -0,0 +1,10 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const get_1 = __importDefault(require("./get"));
const schema = {
get: get_1.default,
};
exports.default = schema;
+1
View File
@@ -9,4 +9,5 @@ export declare const AppNames: {
readonly ReverseProxyForwardURLHeaderName: "x-original-uri";
readonly PrivateAPIAuthHeaderName: "x-api-auth-key";
readonly StaticProxyForwardURLHeaderName: "x-media-path";
readonly SchemaToTypeDefConfigFileName: "dsql-schema-to-typedef.json";
};
+1
View File
@@ -12,4 +12,5 @@ exports.AppNames = {
ReverseProxyForwardURLHeaderName: "x-original-uri",
PrivateAPIAuthHeaderName: "x-api-auth-key",
StaticProxyForwardURLHeaderName: "x-media-path",
SchemaToTypeDefConfigFileName: "dsql-schema-to-typedef.json",
};
+5 -2
View File
@@ -1,4 +1,5 @@
import { APIResponseObject, DataCrudRequestMethods, DataCrudRequestMethodsLowerCase } from "../../types";
import { APIConnectionOptions, APIResponseObject, DataCrudRequestMethods, DataCrudRequestMethodsLowerCase } from "../../types";
import { GrabHostNamesReturn } from "../../utils/grab-host-names";
type Param<T = {
[k: string]: any;
}> = {
@@ -8,6 +9,8 @@ type Param<T = {
path: string;
method?: (typeof DataCrudRequestMethods)[number] | (typeof DataCrudRequestMethodsLowerCase)[number];
apiKey?: string;
apiConnectionConfig?: APIConnectionOptions;
grabbedHostnames?: GrabHostNamesReturn;
};
/**
* # Query DSQL API
@@ -16,5 +19,5 @@ export default function queryDSQLAPI<T = {
[k: string]: any;
}, P = {
[k: string]: any;
}>({ body, query, useDefault, path: passedPath, method, apiKey, }: Param<T>): Promise<APIResponseObject<P>>;
}>({ body, query, useDefault, path: passedPath, method, apiKey, apiConnectionConfig, grabbedHostnames, }: Param<T>): Promise<APIResponseObject<P>>;
export {};
+10 -8
View File
@@ -21,9 +21,16 @@ const lodash_1 = __importDefault(require("lodash"));
* # Query DSQL API
*/
function queryDSQLAPI(_a) {
return __awaiter(this, arguments, void 0, function* ({ body, query, useDefault, path: passedPath, method, apiKey, }) {
const grabedHostNames = (0, grab_host_names_1.default)({ useDefault });
return __awaiter(this, arguments, void 0, function* ({ body, query, useDefault, path: passedPath, method, apiKey, apiConnectionConfig, grabbedHostnames, }) {
const grabedHostNames = grabbedHostnames || (0, grab_host_names_1.default)({ useDefault, apiConnectionConfig });
const { host, port, scheme } = grabedHostNames;
const finalAPIKey = apiKey ||
(apiConnectionConfig === null || apiConnectionConfig === void 0 ? void 0 : apiConnectionConfig.apiKey) ||
(!method || method == "GET" || method == "get"
? process.env.DSQL_READ_ONLY_API_KEY
: undefined) ||
process.env.DSQL_FULL_ACCESS_API_KEY ||
process.env.DSQL_API_KEY;
try {
/**
* Make https request
@@ -34,12 +41,7 @@ function queryDSQLAPI(_a) {
const reqPayload = body ? JSON.stringify(body) : undefined;
let headers = {
"Content-Type": "application/json",
Authorization: apiKey ||
(!method || method == "GET" || method == "get"
? process.env.DSQL_READ_ONLY_API_KEY
: undefined) ||
process.env.DSQL_FULL_ACCESS_API_KEY ||
process.env.DSQL_API_KEY,
Authorization: finalAPIKey,
};
if (reqPayload) {
headers["Content-Length"] = Buffer.from(reqPayload).length;
+3 -14
View File
@@ -4,31 +4,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = dbSchemaToType;
const fs_1 = __importDefault(require("fs"));
const grab_dir_names_1 = __importDefault(require("../../utils/backend/names/grab-dir-names"));
const lodash_1 = __importDefault(require("lodash"));
const ejson_1 = __importDefault(require("../../utils/ejson"));
const generate_type_definitions_1 = __importDefault(require("./generate-type-definitions"));
const app_names_1 = require("../../dict/app-names");
const defaultFields_json_1 = __importDefault(require("../../data/defaultFields.json"));
function dbSchemaToType(params) {
var _a, _b;
let datasquirelSchema;
const { mainShemaJSONFilePath, defaultTableFieldsJSONFilePath } = (0, grab_dir_names_1.default)();
if (params === null || params === void 0 ? void 0 : params.dbSchema) {
datasquirelSchema = params.dbSchema;
}
else {
const mainSchema = ejson_1.default.parse(fs_1.default.readFileSync(mainShemaJSONFilePath, "utf-8"));
datasquirelSchema = mainSchema.find((sch) => sch.dbFullName == "datasquirel");
}
let datasquirelSchema = params === null || params === void 0 ? void 0 : params.dbSchema;
if (!datasquirelSchema)
return;
let tableNames = `export const DsqlTables = [\n${datasquirelSchema.tables
.map((tbl) => ` "${tbl.tableName}",`)
.join("\n")}\n] as const`;
const defaultFields = ejson_1.default.parse(fs_1.default.readFileSync(defaultTableFieldsJSONFilePath, "utf-8"));
const dbTablesSchemas = datasquirelSchema.tables.map((tblSchm) => {
let newDefaultFields = lodash_1.default.cloneDeep(defaultFields);
let newDefaultFields = lodash_1.default.cloneDeep(defaultFields_json_1.default);
return Object.assign(Object.assign({}, tblSchm), { fields: tblSchm.fields.find((fld) => fld.fieldName == "id")
? tblSchm.fields
: [
+9
View File
@@ -2108,4 +2108,13 @@ export type DsqlConnectionParam = {
*/
config?: ConnectionConfig;
};
export type APIConnectionOptions = {
scheme?: "http" | "https";
apiKey?: string;
host?: string;
localhostPort?: number;
remoteHost?: string;
remoteHostPort?: number;
isLocalhost?: boolean;
};
export {};
+4 -1
View File
@@ -1,10 +1,12 @@
import https from "https";
import http from "http";
type GrabHostNamesReturn = {
import { APIConnectionOptions } from "../types";
export type GrabHostNamesReturn = {
host: string;
port: number | string;
scheme: typeof http | typeof https;
user_id: string | number;
apiKey?: string;
};
type Param = {
userId?: string | number;
@@ -14,6 +16,7 @@ type Param = {
remoteHost?: string;
remoteHostPort?: string;
useDefault?: boolean;
apiConnectionConfig?: APIConnectionOptions;
};
/**
* # Grab Names For Query
+21 -10
View File
@@ -11,26 +11,37 @@ const http_1 = __importDefault(require("http"));
* # Grab Names For Query
*/
function grabHostNames(param) {
var _a, _b;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const finalEnv = (param === null || param === void 0 ? void 0 : param.env)
? Object.assign(Object.assign({}, process.env), param.env) : process.env;
const scheme = finalEnv["DSQL_HTTP_SCHEME"];
const localHost = finalEnv["DSQL_LOCAL_HOST"];
const localHostPort = finalEnv["DSQL_LOCAL_HOST_PORT"];
const scheme = ((_a = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _a === void 0 ? void 0 : _a.scheme) || finalEnv["DSQL_HTTP_SCHEME"];
const localHost = ((_b = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _b === void 0 ? void 0 : _b.isLocalhost)
? "localhost"
: finalEnv["DSQL_LOCAL_HOST"];
const localHostPort = ((_c = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _c === void 0 ? void 0 : _c.localhostPort) ||
finalEnv["DSQL_LOCAL_HOST_PORT"];
const remoteHost = (param === null || param === void 0 ? void 0 : param.useDefault)
? undefined
: ((_a = finalEnv["DSQL_API_REMOTE_HOST"]) === null || _a === void 0 ? void 0 : _a.match(/.*\..*/))
? finalEnv["DSQL_API_REMOTE_HOST"]
: undefined;
: ((_d = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _d === void 0 ? void 0 : _d.remoteHost)
? (_e = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _e === void 0 ? void 0 : _e.remoteHost
: ((_f = finalEnv["DSQL_API_REMOTE_HOST"]) === null || _f === void 0 ? void 0 : _f.match(/.*\..*/))
? finalEnv["DSQL_API_REMOTE_HOST"]
: undefined;
const remoteHostPort = (param === null || param === void 0 ? void 0 : param.useDefault)
? undefined
: ((_b = finalEnv["DSQL_API_REMOTE_HOST_PORT"]) === null || _b === void 0 ? void 0 : _b.match(/./))
? finalEnv["DSQL_API_REMOTE_HOST_PORT"]
: undefined;
: ((_g = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _g === void 0 ? void 0 : _g.remoteHostPort)
? (_h = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _h === void 0 ? void 0 : _h.remoteHostPort
: ((_j = finalEnv["DSQL_API_REMOTE_HOST_PORT"]) === null || _j === void 0 ? void 0 : _j.match(/./))
? finalEnv["DSQL_API_REMOTE_HOST_PORT"]
: undefined;
return {
host: remoteHost || localHost || "www.datasquirel.com",
port: remoteHostPort || localHostPort || 443,
scheme: (scheme === null || scheme === void 0 ? void 0 : scheme.match(/^http$/i)) ? http_1.default : https_1.default,
user_id: (param === null || param === void 0 ? void 0 : param.userId) || String(finalEnv["DSQL_API_USER_ID"] || 0),
apiKey: ((_k = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _k === void 0 ? void 0 : _k.apiKey) ||
process.env.DSQL_FULL_ACCESS_API_KEY ||
process.env.DSQL_API_KEY ||
process.env.DSQL_READ_ONLY_API_KEY,
};
}