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.

wip

Kiran Kef8f1db237d1
diff --git a/apps/web/app/api/links/count/route.ts b/apps/web/app/api/links/count/route.ts
index 4c0b2de47a..3f287d7f88 100644
--- a/apps/web/app/api/links/count/route.ts
+++ b/apps/web/app/api/links/count/route.ts
@@ -1,16 +1,25 @@
import { getLinksCount } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
+import { ErrorResponse, handleApiError } from "@/lib/errors";
+import { GetLinksCountParamsSchema } from "@/lib/zod";
import { NextResponse } from "next/server";
// GET /api/links/count – get the number of links for a project
export const GET = withAuth(async ({ headers, searchParams, project }) => {
- const { userId } = searchParams;
- const count = await getLinksCount({
- searchParams,
- projectId: project.id,
- userId,
- });
- return NextResponse.json(count, {
- headers,
- });
+ try {
+ const { userId, ...params } = GetLinksCountParamsSchema.parse(searchParams);
+
+ const count = await getLinksCount({
+ searchParams: params,
+ projectId: project.id,
+ userId,
+ });
+
+ return NextResponse.json(count, {
+ headers,
+ });
+ } catch (err) {
+ const { error, status } = handleApiError(err);
+ return NextResponse.json<ErrorResponse>({ error }, { headers, status });
+ }
});
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 202aea7edf..80a175f465 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -23,6 +23,7 @@ import cloudinary from "cloudinary";
import { LinkProps, ProjectProps, RedisLinkProps } from "../types";
import { Session } from "../auth";
import { getLinkViaEdge } from "../planetscale";
+import { GetLinksCountParams } from "../zod";
export async function getLinksForProject({
projectId,
@@ -91,17 +92,11 @@ export async function getLinksCount({
projectId,
userId,
}: {
- searchParams: Record<string, string>;
+ searchParams: GetLinksCountParams;
projectId: string;
userId?: string | null;
}) {
- let { groupBy, search, domain, tagId, showArchived } = searchParams as {
- groupBy?: "domain" | "tagId";
- search?: string;
- domain?: string;
- tagId?: string;
- showArchived?: boolean;
- };
+ let { groupBy, search, domain, tagId, showArchived } = searchParams;
if (groupBy) {
return await prisma.link.groupBy({
diff --git a/apps/web/lib/zod/index.ts b/apps/web/lib/zod/index.ts
index ae0541a9c7..382e98aa66 100644
--- a/apps/web/lib/zod/index.ts
+++ b/apps/web/lib/zod/index.ts
@@ -3,4 +3,15 @@ import { extendZodWithOpenApi } from "zod-openapi";
extendZodWithOpenApi(z);
+export const GetLinksCountParamsSchema = z.object({
+ userId: z.string().optional(),
+ tagId: z.string().optional(),
+ domain: z.string().optional(),
+ showArchived: z.coerce.boolean().optional(),
+ search: z.string().optional(),
+ groupBy: z.union([z.literal("domain"), z.literal("tagId")]).optional(),
+});
+
+export type GetLinksCountParams = z.infer<typeof GetLinksCountParamsSchema>;
+
export default z;