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.

support 3 endpoints for now (GET, POST, DELETE)

Steven Tey23f07f6f5981
diff --git a/lib/auth.ts b/lib/auth.ts
index 224a1886da..c357079698 100644
--- a/lib/auth.ts
+++ b/lib/auth.ts
@@ -6,6 +6,7 @@ import { PlanProps, ProjectProps, UserProps } from "#/lib/types";
import { authOptions } from "@/pages/api/auth/[...nextauth]";
import { createHash } from "crypto";
import { ratelimit } from "./upstash";
+import { API_DOMAIN } from "./constants";
export interface Session {
user: {
@@ -233,11 +234,41 @@ const withLinksAuth =
} = {},
) =>
async (req: NextApiRequest, res: NextApiResponse) => {
+ const { slug, domain } = req.query as {
+ slug?: string;
+ domain?: string;
+ };
+
+ // if slug is misconfgured
+ if (slug && typeof slug !== "string") {
+ return res.status(400).end("Missing or misconfigured project slug.");
+ }
+
let session: Session | undefined;
const apiKey = req.headers.authorization?.split("Bearer ")[1];
if (apiKey) {
+ // if there is no slug, it's the default dub.sh link
+ if (!slug) {
+ return res
+ .status(403)
+ .end("Unauthorized: API is not supported for dub.sh links yet.");
+ }
+
+ if (req.method === "PUT") {
+ return res
+ .status(403)
+ .end("Unauthorized: API is not supported for editing links yet.");
+ }
+
+ const url = new URL(req.url || "", API_DOMAIN);
+
+ if (url.pathname.includes("/stats/")) {
+ return res.status(403).end("Unauthorized: Invalid route.");
+ }
+
const hashedKey = hashToken(apiKey);
+
const user = await prisma.user.findFirst({
where: {
tokens: {
@@ -255,6 +286,7 @@ const withLinksAuth =
if (!user) {
return res.status(401).end("Unauthorized: Invalid API key.");
}
+
const { success, limit, reset, remaining } = await ratelimit(
10,
"1 s",
@@ -281,16 +313,6 @@ const withLinksAuth =
}
}
- const { slug, domain } = req.query as {
- slug?: string;
- domain?: string;
- };
-
- // if slug is misconfgured
- if (slug && typeof slug !== "string") {
- return res.status(400).end("Missing or misconfigured project slug.");
- }
-
let project: ProjectProps | undefined;
let link: LinkProps | undefined;
@@ -365,9 +387,9 @@ const withLinksAuth =
}
}
- // if key is defined, check if the current user is the owner of the link (only for dub.sh links)
+ // if key is defined, fetch link details
const { key } = req.query;
- if (key && !domain && !skipKeyCheck) {
+ if (key) {
if (typeof key !== "string") {
return res.status(400).end("Missing or misconfigured link key.");
}
@@ -380,11 +402,12 @@ const withLinksAuth =
},
},
})) || undefined;
+
if (!link) {
return res.status(404).end("Link not found.");
// for dub.sh links, check if the user is the owner of the link
- } else if (!slug && link.userId !== session.user.id) {
+ } else if (!slug && link.userId !== session.user.id && !skipKeyCheck) {
return res.status(404).end("Link not found.");
}
}
diff --git a/lib/constants/index.ts b/lib/constants/index.ts
index 66dee3c6b2..baca4d353a 100644
--- a/lib/constants/index.ts
+++ b/lib/constants/index.ts
@@ -86,6 +86,13 @@ export const API_HOSTNAMES = new Set([
"api.localhost:8888",
]);
+export const API_DOMAIN =
+ process.env.NEXT_PUBLIC_VERCEL_ENV === "production"
+ ? "https://api.dub.co"
+ : process.env.NEXT_PUBLIC_VERCEL_ENV === "preview"
+ ? "https://api.dub.sh"
+ : "http://api.localhost:8888";
+
export const ADMIN_HOSTNAMES = new Set([
"admin.dub.co",
"admin.localhost:8888",
diff --git a/pages/api/links/[key]/index.ts b/pages/api/links/[key]/index.ts
index 9587428f5d..18c692c955 100644
--- a/pages/api/links/[key]/index.ts
+++ b/pages/api/links/[key]/index.ts
@@ -30,6 +30,7 @@ export default withLinksAuth(
return res.status(400).end("Missing domain or key or url.");
}
+ // for default dub.sh links (not part of a project)
if (!project) {
if (key.includes("/")) {
return res.status(422).end("Key cannot contain '/'.");
@@ -99,7 +100,9 @@ export default withLinksAuth(
// DELETE /api/links/:key – delete a link
} else if (req.method === "DELETE") {
const response = await deleteLink(domain || "dub.sh", oldKey);
- return res.status(200).json(response);
+ return res.status(200).json({
+ message: `Link deleted.`,
+ });
} else {
res.setHeader("Allow", ["GET", "PUT", "DELETE"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);