The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Convert PUT → PATCH for link endpoint
Steven Teyb94c91aad40c
diff --git a/apps/docs/api-reference/endpoint/edit-a-link.mdx b/apps/docs/api-reference/endpoint/edit-a-link.mdxindex 1f6f42cefe..d0121043c5 100644--- a/apps/docs/api-reference/endpoint/edit-a-link.mdx+++ b/apps/docs/api-reference/endpoint/edit-a-link.mdx@@ -1,4 +1,4 @@----openapi: put /links/{linkId}+openapi: patch /links/{linkId}og:title: "Edit a link with the Dub.co API - API Reference"---diff --git a/apps/web/app/api/links/[linkId]/archive/route.ts b/apps/web/app/api/links/[linkId]/archive/route.tsdeleted file mode 100644index b89fc3b48b..0000000000--- a/apps/web/app/api/links/[linkId]/archive/route.ts+++ /dev/null@@ -1,15 +0,0 @@-import { archiveLink } from "@/lib/api/links";-import { withWorkspace } from "@/lib/auth";-import { NextResponse } from "next/server";--// POST /api/links/[linkId]/archive – archive a link-export const POST = withWorkspace(async ({ headers, link }) => {- const response = await archiveLink({ linkId: link!.id, archived: true });- return NextResponse.json(response, { headers });-});--// DELETE /api/links/[linkId]/archive – unarchive a link-export const DELETE = withWorkspace(async ({ headers, link }) => {- const response = await archiveLink({ linkId: link!.id, archived: false });- return NextResponse.json(response, { headers });-});diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.tsindex 5cc48b2c7b..91fae0367c 100644--- a/apps/web/app/api/links/[linkId]/route.ts+++ b/apps/web/app/api/links/[linkId]/route.ts@@ -45,66 +45,71 @@ export const GET = withWorkspace(async ({ headers, link }) => {return NextResponse.json(response, { headers });});-// PUT /api/links/[linkId] – update a link-export const PUT = withWorkspace(async ({ req, headers, workspace, link }) => {- if (!link) {- throw new DubApiError({- code: "not_found",- message: "Link not found.",- });- }+// PATCH /api/links/[linkId] – update a link+export const PATCH = withWorkspace(+ async ({ req, headers, workspace, link }) => {+ if (!link) {+ throw new DubApiError({+ code: "not_found",+ message: "Link not found.",+ });+ }- const body = updateLinkBodySchema.parse(await req.json());+ const body = updateLinkBodySchema.parse(await req.json());- // Add body onto existing link but maintain NewLinkProps form for processLink- const updatedLink = {- ...link,- expiresAt:- link.expiresAt instanceof Date- ? link.expiresAt.toISOString()- : link.expiresAt,- geo: link.geo as NewLinkProps["geo"],- ...body,- };+ // Add body onto existing link but maintain NewLinkProps form for processLink+ const updatedLink = {+ ...link,+ expiresAt:+ link.expiresAt instanceof Date+ ? link.expiresAt.toISOString()+ : link.expiresAt,+ geo: link.geo as NewLinkProps["geo"],+ ...body,+ };- if (updatedLink.projectId !== link?.projectId) {- throw new DubApiError({- code: "forbidden",- message:- "Transferring links to another workspace is only allowed via the /links/[linkId]transfer endpoint.",+ if (updatedLink.projectId !== link?.projectId) {+ throw new DubApiError({+ code: "forbidden",+ message:+ "Transferring links to another workspace is only allowed via the /links/[linkId]transfer endpoint.",+ });+ }++ const {+ link: processedLink,+ error,+ code,+ } = await processLink({+ payload: updatedLink,+ workspace,+ // if domain and key are the same, we don't need to check if the key exists+ skipKeyChecks:+ link.domain === updatedLink.domain &&+ link.key.toLowerCase() === updatedLink.key?.toLowerCase(),});- }- const {- link: processedLink,- error,- code,- } = await processLink({- payload: updatedLink,- workspace,- // if domain and key are the same, we don't need to check if the key exists- skipKeyChecks:- link.domain === updatedLink.domain &&- link.key.toLowerCase() === updatedLink.key?.toLowerCase(),- });+ if (error) {+ throw new DubApiError({+ code: code as ErrorCodes,+ message: error,+ });+ }- if (error) {- throw new DubApiError({- code: code as ErrorCodes,- message: error,+ const response = await editLink({+ oldDomain: link.domain,+ oldKey: link.key,+ updatedLink: processedLink,});- }- const response = await editLink({- oldDomain: link.domain,- oldKey: link.key,- updatedLink: processedLink,- });+ return NextResponse.json(response, {+ headers,+ });+ },+);- return NextResponse.json(response, {- headers,- });-});+// backwards compatibility+export const PUT = PATCH;// DELETE /api/links/[linkId] – delete a linkexport const DELETE = withWorkspace(async ({ headers, link }) => {diff --git a/apps/web/lib/openapi/links/index.ts b/apps/web/lib/openapi/links/index.tsindex d97124e07d..02725ad91c 100644--- a/apps/web/lib/openapi/links/index.ts+++ b/apps/web/lib/openapi/links/index.ts@@ -20,7 +20,7 @@ export const linksPaths: ZodOpenApiPathsObject = {get: getLinkInfo,},"/links/{linkId}": {- put: editLink,+ patch: editLink,delete: deleteLink,},"/links/bulk": {diff --git a/apps/web/ui/modals/add-edit-link-modal/index.tsx b/apps/web/ui/modals/add-edit-link-modal/index.tsxindex 140bb03919..fb794dbcba 100644--- a/apps/web/ui/modals/add-edit-link-modal/index.tsx+++ b/apps/web/ui/modals/add-edit-link-modal/index.tsx@@ -275,7 +275,7 @@ function AddEditLinkModal({const endpoint = useMemo(() => {if (props?.key) {return {- method: "PUT",+ method: "PATCH",url: `/api/links/${props.id}?workspaceId=${workspaceId}`,};} else {diff --git a/apps/web/ui/modals/archive-link-modal.tsx b/apps/web/ui/modals/archive-link-modal.tsxindex 28515622d9..bd618bac24 100644--- a/apps/web/ui/modals/archive-link-modal.tsx+++ b/apps/web/ui/modals/archive-link-modal.tsx@@ -23,9 +23,9 @@ const sendArchiveRequest = ({archive: boolean;workspaceId?: string;}) => {- const baseUrl = `/api/links/${linkId}/archive`;- return fetch(`${baseUrl}?workspaceId=${workspaceId}`, {- method: archive ? "POST" : "DELETE",+ return fetch(`/api/links/${linkId}?workspaceId=${workspaceId}`, {+ method: "PATCH",+ body: JSON.stringify({ archived: archive }),headers: {"Content-Type": "application/json",},