Skip to content

Commit

Permalink
fix: remove key expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
elianiva committed Nov 24, 2024
1 parent 328166c commit ac8cd99
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"deno.enable": true,
"deno.unstable": true,
}
27 changes: 15 additions & 12 deletions routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type HandlerResponse = {
url?: string;
isUpdated?: boolean;
error?: string;
}
};

export const handler: Handlers = {
async POST(req, ctx) {
Expand All @@ -17,18 +17,25 @@ export const handler: Handlers = {
case "PUT": {
const originalUrl = form.get("original_url") as string;
const customUrl = form.get("custom_url") as string;
const customised = await customiseShortenedUrl(originalUrl, customUrl);
return ctx.render({
url: customised,
isUpdated: true,
});
try {
const customised = await customiseShortenedUrl(
originalUrl,
customUrl,
);
return ctx.render({
url: customised,
isUpdated: true,
});
} catch {
return ctx.render({ error: "Customised url already exists" });
}
}
default: {
const originalUrl = form.get("url") as string;
if (!originalUrl) return ctx.render({ error: "URL cannot be empty" });
const shortened = await shortenUrl(originalUrl);
return ctx.render({
url: shortened
url: shortened,
});
}
}
Expand All @@ -38,11 +45,7 @@ export const handler: Handlers = {
export default function Home(props: PageProps<HandlerResponse>) {
return (
<div className="flex flex-col gap-10 items-center justify-center h-full">
<img
src="/assets/wri-logo.png"
alt="WRI Logo"
class="w-72"
/>
<img src="/assets/wri-logo.png" alt="WRI Logo" class="w-72" />
<form
method="POST"
className="flex items-center gap-4 p-4 rounded-lg bg-white shadow-lg border border-slate-200"
Expand Down
15 changes: 10 additions & 5 deletions services/shortener.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { nanoid } from 'nanoid';

const kv = await Deno.openKv();
const KEY_EXPIRY = 90 * 24 * 60 * 60; // 90 days

export async function shortenUrl(url: string) {
const token = nanoid(4);
Expand All @@ -10,21 +9,27 @@ export async function shortenUrl(url: string) {
await kv
.atomic()
.check({ key, versionstamp: null })
.set(["url", token], url, { expireIn: KEY_EXPIRY })
.set(["url", token], url)
.commit();
return token;
}

export async function customiseShortenedUrl(original: string, customised: string) {
const originalKey = ["url", original];
const originalShortened = await kv.get(originalKey);
const originalShortened = await kv.get<string>(originalKey);
const customisedKey = ["url", customised];

// check if customised already exists
if (await kv.get(customisedKey)) {
throw new Error("Customised url already exists");
}

await kv
.atomic()
.check(originalShortened)
.check({ key: customisedKey, versionstamp: null })
.delete(originalKey)
.set(customisedKey, originalShortened.value as string, { expireIn: KEY_EXPIRY })
.set(customisedKey, originalShortened.value as string)
.commit();
return customised;
}
Expand All @@ -34,4 +39,4 @@ export async function resolveUrl(token: string): Promise<string | null> {
const url = await kv.get(key);
if (!url) return null;
return url.value as string;
}
}

0 comments on commit ac8cd99

Please sign in to comment.