Refactor Code to typescript
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
export = DB_HANDLER;
|
||||
/**
|
||||
* DSQL user read-only DB handler
|
||||
* @param {object} params
|
||||
* @param {string} params.paradigm
|
||||
* @param {string} params.database
|
||||
* @param {string} params.queryString
|
||||
* @param {string[]} [params.queryValues]
|
||||
*/ declare function DB_HANDLER(...args: any[]): Promise<any>;
|
||||
+6
-15
@@ -1,7 +1,5 @@
|
||||
// @ts-check
|
||||
|
||||
const mysql = require("serverless-mysql");
|
||||
const grabDbSSL = require("../grabDbSSL");
|
||||
import mysql from "serverless-mysql";
|
||||
import grabDbSSL from "../grabDbSSL";
|
||||
|
||||
const MASTER = mysql({
|
||||
config: {
|
||||
@@ -18,14 +16,9 @@ const MASTER = mysql({
|
||||
});
|
||||
|
||||
/**
|
||||
* DSQL user read-only DB handler
|
||||
* @param {object} params
|
||||
* @param {string} params.paradigm
|
||||
* @param {string} params.database
|
||||
* @param {string} params.queryString
|
||||
* @param {string[]} [params.queryValues]
|
||||
*/ // @ts-ignore
|
||||
async function DB_HANDLER(...args) {
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
export default async function DB_HANDLER(...args: any[]) {
|
||||
try {
|
||||
const results = await MASTER.query(...args);
|
||||
|
||||
@@ -33,7 +26,7 @@ async function DB_HANDLER(...args) {
|
||||
await MASTER.end();
|
||||
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (error: any) {
|
||||
console.log("DB Error =>", error);
|
||||
return {
|
||||
success: false,
|
||||
@@ -41,5 +34,3 @@ async function DB_HANDLER(...args) {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DB_HANDLER;
|
||||
@@ -1,18 +0,0 @@
|
||||
export = DSQL_USER_DB_HANDLER;
|
||||
/**
|
||||
* DSQL user read-only DB handler
|
||||
* @param {object} params
|
||||
* @param {"Full Access" | "FA" | "Read Only"} params.paradigm
|
||||
* @param {string} params.database
|
||||
* @param {string} params.queryString
|
||||
* @param {string[]} [params.queryValues]
|
||||
*/
|
||||
declare function DSQL_USER_DB_HANDLER({ paradigm, database, queryString, queryValues, }: {
|
||||
paradigm: "Full Access" | "FA" | "Read Only";
|
||||
database: string;
|
||||
queryString: string;
|
||||
queryValues?: string[];
|
||||
}): Promise<any> | {
|
||||
success: boolean;
|
||||
error: any;
|
||||
};
|
||||
+18
-16
@@ -1,10 +1,10 @@
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
const mysql = require("serverless-mysql");
|
||||
const grabDbSSL = require("../grabDbSSL");
|
||||
import mysql from "serverless-mysql";
|
||||
import grabDbSSL from "../grabDbSSL";
|
||||
|
||||
let DSQL_USER = mysql({
|
||||
config: {
|
||||
@@ -16,20 +16,22 @@ let DSQL_USER = mysql({
|
||||
},
|
||||
});
|
||||
|
||||
type Param = {
|
||||
paradigm: "Full Access" | "FA" | "Read Only";
|
||||
database: string;
|
||||
queryString: string;
|
||||
queryValues?: string[];
|
||||
};
|
||||
|
||||
/**
|
||||
* DSQL user read-only DB handler
|
||||
* @param {object} params
|
||||
* @param {"Full Access" | "FA" | "Read Only"} params.paradigm
|
||||
* @param {string} params.database
|
||||
* @param {string} params.queryString
|
||||
* @param {string[]} [params.queryValues]
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
function DSQL_USER_DB_HANDLER({
|
||||
export default function DSQL_USER_DB_HANDLER({
|
||||
paradigm,
|
||||
database,
|
||||
queryString,
|
||||
queryValues,
|
||||
}) {
|
||||
}: Param) {
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fullAccess = paradigm?.match(/full.access|^fa$/i)
|
||||
@@ -63,7 +65,7 @@ function DSQL_USER_DB_HANDLER({
|
||||
* ### Run query Function
|
||||
* @param {any} results
|
||||
*/
|
||||
function runQuery(results) {
|
||||
function runQuery(results: any) {
|
||||
DSQL_USER.end();
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
}
|
||||
@@ -72,7 +74,7 @@ function DSQL_USER_DB_HANDLER({
|
||||
* ### Query Error
|
||||
* @param {any} err
|
||||
*/
|
||||
function queryError(err) {
|
||||
function queryError(err: any) {
|
||||
DSQL_USER.end();
|
||||
resolve({
|
||||
error: err.message,
|
||||
@@ -97,7 +99,7 @@ function DSQL_USER_DB_HANDLER({
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
////////////////////////////////////////
|
||||
|
||||
fs.appendFileSync(
|
||||
@@ -111,7 +113,7 @@ function DSQL_USER_DB_HANDLER({
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
@@ -1,10 +0,0 @@
|
||||
export = LOCAL_DB_HANDLER;
|
||||
/**
|
||||
* DSQL user read-only DB handler
|
||||
* @param {object} params
|
||||
* @param {string} params.paradigm
|
||||
* @param {string} params.database
|
||||
* @param {string} params.queryString
|
||||
* @param {string[]} [params.queryValues]
|
||||
*/
|
||||
declare function LOCAL_DB_HANDLER(...args: any[]): Promise<any>;
|
||||
+7
-16
@@ -1,17 +1,10 @@
|
||||
// @ts-check
|
||||
|
||||
const mysql = require("serverless-mysql");
|
||||
const grabDbSSL = require("../grabDbSSL");
|
||||
import mysql from "serverless-mysql";
|
||||
import grabDbSSL from "../grabDbSSL";
|
||||
|
||||
/**
|
||||
* DSQL user read-only DB handler
|
||||
* @param {object} params
|
||||
* @param {string} params.paradigm
|
||||
* @param {string} params.database
|
||||
* @param {string} params.queryString
|
||||
* @param {string[]} [params.queryValues]
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
async function LOCAL_DB_HANDLER(/** @type {any[]} */ ...args) {
|
||||
export default async function LOCAL_DB_HANDLER(...args: any[]) {
|
||||
const MASTER = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
@@ -27,10 +20,10 @@ async function LOCAL_DB_HANDLER(/** @type {any[]} */ ...args) {
|
||||
onConnect: () => {
|
||||
console.log("Connection Successful!");
|
||||
},
|
||||
onConnectError: (/** @type {any} */ err) => {
|
||||
onConnectError: (/** @type {any} */ err: any) => {
|
||||
console.log("Connection Error", err.message);
|
||||
},
|
||||
onError: (/** @type {any} */ err) => {
|
||||
onError: (/** @type {any} */ err: any) => {
|
||||
console.log("Client Error", err.message);
|
||||
},
|
||||
});
|
||||
@@ -42,7 +35,7 @@ async function LOCAL_DB_HANDLER(/** @type {any[]} */ ...args) {
|
||||
await MASTER.end();
|
||||
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
console.log("DB Error =>", error.message);
|
||||
return {
|
||||
success: false,
|
||||
@@ -50,5 +43,3 @@ async function LOCAL_DB_HANDLER(/** @type {any[]} */ ...args) {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = LOCAL_DB_HANDLER;
|
||||
@@ -1,12 +0,0 @@
|
||||
export = NO_DB_HANDLER;
|
||||
/**
|
||||
* DSQL user read-only DB handler
|
||||
* @param {object} params
|
||||
* @param {string} params.paradigm
|
||||
* @param {string} params.database
|
||||
* @param {string} params.queryString
|
||||
* @param {string[]} [params.queryValues]
|
||||
*/ declare function NO_DB_HANDLER(...args: any[]): Promise<any> | {
|
||||
success: boolean;
|
||||
error: any;
|
||||
};
|
||||
+6
-15
@@ -1,7 +1,5 @@
|
||||
// @ts-check
|
||||
|
||||
const mysql = require("serverless-mysql");
|
||||
const grabDbSSL = require("../grabDbSSL");
|
||||
import mysql from "serverless-mysql";
|
||||
import grabDbSSL from "../grabDbSSL";
|
||||
|
||||
let NO_DB = mysql({
|
||||
config: {
|
||||
@@ -14,14 +12,9 @@ let NO_DB = mysql({
|
||||
});
|
||||
|
||||
/**
|
||||
* DSQL user read-only DB handler
|
||||
* @param {object} params
|
||||
* @param {string} params.paradigm
|
||||
* @param {string} params.database
|
||||
* @param {string} params.queryString
|
||||
* @param {string[]} [params.queryValues]
|
||||
*/ // @ts-ignore
|
||||
function ROOT_DB_HANDLER(...args) {
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
export default function NO_DB_HANDLER(...args: any[]) {
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
NO_DB.query(...args)
|
||||
@@ -37,12 +30,10 @@ function ROOT_DB_HANDLER(...args) {
|
||||
});
|
||||
});
|
||||
});
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ROOT_DB_HANDLER;
|
||||
+6
-13
@@ -1,7 +1,7 @@
|
||||
// @ts-check
|
||||
|
||||
const mysql = require("serverless-mysql");
|
||||
const grabDbSSL = require("../grabDbSSL");
|
||||
import mysql from "serverless-mysql";
|
||||
import grabDbSSL from "../grabDbSSL";
|
||||
|
||||
let NO_DB = mysql({
|
||||
config: {
|
||||
@@ -14,14 +14,9 @@ let NO_DB = mysql({
|
||||
});
|
||||
|
||||
/**
|
||||
* DSQL user read-only DB handler
|
||||
* @param {object} params
|
||||
* @param {string} params.paradigm
|
||||
* @param {string} params.database
|
||||
* @param {string} params.queryString
|
||||
* @param {string[]} [params.queryValues]
|
||||
*/ // @ts-ignore
|
||||
function NO_DB_HANDLER(...args) {
|
||||
* # Root DB handler
|
||||
*/
|
||||
export default function ROOT_DB_HANDLER(...args: any[]) {
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
NO_DB.query(...args)
|
||||
@@ -37,12 +32,10 @@ function NO_DB_HANDLER(...args) {
|
||||
});
|
||||
});
|
||||
});
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = NO_DB_HANDLER;
|
||||
-4
@@ -1,4 +0,0 @@
|
||||
declare function _exports(): string | (import("tls").SecureContextOptions & {
|
||||
rejectUnauthorized?: boolean | undefined;
|
||||
}) | undefined;
|
||||
export = _exports;
|
||||
+10
-5
@@ -1,11 +1,16 @@
|
||||
// @ts-check
|
||||
import fs from "fs";
|
||||
|
||||
const fs = require("fs");
|
||||
type Return =
|
||||
| string
|
||||
| (import("tls").SecureContextOptions & {
|
||||
rejectUnauthorized?: boolean | undefined;
|
||||
})
|
||||
| undefined;
|
||||
|
||||
/**
|
||||
* @returns {string | (import("tls").SecureContextOptions & { rejectUnauthorized?: boolean | undefined;}) | undefined}
|
||||
* # Grall SSL
|
||||
*/
|
||||
module.exports = function grabDbSSL() {
|
||||
export default function grabDbSSL(): Return {
|
||||
const SSL_DIR = process.env.DSQL_SSL_DIR;
|
||||
if (!SSL_DIR?.match(/./)) {
|
||||
return undefined;
|
||||
@@ -21,4 +26,4 @@ module.exports = function grabDbSSL() {
|
||||
return {
|
||||
ca: fs.readFileSync(`${SSL_DIR}/ca-cert.pem`),
|
||||
};
|
||||
};
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
declare function _exports({ request, cookieString }: {
|
||||
request?: http.IncomingMessage & {
|
||||
[x: string]: any;
|
||||
};
|
||||
cookieString?: string;
|
||||
}): {
|
||||
[x: string]: string;
|
||||
};
|
||||
export = _exports;
|
||||
import http = require("http");
|
||||
+12
-18
@@ -1,6 +1,4 @@
|
||||
// @ts-check
|
||||
|
||||
const http = require("http");
|
||||
import { IncomingMessage } from "http";
|
||||
|
||||
/**
|
||||
* Parse request cookies
|
||||
@@ -8,16 +6,14 @@ const http = require("http");
|
||||
*
|
||||
* @description This function takes in a request object and
|
||||
* returns the cookies as a JS object
|
||||
*
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - main params object
|
||||
* @param {http.IncomingMessage & Object<string, any>} [params.request] - HTTPS request object
|
||||
* @param {string} [params.cookieString]
|
||||
*
|
||||
* @returns {Object<string, string>}
|
||||
*/
|
||||
module.exports = function parseCookies({ request, cookieString }) {
|
||||
export default function parseCookies({
|
||||
request,
|
||||
cookieString,
|
||||
}: {
|
||||
request?: IncomingMessage & { [s: string]: any };
|
||||
cookieString?: string;
|
||||
}): { [s: string]: string } {
|
||||
try {
|
||||
/** @type {string | undefined} */
|
||||
const cookieStr = request
|
||||
@@ -32,11 +28,9 @@ module.exports = function parseCookies({ request, cookieString }) {
|
||||
return {};
|
||||
}
|
||||
|
||||
/** @type {string[]} */
|
||||
const cookieSplitArray = cookieStr.split(";");
|
||||
const cookieSplitArray: string[] = cookieStr.split(";");
|
||||
|
||||
/** @type {Object<string, string>} */
|
||||
let cookieObject = {};
|
||||
let cookieObject: { [k: string]: string } = {};
|
||||
|
||||
cookieSplitArray.forEach((keyValueString) => {
|
||||
const [key, value] = keyValueString.split("=");
|
||||
@@ -50,9 +44,9 @@ module.exports = function parseCookies({ request, cookieString }) {
|
||||
});
|
||||
|
||||
return cookieObject;
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (error: any) {
|
||||
console.log(`ERROR parsing cookies: ${error.message}`);
|
||||
|
||||
return {};
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user