-
Notifications
You must be signed in to change notification settings - Fork 1
/
Request.js
65 lines (52 loc) · 1.45 KB
/
Request.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
/**
* ## req
* convert obj structure of original express request
* ```js
* {
* session // extracted from rf-acl
* token // extracted from rf-acl
* user // extracted from rf-acl
* rights // extracted from rf-acl
* data // data from client
* originalRequest // express req
* }
* ```
*/
var log = require('rf-log');
module.exports = function (req) {
var self = this;
// Original Request
self.originalRequest = req;
// Set session if available
self.session = req._session || null;
// Set token if available
self.token = req._token || null;
// Is the presented token valid
self.tokenValid = req._tokenValid || false;
// internal request (interal token + localhost)?
self.isInternal = req._isInternal;
// Set decoded if available
self.decoded = req._decoded || null;
self.user = null;
self.rights = null;
if (self.session) {
self.user = self.session.user;
self.rights = self.session.rights;
}
self.data = {};
// Decode data depending on the request method
if (req.method === 'POST') {
self.data = req.body.data || {};
} else if (req.method === 'GET') {
// Decode data to get a data body
var decoded = {};
try {
if (req.query.data) {
decoded = JSON.parse(req.query.data);
}
} catch (e) {
log.error(e);
}
self.data = decoded;
}
};