Updates
This commit is contained in:
@@ -1,27 +1,19 @@
|
||||
import https from "node:https";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import grabHostNames from "../utils/grab-host-names";
|
||||
import apiGet from "../functions/api/query/get";
|
||||
import serializeQuery from "../utils/serialize-query";
|
||||
import {
|
||||
ApiGetQueryObject,
|
||||
DSQL_DatabaseSchemaType,
|
||||
GetReqQueryObject,
|
||||
GetReturn,
|
||||
} from "../types";
|
||||
import apiGetGrabQueryAndValues from "../utils/grab-query-and-values";
|
||||
import { APIResponseObject, DsqlCrudQueryObject } from "../types";
|
||||
import debugLog from "../utils/logging/debug-log";
|
||||
import dsqlCrud from "../utils/data-fetching/crud";
|
||||
import grabAPIKey from "../utils/grab-api-key";
|
||||
|
||||
type Param<T extends { [k: string]: any } = { [k: string]: any }> = {
|
||||
key?: string;
|
||||
db?: string;
|
||||
query: string | ApiGetQueryObject<T>;
|
||||
queryValues?: string[];
|
||||
tableName?: string;
|
||||
user_id?: string | number;
|
||||
database: string;
|
||||
query: DsqlCrudQueryObject<T>;
|
||||
table?: string;
|
||||
debug?: boolean;
|
||||
forceLocal?: boolean;
|
||||
useLocal?: boolean;
|
||||
apiVersion?: string;
|
||||
};
|
||||
|
||||
export type ApiGetParams = Param;
|
||||
@@ -34,14 +26,13 @@ export default async function get<
|
||||
R extends any = any
|
||||
>({
|
||||
key,
|
||||
db,
|
||||
database,
|
||||
query,
|
||||
queryValues,
|
||||
tableName,
|
||||
user_id,
|
||||
table,
|
||||
debug,
|
||||
forceLocal,
|
||||
}: Param<T>): Promise<GetReturn<R>> {
|
||||
useLocal,
|
||||
apiVersion = "v1",
|
||||
}: Param<T>): Promise<APIResponseObject<R>> {
|
||||
const grabedHostNames = grabHostNames();
|
||||
const { host, port, scheme } = grabedHostNames;
|
||||
|
||||
@@ -56,30 +47,15 @@ export default async function get<
|
||||
*/
|
||||
const { DSQL_DB_NAME } = process.env;
|
||||
|
||||
if (DSQL_DB_NAME?.match(/./) && global.DSQL_USE_LOCAL) {
|
||||
let dbSchema: DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
try {
|
||||
const localDbSchemaPath = path.resolve(
|
||||
process.cwd(),
|
||||
"dsql.schema.json"
|
||||
);
|
||||
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
|
||||
} catch (error) {}
|
||||
|
||||
if (debug) {
|
||||
debugFn("Running Locally ...");
|
||||
}
|
||||
|
||||
return await apiGet({
|
||||
dbFullName: DSQL_DB_NAME,
|
||||
if (useLocal) {
|
||||
const result = await dsqlCrud({
|
||||
action: "get",
|
||||
table: table || "",
|
||||
dbFullName: database,
|
||||
query,
|
||||
queryValues,
|
||||
tableName,
|
||||
dbSchema,
|
||||
debug,
|
||||
forceLocal,
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,32 +64,13 @@ export default async function get<
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
const httpResponse = await new Promise((resolve, reject) => {
|
||||
const queryAndValues = apiGetGrabQueryAndValues({
|
||||
query,
|
||||
values: queryValues,
|
||||
});
|
||||
|
||||
const queryObject: GetReqQueryObject = {
|
||||
db: process.env.DSQL_API_DB_NAME || String(db),
|
||||
query: queryAndValues.query,
|
||||
queryValues: queryAndValues.valuesString,
|
||||
tableName,
|
||||
debug,
|
||||
};
|
||||
|
||||
if (debug) {
|
||||
debugFn(queryObject, "queryObject");
|
||||
}
|
||||
|
||||
const queryString = serializeQuery({ ...queryObject });
|
||||
const queryString = serializeQuery(query);
|
||||
|
||||
if (debug) {
|
||||
debugFn(queryString, "queryString");
|
||||
}
|
||||
|
||||
let path = `/api/query/${
|
||||
user_id || grabedHostNames.user_id
|
||||
}/get${queryString}`;
|
||||
let path = `/api/${apiVersion}/crud/${database}/${table}`;
|
||||
|
||||
if (debug) {
|
||||
debugFn(path, "path");
|
||||
@@ -123,11 +80,7 @@ export default async function get<
|
||||
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,
|
||||
Authorization: grabAPIKey(key),
|
||||
},
|
||||
port,
|
||||
hostname: host,
|
||||
@@ -152,8 +105,8 @@ export default async function get<
|
||||
|
||||
response.on("end", function () {
|
||||
try {
|
||||
resolve(JSON.parse(str) as GetReturn);
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
resolve(JSON.parse(str) as APIResponseObject);
|
||||
} catch (error: any) {
|
||||
reject({
|
||||
error: error.message,
|
||||
result: str,
|
||||
@@ -170,5 +123,5 @@ export default async function get<
|
||||
.end();
|
||||
});
|
||||
|
||||
return httpResponse as GetReturn;
|
||||
return httpResponse as APIResponseObject<R>;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ type Param = {
|
||||
queryValues?: any[];
|
||||
tableName?: string;
|
||||
user_id?: boolean;
|
||||
forceLocal?: boolean;
|
||||
useLocal?: boolean;
|
||||
debug?: boolean;
|
||||
};
|
||||
|
||||
@@ -28,7 +28,7 @@ export default async function post({
|
||||
database,
|
||||
tableName,
|
||||
user_id,
|
||||
forceLocal,
|
||||
useLocal,
|
||||
debug,
|
||||
}: Param): Promise<PostReturn> {
|
||||
const grabedHostNames = grabHostNames();
|
||||
@@ -50,24 +50,7 @@ export default async function post({
|
||||
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
|
||||
process.env;
|
||||
|
||||
if (
|
||||
DSQL_DB_HOST?.match(/./) &&
|
||||
DSQL_DB_USERNAME?.match(/./) &&
|
||||
DSQL_DB_PASSWORD?.match(/./) &&
|
||||
DSQL_DB_NAME?.match(/./) &&
|
||||
global.DSQL_USE_LOCAL
|
||||
) {
|
||||
/** @type {import("../types").DSQL_DatabaseSchemaType | undefined} */
|
||||
let dbSchema: import("../types").DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
try {
|
||||
const localDbSchemaPath = path.resolve(
|
||||
process.cwd(),
|
||||
"dsql.schema.json"
|
||||
);
|
||||
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
|
||||
} catch (error) {}
|
||||
|
||||
if (useLocal) {
|
||||
if (debug) {
|
||||
debugLog({
|
||||
log: "Using Local DB ...",
|
||||
@@ -76,12 +59,10 @@ export default async function post({
|
||||
}
|
||||
|
||||
return await apiPost({
|
||||
dbFullName: database || DSQL_DB_NAME,
|
||||
dbFullName: database || "",
|
||||
query,
|
||||
dbSchema,
|
||||
queryValues,
|
||||
tableName,
|
||||
forceLocal,
|
||||
debug,
|
||||
});
|
||||
}
|
||||
@@ -101,10 +82,10 @@ export default async function post({
|
||||
|
||||
try {
|
||||
JSON.parse(reqPayloadString);
|
||||
} catch (error) {
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Error Parsing HTTP response for post action`,
|
||||
error as Error
|
||||
} catch (error: any) {
|
||||
console.log(
|
||||
"Error Parsing HTTP response for post action: ",
|
||||
error.message
|
||||
);
|
||||
|
||||
return {
|
||||
|
||||
@@ -89,7 +89,6 @@ export default async function uploadImage({
|
||||
return httpResponse as Return;
|
||||
} catch (error: any) {
|
||||
console.log("Error in uploading file: ", error.message);
|
||||
global.ERROR_CALLBACK?.(`Error Uploading File`, error as Error);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
|
||||
@@ -93,7 +93,6 @@ export default async function uploadImage({
|
||||
return httpResponse as DSQLUploadImageFunctionReturn;
|
||||
} catch (error: any) {
|
||||
console.log("Error in uploading image: ", error.message);
|
||||
global.ERROR_CALLBACK?.(`Error Uploading Image`, error as Error);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
|
||||
@@ -6,12 +6,11 @@ import { AddUserFunctionReturn, UserDataPayload } from "../../types";
|
||||
|
||||
type Param = {
|
||||
key?: string;
|
||||
database?: string;
|
||||
database: string;
|
||||
payload: UserDataPayload;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
user_id?: string | number;
|
||||
apiUserId?: string | number;
|
||||
useLocal?: boolean;
|
||||
verify?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -22,48 +21,18 @@ export default async function addUser({
|
||||
payload,
|
||||
database,
|
||||
encryptionKey,
|
||||
user_id,
|
||||
apiUserId,
|
||||
useLocal,
|
||||
verify,
|
||||
}: Param): Promise<AddUserFunctionReturn> {
|
||||
/**
|
||||
* 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_HOST,
|
||||
DSQL_DB_USERNAME,
|
||||
DSQL_DB_PASSWORD,
|
||||
DSQL_DB_NAME,
|
||||
DSQL_API_USER_ID,
|
||||
} = process.env;
|
||||
|
||||
const grabedHostNames = grabHostNames();
|
||||
const { host, port, scheme } = grabedHostNames;
|
||||
|
||||
if (
|
||||
DSQL_DB_HOST?.match(/./) &&
|
||||
DSQL_DB_USERNAME?.match(/./) &&
|
||||
DSQL_DB_PASSWORD?.match(/./) &&
|
||||
DSQL_DB_NAME?.match(/./) &&
|
||||
global.DSQL_USE_LOCAL
|
||||
) {
|
||||
/** @type {import("../../types").DSQL_DatabaseSchemaType | undefined} */
|
||||
let dbSchema: import("../../types").DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
try {
|
||||
const localDbSchemaPath = path.resolve(
|
||||
process.cwd(),
|
||||
"dsql.schema.json"
|
||||
);
|
||||
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
|
||||
} catch (error) {}
|
||||
|
||||
if (useLocal) {
|
||||
return await apiCreateUser({
|
||||
database: DSQL_DB_NAME,
|
||||
database,
|
||||
encryptionKey,
|
||||
payload,
|
||||
userId: apiUserId,
|
||||
verify,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -92,9 +61,7 @@ export default async function addUser({
|
||||
},
|
||||
port,
|
||||
hostname: host,
|
||||
path: `/api/user/${
|
||||
user_id || grabedHostNames.user_id
|
||||
}/add-user`,
|
||||
path: ``,
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import grabHostNames from "../../utils/grab-host-names";
|
||||
import apiDeleteUser from "../../functions/api/users/api-delete-user";
|
||||
import { UpdateUserFunctionReturn } from "../../types";
|
||||
|
||||
type Param = {
|
||||
key?: string;
|
||||
database?: string;
|
||||
database: string;
|
||||
deletedUserId: string | number;
|
||||
user_id?: boolean;
|
||||
useLocal?: boolean;
|
||||
apiVersion?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -17,40 +16,16 @@ type Param = {
|
||||
export default async function deleteUser({
|
||||
key,
|
||||
database,
|
||||
user_id,
|
||||
deletedUserId,
|
||||
useLocal,
|
||||
apiVersion = "v1",
|
||||
}: Param): Promise<UpdateUserFunctionReturn> {
|
||||
/**
|
||||
* 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_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
|
||||
process.env;
|
||||
|
||||
const grabedHostNames = grabHostNames();
|
||||
const { host, port, scheme } = grabedHostNames;
|
||||
|
||||
if (
|
||||
DSQL_DB_HOST?.match(/./) &&
|
||||
DSQL_DB_USERNAME?.match(/./) &&
|
||||
DSQL_DB_PASSWORD?.match(/./) &&
|
||||
DSQL_DB_NAME?.match(/./) &&
|
||||
global.DSQL_USE_LOCAL
|
||||
) {
|
||||
/** @type {import("../../types").DSQL_DatabaseSchemaType | undefined} */
|
||||
let dbSchema: import("../../types").DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
try {
|
||||
const localDbSchemaPath = path.resolve(
|
||||
process.cwd(),
|
||||
"dsql.schema.json"
|
||||
);
|
||||
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
|
||||
} catch (error) {}
|
||||
|
||||
if (useLocal) {
|
||||
return await apiDeleteUser({
|
||||
dbFullName: DSQL_DB_NAME,
|
||||
dbFullName: database,
|
||||
deletedUserId,
|
||||
});
|
||||
}
|
||||
@@ -79,9 +54,7 @@ export default async function deleteUser({
|
||||
},
|
||||
port,
|
||||
hostname: host,
|
||||
path: `/api/user/${
|
||||
user_id || grabedHostNames.user_id
|
||||
}/delete-user`,
|
||||
path: `/api/${apiVersion}/users/${database}/${deletedUserId}`,
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -79,8 +79,8 @@ export default function getToken({
|
||||
* @description Return User Object
|
||||
*/
|
||||
return { key, csrf };
|
||||
} catch (error) {
|
||||
global.ERROR_CALLBACK?.(`Error Getting Token`, error as Error);
|
||||
} catch (error: any) {
|
||||
console.log("Error Getting Token:", error.message);
|
||||
|
||||
/**
|
||||
* Return User Object
|
||||
|
||||
@@ -9,7 +9,8 @@ type Param = {
|
||||
database: string;
|
||||
userId: number;
|
||||
fields?: string[];
|
||||
apiUserId?: boolean;
|
||||
useLocal?: boolean;
|
||||
apiVersion?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -20,7 +21,8 @@ export default async function getUser({
|
||||
userId,
|
||||
database,
|
||||
fields,
|
||||
apiUserId,
|
||||
useLocal,
|
||||
apiVersion = "v1",
|
||||
}: Param): Promise<GetUserFunctionReturn> {
|
||||
/**
|
||||
* Initialize
|
||||
@@ -54,36 +56,11 @@ export default async function getUser({
|
||||
const grabedHostNames = grabHostNames();
|
||||
const { host, port, scheme } = grabedHostNames;
|
||||
|
||||
/**
|
||||
* 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_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
|
||||
process.env;
|
||||
|
||||
if (
|
||||
DSQL_DB_HOST?.match(/./) &&
|
||||
DSQL_DB_USERNAME?.match(/./) &&
|
||||
DSQL_DB_PASSWORD?.match(/./) &&
|
||||
DSQL_DB_NAME?.match(/./) &&
|
||||
global.DSQL_USE_LOCAL
|
||||
) {
|
||||
/** @type {import("../../types").DSQL_DatabaseSchemaType | undefined} */
|
||||
let dbSchema: import("../../types").DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
try {
|
||||
const localDbSchemaPath = path.resolve(
|
||||
process.cwd(),
|
||||
"dsql.schema.json"
|
||||
);
|
||||
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
|
||||
} catch (error) {}
|
||||
|
||||
if (useLocal) {
|
||||
return await apiGetUser({
|
||||
userId,
|
||||
fields: [...new Set(updatedFields)],
|
||||
dbFullName: DSQL_DB_NAME,
|
||||
dbFullName: database,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -106,9 +83,7 @@ export default async function getUser({
|
||||
},
|
||||
port,
|
||||
hostname: host,
|
||||
path: `/api/user/${
|
||||
apiUserId || grabedHostNames.user_id
|
||||
}/get-user`,
|
||||
path: `/api/${apiVersion}/users/${database}/${userId}`,
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -137,9 +112,5 @@ export default async function getUser({
|
||||
httpsRequest.end();
|
||||
});
|
||||
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
|
||||
return httpResponse as GetUserFunctionReturn;
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ export default async function loginUser({
|
||||
*/
|
||||
if (useLocal) {
|
||||
httpResponse = await apiLoginUser({
|
||||
database: database || process.env.DSQL_DB_NAME || "",
|
||||
database,
|
||||
email: payload.email,
|
||||
username: payload.username,
|
||||
password: payload.password,
|
||||
@@ -193,11 +193,8 @@ export default async function loginUser({
|
||||
try {
|
||||
if (token && encryptedPayload)
|
||||
httpResponse["token"] = encryptedPayload;
|
||||
} catch (error) {
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Login User HTTP Response Error`,
|
||||
error as Error
|
||||
);
|
||||
} catch (error: any) {
|
||||
console.log("Login User HTTP Response Error:", error.message);
|
||||
}
|
||||
|
||||
const cookieNames = getAuthCookieNames({
|
||||
|
||||
@@ -117,7 +117,6 @@ export default function logoutUser({
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.log("Logout Error:", error.message);
|
||||
global.ERROR_CALLBACK?.(`Logout User Error`, error as Error);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
|
||||
@@ -186,12 +186,8 @@ export default async function githubAuth({
|
||||
response.on("end", function () {
|
||||
try {
|
||||
resolve(JSON.parse(str));
|
||||
} catch (error) {
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Github Auth Error`,
|
||||
error as Error
|
||||
);
|
||||
|
||||
} catch (error: any) {
|
||||
console.log("Github Auth Error:", error.message);
|
||||
resolve({
|
||||
success: false,
|
||||
user: null,
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import grabHostNames from "../../utils/grab-host-names";
|
||||
import apiUpdateUser from "../../functions/api/users/api-update-user";
|
||||
import { UpdateUserFunctionReturn } from "../../types";
|
||||
|
||||
type Param = {
|
||||
key?: string;
|
||||
database?: string;
|
||||
database: string;
|
||||
updatedUserId: string | number;
|
||||
payload: { [s: string]: any };
|
||||
user_id?: boolean;
|
||||
useLocal?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -21,41 +20,16 @@ export default async function updateUser({
|
||||
database,
|
||||
user_id,
|
||||
updatedUserId,
|
||||
useLocal,
|
||||
}: Param): Promise<UpdateUserFunctionReturn> {
|
||||
/**
|
||||
* 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_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
|
||||
process.env;
|
||||
|
||||
const grabedHostNames = grabHostNames();
|
||||
const { host, port, scheme } = grabedHostNames;
|
||||
|
||||
if (
|
||||
DSQL_DB_HOST?.match(/./) &&
|
||||
DSQL_DB_USERNAME?.match(/./) &&
|
||||
DSQL_DB_PASSWORD?.match(/./) &&
|
||||
DSQL_DB_NAME?.match(/./) &&
|
||||
global.DSQL_USE_LOCAL
|
||||
) {
|
||||
/** @type {import("../../types").DSQL_DatabaseSchemaType | undefined} */
|
||||
let dbSchema: import("../../types").DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
try {
|
||||
const localDbSchemaPath = path.resolve(
|
||||
process.cwd(),
|
||||
"dsql.schema.json"
|
||||
);
|
||||
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
|
||||
} catch (error) {}
|
||||
|
||||
if (useLocal) {
|
||||
return await apiUpdateUser({
|
||||
payload: payload,
|
||||
dbFullName: DSQL_DB_NAME,
|
||||
dbFullName: database,
|
||||
updatedUserId,
|
||||
dbSchema,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -215,8 +215,6 @@ export default function userAuth({
|
||||
payload: userObject,
|
||||
};
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(`User Auth Error`, error as Error);
|
||||
|
||||
/**
|
||||
* Return User Object
|
||||
*
|
||||
|
||||
@@ -44,10 +44,6 @@ export default async function validateTempEmailCode({
|
||||
|
||||
return null;
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Validate Temp Email Code Error`,
|
||||
error as Error
|
||||
);
|
||||
console.log("validateTempEmailCode error:", error.message);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -66,7 +66,6 @@ export default function validateToken({
|
||||
*/
|
||||
return userObject;
|
||||
} catch (error) {
|
||||
global.ERROR_CALLBACK?.(`Validate Token Error`, error as Error);
|
||||
/**
|
||||
* Return User Object
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user