Refactor Code to typescript
This commit is contained in:
-13
@@ -1,13 +0,0 @@
|
||||
export = decrypt;
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {string} param0.encryptedString
|
||||
* @param {string} [param0.encryptionKey]
|
||||
* @param {string} [param0.encryptionSalt]
|
||||
* @returns
|
||||
*/
|
||||
declare function decrypt({ encryptedString, encryptionKey, encryptionSalt }: {
|
||||
encryptedString: string;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
}): string;
|
||||
+17
-13
@@ -1,16 +1,22 @@
|
||||
// @ts-check
|
||||
|
||||
const { scryptSync, createDecipheriv } = require("crypto");
|
||||
const { Buffer } = require("buffer");
|
||||
import { scryptSync, createDecipheriv } from "crypto";
|
||||
import { Buffer } from "buffer";
|
||||
|
||||
type Param = {
|
||||
encryptedString: string;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {string} param0.encryptedString
|
||||
* @param {string} [param0.encryptionKey]
|
||||
* @param {string} [param0.encryptionSalt]
|
||||
* @returns
|
||||
* # Decrypt Function
|
||||
*/
|
||||
const decrypt = ({ encryptedString, encryptionKey, encryptionSalt }) => {
|
||||
export default function decrypt({
|
||||
encryptedString,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
}: Param) {
|
||||
if (!encryptedString?.match(/./)) {
|
||||
console.log("Encrypted string is invalid");
|
||||
return encryptedString;
|
||||
@@ -38,17 +44,15 @@ const decrypt = ({ encryptedString, encryptionKey, encryptionSalt }) => {
|
||||
|
||||
let key = scryptSync(finalEncryptionKey, finalEncryptionSalt, finalKeyLen);
|
||||
let iv = Buffer.alloc(16, 0);
|
||||
// @ts-ignore
|
||||
|
||||
const decipher = createDecipheriv(algorithm, key, iv);
|
||||
|
||||
try {
|
||||
let decrypted = decipher.update(encryptedString, "hex", "utf8");
|
||||
decrypted += decipher.final("utf8");
|
||||
return decrypted;
|
||||
} catch (/** @type {*} */ error) {
|
||||
} catch (error: any) {
|
||||
console.log("Error in decrypting =>", error.message);
|
||||
return encryptedString;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = decrypt;
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
export = encrypt;
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {string} param0.data
|
||||
* @param {string} [param0.encryptionKey]
|
||||
* @param {string} [param0.encryptionSalt]
|
||||
* @returns {string | null}
|
||||
*/
|
||||
declare function encrypt({ data, encryptionKey, encryptionSalt }: {
|
||||
data: string;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
}): string | null;
|
||||
+16
-11
@@ -1,17 +1,22 @@
|
||||
// @ts-check
|
||||
|
||||
const { scryptSync, createCipheriv } = require("crypto");
|
||||
const { Buffer } = require("buffer");
|
||||
import { scryptSync, createCipheriv } from "crypto";
|
||||
import { Buffer } from "buffer";
|
||||
|
||||
type Param = {
|
||||
data: string;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {string} param0.data
|
||||
* @param {string} [param0.encryptionKey]
|
||||
* @param {string} [param0.encryptionSalt]
|
||||
* @returns {string | null}
|
||||
* # Encrypt String
|
||||
*/
|
||||
const encrypt = ({ data, encryptionKey, encryptionSalt }) => {
|
||||
export default function encrypt({
|
||||
data,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
}: Param): string | null {
|
||||
if (!data?.match(/./)) {
|
||||
console.log("Encryption string is invalid");
|
||||
return data;
|
||||
@@ -46,10 +51,10 @@ const encrypt = ({ data, encryptionKey, encryptionSalt }) => {
|
||||
let encrypted = cipher.update(data, "utf8", "hex");
|
||||
encrypted += cipher.final("hex");
|
||||
return encrypted;
|
||||
} catch (/** @type {*} */ error) {
|
||||
} catch (/** @type {*} */ error: any) {
|
||||
console.log("Error in encrypting =>", error.message);
|
||||
return data;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = encrypt;
|
||||
@@ -1,5 +0,0 @@
|
||||
declare function _exports({ password, encryptionKey }: {
|
||||
password: string;
|
||||
encryptionKey?: string;
|
||||
}): string;
|
||||
export = _exports;
|
||||
+10
-8
@@ -1,15 +1,17 @@
|
||||
// @ts-check
|
||||
import { createHmac } from "crypto";
|
||||
|
||||
const { createHmac } = require("crypto");
|
||||
type Param = {
|
||||
password: string;
|
||||
encryptionKey?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Hash password Function
|
||||
* @param {object} param0
|
||||
* @param {string} param0.password - Password to hash
|
||||
* @param {string} [param0.encryptionKey] - Encryption key
|
||||
* @returns {string}
|
||||
*/
|
||||
module.exports = function hashPassword({ password, encryptionKey }) {
|
||||
export default function hashPassword({
|
||||
password,
|
||||
encryptionKey,
|
||||
}: Param): string {
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
|
||||
@@ -21,4 +23,4 @@ module.exports = function hashPassword({ password, encryptionKey }) {
|
||||
hmac.update(password);
|
||||
let hashed = hmac.digest("base64");
|
||||
return hashed;
|
||||
};
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
export = sqlDeleteGenerator;
|
||||
/**
|
||||
* @typedef {object} SQLDeleteGenReturn
|
||||
* @property {string} query
|
||||
* @property {string[]} values
|
||||
*/
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {any} param0.data
|
||||
* @param {string} param0.tableName
|
||||
*
|
||||
* @return {SQLDeleteGenReturn | undefined}
|
||||
*/
|
||||
declare function sqlDeleteGenerator({ tableName, data }: {
|
||||
data: any;
|
||||
tableName: string;
|
||||
}): SQLDeleteGenReturn | undefined;
|
||||
declare namespace sqlDeleteGenerator {
|
||||
export { SQLDeleteGenReturn };
|
||||
}
|
||||
type SQLDeleteGenReturn = {
|
||||
query: string;
|
||||
values: string[];
|
||||
};
|
||||
@@ -1,41 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* @typedef {object} SQLDeleteGenReturn
|
||||
* @property {string} query
|
||||
* @property {string[]} values
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {any} param0.data
|
||||
* @param {string} param0.tableName
|
||||
*
|
||||
* @return {SQLDeleteGenReturn | undefined}
|
||||
*/
|
||||
function sqlDeleteGenerator({ tableName, data }) {
|
||||
try {
|
||||
let queryStr = `DELETE FROM ${tableName}`;
|
||||
|
||||
/** @type {string[]} */
|
||||
let deleteBatch = [];
|
||||
/** @type {string[]} */
|
||||
let queryArr = [];
|
||||
|
||||
Object.keys(data).forEach((ky) => {
|
||||
deleteBatch.push(`${ky}=?`);
|
||||
queryArr.push(data[ky]);
|
||||
});
|
||||
queryStr += ` WHERE ${deleteBatch.join(" AND ")}`;
|
||||
|
||||
return {
|
||||
query: queryStr,
|
||||
values: queryArr,
|
||||
};
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`SQL delete gen ERROR: ${error.message}`);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = sqlDeleteGenerator;
|
||||
@@ -0,0 +1,36 @@
|
||||
interface SQLDeleteGenReturn {
|
||||
query: string;
|
||||
values: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* # SQL Delete Generator
|
||||
*/
|
||||
export default function sqlDeleteGenerator({
|
||||
tableName,
|
||||
data,
|
||||
}: {
|
||||
data: any;
|
||||
tableName: string;
|
||||
}): SQLDeleteGenReturn | undefined {
|
||||
try {
|
||||
let queryStr = `DELETE FROM ${tableName}`;
|
||||
|
||||
let deleteBatch: string[] = [];
|
||||
let queryArr: string[] = [];
|
||||
|
||||
Object.keys(data).forEach((ky) => {
|
||||
deleteBatch.push(`${ky}=?`);
|
||||
queryArr.push(data[ky]);
|
||||
});
|
||||
queryStr += ` WHERE ${deleteBatch.join(" AND ")}`;
|
||||
|
||||
return {
|
||||
query: queryStr,
|
||||
values: queryArr,
|
||||
};
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
console.log(`SQL delete gen ERROR: ${error.message}`);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
export = sqlGenerator;
|
||||
declare function sqlGenerator(Param0: {
|
||||
genObject?: import("../../../types").ServerQueryParam;
|
||||
tableName: string;
|
||||
}): {
|
||||
string: string;
|
||||
values: string[];
|
||||
} | undefined;
|
||||
+36
-22
@@ -1,28 +1,46 @@
|
||||
// @ts-check
|
||||
import {
|
||||
ServerQueryParam,
|
||||
ServerQueryParamsJoin,
|
||||
ServerQueryQueryObject,
|
||||
} from "../../../types";
|
||||
|
||||
type Param = {
|
||||
genObject?: ServerQueryParam;
|
||||
tableName: string;
|
||||
};
|
||||
|
||||
type Return =
|
||||
| {
|
||||
string: string;
|
||||
values: string[];
|
||||
}
|
||||
| undefined;
|
||||
|
||||
/**
|
||||
* # SQL Query Generator
|
||||
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
|
||||
* @type {import("../../../types").SqlGeneratorFn}
|
||||
*/
|
||||
function sqlGenerator({ tableName, genObject }) {
|
||||
export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
if (!genObject) return undefined;
|
||||
|
||||
const finalQuery = genObject.query ? genObject.query : undefined;
|
||||
|
||||
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
|
||||
|
||||
/** @type {string[]} */
|
||||
const sqlSearhValues = [];
|
||||
const sqlSearhValues: string[] = [];
|
||||
|
||||
/**
|
||||
* # Generate Query
|
||||
* @param {object} param
|
||||
* @param {import("../../../types").ServerQueryQueryObject[string]} param.queryObj
|
||||
* @param {import("../../../types").ServerQueryParamsJoin[]} [param.join]
|
||||
* @param {string} [param.field]
|
||||
*/
|
||||
function genSqlSrchStr({ queryObj, join, field }) {
|
||||
function genSqlSrchStr({
|
||||
queryObj,
|
||||
join,
|
||||
field,
|
||||
}: {
|
||||
queryObj: ServerQueryQueryObject[string];
|
||||
join?: ServerQueryParamsJoin[];
|
||||
field?: string;
|
||||
}) {
|
||||
const finalFieldName = (() => {
|
||||
if (queryObj?.tableName) {
|
||||
return `${queryObj.tableName}.${field}`;
|
||||
@@ -51,7 +69,7 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
}
|
||||
} else if (Array.isArray(queryObj.value)) {
|
||||
/** @type {string[]} */
|
||||
const strArray = [];
|
||||
const strArray: string[] = [];
|
||||
queryObj.value.forEach((val) => {
|
||||
const valueParsed = val;
|
||||
if (queryObj.equality == "LIKE") {
|
||||
@@ -75,16 +93,14 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
|
||||
const sqlSearhString = queryKeys?.map((field) => {
|
||||
const queryObj =
|
||||
/** @type {import("../../../types").ServerQueryQueryObject} */ (
|
||||
finalQuery?.[field]
|
||||
);
|
||||
/** @type {import("../../../types").ServerQueryQueryObject} */ finalQuery?.[
|
||||
field
|
||||
];
|
||||
if (!queryObj) return;
|
||||
|
||||
if (queryObj.__query) {
|
||||
const subQueryGroup =
|
||||
/** @type {import("../../../types").ServerQueryQueryObject}} */ (
|
||||
queryObj.__query
|
||||
);
|
||||
/** @type {import("../../../types").ServerQueryQueryObject}} */ queryObj.__query;
|
||||
|
||||
const subSearchKeys = Object.keys(subQueryGroup);
|
||||
const subSearchString = subSearchKeys.map((_field) => {
|
||||
@@ -109,8 +125,8 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
});
|
||||
|
||||
function generateJoinStr(
|
||||
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch,
|
||||
/** @type {import("../../../types").ServerQueryParamsJoin} */ join
|
||||
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch: import("../../../types").ServerQueryParamsJoinMatchObject,
|
||||
/** @type {import("../../../types").ServerQueryParamsJoin} */ join: import("../../../types").ServerQueryParamsJoin
|
||||
) {
|
||||
return `${
|
||||
typeof mtch.source == "object" ? mtch.source.tableName : tableName
|
||||
@@ -165,7 +181,7 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
|
||||
if (genObject.join) {
|
||||
/** @type {string[]} */
|
||||
const existingJoinTableNames = [tableName];
|
||||
const existingJoinTableNames: string[] = [tableName];
|
||||
|
||||
str +=
|
||||
"," +
|
||||
@@ -263,5 +279,3 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
values: sqlSearhValues,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = sqlGenerator;
|
||||
@@ -1,24 +0,0 @@
|
||||
export = sqlInsertGenerator;
|
||||
/**
|
||||
* @typedef {object} SQLInsertGenReturn
|
||||
* @property {string} query
|
||||
* @property {string[]} values
|
||||
*/
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {any[]} param0.data
|
||||
* @param {string} param0.tableName
|
||||
*
|
||||
* @return {SQLInsertGenReturn | undefined}
|
||||
*/
|
||||
declare function sqlInsertGenerator({ tableName, data }: {
|
||||
data: any[];
|
||||
tableName: string;
|
||||
}): SQLInsertGenReturn | undefined;
|
||||
declare namespace sqlInsertGenerator {
|
||||
export { SQLInsertGenReturn };
|
||||
}
|
||||
type SQLInsertGenReturn = {
|
||||
query: string;
|
||||
values: string[];
|
||||
};
|
||||
+16
-17
@@ -1,23 +1,24 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* @typedef {object} SQLInsertGenReturn
|
||||
* @property {string} query
|
||||
* @property {string[]} values
|
||||
*/
|
||||
interface SQLInsertGenReturn {
|
||||
query: string;
|
||||
values: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {any[]} param0.data
|
||||
* @param {string} param0.tableName
|
||||
*
|
||||
* @return {SQLInsertGenReturn | undefined}
|
||||
* # SQL Insert Generator
|
||||
*/
|
||||
function sqlInsertGenerator({ tableName, data }) {
|
||||
export default function sqlInsertGenerator({
|
||||
tableName,
|
||||
data,
|
||||
}: {
|
||||
data: any[];
|
||||
tableName: string;
|
||||
}): SQLInsertGenReturn | undefined {
|
||||
try {
|
||||
if (Array.isArray(data) && data?.[0]) {
|
||||
/** @type {string[]} */
|
||||
let insertKeys = [];
|
||||
let insertKeys: string[] = [];
|
||||
|
||||
data.forEach((dt) => {
|
||||
const kys = Object.keys(dt);
|
||||
@@ -29,9 +30,9 @@ function sqlInsertGenerator({ tableName, data }) {
|
||||
});
|
||||
|
||||
/** @type {string[]} */
|
||||
let queryBatches = [];
|
||||
let queryBatches: string[] = [];
|
||||
/** @type {string[]} */
|
||||
let queryValues = [];
|
||||
let queryValues: string[] = [];
|
||||
|
||||
data.forEach((item) => {
|
||||
queryBatches.push(
|
||||
@@ -58,10 +59,8 @@ function sqlInsertGenerator({ tableName, data }) {
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
console.log(`SQL insert gen ERROR: ${error.message}`);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = sqlInsertGenerator;
|
||||
Reference in New Issue
Block a user