2023-03-18 22:27:55 +01:00
|
|
|
const { getItemNetworth } = require("skyhelper-networth");
|
2023-03-03 17:48:43 +01:00
|
|
|
const nbt = require("prismarine-nbt");
|
|
|
|
const parseNbt = require("util").promisify(nbt.parse);
|
|
|
|
async function decodeData(buffer) {
|
|
|
|
const parsedNbt = await parseNbt(Buffer.from(buffer, "base64"));
|
|
|
|
return nbt.simplify(parsedNbt);
|
|
|
|
}
|
|
|
|
export default async function handler(req, res) {
|
2023-03-03 23:12:05 +01:00
|
|
|
if (req.method == "POST" || req.method == "GET" || req.method == "PUT") {
|
2023-03-04 18:25:43 +01:00
|
|
|
if (req.body == "") {
|
2023-03-18 22:27:55 +01:00
|
|
|
res.status(400).json({ error: "No body was provided" });
|
2023-03-04 18:25:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(req.body);
|
2023-03-06 16:43:53 +01:00
|
|
|
let dat = req.body;
|
2023-03-04 18:25:43 +01:00
|
|
|
if (typeof dat !== "string" || typeof dat == "undefined") {
|
2023-03-18 22:27:55 +01:00
|
|
|
res.status(400).json({ error: "ByteData is not a string or undefined" });
|
2023-03-04 18:25:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const data = await getItemNetworth((await decodeData(dat)).i[0], {
|
|
|
|
cache: true,
|
|
|
|
});
|
|
|
|
res.status(200).json(data);
|
|
|
|
} catch {
|
2023-03-18 22:27:55 +01:00
|
|
|
res.status(400).json({ error: "Invalid ByteData" });
|
2023-03-03 17:48:43 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-04 18:25:43 +01:00
|
|
|
res
|
|
|
|
.status(400)
|
2023-03-18 22:27:55 +01:00
|
|
|
.json({ error: "This endpoint only accepts GET, POST and PUT requests" });
|
2023-03-03 17:48:43 +01:00
|
|
|
}
|
|
|
|
}
|