-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (73 loc) · 2.52 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
/**
* @private
* @description Supported JSON content type values ['application/json', 'application/problem+json', 'application/vnd.api+json', 'application/hal+json'].
*/
const jsonContentTypes = [
'application/json',
'application/problem+json',
'application/vnd.api+json',
'application/hal+json'
];
const getReqHeader = (req, name) => {
if (req && req.headers) {
if (req.headers.get) {
return req.headers.get(name);
}
return req.headers[name];
}
};
/**
* @function
* @description Function that check if current request is a json request or not.
* @param {Object} req - Http request object
* @param {Object} req.headers - Http request headers object
* @param {string} req.headers."content-type" - Http request content-type header
* @returns {boolean}
*/
export const isJsonReq = (req) => {
const contentType = getReqHeader(req,'content-type');
const normalizedContentType = contentType && contentType.toLowerCase();
if (normalizedContentType && jsonContentTypes.some((value) => normalizedContentType.includes(value))) {
return true;
}
return false;
};
const checkMethod = (req, method) => {
const normalizedReqMethod = req && req.method && req.method && req.method.toLowerCase();
if (normalizedReqMethod === method) {
return true;
}
return false;
};
/**
* @function
* @description Function that check if current request is a http GET method request or not.
* @param {Object} req - Http request object
* @param {string} req.method - Http request method string
* @returns {boolean}
*/
export const isGetReq = (req) => checkMethod(req, 'get');
/**
* @function
* @description Function that check if current request is a http POST method request or not.
* @param {Object} req - Http request object
* @param {string} req.method - Http request method string
* @returns {boolean}
*/
export const isPostReq = (req) => checkMethod(req, 'post');
/**
* @function
* @description Function that check if current request is a http PUT method request or not.
* @param {Object} req - Http request object
* @param {string} req.method - Http request method string
* @returns {boolean}
*/
export const isPutReq = (req) => checkMethod(req, 'put');
/**
* @function
* @description Function that check if current request is a http DELETE method request or not.
* @param {Object} req - Http request object
* @param {string} req.method - Http request method string
* @returns {boolean}
*/
export const isDeleteReq = (req) => checkMethod(req, 'delete');