fix: backend route fix for error handling + fixed package import

This commit is contained in:
theoleuthardt 2025-02-21 15:14:02 +01:00
parent 0833ce98b8
commit 5291a05d2c
2 changed files with 15 additions and 7 deletions

View file

@ -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!" });
} }
}, },
); );

View file

@ -5,6 +5,7 @@
"outDir": "./dist", "outDir": "./dist",
"rootDir": ".", "rootDir": ".",
"strict": true, "strict": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true "allowSyntheticDefaultImports": true
} }
} }