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.

modularize code

Steven Teyf2813591a19e
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 0ecc7af8cf..2c606fc4c1 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -184,8 +184,8 @@ export async function getRandomKey(domain: string): Promise<string> {
}
export async function checkIfKeyExists(domain: string, key: string) {
- // reserved keys for dub.sh
- if (domain === "dub.sh") {
+ // reserved keys for default short domain
+ if (domain === SHORT_DOMAIN) {
if ((await isReservedKey(key)) || DEFAULT_REDIRECTS[key]) {
return true;
}
@@ -273,54 +273,52 @@ export async function processLink({
}
}
- // if domain is not defined, use the project's primary domain
+ // if domain is not defined, set it to the project's primary domain
if (!domain) {
domain = project?.domains?.find((d) => d.primary)?.slug || SHORT_DOMAIN;
+ }
- // if domain is defined, do some checks
- } else {
- // checks for dub.sh domain
- if (domain === "dub.sh") {
- const keyBlacklisted = await isBlacklistedKey(key);
- if (keyBlacklisted) {
- return {
- link: payload,
- error: "Invalid key.",
- status: 422,
- };
- }
- const domainBlacklisted = await isBlacklistedDomain(url);
- if (domainBlacklisted) {
- return {
- link: payload,
- error: "Invalid url.",
- status: 422,
- };
- }
-
- // checks for other Dub-owned domains (chatg.pt, spti.fi, etc.)
- } else if (isDubDomain(domain)) {
- // coerce type with ! cause we already checked if it exists
- const { allowedHostnames } = DUB_DOMAINS.find((d) => d.slug === domain)!;
- const urlDomain = getDomainWithoutWWW(url) || "";
- if (!allowedHostnames.includes(urlDomain)) {
- return {
- link: payload,
- error: `Invalid url. You can only use ${domain} short links for URLs starting with ${allowedHostnames
- .map((d) => `\`${d}\``)
- .join(", ")}.`,
- status: 422,
- };
- }
+ // checks for default short domain
+ if (domain === SHORT_DOMAIN) {
+ const keyBlacklisted = await isBlacklistedKey(key);
+ if (keyBlacklisted) {
+ return {
+ link: payload,
+ error: "Invalid key.",
+ status: 422,
+ };
+ }
+ const domainBlacklisted = await isBlacklistedDomain(url);
+ if (domainBlacklisted) {
+ return {
+ link: payload,
+ error: "Invalid url.",
+ status: 422,
+ };
+ }
- // else, check if the domain belongs to the project
- } else if (!project?.domains?.find((d) => d.slug === domain)) {
+ // checks for other Dub-owned domains (chatg.pt, spti.fi, etc.)
+ } else if (isDubDomain(domain)) {
+ // coerce type with ! cause we already checked if it exists
+ const { allowedHostnames } = DUB_DOMAINS.find((d) => d.slug === domain)!;
+ const urlDomain = getDomainWithoutWWW(url) || "";
+ if (!allowedHostnames.includes(urlDomain)) {
return {
link: payload,
- error: "Domain does not belong to project.",
- status: 403,
+ error: `Invalid url. You can only use ${domain} short links for URLs starting with ${allowedHostnames
+ .map((d) => `\`${d}\``)
+ .join(", ")}.`,
+ status: 422,
};
}
+
+ // else, check if the domain belongs to the project
+ } else if (!project?.domains?.find((d) => d.slug === domain)) {
+ return {
+ link: payload,
+ error: "Domain does not belong to project.",
+ status: 403,
+ };
}
if (!key) {