The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
fix: allow clearing link externalId with empty string or null
Cursor Agent08cb0a6f47d8
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.tsindex d9e60df919..bfa5b0344d 100644--- a/apps/web/lib/zod/schemas/links.ts+++ b/apps/web/lib/zod/schemas/links.ts@@ -332,13 +332,15 @@ export const createLinkBodySchema = z.object({),externalId: z.string()- .min(1).max(255)- // remove `ext_` prefix if user passes it- .transform((v) => (v?.startsWith("ext_") ? v.slice(4) : v))+ // remove `ext_` prefix if user passes it; empty string clears the field+ .transform((v) => {+ const stripped = v?.startsWith("ext_") ? v.slice(4) : v;+ return stripped === "" ? null : stripped;+ }).nullish().describe(- "The ID of the link in your database. If set, it can be used to identify the link in future API requests (must be prefixed with 'ext_' when passed as a query parameter). This key is unique across your workspace.",+ "The ID of the link in your database. If set, it can be used to identify the link in future API requests (must be prefixed with 'ext_' when passed as a query parameter). This key is unique across your workspace. Pass `null` or an empty string to remove it.",).meta({ example: "123456" }),tenantId: zdiff --git a/apps/web/playwright/api/links/links.spec.ts b/apps/web/playwright/api/links/links.spec.tsnew file mode 100644index 0000000000..2fab2a9ae8--- /dev/null+++ b/apps/web/playwright/api/links/links.spec.ts@@ -0,0 +1,86 @@+import { expect } from "@playwright/test";+import { nanoid } from "@dub/utils";+import { test, type ApiClient } from "../fixtures";+import { TEST_WORKSPACE } from "../setup-test-workspace";++type Link = {+ id: string;+ url: string;+ domain: string;+ externalId: string | null;+};++const domain = TEST_WORKSPACE.program.domain;++async function createLink(+ api: ApiClient,+ overrides: Record<string, unknown> = {},+) {+ return api.post<Link>("/api/links", {+ url: `https://example.com/${nanoid()}`,+ domain,+ key: nanoid(12),+ ...overrides,+ });+}++async function deleteLink(api: ApiClient, id: string | undefined) {+ if (!id) return;+ await api.delete(`/api/links/${id}`);+}++test("PATCH /links/:id – clear externalId with empty string", async ({+ api,+}) => {+ let linkId: string | undefined;+ const externalId = nanoid(12);++ try {+ const { status: createStatus, data: created } = await createLink(api, {+ externalId,+ });+ linkId = created.id;++ expect(createStatus).toEqual(200);+ expect(created.externalId).toEqual(externalId);++ const { status, data: updated } = await api.patch<Link>(+ `/api/links/${created.id}`,+ { externalId: "" },+ );++ expect(status).toEqual(200);+ expect(updated.externalId).toBeNull();++ const { status: getStatus, data: fetched } = await api.get<Link>(+ `/api/links/${created.id}`,+ );++ expect(getStatus).toEqual(200);+ expect(fetched.externalId).toBeNull();+ } finally {+ await deleteLink(api, linkId);+ }+});++test("PATCH /links/:id – clear externalId with null", async ({ api }) => {+ let linkId: string | undefined;+ const externalId = nanoid(12);++ try {+ const { data: created } = await createLink(api, { externalId });+ linkId = created.id;++ expect(created.externalId).toEqual(externalId);++ const { status, data: updated } = await api.patch<Link>(+ `/api/links/${created.id}`,+ { externalId: null },+ );++ expect(status).toEqual(200);+ expect(updated.externalId).toBeNull();+ } finally {+ await deleteLink(api, linkId);+ }+});diff --git a/apps/web/tests/links/update-link.test.ts b/apps/web/tests/links/update-link.test.tsindex 3dd59b1e7f..c1601096e0 100644--- a/apps/web/tests/links/update-link.test.ts+++ b/apps/web/tests/links/update-link.test.ts@@ -179,6 +179,43 @@ describe.sequential("PATCH /links/{linkId}", async () => {expect(linkUpdated.url).toEqual("https://github.com/dubinc");});++ test("remove externalId with empty string", async () => {+ const { status, data: updatedLink } = await http.patch<Link>({+ path: `/links/${link.id}`,+ body: {+ externalId: "",+ },+ });++ expect(status).toEqual(200);+ expect(updatedLink.externalId).toEqual(null);++ const { data: fetchedLink } = await http.get<Link>({+ path: `/links/${link.id}`,+ });++ expect(fetchedLink.externalId).toEqual(null);+ });++ test("remove externalId with null", async () => {+ // Re-set an externalId first so we can clear it again+ const newExternalId = randomId();+ await http.patch<Link>({+ path: `/links/${link.id}`,+ body: { externalId: newExternalId },+ });++ const { status, data: updatedLink } = await http.patch<Link>({+ path: `/links/${link.id}`,+ body: {+ externalId: null,+ },+ });++ expect(status).toEqual(200);+ expect(updatedLink.externalId).toEqual(null);+ });});describe.sequential("PATCH /links/{linkId} - UTM parameters", async () => {diff --git a/apps/web/ui/links/link-builder/use-link-builder-submit.tsx b/apps/web/ui/links/link-builder/use-link-builder-submit.tsxindex caaeb4cf0c..7fb337d95b 100644--- a/apps/web/ui/links/link-builder/use-link-builder-submit.tsx+++ b/apps/web/ui/links/link-builder/use-link-builder-submit.tsx@@ -37,6 +37,8 @@ export function useLinkBuilderSubmit({expiredUrl: rest.expiredUrl || null,ios: rest.ios || null,android: rest.android || null,+ externalId: rest.externalId || null,+ tenantId: rest.tenantId || null,// Create partner links...(partnerId