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.

simplify crawl logic

Steven Tey560be3c1d2d1
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index b926b4e9d5..261d95be63 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -31,6 +31,7 @@ import { clickCache } from "../api/links/click-cache";
import { getLinkViaEdge } from "../planetscale";
import { getDomainViaEdge } from "../planetscale/get-domain-via-edge";
import { getPartnerAndDiscount } from "../planetscale/get-partner-discount";
+import { crawlBitly } from "./utils/crawl-bitly";
import { resolveABTestURL } from "./utils/resolve-ab-test-url";
export default async function LinkMiddleware(
@@ -88,11 +89,8 @@ export default async function LinkMiddleware(
});
if (!linkData) {
- // TODO: remove this once everything is migrated over
if (domain === "buff.ly") {
- return NextResponse.rewrite(
- new URL(`/api/links/crawl/bitly/${domain}/${key}`, req.url),
- );
+ return await crawlBitly(req);
}
// check if domain has notFoundUrl configured
diff --git a/apps/web/app/api/links/crawl/bitly/[domain]/[key]/route.ts b/apps/web/lib/middleware/utils/crawl-bitly.ts
similarity index 61%
rename from apps/web/app/api/links/crawl/bitly/[domain]/[key]/route.ts
rename to apps/web/lib/middleware/utils/crawl-bitly.ts
index 3c1747aefd..3d4f196c49 100644
--- a/apps/web/app/api/links/crawl/bitly/[domain]/[key]/route.ts
+++ b/apps/web/lib/middleware/utils/crawl-bitly.ts
@@ -1,18 +1,10 @@
import { redis } from "@/lib/upstash";
-import z from "@/lib/zod";
-import { DUB_HEADERS, getUrlFromStringIfValid } from "@dub/utils";
+import { DUB_HEADERS } from "@dub/utils";
import { NextRequest, NextResponse } from "next/server";
+import { parse } from "./parse";
-const workspaceId = "cm05wnnpo000711ztj05wwdbu";
-
-// GET /api/links/crawl/bitly – crawl a bitly link and redirect to the destination if exists
-export const GET = async (_req: NextRequest, { params }) => {
- const { domain, key } = z
- .object({
- domain: z.string(),
- key: z.string(),
- })
- .parse(params);
+export const crawlBitly = async (req: NextRequest) => {
+ const { domain, key } = parse(req);
// bitly doesn't support the following characters: ` ~ , . < > ; ‘ : “ / \ [ ] ^ { } ( ) = + ! * @ & $ £ ? % # |
// @see: https://support.bitly.com/hc/en-us/articles/360030780892-What-characters-are-supported-when-customizing-links
@@ -21,14 +13,10 @@ export const GET = async (_req: NextRequest, { params }) => {
if (key && !invalidBitlyKeyRegex.test(key)) {
const link = await fetchBitlyLink({ domain, key });
if (link) {
- const sanitizedUrl = getUrlFromStringIfValid(link.long_url);
-
- if (sanitizedUrl) {
- return NextResponse.redirect(sanitizedUrl, {
- headers: DUB_HEADERS,
- status: 302,
- });
- }
+ return NextResponse.redirect(link.long_url, {
+ headers: DUB_HEADERS,
+ status: 302,
+ });
}
}
@@ -38,6 +26,8 @@ export const GET = async (_req: NextRequest, { params }) => {
});
};
+const BUFFER_WORKSPACE_ID = "cm05wnnpo000711ztj05wwdbu";
+
async function fetchBitlyLink({
domain,
key,
@@ -45,10 +35,12 @@ async function fetchBitlyLink({
domain: string;
key: string;
}) {
- const apiKey = await redis.get<string>(`import:bitly:${workspaceId}`);
+ const apiKey = await redis.get<string>(`import:bitly:${BUFFER_WORKSPACE_ID}`);
if (!apiKey) {
- console.error(`[Bitly] No API key found for workspace ${workspaceId}`);
+ console.error(
+ `[Bitly] No API key found for workspace ${BUFFER_WORKSPACE_ID}`,
+ );
return null;
}