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 tests, "Edit" → "Update"

Steven Teya00e53e259ab
diff --git a/apps/docs/api-reference/endpoint/edit-a-link.mdx b/apps/docs/api-reference/endpoint/edit-a-link.mdx
deleted file mode 100644
index d0121043c5..0000000000
--- a/apps/docs/api-reference/endpoint/edit-a-link.mdx
+++ /dev/null
@@ -1,4 +0,0 @@
----
-openapi: patch /links/{linkId}
-og:title: "Edit a link with the Dub.co API - API Reference"
----
diff --git a/apps/docs/api-reference/endpoint/update-a-link.mdx b/apps/docs/api-reference/endpoint/update-a-link.mdx
new file mode 100644
index 0000000000..2b4d766cf7
--- /dev/null
+++ b/apps/docs/api-reference/endpoint/update-a-link.mdx
@@ -0,0 +1,4 @@
+---
+openapi: patch /links/{linkId}
+og:title: "Update a link with the Dub.co API - API Reference"
+---
diff --git a/apps/docs/mint.json b/apps/docs/mint.json
index 6870bd829f..4e71369bf8 100644
--- a/apps/docs/mint.json
+++ b/apps/docs/mint.json
@@ -232,6 +232,10 @@
{
"source": "/api-reference/endpoint/retrieve-a-list-of-projects",
"destination": "/api-reference/endpoint/retrieve-a-list-of-workspaces"
+ },
+ {
+ "source": "/api-reference/endpoint/edit-a-link",
+ "destination": "/api-reference/endpoint/update-a-link"
}
]
}
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 91fae0367c..2eaf3fe3a1 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -1,9 +1,9 @@
import { DubApiError, ErrorCodes } from "@/lib/api/errors";
import {
deleteLink,
- editLink,
processLink,
transformLink,
+ updateLink,
} from "@/lib/api/links";
import { withWorkspace } from "@/lib/auth";
import prisma from "@/lib/prisma";
@@ -96,7 +96,7 @@ export const PATCH = withWorkspace(
});
}
- const response = await editLink({
+ const response = await updateLink({
oldDomain: link.domain,
oldKey: link.key,
updatedLink: processedLink,
diff --git a/apps/web/lib/api/links/index.ts b/apps/web/lib/api/links/index.ts
index 154a594431..88947b3180 100644
--- a/apps/web/lib/api/links/index.ts
+++ b/apps/web/lib/api/links/index.ts
@@ -2,9 +2,9 @@ export * from "./archive-link";
export * from "./bulk-create-links";
export * from "./create-link";
export * from "./delete-link";
-export * from "./edit-link";
export * from "./get-links-count";
export * from "./get-links-for-workspace";
export * from "./process-link";
export * from "./transfer-link";
+export * from "./update-link";
export * from "./utils";
diff --git a/apps/web/lib/api/links/edit-link.ts b/apps/web/lib/api/links/update-link.ts
similarity index 98%
rename from apps/web/lib/api/links/edit-link.ts
rename to apps/web/lib/api/links/update-link.ts
index a46764a5c9..e96e934856 100644
--- a/apps/web/lib/api/links/edit-link.ts
+++ b/apps/web/lib/api/links/update-link.ts
@@ -7,7 +7,7 @@ import { SHORT_DOMAIN, getParamsFromURL, truncate } from "@dub/utils";
import { Prisma } from "@prisma/client";
import { combineTagIds, transformLink } from "./utils";
-export async function editLink({
+export async function updateLink({
oldDomain = SHORT_DOMAIN,
oldKey,
updatedLink,
diff --git a/apps/web/lib/openapi/links/index.ts b/apps/web/lib/openapi/links/index.ts
index 02725ad91c..f2b868a47d 100644
--- a/apps/web/lib/openapi/links/index.ts
+++ b/apps/web/lib/openapi/links/index.ts
@@ -3,10 +3,10 @@ import { ZodOpenApiPathsObject } from "zod-openapi";
import { createBulkLink } from "./create-bulk-link";
import { createLink } from "./create-link";
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";
+import { updateLink } from "./update-link";
export const linksPaths: ZodOpenApiPathsObject = {
"/links": {
@@ -20,7 +20,7 @@ export const linksPaths: ZodOpenApiPathsObject = {
get: getLinkInfo,
},
"/links/{linkId}": {
- patch: editLink,
+ patch: updateLink,
delete: deleteLink,
},
"/links/bulk": {
diff --git a/apps/web/lib/openapi/links/edit-link.ts b/apps/web/lib/openapi/links/update-link.ts
similarity index 69%
rename from apps/web/lib/openapi/links/edit-link.ts
rename to apps/web/lib/openapi/links/update-link.ts
index 84414b7660..477b5b0639 100644
--- a/apps/web/lib/openapi/links/edit-link.ts
+++ b/apps/web/lib/openapi/links/update-link.ts
@@ -4,18 +4,18 @@ import { LinkSchema, createLinkBodySchema } from "@/lib/zod/schemas";
import { ZodOpenApiOperationObject } from "zod-openapi";
import { workspaceParamsSchema } from "../request";
-export const editLink: ZodOpenApiOperationObject = {
- operationId: "editLink",
+export const updateLink: ZodOpenApiOperationObject = {
+ operationId: "updateLink",
"x-speakeasy-name-override": "update",
"x-speakeasy-max-method-params": 2,
- summary: "Edit a link",
- description: "Edit a link for the authenticated workspace.",
+ summary: "Update a link",
+ description: "Update a link for the authenticated workspace.",
requestParams: {
query: workspaceParamsSchema,
path: z.object({
linkId: z.string().openapi({
description:
- "The id of the link to edit. You may use either `linkId` (obtained via `/links/info` endpoint) or `externalId` prefixed with `ext_`.",
+ "The id of the link to update. You may use either `linkId` (obtained via `/links/info` endpoint) or `externalId` prefixed with `ext_`.",
}),
}),
},
@@ -28,7 +28,7 @@ export const editLink: ZodOpenApiOperationObject = {
},
responses: {
"200": {
- description: "The edited link",
+ description: "The updated link",
content: {
"application/json": {
schema: LinkSchema,
diff --git a/apps/web/tests/links/update-link.test.ts b/apps/web/tests/links/update-link.test.ts
index e0070fd110..af5822b5d6 100644
--- a/apps/web/tests/links/update-link.test.ts
+++ b/apps/web/tests/links/update-link.test.ts
@@ -46,6 +46,48 @@ describe.sequential("PUT /links/{linkId}", async () => {
});
test("update link using linkId", async () => {
+ const { data: updatedLink } = await http.patch<Link>({
+ path: `/links/${link.id}`,
+ query: { workspaceId },
+ body: { ...toUpdate },
+ });
+
+ expect(updatedLink).toStrictEqual({
+ ...expectedLink,
+ ...toUpdate,
+ domain,
+ workspaceId,
+ externalId,
+ userId: user.id,
+ expiresAt: "2030-04-16T17:00:00.000Z",
+ projectId: workspaceId.replace("ws_", ""),
+ shortLink: `https://${domain}/${toUpdate.key}`,
+ qrCode: `https://api.dub.co/qr?url=https://${domain}/${toUpdate.key}?qr=1`,
+ tags: [],
+ });
+
+ // Fetch the link
+ const { data: fetchedLink } = await http.get<Link>({
+ path: `/links/${link.id}`,
+ query: { workspaceId },
+ });
+
+ expect(fetchedLink).toStrictEqual({
+ ...expectedLink,
+ ...toUpdate,
+ domain,
+ workspaceId,
+ externalId,
+ userId: user.id,
+ expiresAt: "2030-04-16T17:00:00.000Z",
+ projectId: workspaceId.replace("ws_", ""),
+ shortLink: `https://${domain}/${toUpdate.key}`,
+ qrCode: `https://api.dub.co/qr?url=https://${domain}/${toUpdate.key}?qr=1`,
+ tags: [],
+ });
+ });
+
+ test("update link using PUT (backwards compatibility)", async () => {
const { data: updatedLink } = await http.put<Link>({
path: `/links/${link.id}`,
query: { workspaceId },
@@ -89,7 +131,7 @@ describe.sequential("PUT /links/{linkId}", async () => {
// Archive the link
test("archive link", async () => {
- const { status, data: updatedLink } = await http.put<Link>({
+ const { status, data: updatedLink } = await http.patch<Link>({
path: `/links/${link.id}`,
query: { workspaceId },
body: {
@@ -124,7 +166,7 @@ describe.sequential("PUT /links/{linkId}", async () => {
// Unarchive the link
test("unarchive link", async () => {
- const { status, data: updatedLink } = await http.put<Link>({
+ const { status, data: updatedLink } = await http.patch<Link>({
path: `/links/${link.id}`,
query: { workspaceId },
body: {
@@ -159,7 +201,7 @@ describe.sequential("PUT /links/{linkId}", async () => {
// Update the link using externalId
test("update link using externalId", async () => {
- const { status, data: updatedLink } = await http.put<Link>({
+ const { status, data: updatedLink } = await http.patch<Link>({
path: `/links/ext_${externalId}`,
query: { workspaceId },
body: {
diff --git a/apps/web/tests/utils/http.ts b/apps/web/tests/utils/http.ts
index d6b4df023d..b32bbf0bc5 100644
--- a/apps/web/tests/utils/http.ts
+++ b/apps/web/tests/utils/http.ts
@@ -26,7 +26,7 @@ export class HttpClient {
}
private async request<TResponse>(
- method: "GET" | "POST" | "PUT" | "DELETE",
+ method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
req: Request,
): Promise<Response<TResponse>> {
const headers = {
@@ -59,6 +59,10 @@ export class HttpClient {
return await this.request<TResponse>("POST", req);
}
+ public async patch<TResponse>(req: Request) {
+ return await this.request<TResponse>("PATCH", req);
+ }
+
public async put<TResponse>(req: Request) {
return await this.request<TResponse>("PUT", req);
}