Skip to content

Commit 7b039e2

Browse files
committed
v0.0.1
1 parent 0cbab21 commit 7b039e2

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

netlify.toml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
[build.environment]
55
NODE_VERSION = "20"
66

7+
[[edge_functions]]
8+
path = "/*"
9+
function = "redirect"
10+
711
[[edge_functions]]
812
path = "/shorten"
913
function = "shorten"
@@ -15,9 +19,3 @@
1519
[[edge_functions]]
1620
path = "/latest"
1721
function = "latest"
18-
19-
20-
[[edge_functions]]
21-
path = "/:shortUrl"
22-
function = "redirect"
23-

netlify/edge-functions/redirect.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import handler from "./utils.ts";
22
const { fetchFromSupabase } = handler();
3+
34
export default async (request: Request): Promise<Response> => {
45
try {
56
const shortUrl = new URL(request.url).pathname.replace("/", "");
7+
68
const data = await fetchFromSupabase(
79
`urls?select=long_url&short_url=eq.${shortUrl}`,
810
{ method: "GET" }
911
);
12+
1013
if (data.length === 0) {
1114
return new Response(JSON.stringify({ error: "URL not found" }), {
1215
status: 404,
1316
});
1417
}
18+
1519
return new Response(null, {
1620
status: 301,
1721
headers: { Location: data[0].long_url },
1822
});
1923
} catch (error) {
24+
console.error("Error:", error);
2025
return new Response(
2126
JSON.stringify({
2227
error: "Internal server error",
@@ -26,4 +31,3 @@ export default async (request: Request): Promise<Response> => {
2631
);
2732
}
2833
};
29-
export const config = { path: "/" };

netlify/edge-functions/shorten.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export default async (request: Request): Promise<Response> => {
1616
});
1717
}
1818

19-
let shortUrl = urlBase + (await generateShortUrl(url));
19+
let shortUrl = await generateShortUrl(url);
20+
shortUrl = urlBase + shortUrl;
2021

2122
return new Response(JSON.stringify({ shortUrl }), { status: 200, headers });
2223
} catch (error) {

netlify/edge-functions/utils.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export async function fetchFromSupabase(
55
endpoint: string,
66
options: RequestInit
77
) {
8-
console.log("options", options);
98
const response = await fetch(`${SUPABASE_URL}/rest/v1/${endpoint}`, {
109
...options,
1110
headers: {
@@ -19,26 +18,22 @@ export async function fetchFromSupabase(
1918
throw new Error(`Supabase request failed: ${response.statusText}`);
2019
}
2120
let jsonResponse = await response.json();
22-
console.log("jsonResponse", jsonResponse);
21+
2322
return jsonResponse;
2423
}
25-
import {
26-
fetchFromSupabase,
27-
generateShortUrl as generateUuidShort,
28-
} from "./utils.ts";
24+
2925
export async function generateShortUrl(longUrl: string): Promise<string> {
3026
try {
31-
let { data, error } = await fetchFromSupabase(
27+
const data = await fetchFromSupabase(
3228
"urls?select=short_url&long_url=eq." + longUrl,
3329
{ method: "GET" }
3430
);
35-
if (error) {
36-
console.error("Error checking for existing long URL:", error);
31+
32+
if (!data) {
33+
console.error("Error checking for existing long URL.");
3734
throw error;
3835
}
3936
if (data && data[0]?.short_url) {
40-
console.log("data");
41-
console.log(data);
4237
return data[0].short_url;
4338
}
4439
let shortUrl: string;
@@ -53,8 +48,6 @@ export async function generateShortUrl(longUrl: string): Promise<string> {
5348
console.error("Error checking for collision:", error);
5449
throw error;
5550
}
56-
console.log("data");
57-
console.log(data);
5851
isCollision = !!data;
5952
}
6053
({ data, error } = await fetchFromSupabase("urls", {

0 commit comments

Comments
 (0)