Skip to content

Commit 9fd4f41

Browse files
committed
Added Comments and isURL validation
1 parent 7690b85 commit 9fd4f41

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

netlify/edge-functions/shorten.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
import { multiParser } from "https://deno.land/x/multiparser@0.114.0/mod.ts";
2+
import { isURL } from "https://deno.land/x/is_url/mod.ts";
3+
24
import { headers } from "./headers.ts";
35
import handler from "./utils.ts";
46
const urlBase = Deno.env.get("URL_BASE") || "";
57
const { generateShortUrl } = handler();
68

9+
/**
10+
* Redirects to the long URL associated with the given short URL.
11+
*
12+
* @param request - The incoming HTTP request.
13+
* @returns A redirection response to the long URL or an error message.
14+
* @throws If an error occurs while fetching the long URL from Supabase.
15+
*
16+
* @example
17+
* // Example usage
18+
* curl -X GET https://your-api-url/shortUrl
19+
*/
720
export default async (request: Request): Promise<Response> => {
821
try {
922
const formData = await multiParser(request);
@@ -16,6 +29,13 @@ export default async (request: Request): Promise<Response> => {
1629
});
1730
}
1831

32+
if (!isURL(url)) {
33+
return new Response(JSON.stringify({ error: "Invalid URL provided" }), {
34+
status: 400,
35+
headers,
36+
});
37+
}
38+
1939
let shortUrl = await generateShortUrl(url);
2040
shortUrl = urlBase + shortUrl;
2141

0 commit comments

Comments
 (0)