mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-21 07:56:16 -04:00
APIHandler.js: use promises
This commit is contained in:
parent
ec5baa2ab3
commit
c4f1f83747
1 changed files with 22 additions and 28 deletions
|
@ -150,7 +150,7 @@ exports.version = version;
|
||||||
* @req express request object
|
* @req express request object
|
||||||
* @res express response object
|
* @res express response object
|
||||||
*/
|
*/
|
||||||
exports.handle = function(apiVersion, functionName, fields, req, res)
|
exports.handle = async function(apiVersion, functionName, fields, req, res)
|
||||||
{
|
{
|
||||||
//check if this is a valid apiversion
|
//check if this is a valid apiversion
|
||||||
var isKnownApiVersion = false;
|
var isKnownApiVersion = false;
|
||||||
|
@ -194,41 +194,38 @@ exports.handle = function(apiVersion, functionName, fields, req, res)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// sanitize any padIDs before continuing
|
try {
|
||||||
if (fields["padID"]) {
|
// sanitize any padIDs before continuing
|
||||||
padManager.sanitizePadId(fields["padID"], function(padId) {
|
if (fields["padID"]) {
|
||||||
fields["padID"] = padId;
|
fields["padID"] = await padManager.sanitizePadId(fields["padID"]);
|
||||||
callAPI(apiVersion, functionName, fields, req, res);
|
} else if (fields["padName"]) {
|
||||||
});
|
fields["padName"] = await padManager.sanitizePadId(fields["padName"]);
|
||||||
} else if (fields["padName"]) {
|
}
|
||||||
padManager.sanitizePadId(fields["padName"], function(padId) {
|
await callAPI(apiVersion, functionName, fields, req, res);
|
||||||
fields["padName"] = padId;
|
} catch (e) {
|
||||||
callAPI(apiVersion, functionName, fields, req, res);
|
ERR(e);
|
||||||
});
|
|
||||||
} else {
|
|
||||||
callAPI(apiVersion, functionName, fields, req, res);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// calls the api function
|
// calls the api function
|
||||||
function callAPI(apiVersion, functionName, fields, req, res)
|
async function callAPI(apiVersion, functionName, fields, req, res)
|
||||||
{
|
{
|
||||||
// put the function parameters in an array
|
// put the function parameters in an array
|
||||||
var functionParams = version[apiVersion][functionName].map(function (field) {
|
var functionParams = version[apiVersion][functionName].map(function (field) {
|
||||||
return fields[field]
|
return fields[field]
|
||||||
});
|
});
|
||||||
|
|
||||||
// add a callback function to handle the response
|
try {
|
||||||
functionParams.push(function(err, data) {
|
// call the api function
|
||||||
if (err == null) {
|
let data = await api[functionName].apply(this, functionParams);
|
||||||
// no error happened, everything is fine
|
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
data = null;
|
data = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.send({code: 0, message: "ok", data: data});
|
res.send({code: 0, message: "ok", data: data});
|
||||||
} else if (err.name == "apierror") {
|
} catch (err) {
|
||||||
|
if (err.name == "apierror") {
|
||||||
// parameters were wrong and the api stopped execution, pass the error
|
// parameters were wrong and the api stopped execution, pass the error
|
||||||
|
|
||||||
res.send({code: 1, message: err.message, data: null});
|
res.send({code: 1, message: err.message, data: null});
|
||||||
|
@ -236,10 +233,7 @@ function callAPI(apiVersion, functionName, fields, req, res)
|
||||||
// an unknown error happened
|
// an unknown error happened
|
||||||
|
|
||||||
res.send({code: 2, message: "internal error", data: null});
|
res.send({code: 2, message: "internal error", data: null});
|
||||||
ERR(err);
|
throw err;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
// call the api function
|
|
||||||
api[functionName].apply(this, functionParams);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue