Преглед изворни кода

fix(scraper): resolve "Body already consumed" error in custom-urls handler

- Parse request body only once for POST/PATCH/DELETE methods
- Use query params for GET requests, body for other methods
- Fixes disable URL functionality in scraper management

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Fszontagh пре 4 месеци
родитељ
комит
ff49200c9d
1 измењених фајлова са 26 додато и 4 уклоњено
  1. 26 4
      supabase/functions/scraper-management/index.ts

+ 26 - 4
supabase/functions/scraper-management/index.ts

@@ -371,7 +371,17 @@ Deno.serve(async (req) => {
       case 'custom-urls': {
       case 'custom-urls': {
         // Handle GET and POST for listing/adding custom URLs
         // Handle GET and POST for listing/adding custom URLs
         if (pathParts.length === 1) {
         if (pathParts.length === 1) {
-          const storeId = url.searchParams.get('store_id') || (await req.json())?.store_id;
+          // For GET: use query params, for POST: use body
+          let storeId: string | null = null;
+          let bodyData: any = null;
+
+          if (req.method === 'GET') {
+            storeId = url.searchParams.get('store_id');
+          } else if (req.method === 'POST') {
+            bodyData = await req.json();
+            storeId = bodyData?.store_id;
+          }
+
           if (!storeId) {
           if (!storeId) {
             return new Response(
             return new Response(
               JSON.stringify({ error: 'store_id is required' }),
               JSON.stringify({ error: 'store_id is required' }),
@@ -402,8 +412,9 @@ Deno.serve(async (req) => {
             );
             );
 
 
           } else if (req.method === 'POST') {
           } else if (req.method === 'POST') {
-            // Add custom URL
-            const { url: customUrl, content_type } = await req.json();
+            // Add custom URL - bodyData was already parsed above
+            const customUrl = bodyData?.url;
+            const content_type = bodyData?.content_type;
 
 
             if (!customUrl || !content_type) {
             if (!customUrl || !content_type) {
               return new Response(
               return new Response(
@@ -439,7 +450,18 @@ Deno.serve(async (req) => {
           const customUrlId = pathParts[1];
           const customUrlId = pathParts[1];
           const subAction = pathParts[2]; // 'toggle' or undefined
           const subAction = pathParts[2]; // 'toggle' or undefined
 
 
-          const { store_id, enabled } = await req.json().catch(() => ({ store_id: null, enabled: null }));
+          // Parse body once for PATCH/DELETE
+          let bodyData: any = null;
+          if (req.method === 'PATCH' || req.method === 'DELETE') {
+            try {
+              bodyData = await req.json();
+            } catch {
+              bodyData = {};
+            }
+          }
+
+          const store_id = bodyData?.store_id;
+          const enabled = bodyData?.enabled;
 
 
           if (!store_id) {
           if (!store_id) {
             return new Response(
             return new Response(