2020-04-04 20:20:41 +02:00
|
|
|
module.exports.promisify = (method, options = null) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const callback = (err, result) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
return resolve(result);
|
|
|
|
};
|
|
|
|
if (options) {
|
|
|
|
method(options, callback);
|
|
|
|
} else {
|
|
|
|
method(callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.asyncRouter = (app) => {
|
2020-04-06 20:41:13 +02:00
|
|
|
const debug = require("debug")("router");
|
|
|
|
|
|
|
|
let wrapper = (debugMsg, fn) => async (req, res, next) => {
|
2020-04-04 20:20:41 +02:00
|
|
|
try {
|
2020-04-06 20:41:13 +02:00
|
|
|
debug(debugMsg);
|
2020-04-04 20:20:41 +02:00
|
|
|
await fn(req, res);
|
|
|
|
} catch (e) {
|
|
|
|
next(e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
get: (path, fn) => {
|
2020-04-06 20:41:13 +02:00
|
|
|
app.get(path, wrapper(`GET ${path}`, fn));
|
2020-04-04 20:20:41 +02:00
|
|
|
},
|
|
|
|
post: (path, fn) => {
|
2020-04-06 20:41:13 +02:00
|
|
|
debug(`POST ${path}`);
|
|
|
|
app.post(path, wrapper(`POST ${path}`, fn));
|
2020-04-04 20:20:41 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|