@@ -7,20 +7,32 @@ const urlBase = Deno.env.get("URL_BASE") || "";
7
7
const { generateShortUrl } = handler ( ) ;
8
8
9
9
/**
10
- * Redirects to the long URL associated with the given short URL .
10
+ * Shortens a given long URL and stores it in Supabase .
11
11
*
12
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.
13
+ * @returns The shortened URL or an error message.
14
+ * @throws If an error occurs while shortening the URL or storing it in Supabase.
15
15
*
16
16
* @example
17
17
* // 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"
19
20
*/
20
21
export default async ( request : Request ) : Promise < Response > => {
21
22
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
+ }
24
36
25
37
if ( ! url ) {
26
38
return new Response ( JSON . stringify ( { error : "No URL provided" } ) , {
0 commit comments