This commit is contained in:
Benjamin Toby
2025-07-09 20:30:45 +01:00
parent ccb5605b58
commit c37d105dee
264 changed files with 2389 additions and 3743 deletions
+9 -42
View File
@@ -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: ``,
},
/**
+8 -35
View File
@@ -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}`,
},
/**
+2 -2
View File
@@ -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
+7 -36
View File
@@ -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;
}
+3 -6
View File
@@ -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,
+5 -31
View File
@@ -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
*