From 8672181a319c8b5ed79ae5d5abfac674d23ffc5f Mon Sep 17 00:00:00 2001 From: MUI bot <2109932+Janpot@users.noreply.github.com> Date: Tue, 21 Jan 2025 16:25:08 +0100 Subject: [PATCH] qr --- dangerFileContent.ts | 4 ++-- netlify.toml | 6 ++++++ netlify/edge-functions/qr-code.ts | 33 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 netlify/edge-functions/qr-code.ts diff --git a/dangerFileContent.ts b/dangerFileContent.ts index 0489aed70e162a..e47303d8584b0e 100644 --- a/dangerFileContent.ts +++ b/dangerFileContent.ts @@ -221,8 +221,8 @@ async function addDeployPreviewUrls() { async function formatMdLinkWithQrCode(path: string) { const url = String(formatFileToLink(path)); - const qrDataUrl = await QRCode.toDataURL(url, {}); - return `
[${path}](${url})![${path}](${qrDataUrl})
`; + const qr = `${netlifyPreview}edge-functions/qr-code?text=${encodeURIComponent(url)}`; + return `
${path}QR code
`; } const files = [...danger.git.created_files, ...danger.git.modified_files]; diff --git a/netlify.toml b/netlify.toml index 869ce37a1c4dfc..90ead4ac9baac4 100644 --- a/netlify.toml +++ b/netlify.toml @@ -1,3 +1,9 @@ +# run `pnpm --package netlify-cli dlx netlify dev --cwd .` to run functions locally +[dev] + command = "pnpm docs:dev" + targetPort = 3000 + port = 8888 + [build] # Directory (relative to root of your repo) that contains the deploy-ready # HTML files and assets generated by the build. If a base directory has diff --git a/netlify/edge-functions/qr-code.ts b/netlify/edge-functions/qr-code.ts new file mode 100644 index 00000000000000..106c6fe1bcc8c2 --- /dev/null +++ b/netlify/edge-functions/qr-code.ts @@ -0,0 +1,33 @@ +import qr from 'https://esm.sh/qr-image@3.2.0'; + +export default async function handler(req: Request) { + const params = new URL(req.url).searchParams; + const text = params.get('text'); + + if (!text) { + return new Response('Missing text query parameter', { status: 400 }); + } + + if (text.length > 1000) { + return new Response('Text query parameter is too long', { status: 400 }); + } + + const data = qr.imageSync(text, { type: 'png' }); + + const v = params.get('v') || ''; + + const cacheKey = new URLSearchParams(); + cacheKey.append('text', text); + cacheKey.append('v', v); + + return new Response(data, { + headers: { + 'Content-Type': 'image/png', + 'Cache-Control': 'public, max-age=31536000, immutable', // Cache for 1 year + }, + }); +} +export const config = { + cache: 'manual', + path: '/edge-functions/qr-code', +};