The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
simplify & fixes
Steven Tey634d6ad02f6e
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.tsindex f750a4a17b..1bacf840d9 100644--- a/apps/web/app/api/links/[linkId]/route.ts+++ b/apps/web/app/api/links/[linkId]/route.ts@@ -24,46 +24,26 @@ export const GET = withWorkspace(linkId: params.linkId,});- const [tags, webhooks] = await Promise.all([- prisma.tag.findMany({- where: {- links: {- some: {- linkId: link.id,- },+ const tags = await prisma.tag.findMany({+ where: {+ links: {+ some: {+ linkId: link.id,},},- select: {- id: true,- name: true,- color: true,- },- }),-- prisma.webhook.findMany({- where: {- links: {- some: {- linkId: link.id,- },- },- },- select: {- id: true,- name: true,- url: true,- },- }),- ]);+ },+ select: {+ id: true,+ name: true,+ color: true,+ },+ });const response = transformLink({...link,tags: tags.map((tag) => {return { tag };}),- webhooks: webhooks.map((webhook) => {- return { webhook };- }),});return NextResponse.json(response, { headers });diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.tsindex 3f3df00f01..c8b46721ae 100644--- a/apps/web/app/api/links/route.ts+++ b/apps/web/app/api/links/route.ts@@ -32,6 +32,7 @@ export const GET = withWorkspace(showArchived,withTags,includeUser,+ includeWebhooks,linkIds,} = getLinksQuerySchemaExtended.parse(searchParams);@@ -52,6 +53,7 @@ export const GET = withWorkspace(showArchived,withTags,includeUser,+ includeWebhooks,linkIds,});diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.tsindex 1d4dbaf448..f008522705 100644--- a/apps/web/lib/api/links/create-link.ts+++ b/apps/web/lib/api/links/create-link.ts@@ -94,6 +94,7 @@ export async function createLink(link: ProcessedLinkProps) {},},},+ webhooks: webhookIds ? true : false,},});diff --git a/apps/web/lib/api/links/get-links-for-workspace.ts b/apps/web/lib/api/links/get-links-for-workspace.tsindex d620df2cc4..3f48d9da66 100644--- a/apps/web/lib/api/links/get-links-for-workspace.ts+++ b/apps/web/lib/api/links/get-links-for-workspace.ts@@ -17,6 +17,7 @@ export async function getLinksForWorkspace({showArchived,withTags,includeUser,+ includeWebhooks,linkIds,}: z.infer<typeof getLinksQuerySchemaExtended> & {workspaceId: string;@@ -64,7 +65,6 @@ export async function getLinksForWorkspace({...(linkIds && { id: { in: linkIds } }),},include: {- user: includeUser,tags: {include: {tag: {@@ -76,17 +76,8 @@ export async function getLinksForWorkspace({},},},- webhooks: {- select: {- webhook: {- select: {- id: true,- name: true,- url: true,- },- },- },- },+ user: includeUser,+ webhooks: includeWebhooks,},orderBy: {[sort]: "desc",diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.tsindex 3ecd00e390..504f7e3631 100644--- a/apps/web/lib/api/links/update-link.ts+++ b/apps/web/lib/api/links/update-link.ts@@ -134,17 +134,7 @@ export async function updateLink({},},},- webhooks: {- select: {- webhook: {- select: {- id: true,- name: true,- url: true,- },- },- },- },+ webhooks: webhookIds ? true : false,},});@@ -154,9 +144,7 @@ export async function updateLink({redis.hset(updatedLink.domain.toLowerCase(), {[updatedLink.key.toLowerCase()]: await formatRedisLink({...response,- ...(response.webhooks.length > 0 && {- webhookIds: response.webhooks.map(({ webhook }) => webhook.id),- }),+ ...(webhookIds && { webhookIds }),}),}),// record link in Tinybirddiff --git a/apps/web/lib/api/links/utils/transform-link.ts b/apps/web/lib/api/links/utils/transform-link.tsindex 530cf4f91c..18d1213f09 100644--- a/apps/web/lib/api/links/utils/transform-link.ts+++ b/apps/web/lib/api/links/utils/transform-link.ts@@ -1,20 +1,20 @@-import { Link, Tag, Webhook } from "@prisma/client";+import { Link, Tag } from "@prisma/client";export type LinkWithTags = Link & {tags?: { tag: Pick<Tag, "id" | "name" | "color"> }[];- webhooks?: { webhook: Pick<Webhook, "id" | "name" | "url"> }[];+ webhooks?: { webhookId: string }[];};// Transform link with additional propertiesexport const transformLink = (link: LinkWithTags) => {const tags = (link.tags || []).map(({ tag }) => tag);- const webhooks = (link.webhooks || []).map(({ webhook }) => webhook);+ const webhookIds = link.webhooks?.map(({ webhookId }) => webhookId) ?? [];return {...link,tagId: tags?.[0]?.id ?? null, // backwards compatibilitytags,- webhooks,+ webhookIds,qrCode: `https://api.dub.co/qr?url=${link.shortLink}?qr=1`,workspaceId: link.projectId ? `ws_${link.projectId}` : null,};diff --git a/apps/web/lib/swr/use-default-domains.ts b/apps/web/lib/swr/use-default-domains.tsindex d289c3f826..32f8a91c2b 100644--- a/apps/web/lib/swr/use-default-domains.ts+++ b/apps/web/lib/swr/use-default-domains.ts@@ -26,8 +26,6 @@ export default function useDefaultDomains({: data;}, [data, flags]);- console.log({ defaultDomains });-return {defaultDomains,loading: !data && !error,diff --git a/apps/web/lib/swr/use-links.ts b/apps/web/lib/swr/use-links.tsindex 6558fe3e14..03b561c335 100644--- a/apps/web/lib/swr/use-links.ts+++ b/apps/web/lib/swr/use-links.ts@@ -13,7 +13,7 @@ export default function useLinks(opts: z.infer<typeof partialQuerySchema> = {},swrOpts: SWRConfiguration = {},) {- const { id } = useWorkspace();+ const { id, webhookEnabled } = useWorkspace();const { getQueryString } = useRouterStuff();const [admin, setAdmin] = useState(false);@@ -30,7 +30,12 @@ export default function useLinks(>(id? `/api/links${getQueryString(- { workspaceId: id, includeUser: "true", ...opts },+ {+ workspaceId: id,+ includeUser: "true",+ includeWebhooks: "true",+ ...opts,+ },{ignore: ["import", "upgrade", "newLink"],},diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex c3871dc9fe..6fb9c1a2c4 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -20,7 +20,7 @@ export type LinkProps = Link;export interface LinkWithTagsProps extends LinkProps {tags: TagProps[];- webhooks: WebhookProps[];+ webhookIds: string[];}export interface SimpleLinkProps {diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.tsindex 7000f5bbcb..a2246e26c1 100644--- a/apps/web/lib/zod/schemas/links.ts+++ b/apps/web/lib/zod/schemas/links.ts@@ -12,7 +12,6 @@ import {parseUrlSchema,parseUrlSchemaAllowEmpty,} from "./utils";-import { webhookSchema } from "./webhooks";export const getUrlQuerySchema = z.object({url: parseUrlSchema,@@ -68,8 +67,9 @@ const LinksQuerySchema = z.object({.optional().default("false").describe(- "Whether to include tags in the response. Defaults to `false` if not provided.",- ),+ "DEPRECATED. Filter for links that have at least one tag assigned to them.",+ )+ .openapi({ deprecated: true }),});export const getLinksQuerySchema = LinksQuerySchema.merge(@@ -491,11 +491,11 @@ export const LinkSchema = ztags: TagSchema.array().nullable().describe("The tags assigned to the short link."),- webhooks: webhookSchema- .pick({ id: true, name: true, url: true })- .array()- .nullable()- .describe("The webhooks assigned to the short link."),+ webhookIds: z+ .array(z.string())+ .describe(+ "The IDs of the webhooks that the short link is associated with.",+ ),comments: z.string().nullable()@@ -589,6 +589,7 @@ export const getLinksQuerySchemaExtended = getLinksQuerySchema.merge(z.object({// Only Dub UI uses the following query parametersincludeUser: booleanQuerySchema.default("false"),+ includeWebhooks: booleanQuerySchema.default("false"),linkIds: z.union([z.string(), z.array(z.string())]).transform((v) => (Array.isArray(v) ? v : v.split(",")))diff --git a/apps/web/tests/links/bulk-create-link.test.ts b/apps/web/tests/links/bulk-create-link.test.tsindex bed658c68b..75cc738a11 100644--- a/apps/web/tests/links/bulk-create-link.test.ts+++ b/apps/web/tests/links/bulk-create-link.test.ts@@ -4,10 +4,10 @@ import { Link } from "@prisma/client";import { expect, test } from "vitest";import { randomId } from "../utils/helpers";import { IntegrationHarness } from "../utils/integration";-import { link } from "../utils/resource";+import { E2E_LINK } from "../utils/resource";import { expectedLink } from "../utils/schema";-const { domain } = link;+const { domain } = E2E_LINK;test("POST /links/bulk", async (ctx) => {const h = new IntegrationHarness(ctx);diff --git a/apps/web/tests/links/bulk-delete-link.test.ts b/apps/web/tests/links/bulk-delete-link.test.tsindex 4140c1fcf4..0c473ff944 100644--- a/apps/web/tests/links/bulk-delete-link.test.ts+++ b/apps/web/tests/links/bulk-delete-link.test.ts@@ -2,9 +2,9 @@ import { Link } from "@prisma/client";import { expect, test } from "vitest";import { randomId } from "../utils/helpers";import { IntegrationHarness } from "../utils/integration";-import { link } from "../utils/resource";+import { E2E_LINK } from "../utils/resource";-const { domain } = link;+const { domain } = E2E_LINK;test("DELETE /links/bulk", async (ctx) => {const h = new IntegrationHarness(ctx);diff --git a/apps/web/tests/links/bulk-update-link.test.ts b/apps/web/tests/links/bulk-update-link.test.tsindex 0123723397..9d5e77aea7 100644--- a/apps/web/tests/links/bulk-update-link.test.ts+++ b/apps/web/tests/links/bulk-update-link.test.ts@@ -2,10 +2,10 @@ import { Link, Tag } from "@prisma/client";import { afterAll, expect, test } from "vitest";import { randomId } from "../utils/helpers";import { IntegrationHarness } from "../utils/integration";-import { link } from "../utils/resource";+import { E2E_LINK } from "../utils/resource";import { expectedLink } from "../utils/schema";-const { domain, url } = link;+const { domain, url } = E2E_LINK;test("PATCH /links/bulk", async (ctx) => {const h = new IntegrationHarness(ctx);diff --git a/apps/web/tests/links/count-links.test.ts b/apps/web/tests/links/count-links.test.tsindex 2286cb3763..0234e52be0 100644--- a/apps/web/tests/links/count-links.test.ts+++ b/apps/web/tests/links/count-links.test.ts@@ -1,9 +1,9 @@import { Link } from "@prisma/client";import { afterAll, expect, test } from "vitest";import { IntegrationHarness } from "../utils/integration";-import { link } from "../utils/resource";+import { E2E_LINK } from "../utils/resource";-const { domain, url } = link;+const { domain, url } = E2E_LINK;test("GET /links/count", async (ctx) => {const h = new IntegrationHarness(ctx);diff --git a/apps/web/tests/links/create-link-error.test.ts b/apps/web/tests/links/create-link-error.test.tsindex 7bc9b9c5e4..30c3969fcd 100644--- a/apps/web/tests/links/create-link-error.test.ts+++ b/apps/web/tests/links/create-link-error.test.ts@@ -1,9 +1,9 @@import { Link } from "@prisma/client";import { expect, test } from "vitest";import { IntegrationHarness } from "../utils/integration";-import { link } from "../utils/resource";+import { E2E_LINK } from "../utils/resource";-const { domain, url } = link;+const { domain, url } = E2E_LINK;const cases = [{diff --git a/apps/web/tests/links/create-link.test.ts b/apps/web/tests/links/create-link.test.tsindex 69b552da8f..1cc151b58b 100644--- a/apps/web/tests/links/create-link.test.ts+++ b/apps/web/tests/links/create-link.test.ts@@ -4,10 +4,10 @@ import { IntegrationHarnessOld } from "tests/utils/integration-old";import { afterAll, describe, expect, test } from "vitest";import { randomId } from "../utils/helpers";import { IntegrationHarness } from "../utils/integration";-import { link } from "../utils/resource";+import { E2E_LINK, E2E_WEBHOOK_ID } from "../utils/resource";import { expectedLink } from "../utils/schema";-const { domain, url } = link;+const { domain, url } = E2E_LINK;describe.sequential("POST /links", async () => {const h = new IntegrationHarness();@@ -401,6 +401,34 @@ describe.sequential("POST /links", async () => {await h.deleteLink(link.id);});});++ test("webhooks", async () => {+ const { status, data: link } = await http.post<Link & { tags: [] }>({+ path: "/links",+ body: {+ url,+ domain,+ webhookIds: [E2E_WEBHOOK_ID],+ },+ });++ expect(status).toEqual(200);+ expect(link).toStrictEqual({+ ...expectedLink,+ url,+ userId: user.id,+ projectId,+ workspaceId,+ shortLink: `https://${domain}/${link.key}`,+ qrCode: `https://api.dub.co/qr?url=https://${domain}/${link.key}?qr=1`,+ webhookIds: [E2E_WEBHOOK_ID],+ });+ expect(LinkSchema.strict().parse(link)).toBeTruthy();++ afterAll(async () => {+ await h.deleteLink(link.id);+ });+ });});describe.sequential("POST /links?workspaceId=xxx", async () => {diff --git a/apps/web/tests/links/delete-link.test.ts b/apps/web/tests/links/delete-link.test.tsindex a25b32c29e..8643b37885 100644--- a/apps/web/tests/links/delete-link.test.ts+++ b/apps/web/tests/links/delete-link.test.ts@@ -1,9 +1,9 @@import { Link } from "@prisma/client";import { expect, test } from "vitest";import { IntegrationHarness } from "../utils/integration";-import { link } from "../utils/resource";+import { E2E_LINK } from "../utils/resource";-const { domain, url } = link;+const { domain, url } = E2E_LINK;test("DELETE /links/{linkId}", async (ctx) => {const h = new IntegrationHarness(ctx);diff --git a/apps/web/tests/links/list-links.test.ts b/apps/web/tests/links/list-links.test.tsindex b49b96552f..79bccbd845 100644--- a/apps/web/tests/links/list-links.test.ts+++ b/apps/web/tests/links/list-links.test.ts@@ -1,9 +1,9 @@import { Link } from "@prisma/client";import { afterAll, expect, test } from "vitest";import { IntegrationHarness } from "../utils/integration";-import { link } from "../utils/resource";+import { E2E_LINK } from "../utils/resource";-const { domain, url } = link;+const { domain, url } = E2E_LINK;test("GET /links", async (ctx) => {const h = new IntegrationHarness(ctx);diff --git a/apps/web/tests/links/retrieve-link.test.ts b/apps/web/tests/links/retrieve-link.test.tsindex fef4dbf0d4..94786f5034 100644--- a/apps/web/tests/links/retrieve-link.test.ts+++ b/apps/web/tests/links/retrieve-link.test.ts@@ -3,9 +3,9 @@ import { expectedLink } from "tests/utils/schema";import { afterAll, describe, expect, test } from "vitest";import { randomId } from "../utils/helpers";import { IntegrationHarness } from "../utils/integration";-import { link } from "../utils/resource";+import { E2E_LINK } from "../utils/resource";-const { domain, url } = link;+const { domain, url } = E2E_LINK;describe.sequential("GET /links/{linkId}", async () => {const h = new IntegrationHarness();diff --git a/apps/web/tests/links/update-link.test.ts b/apps/web/tests/links/update-link.test.tsindex c80ef57c81..08e9f29f5b 100644--- a/apps/web/tests/links/update-link.test.ts+++ b/apps/web/tests/links/update-link.test.ts@@ -2,10 +2,10 @@ import { Link } from "@prisma/client";import { afterAll, describe, expect, test } from "vitest";import { randomId } from "../utils/helpers";import { IntegrationHarness } from "../utils/integration";-import { link } from "../utils/resource";+import { E2E_LINK } from "../utils/resource";import { expectedLink } from "../utils/schema";-const { domain, url } = link;+const { domain, url } = E2E_LINK;describe.sequential("PATCH /links/{linkId}", async () => {const h = new IntegrationHarness();diff --git a/apps/web/tests/links/upsert-link.test.ts b/apps/web/tests/links/upsert-link.test.tsindex a7b6e9c6c1..8a519d5072 100644--- a/apps/web/tests/links/upsert-link.test.ts+++ b/apps/web/tests/links/upsert-link.test.ts@@ -2,10 +2,10 @@ import { Link } from "@prisma/client";import { afterAll, describe, expect, test } from "vitest";import { randomId } from "../utils/helpers";import { IntegrationHarness } from "../utils/integration";-import { link } from "../utils/resource";+import { E2E_LINK } from "../utils/resource";import { expectedLink } from "../utils/schema";-const { domain } = link;+const { domain } = E2E_LINK;const url = `https://example.com/${randomId()}`;describe.sequential("PUT /links/upsert", async () => {diff --git a/apps/web/tests/tracks/track-lead.test.ts b/apps/web/tests/tracks/track-lead.test.tsindex 4dbe7b7c14..09a630da7c 100644--- a/apps/web/tests/tracks/track-lead.test.ts+++ b/apps/web/tests/tracks/track-lead.test.ts@@ -1,6 +1,6 @@import { TrackLeadResponse } from "@/lib/types";import { randomCustomer } from "tests/utils/helpers";-import { clickId } from "tests/utils/resource";+import { E2E_CLICK_ID } from "tests/utils/resource";import { describe, expect, test } from "vitest";import { IntegrationHarness } from "../utils/integration";@@ -13,7 +13,7 @@ describe("POST /track/lead", async () => {const response = await http.post<TrackLeadResponse>({path: "/track/lead",body: {- clickId,+ clickId: E2E_CLICK_ID,eventName: "Signup",customerId: customer.id,customerName: customer.name,@@ -24,13 +24,13 @@ describe("POST /track/lead", async () => {expect(response.status).toEqual(200);expect(response.data).toStrictEqual({- clickId,+ clickId: E2E_CLICK_ID,customerId: customer.id,customerName: customer.name,customerEmail: customer.email,customerAvatar: customer.avatar,click: {- id: clickId,+ id: E2E_CLICK_ID,},customer: {id: customer.id,@@ -45,7 +45,7 @@ describe("POST /track/lead", async () => {const response = await http.post<TrackLeadResponse>({path: "/track/lead",body: {- clickId,+ clickId: E2E_CLICK_ID,eventName: "Signup",customerId: customer.id,},diff --git a/apps/web/tests/tracks/track-sale.test.ts b/apps/web/tests/tracks/track-sale.test.tsindex e2e6aedae5..db572d4559 100644--- a/apps/web/tests/tracks/track-sale.test.ts+++ b/apps/web/tests/tracks/track-sale.test.ts@@ -1,6 +1,6 @@import { TrackLeadResponse, TrackSaleResponse } from "@/lib/types";import { randomCustomer, randomId } from "tests/utils/helpers";-import { clickId } from "tests/utils/resource";+import { E2E_CLICK_ID } from "tests/utils/resource";import { expect, test } from "vitest";import { IntegrationHarness } from "../utils/integration";@@ -20,7 +20,7 @@ test("POST /track/sale", async () => {const { data: lead } = await http.post<TrackLeadResponse>({path: "/track/lead",body: {- clickId,+ clickId: E2E_CLICK_ID,eventName: "Signup",customerId: customer.id,customerName: customer.name,diff --git a/apps/web/tests/utils/resource.ts b/apps/web/tests/utils/resource.tsindex b614ad3135..5a2bd100a7 100644--- a/apps/web/tests/utils/resource.ts+++ b/apps/web/tests/utils/resource.ts@@ -1,6 +1,8 @@-export const link = {+export const E2E_LINK = {domain: "dub.sh",url: "https://github.com/dubinc",};-export const clickId = "D1AwwfK6Nf6akeC2";+export const E2E_CLICK_ID = "D1AwwfK6Nf6akeC2";++export const E2E_WEBHOOK_ID = "wh_1C78FQhIDgpPYpZmyFolg4qJg";diff --git a/apps/web/ui/modals/link-builder/index.tsx b/apps/web/ui/modals/link-builder/index.tsxindex 7fb5d1f106..dd4d2985af 100644--- a/apps/web/ui/modals/link-builder/index.tsx+++ b/apps/web/ui/modals/link-builder/index.tsx@@ -240,15 +240,12 @@ function LinkBuilderInner({ref={formRef}onSubmit={handleSubmit(async (data) => {// @ts-ignore – exclude extra attributes from `data` object before sending to API- const { user, tags, tagId, webhooks, ...rest } = data;+ const { user, tags, tagId, ...rest } = data;const bodyData = {...rest,// Map tags to tagIdstagIds: tags.map(({ id }) => id),- // Map webhooks to webhookIds- webhookIds: webhooks.map(({ id }) => id),-// Manually reset empty strings to nullexpiredUrl: rest.expiredUrl || null,ios: rest.ios || null,diff --git a/apps/web/ui/modals/link-builder/webhook-select.tsx b/apps/web/ui/modals/link-builder/webhook-select.tsxindex 93ebae002b..d15334af64 100644--- a/apps/web/ui/modals/link-builder/webhook-select.tsx+++ b/apps/web/ui/modals/link-builder/webhook-select.tsx@@ -11,7 +11,7 @@ export function WebhookSelect() {const { watch, setValue } = useFormContext<LinkFormData>();useKeyboardShortcut("w", () => setIsOpen(true), { modal: true });- const webhooks = watch("webhooks") || [];+ const webhookIds = watch("webhookIds") as string[];const linkLevelWebhooks = availableWebhooks?.filter((webhook) =>webhook.triggers.includes("link.clicked"),@@ -29,11 +29,11 @@ export function WebhookSelect() {const selectedWebhooks = useMemo(() =>- webhooks- .map(({ id }) => options?.find(({ value }) => value === id)!)+ webhookIds+ .map((id) => options?.find(({ value }) => value === id)!).filter(Boolean),- [webhooks, options],+ [webhookIds, options],);const hasSelectedWebhooks = selectedWebhooks.length > 0;@@ -44,12 +44,7 @@ export function WebhookSelect() {selected={selectedWebhooks || []}setSelected={(webhooks) => {const selectedIds = webhooks.map(({ value }) => value);-- setValue(- "webhooks",- selectedIds.map((id) => availableWebhooks?.find((t) => t.id === id)),- { shouldDirty: true },- );+ setValue("webhookIds", selectedIds, { shouldDirty: true });}}options={options}icon={