feat: backend route for generating qr codes out of data

This commit is contained in:
theoleuthardt 2025-02-20 23:19:38 +01:00
parent ccca78cec0
commit 3d3f56991b
4 changed files with 356 additions and 2 deletions

View file

@ -0,0 +1,33 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import QRCode from "qrcode";
interface RequestBody {
text: string;
}
export async function generateQRCode(app: FastifyInstance) {
app.post(
"/api/generate-qrcode",
async (
request: FastifyRequest<{ Body: RequestBody }>,
reply: FastifyReply,
) => {
const data = request.body;
if (!data) {
return reply.status(400).send({ error: "Missing data parameter" });
}
try {
const qrCodeDataUrl = QRCode.toDataURL(data.text);
return reply
.header("Content-Type", "application/json")
.status(200)
.send({ qrCode: qrCodeDataUrl });
} catch (error) {
console.error("QR Code generation error:", error);
reply.status(500).send({ error: "Error generating QR code" });
}
},
);
}