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.

split @lib/tinybird

Steven Tey5e5d1d36db65
diff --git a/apps/web/lib/tinybird/get-click-event.ts b/apps/web/lib/tinybird/get-click-event.ts
new file mode 100644
index 0000000000..4f5d6d01e3
--- /dev/null
+++ b/apps/web/lib/tinybird/get-click-event.ts
@@ -0,0 +1,29 @@
+import { clickEventSchemaTB } from "../zod/schemas/conversions";
+
+export async function getClickEvent(clickId: string) {
+ const url = new URL(
+ `${process.env.TINYBIRD_API_URL}/v0/pipes/click_by_id.json`,
+ );
+
+ url.searchParams.append("clickId", clickId);
+
+ const response = await fetch(url.toString(), {
+ headers: {
+ Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
+ },
+ });
+
+ if (!response.ok) {
+ console.error("Error fetching click event from Tinybird", response);
+ return null;
+ }
+
+ const { rows, data } = await response.json();
+
+ if (rows === 0) {
+ console.error("No click event found in Tinybird", clickId);
+ return null;
+ }
+
+ return clickEventSchemaTB.parse(data[0]);
+}
diff --git a/apps/web/lib/tinybird/index.ts b/apps/web/lib/tinybird/index.ts
new file mode 100644
index 0000000000..cb6a9653ca
--- /dev/null
+++ b/apps/web/lib/tinybird/index.ts
@@ -0,0 +1,4 @@
+export * from "./get-click-event";
+export * from "./record-click";
+export * from "./record-conversion";
+export * from "./record-link";
diff --git a/apps/web/lib/tinybird.ts b/apps/web/lib/tinybird/record-click.ts
similarity index 65%
rename from apps/web/lib/tinybird.ts
rename to apps/web/lib/tinybird/record-click.ts
index 6518936834..ae55d869f4 100644
--- a/apps/web/lib/tinybird.ts
+++ b/apps/web/lib/tinybird/record-click.ts
@@ -8,11 +8,9 @@ import {
import { ipAddress } from "@vercel/edge";
import { nanoid } from "ai";
import { NextRequest, userAgent } from "next/server";
-import { detectBot, detectQr, getIdentityHash } from "./middleware/utils";
-import { conn } from "./planetscale";
-import { LinkProps } from "./types";
-import { ratelimit } from "./upstash";
-import { ConversionEvent, clickEventSchemaTB } from "./zod/schemas/conversions";
+import { detectBot, detectQr, getIdentityHash } from "../middleware/utils";
+import { conn } from "../planetscale";
+import { ratelimit } from "../upstash";
/**
* Recording clicks with geo, ua, referer and timestamp data
@@ -127,78 +125,3 @@ export async function recordClick({
],
]);
}
-
-export async function recordLink({
- link,
- deleted,
-}: {
- link: Partial<LinkProps> & {
- tags?: { tagId: string }[];
- };
- deleted?: boolean;
-}) {
- return await fetch(
- `${process.env.TINYBIRD_API_URL}/v0/events?name=dub_links_metadata&wait=true`,
- {
- method: "POST",
- headers: {
- Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
- },
- body: JSON.stringify({
- timestamp: new Date(Date.now()).toISOString(),
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tagIds: link.tags?.map(({ tagId }) => tagId) || [],
- project_id: link.projectId || "",
- deleted: deleted ? 1 : 0,
- }),
- },
- ).then((res) => res.json());
-}
-
-export async function getClickEvent(clickId: string) {
- const url = new URL(
- `${process.env.TINYBIRD_API_URL}/v0/pipes/click_by_id.json`,
- );
-
- url.searchParams.append("clickId", clickId);
-
- const response = await fetch(url.toString(), {
- headers: {
- Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
- },
- });
-
- if (!response.ok) {
- console.error("Error fetching click event from Tinybird", response);
- return null;
- }
-
- const { rows, data } = await response.json();
-
- if (rows === 0) {
- console.error("No click event found in Tinybird", clickId);
- return null;
- }
-
- return clickEventSchemaTB.parse(data[0]);
-}
-
-// Record conversion event in Tinybird
-export async function recordConversion(event: ConversionEvent) {
- return await fetch(
- `${process.env.TINYBIRD_API_URL}/v0/events?name=dub_conversion_events&wait=true`,
- {
- method: "POST",
- headers: {
- Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
- },
- body: JSON.stringify({
- ...event,
- timestamp: new Date(Date.now()).toISOString(),
- }),
- },
- );
-}
diff --git a/apps/web/lib/tinybird/record-conversion.ts b/apps/web/lib/tinybird/record-conversion.ts
new file mode 100644
index 0000000000..f95355168b
--- /dev/null
+++ b/apps/web/lib/tinybird/record-conversion.ts
@@ -0,0 +1,18 @@
+import { ConversionEvent } from "../zod/schemas/conversions";
+
+// Record conversion event in Tinybird
+export async function recordConversion(event: ConversionEvent) {
+ return await fetch(
+ `${process.env.TINYBIRD_API_URL}/v0/events?name=dub_conversion_events&wait=true`,
+ {
+ method: "POST",
+ headers: {
+ Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
+ },
+ body: JSON.stringify({
+ ...event,
+ timestamp: new Date(Date.now()).toISOString(),
+ }),
+ },
+ );
+}
diff --git a/apps/web/lib/tinybird/record-link.ts b/apps/web/lib/tinybird/record-link.ts
new file mode 100644
index 0000000000..4d72a842db
--- /dev/null
+++ b/apps/web/lib/tinybird/record-link.ts
@@ -0,0 +1,31 @@
+import { LinkProps } from "../types";
+
+export async function recordLink({
+ link,
+ deleted,
+}: {
+ link: Partial<LinkProps> & {
+ tags?: { tagId: string }[];
+ };
+ deleted?: boolean;
+}) {
+ return await fetch(
+ `${process.env.TINYBIRD_API_URL}/v0/events?name=dub_links_metadata&wait=true`,
+ {
+ method: "POST",
+ headers: {
+ Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
+ },
+ body: JSON.stringify({
+ timestamp: new Date(Date.now()).toISOString(),
+ link_id: link.id,
+ domain: link.domain,
+ key: link.key,
+ url: link.url,
+ tagIds: link.tags?.map(({ tagId }) => tagId) || [],
+ project_id: link.projectId || "",
+ deleted: deleted ? 1 : 0,
+ }),
+ },
+ ).then((res) => res.json());
+}