mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-12-18 20:25:45 +01:00
Commonalize Api-server response handling
- Fixed issue with error headers - Aggregated response handling
This commit is contained in:
parent
4a3ee95b63
commit
5386f8e027
@ -18,7 +18,7 @@ async function initialize(win) {
|
|||||||
body += chunk.toString(); // convert Buffer to string
|
body += chunk.toString(); // convert Buffer to string
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on("end", () => {
|
req.on("end", async () => {
|
||||||
const providedToken = req.headers?.authorization?.replace('Bearer ', '') ?? '';
|
const providedToken = req.headers?.authorization?.replace('Bearer ', '') ?? '';
|
||||||
const isValid = providedToken === getAuthenticationToken();
|
const isValid = providedToken === getAuthenticationToken();
|
||||||
if (isValid) {
|
if (isValid) {
|
||||||
@ -48,17 +48,11 @@ async function initialize(win) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let result;
|
||||||
switch(req.method) {
|
switch(req.method) {
|
||||||
// Request files
|
// Request files
|
||||||
case "GET":
|
case "GET":
|
||||||
window.webContents.executeJavaScript(`document.getFiles("${data.filename}", "${data.code}")`).then((result) => {
|
result = await window.webContents.executeJavaScript(`document.getFiles("${data.filename}", "${data.code}")`);
|
||||||
res.end(JSON.stringify({
|
|
||||||
success: result.res,
|
|
||||||
msg: result.msg,
|
|
||||||
data: result.data
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Create or update files
|
// Create or update files
|
||||||
@ -72,36 +66,30 @@ async function initialize(win) {
|
|||||||
success: false,
|
success: false,
|
||||||
msg: 'Invalid script update request - No data'
|
msg: 'Invalid script update request - No data'
|
||||||
}));
|
}));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
window.webContents.executeJavaScript(`document.saveFile("${data.filename}", "${data.code}")`).then((result) => {
|
result = await window.webContents.executeJavaScript(`document.saveFile("${data.filename}", "${data.code}")`);
|
||||||
res.write(result);
|
|
||||||
|
|
||||||
if (!result.res) {
|
|
||||||
//We've encountered an error
|
|
||||||
res.writeHead(JSON.stringify({
|
|
||||||
success: result.res,
|
|
||||||
msg: result.msg,
|
|
||||||
data: result.data
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
res.end();
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Delete files
|
// Delete files
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
window.webContents.executeJavaScript(`document.deleteFiles("${data.filename}", "${data.code}")`).then((result) => {
|
result = await window.webContents.executeJavaScript(`document.deleteFiles("${data.filename}", "${data.code}")`);
|
||||||
res.end(JSON.stringify({
|
|
||||||
success: result.res,
|
|
||||||
msg: result.msg,
|
|
||||||
data: result.data
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!result.res) {
|
||||||
|
//We've encountered an error
|
||||||
|
res.writeHead(400);
|
||||||
|
log.warn(`Api Server Error`, result.msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.end(JSON.stringify({
|
||||||
|
success: result.res,
|
||||||
|
msg: result.msg,
|
||||||
|
data: result.data
|
||||||
|
}));
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user