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.

add prefix prop

Steven Tey6789b220dae1
diff --git a/apps/web/app/api/route.ts b/apps/web/app/api/route.ts
index 45aacd7378..e9bb9568e7 100644
--- a/apps/web/app/api/route.ts
+++ b/apps/web/app/api/route.ts
@@ -748,6 +748,11 @@ export function GET(): NextResponse<OpenAPIV3.Document> {
description:
"The short link slug. If not provided, a random 7-character slug will be generated.",
},
+ prefix: {
+ type: "string",
+ description:
+ "The prefix of the short link slug for randomly-generated keys (e.g. if prefix is `/c/`, generated keys will be in the `/c/:key` format). Will be ignored if `key` is provided.",
+ },
url: {
type: "string",
description: "The destination URL of the short link.",
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 8d8fa44bd3..965d91133c 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -223,13 +223,19 @@ export async function checkIfKeyExists(domain: string, key: string) {
return !!link;
}
-export async function getRandomKey(domain: string): Promise<string> {
+export async function getRandomKey(
+ domain: string,
+ prefix?: string,
+): Promise<string> {
/* recursively get random key till it gets one that's available */
- const key = nanoid();
+ let key = nanoid();
+ if (prefix) {
+ key = `${prefix.replace(/^\/|\/$/g, "")}/${key}`;
+ }
const response = await checkIfKeyExists(domain, key);
if (response) {
// by the off chance that key already exists
- return getRandomKey(domain);
+ return getRandomKey(domain, prefix);
} else {
return key;
}
@@ -302,11 +308,11 @@ export async function processLink({
};
}
// can't use `/` in key on free plan
- if (key?.includes("/")) {
+ if (key?.includes("/") || payload["prefix"]) {
return {
link: payload,
error:
- "Key cannot contain '/'. You can only use this on a Pro plan and above. Upgrade to Pro to use this feature.",
+ "Key cannot contain '/'. You can only use key prefixes on a Pro plan and above. Upgrade to Pro to use this feature.",
status: 422,
};
}
@@ -361,7 +367,7 @@ export async function processLink({
}
if (!key) {
- key = await getRandomKey(domain);
+ key = await getRandomKey(domain, payload["prefix"]);
}
if (bulk) {
@@ -449,6 +455,7 @@ export async function processLink({
// remove polyfill attributes from payload
delete payload["shortLink"];
delete payload["qrCode"];
+ delete payload["prefix"];
return {
link: {