Skip to content

Commit da06cda

Browse files
committed
Update shorten.ts
1 parent e57661d commit da06cda

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

netlify/edge-functions/shorten.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,32 @@ const urlBase = Deno.env.get("URL_BASE") || "";
77
const { generateShortUrl } = handler();
88

99
/**
10-
* Redirects to the long URL associated with the given short URL.
10+
* Shortens a given long URL and stores it in Supabase.
1111
*
1212
* @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.
13+
* @returns The shortened URL or an error message.
14+
* @throws If an error occurs while shortening the URL or storing it in Supabase.
1515
*
1616
* @example
1717
* // Example usage
18-
* curl -X GET https://your-api-url/shortUrl
18+
* curl -X POST https://your-api-url/shorten -d "url=https://example.com"
19+
* curl -X POST https://your-api-url/shorten -F "url=https://example.com"
1920
*/
2021
export default async (request: Request): Promise<Response> => {
2122
try {
22-
const formData = await multiParser(request);
23-
const url = formData.fields.url;
23+
let url: string | null = null;
24+
25+
const contentType = request.headers.get("content-type");
26+
if (contentType && contentType.includes("multipart/form-data")) {
27+
const formData = await multiParser(request);
28+
url = formData.fields.url;
29+
} else if (
30+
contentType &&
31+
contentType.includes("application/x-www-form-urlencoded")
32+
) {
33+
const formData = new URLSearchParams(await request.text());
34+
url = formData.get("url");
35+
}
2436

2537
if (!url) {
2638
return new Response(JSON.stringify({ error: "No URL provided" }), {

0 commit comments

Comments
 (0)