The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
update api
Kiran K0df9546bf179
diff --git a/apps/web/app/api/referrals/token/route.ts b/apps/web/app/api/referrals/token/route.tsnew file mode 100644index 0000000000..99d04deb5b--- /dev/null+++ b/apps/web/app/api/referrals/token/route.ts@@ -0,0 +1,7 @@+import { withEmbedToken } from "@/lib/auth/embed-token";+import { NextResponse } from "next/server";++// GET /api/referrals/token - get the embed token for the given link+export const GET = withEmbedToken(async ({ linkToken }) => {+ return NextResponse.json(linkToken);+});diff --git a/apps/web/app/app.dub.co/(dashboard)/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/layout.tsxindex a0fc9ef0a4..3197c7b937 100644--- a/apps/web/app/app.dub.co/(dashboard)/layout.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/layout.tsx@@ -4,7 +4,6 @@ import { HelpButtonRSC } from "@/ui/layout/sidebar/help-button-rsc";import { NewsRSC } from "@/ui/layout/sidebar/news-rsc";import { ReferButton } from "@/ui/layout/sidebar/refer-button";import Toolbar from "@/ui/layout/toolbar/toolbar";-import { DubWidget } from "@dub/embed-react";import { constructMetadata } from "@dub/utils";import { ReactNode } from "react";diff --git a/apps/web/app/app.dub.co/embed/dashboard/page-client.tsx b/apps/web/app/app.dub.co/embed/dashboard/page-client.tsxindex af3745615c..298dafdb01 100644--- a/apps/web/app/app.dub.co/embed/dashboard/page-client.tsx+++ b/apps/web/app/app.dub.co/embed/dashboard/page-client.tsx@@ -19,6 +19,7 @@ import { Copy, MoneyBill2 } from "@dub/ui/src/icons";import { getPrettyUrl } from "@dub/utils";import { Link, Program } from "@prisma/client";import { useContext, useEffect } from "react";+import { LinkToken } from "../token";import useReferralAnalytics from "../use-referral-analytics";import { SaleTable } from "./sale-table";@@ -127,6 +128,8 @@ export function RewardDashboardPageClient({<Wordmark className="h-4" /></a></div>++ <LinkToken /></>);}diff --git a/apps/web/app/app.dub.co/embed/token.tsx b/apps/web/app/app.dub.co/embed/token.tsxnew file mode 100644index 0000000000..e451661627--- /dev/null+++ b/apps/web/app/app.dub.co/embed/token.tsx@@ -0,0 +1,28 @@+"use client";++import { fetcher } from "@dub/utils";+import { useEffect } from "react";+import useSWR from "swr";++export const LinkToken = () => {+ const { error } = useSWR<{ token: number }>(`/api/referrals/token`, fetcher, {+ revalidateOnFocus: true,+ refreshInterval: 50000,+ });++ // Inform the parent if there's an error (Eg: token is expired)+ useEffect(() => {+ if (error) {+ window.parent.postMessage(+ {+ originator: "Dub",+ event: "ERROR",+ data: error.info,+ },+ "*",+ );+ }+ }, [error]);++ return null;+};diff --git a/apps/web/app/app.dub.co/embed/widget/page-client.tsx b/apps/web/app/app.dub.co/embed/widget/page-client.tsxindex 4bc0dcbed1..f39862ac9d 100644--- a/apps/web/app/app.dub.co/embed/widget/page-client.tsx+++ b/apps/web/app/app.dub.co/embed/widget/page-client.tsx@@ -1,5 +1,6 @@"use client";+import { PartnerSaleResponse } from "@/lib/types";import {Button,Check2,@@ -9,9 +10,11 @@ import {Wordmark,} from "@dub/ui";import { GiftFill } from "@dub/ui/src/icons";-import { currencyFormatter, getPrettyUrl } from "@dub/utils";+import { currencyFormatter, fetcher, getPrettyUrl } from "@dub/utils";import { Link, Program } from "@prisma/client";-import { useEffect, useState } from "react";+import { useState } from "react";+import useSWR from "swr";+import { LinkToken } from "../token";type Tab = "invite" | "rewards";@@ -25,18 +28,19 @@ export function EmbedWidgetPageClient({const [copied, copyToClipboard] = useCopyToClipboard();const [selectedTab, setSelectedTab] = useState<Tab>("invite");- // useEffect(() => {- // if (error) {- // window.parent.postMessage(- // {- // originator: "Dub",- // event: "ERROR",- // data: error.info,- // },- // "*",- // );- // }- // }, [error]);+ // TODO:+ // Add sales table++ const {+ data: sales,+ isLoading,+ error,+ } = useSWR<PartnerSaleResponse[]>(`/api/referrals/sales`, fetcher);++ const { data: salesCount } = useSWR<{ count: number }>(+ `/api/referrals/sales/count`,+ fetcher,+ );return (<div>@@ -133,6 +137,8 @@ export function EmbedWidgetPageClient({<Wordmark className="h-4" /></a></div>++ <LinkToken /></div></div></div>diff --git a/apps/web/lib/auth/embed-token.ts b/apps/web/lib/auth/embed-token.tsindex 4bc4c1c4ae..8537c07dd8 100644--- a/apps/web/lib/auth/embed-token.ts+++ b/apps/web/lib/auth/embed-token.ts@@ -2,7 +2,7 @@ import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";import { prisma } from "@/lib/prisma";import { ratelimit } from "@/lib/upstash";import { getSearchParams } from "@dub/utils";-import { Link, Program, Project } from "@prisma/client";+import { EmbedPublicToken, Link, Program, Project } from "@prisma/client";import { AxiomRequest, withAxiom } from "next-axiom";import { cookies } from "next/headers";import { EMBED_PUBLIC_TOKEN_COOKIE_NAME } from "../referrals/constants";@@ -15,6 +15,7 @@ interface WithEmbedTokenHandler {workspace,link,program,+ linkToken,}: {req: Request;params: Record<string, string>;@@ -22,6 +23,7 @@ interface WithEmbedTokenHandler {workspace: Project;link: Link;program: Program;+ linkToken: EmbedPublicToken;}): Promise<Response>;}@@ -51,13 +53,13 @@ export const withEmbedToken = (handler: WithEmbedTokenHandler) => {});}- const publicToken = await prisma.embedPublicToken.findUnique({+ const linkToken = await prisma.embedPublicToken.findUnique({where: {publicToken: tokenFromCookie,},});- if (!publicToken) {+ if (!linkToken) {cookieStore.delete(EMBED_PUBLIC_TOKEN_COOKIE_NAME);throw new DubApiError({@@ -66,7 +68,7 @@ export const withEmbedToken = (handler: WithEmbedTokenHandler) => {});}- if (publicToken.expires < new Date()) {+ if (linkToken.expires < new Date()) {cookieStore.delete(EMBED_PUBLIC_TOKEN_COOKIE_NAME);throw new DubApiError({@@ -96,7 +98,7 @@ export const withEmbedToken = (handler: WithEmbedTokenHandler) => {link = await prisma.link.findUniqueOrThrow({where: {- id: publicToken.linkId,+ id: linkToken.linkId,},});@@ -128,6 +130,7 @@ export const withEmbedToken = (handler: WithEmbedTokenHandler) => {workspace,link,program,+ linkToken,});} catch (error) {req.log.error(error);