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.

format webhookIds inside linkCache.mset (via formatRedisLink)

Steven Tey4f61e5504ad5
diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.ts
index 4b6aedc80a..90e16ac055 100644
--- a/apps/web/app/api/webhooks/[webhookId]/route.ts
+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts
@@ -161,14 +161,7 @@ export const PATCH = withWorkspace(
},
});
- const formatedLinks = links.map((link) => {
- return {
- ...link,
- webhookIds: link.webhooks.map(({ webhookId }) => webhookId),
- };
- });
-
- await linkCache.mset(formatedLinks);
+ await linkCache.mset(links);
}
// If the webhook is being changed from workspace level to link level, set the cache
@@ -209,16 +202,7 @@ export const PATCH = withWorkspace(
},
});
- const formatedLinks = links.map((link) => {
- return {
- ...link,
- ...(link.webhooks.length > 0 && {
- webhookIds: link.webhooks.map(({ webhookId }) => webhookId),
- }),
- };
- });
-
- await linkCache.mset(formatedLinks);
+ await linkCache.mset(links);
})(),
);
@@ -291,15 +275,8 @@ export const DELETE = withWorkspace(
},
});
- const formatedLinks = links.map((link) => {
- return {
- ...link,
- webhookIds: link.webhooks.map((webhook) => webhook.webhookId),
- };
- });
-
await Promise.all([
- linkCache.mset(formatedLinks),
+ linkCache.mset(links),
webhookCache.delete(webhookId),
]);
})(),
diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.ts
index d2401ea3df..7417abbfd3 100644
--- a/apps/web/app/api/webhooks/route.ts
+++ b/apps/web/app/api/webhooks/route.ts
@@ -144,17 +144,8 @@ export const POST = withWorkspace(
},
});
- const formatedLinks = links.map((link) => {
- return {
- ...link,
- webhookIds: link.webhooks.map((webhook) => webhook.webhookId),
- };
- });
-
Promise.all([
- ...(links && links.length > 0
- ? [linkCache.mset(formatedLinks), []]
- : []),
+ ...(links && links.length > 0 ? [linkCache.mset(links), []] : []),
...(isLinkLevelWebhook(webhook) ? [webhookCache.set(webhook)] : []),
diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.ts
index 30800f2946..7d4af8543d 100644
--- a/apps/web/lib/api/links/cache.ts
+++ b/apps/web/lib/api/links/cache.ts
@@ -1,10 +1,11 @@
import { LinkProps, RedisLinkProps } from "@/lib/types";
import { formatRedisLink, redis } from "@/lib/upstash";
+import { ExpandedLink } from "./utils/transform-link";
const CACHE_EXPIRATION = 60 * 60 * 24; // 24 hours
class LinkCache {
- async mset(links: (LinkProps & { webhookIds?: string[] })[]) {
+ async mset(links: ExpandedLink[]) {
if (links.length === 0) {
return;
}
@@ -36,7 +37,7 @@ class LinkCache {
return await pipeline.exec();
}
- async set(link: LinkProps & { webhookIds?: string[] }) {
+ async set(link: ExpandedLink) {
const redisLink = await formatRedisLink(link);
const hasWebhooks = redisLink.webhookIds && redisLink.webhookIds.length > 0;
diff --git a/apps/web/lib/api/links/propagate-bulk-link-changes.ts b/apps/web/lib/api/links/propagate-bulk-link-changes.ts
index ebf090ab55..0a0d6f94da 100644
--- a/apps/web/lib/api/links/propagate-bulk-link-changes.ts
+++ b/apps/web/lib/api/links/propagate-bulk-link-changes.ts
@@ -1,6 +1,6 @@
import { recordLink } from "@/lib/tinybird";
import { linkCache } from "./cache";
-import { ExpandedLink } from "./utils";
+import { ExpandedLink } from "./utils/transform-link";
export async function propagateBulkLinkChanges(links: ExpandedLink[]) {
return await Promise.all([
diff --git a/apps/web/lib/api/links/utils/transform-link.ts b/apps/web/lib/api/links/utils/transform-link.ts
index 039a6ff6b4..3f41872fa7 100644
--- a/apps/web/lib/api/links/utils/transform-link.ts
+++ b/apps/web/lib/api/links/utils/transform-link.ts
@@ -1,5 +1,7 @@
import { Dashboard, Link, Tag } from "@dub/prisma/client";
+// used in API (e.g. transformLink)
+// TODO: standardize this with ExpandedLinkProps
export type ExpandedLink = Link & {
tags?: { tag: Pick<Tag, "id" | "name" | "color"> }[];
webhooks?: { webhookId: string }[];
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index f513bf0edb..fe284bbf5a 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -49,6 +49,8 @@ import {
export type LinkProps = Link;
+// used on client side (e.g. Link builder)
+// TODO: standardize this with ExpandedLink
export interface ExpandedLinkProps extends LinkProps {
tags: TagProps[];
webhookIds: string[];
diff --git a/apps/web/lib/upstash/format-redis-link.ts b/apps/web/lib/upstash/format-redis-link.ts
index dbfa7f0737..6c20c20bb5 100644
--- a/apps/web/lib/upstash/format-redis-link.ts
+++ b/apps/web/lib/upstash/format-redis-link.ts
@@ -1,10 +1,9 @@
import { isIframeable } from "@dub/utils";
-import { LinkProps, RedisLinkProps } from "../types";
+import { ExpandedLink } from "../api/links/utils/transform-link";
+import { RedisLinkProps } from "../types";
export async function formatRedisLink(
- link: LinkProps & {
- webhooks?: { webhookId: string }[];
- },
+ link: ExpandedLink,
): Promise<RedisLinkProps> {
const {
id,