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 tags endpoint + add withTags to links endpoint

Steven Teyc8279b509f4c
diff --git a/apps/docs/api-reference/endpoint/create-a-new-tag.mdx b/apps/docs/api-reference/endpoint/create-a-new-tag.mdx
index 2f9c21ec94..f2ad25d556 100644
--- a/apps/docs/api-reference/endpoint/create-a-new-tag.mdx
+++ b/apps/docs/api-reference/endpoint/create-a-new-tag.mdx
@@ -1,4 +1,4 @@
---
-openapi: post /projects/{projectSlug}/tags
+openapi: post /tags
description: Create a new tag for the authenticated project.
---
diff --git a/apps/docs/api-reference/endpoint/retrieve-a-list-of-tags.mdx b/apps/docs/api-reference/endpoint/retrieve-a-list-of-tags.mdx
index 2d5ca266e4..90479a7187 100644
--- a/apps/docs/api-reference/endpoint/retrieve-a-list-of-tags.mdx
+++ b/apps/docs/api-reference/endpoint/retrieve-a-list-of-tags.mdx
@@ -1,4 +1,4 @@
---
-openapi: get /projects/{projectSlug}/tags
+openapi: get /tags
description: Retrieve a list of tags for the authenticated project.
---
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 073bc887f3..e0578163bb 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -7,7 +7,7 @@ import { NextResponse } from "next/server";
// GET /api/links – get all user links
export const GET = withAuth(async ({ headers, searchParams, project }) => {
- const { domain, tagId, search, sort, page, userId, showArchived } =
+ const { domain, tagId, search, sort, page, userId, showArchived, withTags } =
searchParams as {
domain?: string;
tagId?: string;
@@ -16,6 +16,7 @@ export const GET = withAuth(async ({ headers, searchParams, project }) => {
page?: string;
userId?: string;
showArchived?: string;
+ withTags?: string;
};
const response = await getLinksForProject({
projectId: project.id,
@@ -26,6 +27,7 @@ export const GET = withAuth(async ({ headers, searchParams, project }) => {
page,
userId,
showArchived: showArchived === "true" ? true : false,
+ withTags: withTags === "true" ? true : false,
});
return NextResponse.json(response, {
headers,
diff --git a/apps/web/app/api/projects/[slug]/(old)/tags/[id]/route.ts b/apps/web/app/api/projects/[slug]/(old)/tags/[id]/route.ts
new file mode 100644
index 0000000000..884e2c2e5c
--- /dev/null
+++ b/apps/web/app/api/projects/[slug]/(old)/tags/[id]/route.ts
@@ -0,0 +1 @@
+export * from "../../../../../tags/[id]/route";
\ No newline at end of file
diff --git a/apps/web/app/api/projects/[slug]/(old)/tags/route.ts b/apps/web/app/api/projects/[slug]/(old)/tags/route.ts
new file mode 100644
index 0000000000..b69b53723e
--- /dev/null
+++ b/apps/web/app/api/projects/[slug]/(old)/tags/route.ts
@@ -0,0 +1 @@
+export * from "../../../../tags/route";
diff --git a/apps/web/app/api/route.ts b/apps/web/app/api/route.ts
index 1b554ba603..4835263ec2 100644
--- a/apps/web/app/api/route.ts
+++ b/apps/web/app/api/route.ts
@@ -131,6 +131,19 @@ export function GET(): NextResponse<OpenAPIV3.Document> {
enum: [true, false],
},
},
+ {
+ name: "withTags",
+ description:
+ "Only return links with tags. Defaults to `false` if not provided.",
+ in: "query",
+ required: false,
+ schema: {
+ description:
+ "Only return links with tags. Defaults to `false` if not provided.",
+ type: "boolean",
+ enum: [true, false],
+ },
+ },
],
responses: {
"200": {
diff --git a/apps/web/app/api/projects/[slug]/tags/[id]/route.ts b/apps/web/app/api/tags/[id]/route.ts
similarity index 100%
rename from apps/web/app/api/projects/[slug]/tags/[id]/route.ts
rename to apps/web/app/api/tags/[id]/route.ts
diff --git a/apps/web/app/api/projects/[slug]/tags/route.ts b/apps/web/app/api/tags/route.ts
similarity index 100%
rename from apps/web/app/api/projects/[slug]/tags/route.ts
rename to apps/web/app/api/tags/route.ts
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index a3ee021dc6..a6a5e0c1d1 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -34,6 +34,7 @@ export async function getLinksForProject({
page,
userId,
showArchived,
+ withTags,
}: {
projectId: string;
domain?: string;
@@ -43,6 +44,7 @@ export async function getLinksForProject({
page?: string;
userId?: string | null;
showArchived?: boolean;
+ withTags?: boolean;
}): Promise<LinkProps[]> {
const links = await prisma.link.findMany({
where: {
@@ -59,7 +61,7 @@ export async function getLinksForProject({
},
],
}),
- ...(tagId && { tagId }),
+ ...(tagId ? { tagId } : withTags ? { tagId: { not: null } } : {}),
...(userId && { userId }),
},
include: {
@@ -96,13 +98,15 @@ export async function getLinksCount({
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, withTags } =
+ searchParams as {
+ groupBy?: "domain" | "tagId";
+ search?: string;
+ domain?: string;
+ tagId?: string;
+ showArchived?: boolean;
+ withTags?: boolean;
+ };
if (groupBy) {
return await prisma.link.groupBy({
@@ -131,8 +135,8 @@ export async function getLinksCount({
groupBy !== "tagId" && {
tagId,
}),
- // for the "Tags" filter group, only count links that have a tagId
- ...(groupBy === "tagId" && {
+ // for the "Tags" filter group (or if withTags is true), only count links that have a tagId
+ ...((groupBy === "tagId" || withTags) && {
NOT: {
tagId: null,
},
@@ -162,7 +166,7 @@ export async function getLinksCount({
],
}),
...(domain && { domain }),
- ...(tagId && { tagId }),
+ ...(tagId ? { tagId } : withTags ? { tagId: { not: null } } : {}),
},
});
}
diff --git a/apps/web/lib/swr/use-tags.ts b/apps/web/lib/swr/use-tags.ts
index 7583d9b362..9721c0e037 100644
--- a/apps/web/lib/swr/use-tags.ts
+++ b/apps/web/lib/swr/use-tags.ts
@@ -7,7 +7,7 @@ export default function useTags() {
const { slug } = useParams() as { slug?: string };
const { data: tags, isValidating } = useSWR<TagProps[]>(
- slug && `/api/projects/${slug}/tags`,
+ slug && `/api/tags?projectSlug=${slug}`,
fetcher,
{
dedupingInterval: 30000,
diff --git a/apps/web/ui/links/link-filters.tsx b/apps/web/ui/links/link-filters.tsx
index ca10c363a1..03a8095c13 100644
--- a/apps/web/ui/links/link-filters.tsx
+++ b/apps/web/ui/links/link-filters.tsx
@@ -391,7 +391,7 @@ const TagPopover = ({ tag, count }: { tag: TagProps; count: number }) => {
e.stopPropagation();
e.preventDefault();
setProcessing(true);
- fetch(`/api/projects/${slug}/tags/${tag.id}`, {
+ fetch(`/api/tags/${tag.id}?projectSlug${slug}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
@@ -400,7 +400,7 @@ const TagPopover = ({ tag, count }: { tag: TagProps; count: number }) => {
}).then(async (res) => {
setProcessing(false);
if (res.ok) {
- await mutate(`/api/projects/${slug}/tags`);
+ await mutate(`/api/tags?projectSlug${slug}`);
toast.success("Tag updated");
} else {
toast.error("Something went wrong");
@@ -410,11 +410,11 @@ const TagPopover = ({ tag, count }: { tag: TagProps; count: number }) => {
const handleDelete = async () => {
setProcessing(true);
- fetch(`/api/projects/${slug}/tags/${tag.id}`, {
+ fetch(`/api/tags/${tag.id}?projectSlug${slug}`, {
method: "DELETE",
}).then(async (res) => {
if (res.ok) {
- await mutate(`/api/projects/${slug}/tags`);
+ await mutate(`/api/tags?projectSlug${slug}`);
toast.success("Tag deleted");
} else {
toast.error("Something went wrong");
diff --git a/apps/web/ui/modals/add-edit-link-modal/tags-section.tsx b/apps/web/ui/modals/add-edit-link-modal/tags-section.tsx
index 0556a42697..f72f1482c8 100644
--- a/apps/web/ui/modals/add-edit-link-modal/tags-section.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/tags-section.tsx
@@ -39,7 +39,7 @@ export default function TagsSection({
const createTag = async (tag: string) => {
setCreatingTag(true);
- fetch(`/api/projects/${slug}/tags`, {
+ fetch(`/api/tags?projectSlug${slug}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -47,7 +47,7 @@ export default function TagsSection({
body: JSON.stringify({ tag }),
}).then(async (res) => {
if (res.ok) {
- await mutate(`/api/projects/${slug}/tags`);
+ await mutate(`/api/tags?projectSlug${slug}`);
const newTag = await res.json();
setData({ ...data, tagId: newTag.id });
toast.success(`Successfully created tag!`);