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.

More types work

Tim Wilsonc40d2893ca3d
diff --git a/apps/web/app/api/cron/import/csv/utils.ts b/apps/web/app/api/cron/import/csv/utils.ts
index d39283efbb..588e210d95 100644
--- a/apps/web/app/api/cron/import/csv/utils.ts
+++ b/apps/web/app/api/cron/import/csv/utils.ts
@@ -1,7 +1,7 @@
import { bulkCreateLinks, processLink } from "@/lib/api/links";
import { qstash } from "@/lib/cron";
import prisma from "@/lib/prisma";
-import { LinkWithTagIdsProps, ProjectProps } from "@/lib/types";
+import { NewLinkProps, ProcessedLinkProps, ProjectProps } from "@/lib/types";
import { redis } from "@/lib/upstash";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { sendEmail } from "emails";
@@ -19,11 +19,7 @@ export const importLinksFromCSV = async ({
count?: number;
}) => {
const [links, totalCount] = await Promise.all([
- redis.lrange<LinkWithTagIdsProps>(
- `import:csv:${projectId}`,
- count,
- count + 100,
- ),
+ redis.lrange<NewLinkProps>(`import:csv:${projectId}`, count, count + 100),
redis.llen(`import:csv:${projectId}`),
]);
@@ -40,10 +36,12 @@ export const importLinksFromCSV = async ({
);
const validLinks = processedLinks
- .filter(({ error }) => !error)
+ .filter(({ error }) => error != null)
.map(({ link }) => link);
- const importedLinks = await bulkCreateLinks({ links: validLinks });
+ const importedLinks = await bulkCreateLinks({
+ links: validLinks as ProcessedLinkProps[],
+ });
count += importedLinks.length;
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 070dee26df..7d93ea890a 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -2,7 +2,7 @@ import { DubApiError, exceededLimitError } from "@/lib/api/errors";
import { bulkCreateLinks, combineTagIds, processLink } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
import prisma from "@/lib/prisma";
-import { LinkWithTagIdsProps } from "@/lib/types";
+import { ProcessedLinkProps } from "@/lib/types";
import { bulkCreateLinksBodySchema } from "@/lib/zod/schemas/links";
import { NextResponse } from "next/server";
@@ -51,7 +51,7 @@ export const POST = withAuth(
const processedLinks = await Promise.all(
links.map(async (link) =>
processLink({
- payload: link as LinkWithTagIdsProps,
+ payload: link,
project,
userId: session.user.id,
bulk: true,
@@ -60,7 +60,7 @@ export const POST = withAuth(
);
let validLinks = processedLinks
- .filter(({ error }) => !error)
+ .filter(({ error }) => error != null)
.map(({ link }) => link);
let errorLinks = processedLinks
@@ -101,7 +101,9 @@ export const POST = withAuth(
});
const validLinksResponse =
- validLinks.length > 0 ? await bulkCreateLinks({ links: validLinks }) : [];
+ validLinks.length > 0
+ ? await bulkCreateLinks({ links: validLinks as ProcessedLinkProps[] })
+ : [];
return NextResponse.json([...validLinksResponse, ...errorLinks], {
headers,
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index f697ed5be8..9881ee8612 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -2,7 +2,6 @@ import { DubApiError, ErrorCodes } from "@/lib/api/errors";
import { addLink, getLinksForProject, processLink } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
import { qstash } from "@/lib/cron";
-import { ProcessedLinkProps } from "@/lib/types";
import { ratelimit } from "@/lib/upstash";
import {
createLinkBodySchema,
@@ -77,14 +76,14 @@ export const POST = withAuth(
...(session && { userId: session.user.id }),
});
- if (error) {
+ if (error != null) {
throw new DubApiError({
code: code as ErrorCodes,
message: error,
});
}
- const response = await addLink(link as ProcessedLinkProps);
+ const response = await addLink(link);
await qstash.publishJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/event`,
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index f235523bdb..d6609096a6 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -304,12 +304,20 @@ export async function processLink<T extends Record<string, any>>({
userId?: string;
bulk?: boolean;
skipKeyChecks?: boolean;
-}): Promise<{
- link: NewLinkProps & T;
- error?: string | null;
- code?: string;
- status?: number;
-}> {
+}): Promise<
+ | {
+ link: NewLinkProps & T;
+ error: string;
+ code?: string;
+ status?: number;
+ }
+ | {
+ link: ProcessedLinkProps & T;
+ error: null;
+ code?: never;
+ status?: never;
+ }
+> {
let {
domain,
key,
@@ -529,13 +537,10 @@ export async function processLink<T extends Record<string, any>>({
domain,
key,
url: processedUrl,
+ userId: userId || null,
// make sure projectId is set to the current project
projectId: project?.id || null,
- // if userId is passed, set it (we don't change the userId if it's already set, e.g. when editing a link)
- ...(userId && {
- userId,
- }),
- } as NewLinkProps & T,
+ },
error: null,
};
}