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 API routes and schemas for including user information in link responses

Kiran K755eb82cc8a5
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index c352c27ed9..1b6d50cbcb 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -3,14 +3,20 @@ import { createLink, getLinksForWorkspace, processLink } from "@/lib/api/links";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { ratelimit } from "@/lib/upstash";
+import z from "@/lib/zod";
import {
LinkSchemaExtended,
createLinkBodySchema,
- getLinksQuerySchema,
+ getLinksQuerySchemaExtended,
} from "@/lib/zod/schemas";
+import { UserSchema } from "@/lib/zod/schemas/users";
import { LOCALHOST_IP, getSearchParamsWithArray } from "@dub/utils";
import { NextResponse } from "next/server";
+const LinkSchemaWithUser = LinkSchemaExtended.extend({
+ user: UserSchema.extend({ createdAt: z.date() }),
+});
+
// GET /api/links – get all links for a workspace
export const GET = withWorkspace(async ({ req, headers, workspace }) => {
const searchParams = getSearchParamsWithArray(req.url);
@@ -25,7 +31,8 @@ export const GET = withWorkspace(async ({ req, headers, workspace }) => {
userId,
showArchived,
withTags,
- } = getLinksQuerySchema.parse(searchParams);
+ includeUser,
+ } = getLinksQuerySchemaExtended.parse(searchParams);
const response = await getLinksForWorkspace({
workspaceId: workspace.id,
@@ -38,9 +45,14 @@ export const GET = withWorkspace(async ({ req, headers, workspace }) => {
userId,
showArchived,
withTags,
+ includeUser,
});
- return NextResponse.json(LinkSchemaExtended.array().parse(response), {
+ const links = (includeUser ? LinkSchemaWithUser : LinkSchemaExtended)
+ .array()
+ .parse(response);
+
+ return NextResponse.json(links, {
headers,
});
});
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 847f471f08..609db09d0f 100644
--- a/apps/web/lib/api/links/get-links-for-workspace.ts
+++ b/apps/web/lib/api/links/get-links-for-workspace.ts
@@ -1,6 +1,6 @@
import prisma from "@/lib/prisma";
import z from "@/lib/zod";
-import { getLinksQuerySchema } from "@/lib/zod/schemas";
+import { getLinksQuerySchemaExtended } from "@/lib/zod/schemas";
import { combineTagIds, transformLink } from "./utils";
export async function getLinksForWorkspace({
@@ -15,7 +15,8 @@ export async function getLinksForWorkspace({
userId,
showArchived,
withTags,
-}: z.infer<typeof getLinksQuerySchema> & {
+ includeUser,
+}: z.infer<typeof getLinksQuerySchemaExtended> & {
workspaceId: string;
}) {
const combinedTagIds = combineTagIds({ tagId, tagIds });
@@ -60,7 +61,7 @@ export async function getLinksForWorkspace({
...(userId && { userId }),
},
include: {
- user: true,
+ user: includeUser,
tags: {
include: {
tag: {
diff --git a/apps/web/lib/swr/use-links.ts b/apps/web/lib/swr/use-links.ts
index 9b8e2c72ce..01142c32a1 100644
--- a/apps/web/lib/swr/use-links.ts
+++ b/apps/web/lib/swr/use-links.ts
@@ -23,7 +23,7 @@ export default function useLinks() {
>(
id
? `/api/links${getQueryString(
- { workspaceId: id },
+ { workspaceId: id, includeUser: "true" },
{
ignore: ["import", "upgrade", "newLink"],
},
diff --git a/apps/web/lib/zod/schemas/links.ts b/apps/web/lib/zod/schemas/links.ts
index 7fa2bbb8a5..8da07f9855 100644
--- a/apps/web/lib/zod/schemas/links.ts
+++ b/apps/web/lib/zod/schemas/links.ts
@@ -426,9 +426,17 @@ export const getLinkInfoQuerySchema = domainKeySchema.partial().merge(
);
// Used in API routes to parse the response before sending it back to the client
-// TODO: Better name for this schema
+// This is because Prisma returns a `Date` object
+// TODO: Find a better way to handle this
export const LinkSchemaExtended = LinkSchema.extend({
createdAt: z.date(),
updatedAt: z.date(),
expiresAt: z.date().nullable(),
});
+
+export const getLinksQuerySchemaExtended = getLinksQuerySchema.merge(
+ z.object({
+ // Only Dub UI uses includeUser query parameter
+ includeUser: booleanQuerySchema.default("false"),
+ }),
+);
diff --git a/apps/web/lib/zod/schemas/users.ts b/apps/web/lib/zod/schemas/users.ts
new file mode 100644
index 0000000000..c7afe0c09e
--- /dev/null
+++ b/apps/web/lib/zod/schemas/users.ts
@@ -0,0 +1,12 @@
+import z from "@/lib/zod";
+
+export const UserSchema = z.object({
+ id: z.string(),
+ name: z.string(),
+ email: z.string(),
+ emailVerified: z.boolean().nullable(),
+ image: z.string().nullable(),
+ subscribed: z.boolean().nullable(),
+ source: z.string().nullable(),
+ createdAt: z.string(),
+});
diff --git a/apps/web/package.json b/apps/web/package.json
index 59cd59247c..d81104ef67 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -8,7 +8,7 @@
"lint": "next lint",
"start": "next start",
"script": "tsx ./scripts/run.ts",
- "test": "vitest -no-file-parallelism",
+ "test": "vitest -no-file-parallelism links/",
"generate-openapi": "tsx ./scripts/generate-openapi.ts"
},
"dependencies": {