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.

improve getRandomKey implementation

Steven Teyc74c8b54bd60
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 2c606fc4c1..f286ca7c92 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -164,25 +164,6 @@ export async function getLinksCount({
}
}
-export async function getRandomKey(domain: string): Promise<string> {
- /* recursively get random key till it gets one that's available */
- const key = nanoid();
- const response = await prisma.link.findUnique({
- where: {
- domain_key: {
- domain,
- key,
- },
- },
- });
- if (response) {
- // by the off chance that key already exists
- return getRandomKey(domain);
- } else {
- return key;
- }
-}
-
export async function checkIfKeyExists(domain: string, key: string) {
// reserved keys for default short domain
if (domain === SHORT_DOMAIN) {
@@ -199,6 +180,18 @@ export async function checkIfKeyExists(domain: string, key: string) {
return !!link;
}
+export async function getRandomKey(domain: string): Promise<string> {
+ /* recursively get random key till it gets one that's available */
+ const key = nanoid();
+ const response = await checkIfKeyExists(domain, key);
+ if (response) {
+ // by the off chance that key already exists
+ return getRandomKey(domain);
+ } else {
+ return key;
+ }
+}
+
export function processKey(key: string) {
if (!validKeyRegex.test(key)) {
return null;
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 9f83e85c0c..64cd694ce5 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -19,7 +19,7 @@ export interface RedisLinkProps {
proxy?: boolean;
rewrite?: boolean;
iframeable?: boolean;
- expiresAt?: string;
+ expiresAt?: Date;
ios?: string;
android?: string;
geo?: object;
diff --git a/apps/web/lib/upstash.ts b/apps/web/lib/upstash.ts
index 88384dad6a..6ba9a8c5e8 100644
--- a/apps/web/lib/upstash.ts
+++ b/apps/web/lib/upstash.ts
@@ -37,30 +37,6 @@ export const ratelimit = (
});
};
-// only for dub.sh public demo
-export async function setRandomKey(
- url: string,
-): Promise<{ response: string; key: string }> {
- /* recursively set link till successful */
- const key = nanoid();
- const response = await redis.set(
- `dub.sh:${key}`,
- {
- url,
- },
- {
- nx: true,
- ex: 30 * 60, // 30 minutes
- },
- );
- if (response !== "OK") {
- // by the off chance that key already exists
- return setRandomKey(url);
- } else {
- return { response, key };
- }
-}
-
/**
* Recording metatags that were generated via `/api/edge/metatags`
* If there's an error, it will be logged to a separate redis list for debugging