Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

add more options to qr API

Steven Teybb66d16c1df7
diff --git a/apps/web/app/api/qr/route.tsx b/apps/web/app/api/qr/route.tsx
index c4c0049b5a..7227b9e914 100644
--- a/apps/web/app/api/qr/route.tsx
+++ b/apps/web/app/api/qr/route.tsx
@@ -5,6 +5,13 @@ import { ipAddress } from "@vercel/edge";
import { getToken } from "next-auth/jwt";
import { ImageResponse } from "next/og";
import { QRCodeSVG } from "@/lib/qr/utils";
+import {
+ DEFAULT_BGCOLOR,
+ DEFAULT_FGCOLOR,
+ DEFAULT_INCLUDEMARGIN,
+ DEFAULT_LEVEL,
+ QR_LEVELS,
+} from "@/lib/qr/constants";
export const runtime = "edge";
@@ -24,12 +31,26 @@ export async function GET(req: NextRequest) {
const url = req.nextUrl.searchParams.get("url") || "https://dub.co";
const size = parseInt(req.nextUrl.searchParams.get("size") || "600", 10);
+ const level = req.nextUrl.searchParams.get("level") || DEFAULT_LEVEL;
+ if (!QR_LEVELS.includes(level)) {
+ return new Response("Invalid QR code level.", { status: 400 });
+ }
+ const fgColor = req.nextUrl.searchParams.get("fgColor") || DEFAULT_FGCOLOR;
+ const bgColor = req.nextUrl.searchParams.get("bgColor") || DEFAULT_BGCOLOR;
+
+ const includeMargin =
+ req.nextUrl.searchParams.get("includeMargin") || DEFAULT_INCLUDEMARGIN;
+
// const logo = req.nextUrl.searchParams.get("logo") || "https://d2vwwcvoksz7ty.cloudfront.net/logo.png";
return new ImageResponse(
QRCodeSVG({
value: url,
- size: size,
+ size,
+ level,
+ includeMargin: includeMargin === "true",
+ fgColor,
+ bgColor,
// imageSettings: {
// src: logo,
// height: size / 4,
diff --git a/apps/web/lib/qr/constants.ts b/apps/web/lib/qr/constants.ts
index 6e060cb8bc..2b78b96510 100644
--- a/apps/web/lib/qr/constants.ts
+++ b/apps/web/lib/qr/constants.ts
@@ -15,6 +15,8 @@ export const DEFAULT_INCLUDEMARGIN = false;
export const MARGIN_SIZE = 4;
+export const QR_LEVELS = ["L", "M", "Q", "H"];
+
// This is *very* rough estimate of max amount of QRCode allowed to be covered.
// It is "wrong" in a lot of ways (area is a terrible way to estimate, it
// really should be number of modules covered), but if for some reason we don't
diff --git a/apps/web/lib/qr/types.ts b/apps/web/lib/qr/types.ts
index d730baddf5..73733f1039 100644
--- a/apps/web/lib/qr/types.ts
+++ b/apps/web/lib/qr/types.ts
@@ -16,7 +16,6 @@ export type ImageSettings = {
export type QRProps = {
value: string;
size?: number;
- // Should be a real enum, but doesn't seem to be compatible with real code.
level?: string;
bgColor?: string;
fgColor?: string;