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 Bitly link import middleware for buff.ly domains

Kiran K693c6c810c95
diff --git a/apps/web/lib/middleware/bitly.ts b/apps/web/lib/middleware/bitly.ts
new file mode 100644
index 0000000000..5fa1d60b13
--- /dev/null
+++ b/apps/web/lib/middleware/bitly.ts
@@ -0,0 +1,101 @@
+import { getUrlFromStringIfValid, linkConstructorSimple } from "@dub/utils";
+import { waitUntil } from "@vercel/functions";
+import { ExpandedLink } from "../api/links";
+import { createId } from "../api/utils";
+import { conn } from "../planetscale/connection";
+import { recordLink } from "../tinybird/record-link";
+
+type BitlyLink = {
+ id: string;
+ long_url: string;
+ archived: boolean;
+ created_at: string;
+ custom_bitlinks: string[];
+ tags: string[];
+};
+
+export const createBitlyLink = async (bitlyLink: string) => {
+ const response = await fetch(
+ `https://api-ssl.bitly.com/v4/bitlinks/${bitlyLink}`,
+ {
+ headers: {
+ Authorization: "Bearer xxxx", // TODO: add token
+ },
+ },
+ );
+
+ const data = await response.json();
+
+ if (!response.ok) {
+ console.error(`[Bitly] Error retrieving Bitly link: ${bitlyLink}`, data);
+ return null;
+ }
+
+ const link = data as BitlyLink;
+
+ const [domain, key] = link.id.split("/");
+
+ if (!domain || !key) {
+ return null;
+ }
+
+ const sanitizedUrl = getUrlFromStringIfValid(link.long_url);
+
+ if (!sanitizedUrl) {
+ return null;
+ }
+
+ const workspaceId = "";
+ const userId = "";
+ const folderId = "";
+
+ const newLink = {
+ id: createId({ prefix: "link_" }),
+ projectId: workspaceId,
+ userId,
+ domain,
+ key,
+ url: sanitizedUrl,
+ shortLink: linkConstructorSimple({
+ domain,
+ key,
+ }),
+ archived: link.archived,
+ createdAt: new Date(link.created_at),
+ updatedAt: new Date(link.created_at),
+ folderId,
+ tagIds: [], // TODO: add tags
+ };
+
+ console.log("[Bitly] Creating link", newLink);
+
+ await conn.execute(
+ "INSERT INTO Link (id, projectId, userId, domain, `key`, url, shortLink, archived, createdAt, updatedAt, folderId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+ [
+ newLink.id,
+ newLink.projectId,
+ newLink.userId,
+ newLink.domain,
+ newLink.key,
+ newLink.url,
+ newLink.shortLink,
+ newLink.archived,
+ newLink.createdAt,
+ newLink.updatedAt,
+ newLink.folderId,
+ ],
+ );
+
+ // TODO: fetch tags
+ waitUntil(
+ recordLink({
+ ...newLink,
+ tenantId: "",
+ programId: "",
+ partnerId: "",
+ tags: [],
+ } as unknown as ExpandedLink),
+ );
+
+ return newLink;
+};
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index a102091ba5..93bbdc286e 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -26,6 +26,7 @@ import {
import { linkCache } from "../api/links/cache";
import { getLinkViaEdge } from "../planetscale";
import { getDomainViaEdge } from "../planetscale/get-domain-via-edge";
+import { createBitlyLink } from "./bitly";
import { hasEmptySearchParams } from "./utils/has-empty-search-params";
export default async function LinkMiddleware(
@@ -68,7 +69,15 @@ export default async function LinkMiddleware(
let link = await linkCache.get({ domain, key });
if (!link) {
- const linkData = await getLinkViaEdge(domain, key);
+ let linkData = await getLinkViaEdge(domain, key);
+
+ if (domain === "buff.ly") {
+ const newLink = await createBitlyLink(`buff.ly/${key}`);
+
+ if (newLink) {
+ linkData = newLink;
+ }
+ }
if (!linkData) {
// check if domain has notFoundUrl configured