-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.ts
141 lines (120 loc) · 3.21 KB
/
helper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import env from "./env";
import { v4 as uuidv4 } from 'uuid';
export const isWindows = env.MACHINE === "windows";
export const getQuery = (req) => {
return JSON.parse(JSON.stringify(req.query));
};
// generate crypto safe random string
export function randomToken() {
return uuidv4().replace(/-/g, '')
}
export interface commonResponseType {
msg: string;
code: number;
status: "exited" | "success" | "failed" | "unauthorized" | "overflow";
error?: Error;
extra?: any;
data?: any;
fix?: any;
}
const modeRunMaker = (mode: string) => {
const inner = (callback: any) => {
if (env.MODE === mode) {
// check if callback is a function
if (typeof callback === "function") {
return callback();
} else {
return callback;
}
}
return "";
};
return inner;
};
export const runDev = modeRunMaker("dev");
export const runProd = modeRunMaker("prod");
export const withMode = (object) => {
if (env.MODE === "prod" || env.MODE === "PROD") {
return object.prod;
} else {
return object.dev;
}
};
export const withModeDB = (object) => {
if (env.DB_MODE === "prod" || env.DB_MODE === "PROD") {
return object.prod;
} else {
return object.dev;
}
}
// export const withModeObject = (
// mode: "dev" | "prod",
// choice: "db" | "redis"
// ) => {
// switch (choice) {
// case "redis":
// return withMode(mode, env.REDIS_CONFIG);
// case "db":
// return withMode(mode, env.DB_URL);
// }
// };
export function cres(object: commonResponseType) {
return object;
}
export const fakeNumString = (length = 5) => {
const inner = (length) => {
let temp = "";
for (let x = 0; x <= length; x++) {
temp += Math.floor(Math.random() * 10);
}
return temp;
};
return inner(length);
};
function stringContains(str, text) {
return str.indexOf(text) > -1;
}
// const is = {
// arr: (a) => Array.isArray(a),
// obj: (a) => stringContains(Object.prototype.toString.call(a), "Object"),
// pth: (a) => is.obj(a) && a.hasOwnProperty("totalLength"),
// str: (a) => typeof a === "string",
// fnc: (a) => typeof a === "function",
// und: (a) => typeof a === "undefined",
// nil: (a) => is.und(a) || a === null,
// hex: (a) => /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(a),
// };
export const isEmpty = (arg) => {
try {
if (arg == null) {
return true;
} else if (typeof arg === "undefined") {
return true;
} else if (arg.length === 0) {
return true;
} else if (typeof arg === "object" && Object.keys(arg).length === 0) {
return true;
}
return false;
} catch (error) {
return false;
}
};
export const showError = (Error: Error) => {
return {
exists: true,
error: {
message: Error.message,
stack: runDev(Error.stack),
},
};
};
export const showAJVErrors = (errors: any) => {
return errors?.map((err) => {
return {
key: err.instancePath.split("/")[1],
message: err.message,
params: err.params,
};
});
};