openapi: minor improvements

pull/3851/head
Viljami Kuosmanen 2020-03-29 19:35:25 +02:00 committed by muxator
parent 172da1483e
commit c2cca39c7d
1 changed files with 2 additions and 28 deletions

View File

@ -185,37 +185,11 @@ exports.handle = async function(apiVersion, functionName, fields, req, res)
fields["padName"] = await padManager.sanitizePadId(fields["padName"]);
}
// no need to await - callAPI returns a promise
return callAPI(apiVersion, functionName, fields, req, res);
}
// calls the api function
async function callAPI(apiVersion, functionName, fields, req, res)
{
// put the function parameters in an array
var functionParams = version[apiVersion][functionName].map(function (field) {
return fields[field]
});
try {
// call the api function
let data = await api[functionName].apply(this, functionParams);
if (!data) {
data = null;
}
res.send({code: 0, message: "ok", data: data});
} catch (err) {
if (err.name == "apierror") {
// parameters were wrong and the api stopped execution, pass the error
res.send({code: 1, message: err.message, data: null});
} else {
// an unknown error happened
res.send({code: 2, message: "internal error", data: null});
throw err;
}
}
// call the api function
return api[functionName].apply(this, functionParams);
}