mirror of
https://github.com/theoleuthardt/werkzeugkiste.git
synced 2026-06-13 09:37:53 +00:00
fix: backend route fix for error handling + fixed package import
This commit is contained in:
parent
0833ce98b8
commit
5291a05d2c
2 changed files with 15 additions and 7 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
||||||
import QRCode from "qrcode";
|
import * as QRCode from "qrcode";
|
||||||
|
|
||||||
interface RequestBody {
|
interface RequestBody {
|
||||||
text: string;
|
qrcodeContent: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function generateQRCode(app: FastifyInstance) {
|
export async function generateQRCode(app: FastifyInstance) {
|
||||||
|
|
@ -14,19 +14,26 @@ export async function generateQRCode(app: FastifyInstance) {
|
||||||
) => {
|
) => {
|
||||||
const data = request.body;
|
const data = request.body;
|
||||||
|
|
||||||
if (!data) {
|
if (!data.qrcodeContent) {
|
||||||
return reply.status(400).send({ error: "Missing data parameter" });
|
return reply.status(400).send({ error: "Missing text parameter" });
|
||||||
|
} else if (data.qrcodeContent === "") {
|
||||||
|
return reply
|
||||||
|
.status(400)
|
||||||
|
.send({ error: "Text parameter cannot be empty" });
|
||||||
|
} else if (data.qrcodeContent.length > 1000) {
|
||||||
|
return reply.status(400).send({ error: "Text parameter too long" });
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const qrCodeDataUrl = QRCode.toDataURL(data.text);
|
const qrCodeUrl = await QRCode.toDataURL(data.qrcodeContent);
|
||||||
|
console.log("QR Code generated:", qrCodeUrl);
|
||||||
return reply
|
return reply
|
||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.status(200)
|
.status(200)
|
||||||
.send({ qrCode: qrCodeDataUrl });
|
.send({ qrCode: qrCodeUrl });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("QR Code generation error:", error);
|
console.error("QR Code generation error:", error);
|
||||||
reply.status(500).send({ error: "Error generating QR code" });
|
return reply.status(500).send({ error: "Error generating QR code!" });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"rootDir": ".",
|
"rootDir": ".",
|
||||||
"strict": true,
|
"strict": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
"allowSyntheticDefaultImports": true
|
"allowSyntheticDefaultImports": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue