-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.cjs
84 lines (71 loc) · 2.48 KB
/
index.cjs
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
'use strict';
/**
* @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'
];
/**
* @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}
*/
const isJsonReq = (req) => {
const contentType = req && req.headers && req.headers['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}
*/
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}
*/
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}
*/
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}
*/
const isDeleteReq = (req) => checkMethod(req, 'delete');
exports.isDeleteReq = isDeleteReq;
exports.isGetReq = isGetReq;
exports.isJsonReq = isJsonReq;
exports.isPostReq = isPostReq;
exports.isPutReq = isPutReq;