-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
488 lines (434 loc) · 12.3 KB
/
index.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
"use strict";
const { AllHtmlEntities } = require("html-entities"),
cryptr = require("cryptr"),
Slugify = require("slugify"),
Axios = require("axios"),
{ readFile, writeFile, mkdir } = require("fs"),
moment = require("moment"),
entities = new AllHtmlEntities(),
multer = require("multer"),
{ access, unlink, F_OK } = require("fs"),
jwt = require("jsonwebtoken"),
{ createHash } = require("crypto"),
ERRORS = require("./error");
const randomString = (N = 10) => {
return Array(N + 1)
.join((Math.random().toString(36) + "00000000000000000").slice(2, 18))
.slice(0, N);
};
const md5 = (plainText = Date.now().toString()) => {
return createHash("md5")
.update(plainText)
.digest("hex");
};
const deleteFile = async (file) => {
if (await fileExists(file)) {
return new Promise((resolve, reject) => {
unlink(file, (err) => {
return err ? reject(err) : resolve(true);
});
});
}
return false;
};
const pickFields = (fields = [], data = {}) => {
let output = {};
const totalCount = fields.length;
for (let i = 0; i < totalCount; i++) {
if (data[fields[i]]) output[fields[i]] = data[fields[i]];
}
return output;
};
const decodeJwt = (cipher, secreteKey = process.env.APP_KEY) => {
const token = cipher.split(" ").pop();
return new Promise((ful, rej) => {
if (!secreteKey) return rej(new Error("Kindly supply secret key"));
jwt.verify(token, secreteKey, (err, data) => {
if (err) rej(err);
return ful(data);
});
});
};
const EncodeJwt = (
data,
secreteKey = process.env.APP_KEY,
duration = "24h"
) => {
return new Promise((ful, rej) => {
if (!secreteKey) return rej(new Error("Kindly supply secret key"));
jwt.sign(data, secreteKey, { expiresIn: duration }, (err, token) => {
if (err) rej(err);
ful(token);
});
});
};
const encodeJwt = ({
data,
secreteKey = process.env.APP_KEY,
duration = "24h",
}) => {
return new Promise((ful, rej) => {
if (!secreteKey) return rej(new Error("Kindly supply secret key"));
jwt.sign(data, secreteKey, { expiresIn: duration }, (err, token) => {
if (err) rej(err);
ful(token);
});
});
};
const fileExists = (file) => {
return new Promise((resolve, reject) => {
access(file, F_OK, (err) => {
if (err) resolve(false);
resolve(true);
});
});
};
const createPath = (path) =>
new Promise((ful, rej) => {
fileExists(path)
.then((exists) => {
if (exists) return ful(true);
mkdir(path, { recursive: true }, (err) => {
if (err) return rej(err);
return ful(true);
});
})
.catch((err) => rej(err));
});
const validate = (
schema,
object,
option = { abortEarly: true, allowUnknown: false }
) => {
const check = schema.validate(object, option);
if (check.error) {
throw new ERRORS.ValidationError(check.error.details[0].message);
}
return check.value;
};
function joiValidator(constraint, isMiddleware = true) {
if (!constraint)
throw new ERRORS.ValidationError(
"Kindly supply validation schema to joiValidator"
);
if (!isMiddleware) {
return validate(constraint.schema, constraint.data, constraint.option);
}
return async (req, res, next) => {
try {
if (constraint.body) {
req.body = validate(
constraint.body.schema,
req.body,
constraint.body.options
);
}
if (constraint.params)
req.params = validate(
constraint.params.schema,
req.params,
constraint.params.options
);
if (constraint.query)
req.query = validate(
constraint.query.schema,
req.query,
constraint.query.options
);
if (constraint.headers)
req.headers = validate(
constraint.headers.schema,
req.headers,
constraint.headers.options
);
return next();
} catch (err) {
next(err);
}
};
}
const removeUpload = async (files) => {
if (Array.isArray(files)) {
files.map(async (image) => await deleteFile(image.path));
} else {
await deleteFile(files.path);
}
};
const uniqueString = (capitalize = false) => {
const now = Array.from(Date.now().toString());
let result = "";
for (let i = 0; i < now.length; i++) {
if (i % 4 === 0) result += randomString(2);
result += now[i];
}
return capitalize ? result.toUpperCase() : result;
};
const shuffelWord = (word) => {
let shuffledWord = "";
word = word.split("");
while (word.length > 0) {
shuffledWord += word.splice((word.length * Math.random()) << 0, 1);
}
return shuffledWord;
};
const errorMessage = (err = void 0, ERROR_TYPE = "FATAL_ERROR") => {
let message;
if (err && err.errors)
message = err.errors[0] ? err.errors[0].message : "Something went wrong.";
else if (err && err.message) message = err.message;
else if (typeof err == "string") message = err;
else message = "Something went wrong";
if (process.env.NODE_ENV !== "production") {
console.log("=======================================");
console.log(err);
console.log("=======================================");
}
const response = { success: false, message };
response.error =
err.name || ERRORS.HTTP_STATUS_CODE_ERROR[err.httpStatusCode] || ERROR_TYPE;
if (err.httpStatusCode) response.httpStatusCode = err.httpStatusCode;
response.service =
err.service || process.env.APP_NAME || process.env.SERVICE_NAME;
if(err.isAxiosError){
response.message = err.response.data.message || "Something went wrong";
response.httpStatusCode = err.response.data.httpStatusCode || err.response.status
response.error = err.response.data.error || ERRORS.HTTP_STATUS_CODE_ERROR[response.httpStatusCode] || ERROR_TYPE
}
return response;
};
const decrypt = (cipherText, secret) => {
let crypto = new cryptr(secret);
crypto = crypto.decrypt(cipherText);
if (!crypto) throw new ERRORS.InvalidTokenError("Supplied token not valid");
let { plainText, expire = void 0 } = JSON.parse(crypto);
if (expire && Date.now() > expire)
throw new ERRORS.TokenExpiredError("Supplied token expired");
return plainText;
};
const rand = (min = 0, max = 10000) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
const uploadFile = ({
name = null,
limit = 5,
allowedFormat = ["jpg", "jpeg", "png", "gif"],
location = "/",
}) => {
/* Set storage to s3 */
const storage = multer.diskStorage({
destination: async (req, file, cb) => {
await createPath(location);
cb(null, location);
},
filename: (req, file, cb) => {
let mimetype = file.mimetype.split("/")[1];
cb(null, name ? name + "." + mimetype : `${uniqueString()}.${mimetype}`);
},
});
/* Limit is converted to bytes from megabyte */
const limits = { fileSize: limit * 1000000 };
/* Restrict file format to allowed ones */
const fileFilter = (req, file, cb) => {
if (allowedFormat.length === 0) return cb(null, true);
if (
allowedFormat.includes(
file.originalname
.split(".")
.pop()
.toLowerCase()
)
) {
return cb(null, true);
} else {
return cb(
`File format not allowed, allowed formats are: ${allowedFormat.join(
", "
)}`
);
}
};
return multer({ storage, limits, fileFilter });
};
function base64ToFile(base64String, path) {
return new Promise((ful, rej) => {
let file = base64String.replace(/^data:image\/\w+;base64,/, "");
let format = file.charAt(0);
if (format === "/") format = "jpg";
else if (format === "i") format = "png";
else if (format === "R") format = "gif";
else if (format === "U") format = "webp";
else if (format === "J") format = "pdf";
else if (format === "U") format = "docx";
createPath(path)
.then(() => {
path = `${path}/${uniqueString()}.${format}`;
writeFile(path, file, "base64", (err) => {
if (err) rej(err);
ful(path);
});
})
.catch((err) => rej(err));
});
}
module.exports = {
success: true,
pickFields,
base64ToFile,
randomString,
uniqueString,
uploadFile,
rand,
decrypt,
errorMessage,
deleteFile,
encodeJwt,
EncodeJwt,
decodeJwt,
fileExists,
removeUpload,
md5,
validate,
joiValidator,
shuffelWord,
urlQueryToString: (query) => {
let queryString = "?";
for (let key in query) queryString += `${key}=${query[key]}&`;
return queryString;
},
InvalidTokenError: ERRORS.InvalidTokenError,
TokenExpiredError: ERRORS.TokenExpiredError,
AuthenticationError: ERRORS.AuthenticationError,
AuthorizationError: ERRORS.AuthorizationError,
EntryExistError: ERRORS.EntryExistError,
EntryNotFoundError: ERRORS.EntryNotFoundError,
NotFoundError: ERRORS.EntryNotFoundError,
ExistsError: ERRORS.EntryExistError,
ValidationError: ERRORS.ValidationError,
PaymentRequiredError: ERRORS.PaymentRequiredError,
slugify: (value, lowerCase = true) => {
if (lowerCase)
return Slugify(value, {
remove: /[*,}{》《`^#+~.()%&'"!:@]/g,
lower: true,
});
return Slugify(value, {
remove: /[*,}{》《`^#+~.()%&'"!:@]/g,
lower: false,
});
},
htmlEncode: (value) => {
return entities.encodeNonUTF(value);
},
htmlDecode: (value) => {
return entities.decode(value);
},
/* Display success message */
successMessage: (message) => {
return { success: true, message };
},
/* Make a get request */
getContent: async ({
url,
method = "GET",
headers = {},
token = undefined,
data = undefined,
}) => {
try {
headers["X-Requested-With"] = "XMLHttpRequest";
token && (headers["Authorization"] = token);
const payload = {
method,
url,
headers,
};
if (data) payload.data = data;
const result = await Axios(payload);
return result.data;
} catch (err) {
throw err.response
? { ...err.response.data, httpStatusCode: err.response.status } ||
err.response
: err;
}
},
/* Make a post request */
postContent: async ({ url, token, data, method = "POST", headers = {} }) => {
try {
headers["X-Requested-With"] = "XMLHttpRequest";
token && (headers["Authorization"] = token);
const result = await Axios({
method,
url,
data,
headers,
});
return result.data;
} catch (err) {
throw err.response
? { ...err.response.data, httpStatusCode: err.response.status } ||
err.response
: err;
}
},
encrypt: (plainText, secret, expire = undefined) => {
const crypto = new cryptr(secret);
/* Convert expire duration to miliseconds */
expire = expire ? Date.now() + expire * 60 * 1000 : expire;
return crypto.encrypt(JSON.stringify({ plainText, expire }));
},
readJson: (path) => {
return new Promise((ful, rej) => {
readFile(path, (err, data) => {
if (err) {
rej(err);
} else {
ful(JSON.parse(data));
}
});
});
},
dateFormat: (date, format = "DD-MM-YYYY") => {
return moment(date).format(format);
},
isFile: async (filePath) => {
return new Promise((resolve, reject) => {
access(filePath, F_OK, (err) => {
return err ? resolve(false) : resolve(true);
});
});
},
notFound: (message) => {
return { success: false, message };
},
appRequestOnly: () => {
return (req, res, next) => {
try {
const APP_KEY = req.headers.app_key;
if (!APP_KEY)
return res
.status(401)
.json(errorMessage("Missing request parameter"));
if (decrypt(APP_KEY, process.env.APP_KEY) !== process.env.APP_KEY)
return res
.status(401)
.json(errorMessage("Unknown request source not allowed"));
return next();
} catch (err) {
return res.status(500).json(errorMessage(err));
}
};
},
paginate: (totalCount, currentPage, perPage) => {
const previousPage = currentPage - 1;
return {
pageCount: Math.ceil(totalCount / perPage),
offset: currentPage > 1 ? previousPage * perPage : 0,
};
},
isAjaxRequest: (xhr, headers = null) => {
if (xhr) return true;
else if (headers && headers.accept.indexOf("json") > -1) return true;
return false;
},
};