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.

update the pagination params

Kiran K975673eaa06d
diff --git a/apps/web/app/api/domains/route.ts b/apps/web/app/api/domains/route.ts
index c9d4234cc4..dac4b79408 100644
--- a/apps/web/app/api/domains/route.ts
+++ b/apps/web/app/api/domains/route.ts
@@ -16,7 +16,7 @@ import { z } from "zod";
export const GET = withWorkspace(
async ({ req, workspace }) => {
const searchParams = getSearchParams(req.url);
- const { search, archived, page } =
+ const { search, archived, page, pageSize } =
getDomainsQuerySchema.parse(searchParams);
const domains = await prisma.domain.findMany({
@@ -29,8 +29,8 @@ export const GET = withWorkspace(
},
}),
},
- take: 50,
- skip: page ? page * 100 : 0,
+ take: pageSize,
+ skip: (page - 1) * pageSize,
});
return NextResponse.json(z.array(DomainSchema).parse(domains));
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index b64430c4be..ee13311683 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -24,6 +24,7 @@ export const GET = withWorkspace(
search,
sort,
page,
+ pageSize,
userId,
showArchived,
withTags,
@@ -42,6 +43,7 @@ export const GET = withWorkspace(
search,
sort,
page,
+ pageSize,
userId,
showArchived,
withTags,
diff --git a/apps/web/lib/api/links/get-links-for-workspace.ts b/apps/web/lib/api/links/get-links-for-workspace.ts
index 5e3148a64a..c75440525c 100644
--- a/apps/web/lib/api/links/get-links-for-workspace.ts
+++ b/apps/web/lib/api/links/get-links-for-workspace.ts
@@ -12,6 +12,7 @@ export async function getLinksForWorkspace({
search,
sort = "createdAt",
page,
+ pageSize,
userId,
showArchived,
withTags,
@@ -77,9 +78,9 @@ export async function getLinksForWorkspace({
orderBy: {
[sort]: "desc",
},
- take: 100,
+ take: pageSize,
...(page && {
- skip: (page - 1) * 100,
+ skip: (page - 1) * pageSize,
}),
});
diff --git a/apps/web/lib/zod/schemas/domains.ts b/apps/web/lib/zod/schemas/domains.ts
index 56d2c85508..22094095ff 100644
--- a/apps/web/lib/zod/schemas/domains.ts
+++ b/apps/web/lib/zod/schemas/domains.ts
@@ -1,5 +1,5 @@
import z from "@/lib/zod";
-import { booleanQuerySchema } from "./misc";
+import { booleanQuerySchema, paginationQuerySchema } from "./misc";
import { parseUrlSchemaAllowEmpty } from "./utils";
export const DomainSchema = z.object({
@@ -38,26 +38,20 @@ export const DomainSchema = z.object({
updatedAt: z.date().describe("The date the domain was last updated."),
});
-export const getDomainsQuerySchema = z.object({
- archived: booleanQuerySchema
- .optional()
- .default("false")
- .describe(
- "Whether to include archived domains in the response. Defaults to `false` if not provided.",
- ),
- search: z
- .string()
- .optional()
- .describe("The search term to filter the domains by."),
- page: z.coerce
- .number()
- .int()
- .nonnegative()
- .optional()
- .describe(
- "The page number for pagination (each page contains 50 domains).",
- ),
-});
+export const getDomainsQuerySchema = z
+ .object({
+ archived: booleanQuerySchema
+ .optional()
+ .default("false")
+ .describe(
+ "Whether to include archived domains in the response. Defaults to `false` if not provided.",
+ ),
+ search: z
+ .string()
+ .optional()
+ .describe("The search term to filter the domains by."),
+ })
+ .merge(paginationQuerySchema);
export const getDomainsCountQuerySchema = getDomainsQuerySchema.omit({
page: true,
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 4fb5d1c3c3..ea82450dc3 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -5,7 +5,7 @@ import {
formatDate,
validDomainRegex,
} from "@dub/utils";
-import { booleanQuerySchema } from "./misc";
+import { booleanQuerySchema, paginationQuerySchema } from "./misc";
import { TagSchema } from "./tags";
import {
parseDateSchema,
@@ -80,16 +80,8 @@ export const getLinksQuerySchema = LinksQuerySchema.merge(
.describe(
"The field to sort the links by. The default is `createdAt`, and sort order is always descending.",
),
- page: z.coerce
- .number()
- .int()
- .nonnegative()
- .optional()
- .describe(
- "The page number for pagination (each page contains 100 links).",
- ),
}),
-);
+).merge(paginationQuerySchema);
export const getLinksCountQuerySchema = LinksQuerySchema.merge(
z.object({
@@ -101,7 +93,7 @@ export const getLinksCountQuerySchema = LinksQuerySchema.merge(
);
export const linksExportQuerySchema = getLinksQuerySchema
- .omit({ page: true })
+ .omit({ page: true, pageSize: true })
.merge(
z.object({
columns: z
diff --git a/apps/web/lib/zod/schemas/misc.ts b/apps/web/lib/zod/schemas/misc.ts
index c203f53c71..bfa81803f1 100644
--- a/apps/web/lib/zod/schemas/misc.ts
+++ b/apps/web/lib/zod/schemas/misc.ts
@@ -1,5 +1,6 @@
import { plans, roles } from "@/lib/types";
import z from "@/lib/zod";
+import { PAGINATION_LIMIT } from "@dub/utils";
export const planSchema = z.enum(plans).describe("The plan of the workspace.");
@@ -14,3 +15,28 @@ export const booleanQuerySchema = z
.openapi({
type: "boolean",
});
+
+// Pagination
+export const paginationQuerySchema = z.object({
+ page: z.coerce
+ .number({ invalid_type_error: "Page must be a number." })
+ .positive({ message: "Page must be greater than 0." })
+ .optional()
+ .default(1)
+ .describe("The page number for pagination.")
+ .openapi({
+ example: 1,
+ }),
+ pageSize: z.coerce
+ .number({ invalid_type_error: "Page size must be a number." })
+ .positive({ message: "Page size must be greater than 0." })
+ .max(PAGINATION_LIMIT, {
+ message: `Max page size is ${PAGINATION_LIMIT}.`,
+ })
+ .optional()
+ .default(PAGINATION_LIMIT)
+ .describe("The number of items per page.")
+ .openapi({
+ example: 50,
+ }),
+});