/
route.tsx
60 lines (55 loc) · 1.63 KB
/
route.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { QRCodeSVG } from "@/lib/qr/utils";
import { ratelimit } from "@/lib/upstash";
import { getQRCodeQuerySchema } from "@/lib/zod/schemas";
import { getSearchParams } from "@dub/utils";
import { ipAddress } from "@vercel/edge";
import { getToken } from "next-auth/jwt";
import { ImageResponse } from "next/og";
import { NextRequest } from "next/server";
export const runtime = "edge";
export async function GET(req: NextRequest) {
try {
// Rate limit if user is not logged in
const session = await getToken({
req,
secret: process.env.NEXTAUTH_SECRET,
});
if (!session?.email) {
const ip = ipAddress(req);
const { success } = await ratelimit().limit(`qr:${ip}`);
if (!success) {
throw new DubApiError({
code: "rate_limit_exceeded",
message: "Don't DDoS me pls 🥺",
});
}
}
const params = getSearchParams(req.url);
const { url, size, level, fgColor, bgColor, includeMargin } =
getQRCodeQuerySchema.parse(params);
// const logo = req.nextUrl.searchParams.get("logo") || "https://assets.dub.co/logo.png";
return new ImageResponse(
QRCodeSVG({
value: url,
size,
level,
includeMargin,
fgColor,
bgColor,
// imageSettings: {
// src: logo,
// height: size / 4,
// width: size / 4,
// excavate: true,
// },
}),
{
width: size,
height: size,
},
);
} catch (error) {
return handleAndReturnErrorResponse(error);
}
}