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.

document /links/count endpoint

Steven Tey5e8484ae3a7e
diff --git a/apps/docs/api-reference/endpoint/retrieve-number-of-links.mdx b/apps/docs/api-reference/endpoint/retrieve-number-of-links.mdx
new file mode 100644
index 0000000000..d258717ff8
--- /dev/null
+++ b/apps/docs/api-reference/endpoint/retrieve-number-of-links.mdx
@@ -0,0 +1,5 @@
+---
+openapi: get /links/count
+og:title: "Get the links count with the Dub.co API - API Reference"
+description: "Retrieve the number of links for the authenticated project. The provided query parameters allow filtering the returned links."
+---
diff --git a/apps/docs/mint.json b/apps/docs/mint.json
index cc1373c1a2..203dd52375 100644
--- a/apps/docs/mint.json
+++ b/apps/docs/mint.json
@@ -83,6 +83,7 @@
"api-reference/endpoint/edit-a-link",
"api-reference/endpoint/delete-a-link",
"api-reference/endpoint/retrieve-a-list-of-links",
+ "api-reference/endpoint/retrieve-number-of-links",
"api-reference/endpoint/bulk-create-links"
]
},
diff --git a/apps/web/app/api/links/count/route.ts b/apps/web/app/api/links/count/route.ts
index 31f8cd1fca..024bac52da 100644
--- a/apps/web/app/api/links/count/route.ts
+++ b/apps/web/app/api/links/count/route.ts
@@ -1,13 +1,13 @@
import { getLinksCount } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
-import { getLinksCountParamsSchema } from "@/lib/zod/schemas/links";
+import { getLinksCountQuerySchema } from "@/lib/zod/schemas/links";
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 }) => {
try {
- const { userId, ...params } = getLinksCountParamsSchema.parse(searchParams);
+ const { userId, ...params } = getLinksCountQuerySchema.parse(searchParams);
const count = await getLinksCount({
searchParams: params,
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 88943ce44e..763d4facdb 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -7,7 +7,7 @@ import {
import prisma from "@/lib/prisma";
import { formatRedisLink, redis } from "@/lib/upstash";
import {
- getLinksCountParamsSchema,
+ getLinksCountQuerySchema,
getLinksQuerySchema,
} from "@/lib/zod/schemas/links";
import {
@@ -116,19 +116,12 @@ export async function getLinksCount({
projectId,
userId,
}: {
- searchParams: z.infer<typeof getLinksCountParamsSchema>;
+ searchParams: z.infer<typeof getLinksCountQuerySchema>;
projectId: string;
userId?: string | null;
}) {
- let { groupBy, search, domain, tagId, showArchived, withTags } =
- searchParams as {
- groupBy?: "domain" | "tagId";
- search?: string;
- domain?: string;
- tagId?: string;
- showArchived?: boolean;
- withTags?: boolean;
- };
+ const { groupBy, search, domain, tagId, showArchived, withTags } =
+ searchParams;
const tagIds = tagId ? tagId.split(",") : [];
diff --git a/apps/web/lib/openapi/links/get-links-count.ts b/apps/web/lib/openapi/links/get-links-count.ts
new file mode 100644
index 0000000000..e2fd10696a
--- /dev/null
+++ b/apps/web/lib/openapi/links/get-links-count.ts
@@ -0,0 +1,27 @@
+import { openApiErrorResponses } from "@/lib/openapi/responses";
+import z from "@/lib/zod";
+import { getLinksCountQuerySchema, LinkSchema } from "@/lib/zod/schemas/links";
+import { ZodOpenApiOperationObject } from "zod-openapi";
+
+export const getLinksCount: ZodOpenApiOperationObject = {
+ operationId: "getLinksCount",
+ summary: "Retrieve the number of links",
+ description:
+ "Retrieve the number of links for the authenticated project. The provided query parameters allow filtering the returned links.",
+ requestParams: {
+ query: getLinksCountQuerySchema,
+ },
+ responses: {
+ "200": {
+ description: "A list of links",
+ content: {
+ "application/json": {
+ schema: z.array(LinkSchema),
+ },
+ },
+ },
+ ...openApiErrorResponses,
+ },
+ tags: ["Links"],
+ security: [{ bearerToken: [] }],
+};
diff --git a/apps/web/lib/openapi/links/index.ts b/apps/web/lib/openapi/links/index.ts
index 4448adc143..d97124e07d 100644
--- a/apps/web/lib/openapi/links/index.ts
+++ b/apps/web/lib/openapi/links/index.ts
@@ -6,12 +6,16 @@ import { deleteLink } from "./delete-link";
import { editLink } from "./edit-link";
import { getLinkInfo } from "./get-link-info";
import { getLinks } from "./get-links";
+import { getLinksCount } from "./get-links-count";
export const linksPaths: ZodOpenApiPathsObject = {
"/links": {
post: createLink,
get: getLinks,
},
+ "/links/count": {
+ get: getLinksCount,
+ },
"/links/info": {
get: getLinkInfo,
},
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 35f2651c21..1bd5e1312c 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -1,7 +1,7 @@
import z from "@/lib/zod";
import { booleanQuerySchema } from ".";
-export const getLinksQuerySchema = z.object({
+const LinksQuerySchema = {
projectSlug: z
.string()
.describe(
@@ -20,17 +20,6 @@ export const getLinksQuerySchema = z.object({
.describe(
"The search term to filter the links by. The search term will be matched against the short link slug and the destination url.",
),
- sort: z
- .enum(["createdAt", "clicks", "lastClicked"])
- .optional()
- .default("createdAt")
- .describe(
- "The field to sort the links by. The default is `createdAt`, and sort order is always descending.",
- ),
- page: z.coerce
- .number()
- .optional()
- .describe("The page number for pagination (each page contains 100 links)."),
userId: z.string().optional().describe("The user ID to filter the links by."),
showArchived: booleanQuerySchema
.optional()
@@ -44,6 +33,29 @@ export const getLinksQuerySchema = z.object({
.describe(
"Whether to include tags in the response. Defaults to `false` if not provided.",
),
+};
+
+export const getLinksQuerySchema = z.object({
+ ...LinksQuerySchema,
+ sort: z
+ .enum(["createdAt", "clicks", "lastClicked"])
+ .optional()
+ .default("createdAt")
+ .describe(
+ "The field to sort the links by. The default is `createdAt`, and sort order is always descending.",
+ ),
+ page: z.coerce
+ .number()
+ .optional()
+ .describe("The page number for pagination (each page contains 100 links)."),
+});
+
+export const getLinksCountQuerySchema = z.object({
+ ...LinksQuerySchema,
+ groupBy: z
+ .union([z.literal("domain"), z.literal("tagId")])
+ .optional()
+ .describe("The field to group the links by."),
});
export const getLinkInfoQuerySchema = z.object({
@@ -184,15 +196,6 @@ export const bulkCreateLinksBodySchema = z
.min(1, "No links created – you must provide at least one link.")
.max(100, "You can only create up to 100 links at a time.");
-export const getLinksCountParamsSchema = z.object({
- userId: z.string().optional(),
- tagId: z.string().optional(),
- domain: z.string().optional(),
- showArchived: booleanQuerySchema.optional(),
- search: z.string().optional(),
- groupBy: z.union([z.literal("domain"), z.literal("tagId")]).optional(),
-});
-
export const LinkSchema = z
.object({
id: z.string().describe("The unique ID of the short link."),