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.

handle params with array of values

Kiran K5e51daa60c70
diff --git a/apps/web/app/api/links/count/route.ts b/apps/web/app/api/links/count/route.ts
index c1edd6a8c0..f81a244df1 100644
--- a/apps/web/app/api/links/count/route.ts
+++ b/apps/web/app/api/links/count/route.ts
@@ -1,10 +1,12 @@
import { getLinksCount } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
import { getLinksCountQuerySchema } from "@/lib/zod/schemas";
+import { getSearchParamsWithArray } from "@dub/utils";
import { NextResponse } from "next/server";
// GET /api/links/count – get the number of links for a workspace
-export const GET = withAuth(async ({ headers, searchParams, workspace }) => {
+export const GET = withAuth(async ({ req, headers, workspace }) => {
+ const searchParams = getSearchParamsWithArray(req.url);
const { userId, ...params } = getLinksCountQuerySchema.parse(searchParams);
const count = await getLinksCount({
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index c7d8aaf82b..5a9ac6df55 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -3,11 +3,13 @@ import { createLink, getLinksForWorkspace, processLink } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
import { ratelimit } from "@/lib/upstash";
import { createLinkBodySchema, getLinksQuerySchema } from "@/lib/zod/schemas";
-import { LOCALHOST_IP } from "@dub/utils";
+import { LOCALHOST_IP, getSearchParamsWithArray } from "@dub/utils";
import { NextResponse } from "next/server";
// GET /api/links – get all links for a workspace
-export const GET = withAuth(async ({ headers, searchParams, workspace }) => {
+export const GET = withAuth(async ({ req, headers, workspace }) => {
+ const searchParams = getSearchParamsWithArray(req.url);
+
const {
domain,
tagId,
diff --git a/packages/utils/src/functions/urls.ts b/packages/utils/src/functions/urls.ts
index 72132bed13..88fbbd7d3d 100644
--- a/packages/utils/src/functions/urls.ts
+++ b/packages/utils/src/functions/urls.ts
@@ -28,6 +28,21 @@ export const getSearchParams = (url: string) => {
return params;
};
+export const getSearchParamsWithArray = (url: string) => {
+ let params = {} as Record<string, string | string[]>;
+
+ new URL(url).searchParams.forEach(function (val, key) {
+ if (key in params) {
+ const param = params[key];
+ Array.isArray(param) ? param.push(val) : (params[key] = [param, val]);
+ } else {
+ params[key] = val;
+ }
+ });
+
+ return params;
+};
+
export const getParamsFromURL = (url: string) => {
if (!url) return {};
try {