Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions apps/web/app/(ee)/api/partners/links/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { parseRequestBody } from "@/lib/api/utils";
import { extractUtmParams } from "@/lib/api/utm/extract-utm-params";
import { withWorkspace } from "@/lib/auth";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { linkEventSchema } from "@/lib/zod/schemas/links";
import { LinkSchema } from "@/lib/zod/schemas/links";
import {
createPartnerLinkSchema,
retrievePartnerLinksSchema,
Expand Down Expand Up @@ -55,6 +55,9 @@ export const GET = withWorkspace(

const { links } = programEnrollment;

// TODO:
// Can we use LinkSchema here to be consistent with other links endpoints?

return NextResponse.json(z.array(ProgramPartnerLinkSchema).parse(links));
},
{
Expand Down Expand Up @@ -151,17 +154,20 @@ export const POST = withWorkspace(
});
}

const partnerLink = await createLink(link);
const response = await createLink(link);
const createdLink = LinkSchema.parse(response);

waitUntil(
sendWorkspaceWebhook({
trigger: "link.created",
workspace,
data: linkEventSchema.parse(partnerLink),
data: createdLink,
}),
);

return NextResponse.json(partnerLink, { status: 201 });
return NextResponse.json(createdLink, {
status: 201,
});
},
{
requiredPlan: ["advanced", "enterprise"],
Expand Down
15 changes: 9 additions & 6 deletions apps/web/app/(ee)/api/partners/links/upsert/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { extractUtmParams } from "@/lib/api/utm/extract-utm-params";
import { withWorkspace } from "@/lib/auth";
import { NewLinkProps } from "@/lib/types";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { linkEventSchema } from "@/lib/zod/schemas/links";
import { LinkSchema } from "@/lib/zod/schemas/links";
import { upsertPartnerLinkSchema } from "@/lib/zod/schemas/partners";
import { prisma } from "@dub/prisma";
import { constructURLFromUTMParams, deepEqual } from "@dub/utils";
Expand Down Expand Up @@ -178,15 +178,17 @@ export const PUT = withWorkspace(
updatedLink: processedLink,
});

const updatedLink = LinkSchema.parse(response);

waitUntil(
sendWorkspaceWebhook({
trigger: "link.updated",
workspace,
data: linkEventSchema.parse(response),
data: updatedLink,
}),
);

return NextResponse.json(response, {
return NextResponse.json(updatedLink, {
headers,
});
} catch (error) {
Expand Down Expand Up @@ -227,17 +229,18 @@ export const PUT = withWorkspace(
});
}

const partnerLink = await createLink(link);
const response = await createLink(link);
const createdLink = LinkSchema.parse(response);

waitUntil(
sendWorkspaceWebhook({
trigger: "link.created",
workspace,
data: linkEventSchema.parse(partnerLink),
data: createdLink,
}),
);

return NextResponse.json(partnerLink, {
return NextResponse.json(createdLink, {
headers,
});
}
Expand Down
13 changes: 8 additions & 5 deletions apps/web/app/api/links/[linkId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { verifyFolderAccess } from "@/lib/folder/permissions";
import { NewLinkProps } from "@/lib/types";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import {
linkEventSchema,
LinkSchema,
updateLinkBodySchemaExtended,
} from "@/lib/zod/schemas/links";
import { prisma } from "@dub/prisma";
Expand Down Expand Up @@ -62,7 +62,7 @@ export const GET = withWorkspace(
{ skipDecodeKey: true },
);

return NextResponse.json(response, { headers });
return NextResponse.json(LinkSchema.parse(response), { headers });
},
{
requiredPermissions: ["links.read"],
Expand Down Expand Up @@ -183,15 +183,17 @@ export const PATCH = withWorkspace(
updatedLink: processedLink,
});

const updatedLink = LinkSchema.parse(response);

waitUntil(
sendWorkspaceWebhook({
trigger: "link.updated",
workspace,
data: linkEventSchema.parse(response),
data: updatedLink,
}),
);

return NextResponse.json(response, {
return NextResponse.json(updatedLink, {
headers,
});
} catch (error) {
Expand Down Expand Up @@ -227,12 +229,13 @@ export const DELETE = withWorkspace(
}

const response = await deleteLink(link.id);
const deletedLink = LinkSchema.parse(response);

waitUntil(
sendWorkspaceWebhook({
trigger: "link.deleted",
workspace,
data: linkEventSchema.parse(response),
data: deletedLink,
}),
);

Expand Down
18 changes: 13 additions & 5 deletions apps/web/app/api/links/bulk/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import { NewLinkProps, ProcessedLinkProps } from "@/lib/types";
import {
bulkCreateLinksBodySchema,
bulkUpdateLinksBodySchema,
LinkSchema,
} from "@/lib/zod/schemas/links";
import { prisma } from "@dub/prisma";
import { R2_URL } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
import { z } from "zod";

// POST /api/links/bulk – bulk create up to 100 links
export const POST = withWorkspace(
Expand Down Expand Up @@ -248,9 +250,12 @@ export const POST = withWorkspace(
const validLinksResponse =
validLinks.length > 0 ? await bulkCreateLinks({ links: validLinks }) : [];

return NextResponse.json([...validLinksResponse, ...errorLinks], {
headers,
});
return NextResponse.json(
[...z.array(LinkSchema).parse(validLinksResponse), ...errorLinks],
{
headers,
},
);
},
{
requiredPermissions: ["links.write"],
Expand Down Expand Up @@ -421,7 +426,7 @@ export const PATCH = withWorkspace(
})),
);

const response =
const validLinksResponse =
validLinkIds.length > 0
? await bulkUpdateLinks({
linkIds: validLinkIds,
Expand Down Expand Up @@ -453,7 +458,10 @@ export const PATCH = withWorkspace(
})(),
);

return NextResponse.json([...response, ...errorLinks], { headers });
return NextResponse.json(
[...z.array(LinkSchema).parse(validLinksResponse), ...errorLinks],
{ headers },
);
},
{
requiredPermissions: ["links.write"],
Expand Down
7 changes: 5 additions & 2 deletions apps/web/app/api/links/info/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { transformLink } from "@/lib/api/links";
import { getLinkOrThrow } from "@/lib/api/links/get-link-or-throw";
import { withWorkspace } from "@/lib/auth";
import { verifyFolderAccess } from "@/lib/folder/permissions";
import { getLinkInfoQuerySchemaExtended } from "@/lib/zod/schemas/links";
import {
getLinkInfoQuerySchemaExtended,
LinkSchema,
} from "@/lib/zod/schemas/links";
import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";

Expand Down Expand Up @@ -61,7 +64,7 @@ export const GET = withWorkspace(
{ skipDecodeKey: true },
);

return NextResponse.json(response, {
return NextResponse.json(LinkSchema.parse(response), {
headers,
});
},
Expand Down
22 changes: 17 additions & 5 deletions apps/web/app/api/links/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import {
createLinkBodySchemaAsync,
getLinksQuerySchemaExtended,
linkEventSchema,
LinkSchema,
} from "@/lib/zod/schemas/links";
import { Folder } from "@dub/prisma/client";
import { LOCALHOST_IP } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
import { z } from "zod";

const AnonymousLinkSchema = LinkSchema.extend({
workspaceId: z.string().nullable(),
projectId: z.string().nullable(),
});

// GET /api/links – get all links for a workspace
export const GET = withWorkspace(
Expand Down Expand Up @@ -74,7 +80,7 @@ export const GET = withWorkspace(
searchMode: selectedFolder?.type === "mega" ? "exact" : "fuzzy",
});

return NextResponse.json(response, {
return NextResponse.json(z.array(LinkSchema).parse(response), {
headers,
});
},
Expand Down Expand Up @@ -125,17 +131,23 @@ export const POST = withWorkspace(
try {
const response = await createLink(link);

if (response.projectId && response.userId) {
const responseSchema = response.projectId
? LinkSchema
: AnonymousLinkSchema;

const createdLink = responseSchema.parse(response);

if (createdLink.projectId && createdLink.userId) {
waitUntil(
sendWorkspaceWebhook({
trigger: "link.created",
workspace,
data: linkEventSchema.parse(response),
data: createdLink as z.infer<typeof LinkSchema>,
}),
);
}

return NextResponse.json(response, {
return NextResponse.json(createdLink, {
headers,
});
} catch (error) {
Expand Down
23 changes: 16 additions & 7 deletions apps/web/app/api/links/upsert/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import { withWorkspace } from "@/lib/auth";
import { verifyFolderAccess } from "@/lib/folder/permissions";
import { NewLinkProps } from "@/lib/types";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import {
createLinkBodySchemaAsync,
linkEventSchema,
} from "@/lib/zod/schemas/links";
import { createLinkBodySchemaAsync, LinkSchema } from "@/lib/zod/schemas/links";
import { prisma } from "@dub/prisma";
import { deepEqual } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
Expand Down Expand Up @@ -153,15 +150,17 @@ export const PUT = withWorkspace(
updatedLink: processedLink,
});

const updatedLink = LinkSchema.parse(response);

waitUntil(
sendWorkspaceWebhook({
trigger: "link.updated",
workspace,
data: linkEventSchema.parse(response),
data: updatedLink,
}),
);

return NextResponse.json(response, {
return NextResponse.json(updatedLink, {
headers,
});
} catch (error) {
Expand All @@ -187,7 +186,17 @@ export const PUT = withWorkspace(

try {
const response = await createLink(link);
return NextResponse.json(response, { headers });
const createdLink = LinkSchema.parse(response);

waitUntil(
sendWorkspaceWebhook({
trigger: "link.created",
workspace,
data: createdLink,
}),
);

return NextResponse.json(createdLink, { headers });
} catch (error) {
throw new DubApiError({
code: "unprocessable_entity",
Expand Down
4 changes: 2 additions & 2 deletions apps/web/lib/api/links/complete-ab-tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getAnalytics } from "@/lib/analytics/get-analytics";
import { recordLink } from "@/lib/tinybird";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { ABTestVariantsSchema, linkEventSchema } from "@/lib/zod/schemas/links";
import { ABTestVariantsSchema, LinkSchema } from "@/lib/zod/schemas/links";
import { prisma } from "@dub/prisma";
import { Link } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
Expand Down Expand Up @@ -81,7 +81,7 @@ export async function completeABTests(link: Link) {
sendWorkspaceWebhook({
trigger: "link.updated",
workspace: response.project,
data: linkEventSchema.parse(response),
data: LinkSchema.parse(response),
}),
]),
);
Expand Down
4 changes: 3 additions & 1 deletion apps/web/lib/webhook/sample-events/lead-created.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@
"projectId": "clrei1gld0002vs9mzn93p8ik",
"testVariants": null,
"testCompletedAt": null,
"testStartedAt": null
"testStartedAt": null,
"linkRetentionCleanupDisabledAt": null,
"identifier": null
},
"metadata": null
}
4 changes: 3 additions & 1 deletion apps/web/lib/webhook/sample-events/link-clicked.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
"projectId": "clrei1gld0002vs9mzn93p8ik",
"testVariants": null,
"testCompletedAt": null,
"testStartedAt": null
"testStartedAt": null,
"linkRetentionCleanupDisabledAt": null,
"identifier": null
}
}
4 changes: 3 additions & 1 deletion apps/web/lib/webhook/sample-events/link-created.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@
"projectId": "cm022sis60003ikt1syy7kfhl",
"testVariants": null,
"testCompletedAt": null,
"testStartedAt": null
"testStartedAt": null,
"linkRetentionCleanupDisabledAt": null,
"identifier": null
}
4 changes: 3 additions & 1 deletion apps/web/lib/webhook/sample-events/link-deleted.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@
"projectId": "cm022sis60003ikt1syy7kfhl",
"testVariants": null,
"testCompletedAt": null,
"testStartedAt": null
"testStartedAt": null,
"linkRetentionCleanupDisabledAt": null,
"identifier": null
}
4 changes: 3 additions & 1 deletion apps/web/lib/webhook/sample-events/link-updated.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@
"projectId": "cm022sis60003ikt1syy7kfhl",
"testVariants": null,
"testCompletedAt": null,
"testStartedAt": null
"testStartedAt": null,
"linkRetentionCleanupDisabledAt": null,
"identifier": null
}
Loading