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.

Trying to clean up types

Tim Wilson4cb5693e417c
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 3d4daeaf17..264f6b865f 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -3,6 +3,7 @@ import { deleteLink, editLink, processLink } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
import { qstash } from "@/lib/cron";
import prisma from "@/lib/prisma";
+import { NewLinkProps } from "@/lib/types";
import { updateLinkBodySchema } from "@/lib/zod/schemas/links";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { NextResponse } from "next/server";
@@ -56,6 +57,11 @@ export const PUT = withAuth(async ({ req, headers, project, link }) => {
const updatedLink = {
...link,
+ expiresAt:
+ link.expiresAt instanceof Date
+ ? link.expiresAt.toISOString()
+ : link.expiresAt,
+ geo: link.geo as NewLinkProps["geo"],
...body,
};
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 4b144c6436..f697ed5be8 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -2,7 +2,7 @@ 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 { LinkWithTagIdsProps } from "@/lib/types";
+import { ProcessedLinkProps } from "@/lib/types";
import { ratelimit } from "@/lib/upstash";
import {
createLinkBodySchema,
@@ -84,7 +84,7 @@ export const POST = withAuth(
});
}
- const response = await addLink(link);
+ const response = await addLink(link as ProcessedLinkProps);
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 8b283b84a4..68afd5c352 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -28,8 +28,8 @@ import { checkIfKeyExists, getRandomKey } from "../planetscale";
import { recordLink } from "../tinybird";
import {
LinkProps,
- LinkWithTagIdsProps,
NewLinkProps,
+ ProcessedLinkProps,
ProjectProps,
RedisLinkProps,
} from "../types";
@@ -530,7 +530,7 @@ export async function processLink({
...(userId && {
userId,
}),
- },
+ } as ProcessedLinkProps,
error: null,
};
}
@@ -549,7 +549,7 @@ export function combineTagIds({
return tagId ? [tagId] : [];
}
-export async function addLink(link: LinkWithTagIdsProps) {
+export async function addLink(link: ProcessedLinkProps) {
let { domain, key, url, expiresAt, title, description, image, proxy, geo } =
link;
const uploadedImage = image && image.startsWith("data:image") ? true : false;
@@ -613,7 +613,7 @@ export async function addLink(link: LinkWithTagIdsProps) {
export async function bulkCreateLinks({
links,
}: {
- links: LinkWithTagIdsProps[];
+ links: ProcessedLinkProps[];
}) {
if (links.length === 0) return [];
@@ -736,7 +736,7 @@ export async function editLink({
}: {
domain?: string;
key: string;
- updatedLink: LinkWithTagIdsProps;
+ updatedLink: LinkProps & ProcessedLinkProps;
}) {
let {
id,
diff --git a/apps/web/lib/auth/index.ts b/apps/web/lib/auth/index.ts
index a5e5bd192d..f0e3824af8 100644
--- a/apps/web/lib/auth/index.ts
+++ b/apps/web/lib/auth/index.ts
@@ -10,7 +10,7 @@ import { Link as LinkProps } from "@prisma/client";
import { createHash } from "crypto";
import { getServerSession } from "next-auth/next";
import { exceededLimitError } from "../api/errors";
-import { PlanProps, ProjectProps, TagProps } from "../types";
+import { PlanProps, ProjectProps } from "../types";
import { ratelimit } from "../upstash";
import { authOptions } from "./options";
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 515db71635..0196ed39b9 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -9,10 +9,6 @@ export interface LinkWithTagsProps extends LinkProps {
tags: TagProps[];
}
-export interface LinkWithTagIdsProps extends LinkProps {
- tagIds: string[];
-}
-
export interface SimpleLinkProps {
domain: string;
key: string;
@@ -161,6 +157,10 @@ export interface SAMLProviderProps {
export type NewLinkProps = z.infer<typeof createLinkBodySchema>;
+type ProcessedLinkOverrides = "domain" | "key" | "url" | "projectId" | "userId";
+export type ProcessedLinkProps = Omit<NewLinkProps, ProcessedLinkOverrides> &
+ Pick<LinkProps, ProcessedLinkOverrides>;
+
export const plans = [
"free",
"pro",
diff --git a/packages/utils/src/functions/truncate.ts b/packages/utils/src/functions/truncate.ts
index eca1877a1e..e05c7c9094 100644
--- a/packages/utils/src/functions/truncate.ts
+++ b/packages/utils/src/functions/truncate.ts
@@ -1,4 +1,4 @@
-export const truncate = (str: string | null, length: number) => {
+export const truncate = (str: string | null | undefined, length: number) => {
if (!str || str.length <= length) return str;
return `${str.slice(0, length - 3)}...`;
};