forked from mirrors/amnezia-wg-easy
wip
This commit is contained in:
parent
eaf3d5c3fb
commit
d7bb645470
25 changed files with 4632 additions and 2 deletions
46
src/lib/Util.js
Normal file
46
src/lib/Util.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
'use strict';
|
||||
|
||||
module.exports = class Util {
|
||||
|
||||
static requireSession(req, res, next) {
|
||||
if (req.session && req.session.authenticated) {
|
||||
return next();
|
||||
}
|
||||
|
||||
return res.status(401).json({
|
||||
error: 'Not Logged In',
|
||||
});
|
||||
}
|
||||
|
||||
static promisify(fn) {
|
||||
return function(req, res) {
|
||||
Promise.resolve().then(async () => fn(req, res))
|
||||
.then(result => {
|
||||
if (res.headersSent) return;
|
||||
|
||||
if (typeof result === 'undefined') {
|
||||
return res
|
||||
.status(204)
|
||||
.end();
|
||||
}
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json(result);
|
||||
})
|
||||
.catch(error => {
|
||||
if (typeof error === 'string') {
|
||||
error = new Error(error);
|
||||
}
|
||||
|
||||
return res
|
||||
.status(error.statusCode || 500)
|
||||
.json({
|
||||
error: error.message || error.toString(),
|
||||
stack: error.stack,
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue