-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreqstore.js
More file actions
43 lines (33 loc) · 997 Bytes
/
Copy pathreqstore.js
File metadata and controls
43 lines (33 loc) · 997 Bytes
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
var debug = require('debug')('reqstore');
var blacklist = ['get', 'set', 'remove'];
exports = module.exports = function addStore(options) {
options = options || {};
return function store(req, res, next) {
// Attach request storage
req.store = req.store || {};
// Fetch saved key
req.store.get = function(key) {
return this[key];
};
// Save a key. Same name key overwrites
req.store.set = function(key, val) {
if (!!~blacklist.indexOf(key)) throw new Error('Can\'t set key named \'' + key + '\'');
if ('object' === typeof key) {
for (attr in key) {
if (key.hasOwnProperty(attr)) {
this[attr] = key[attr];
}
}
} else if ('string' === typeof key) {
this[key] = val;
}
};
// Removes value
req.store.remove = function(key) {
if ('string' !== typeof key) return;
delete this[key];
};
debug("Attaching storage to request");
next();
};
};