-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtilities.js
58 lines (52 loc) · 1.49 KB
/
Utilities.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
const apiResponse = (res, status, message = '', data) => {
var new_message = ''
if(process.env.APP_DEBUG === "true"){
new_message = message.message || message
}else{
if(message.message){
new_message = 'somthing went wroung please try again later'
}else{
new_message = message
}
}
res.status(status).send({
data : data || [],
status : status,
message : new_message
});
}
module.exports.view = (res, file_name, title, message = '', data) => {
res.render(file_name, {
data : data || [] ,
title : title || '',
message : message.message || message
});
}
module.exports.send404 = (req, res) => {
try {
apiResponse(res, 404, '404 API Not Found')
} catch (error) {
apiResponse(res, 500, 'Internal Server Error')
}
}
module.exports.send405 = (req, res, next) => {
try {
apiResponse(res, 405, `${req.method} Method not allowed`)
} catch (err) {
apiResponse(res, 500, err)
}
}
module.exports.apiKeyValidate = (req, res, next) => {
try {
let apiKey = req.headers.apikey
if(!apiKey){
return apiResponse(res, 401, 'No API Key Provided', [])
}else if(apiKey !== process.env.API_KEY){
return apiResponse(res, 401, 'Invalid API Key', [])
}
next()
} catch (err) {
apiResponse(res, 500, err)
}
}
module.exports.apiResponse = apiResponse