Browse Source

fix: correct URL path parsing for shoprenter-proxy Edge Function #55

- Fixed path extraction logic to handle Supabase Edge Functions URL structure
- In production, req.url pathname is just the path after function name (e.g., /api/customers)
- Removed old regex matching that expected full path with /shoprenter-proxy/ prefix
- Added detailed logging for debugging URL parsing
- Now correctly forwards requests to ShopRenter API

When calling: https://ztklqodcdjeqpsvhlpud.supabase.co/functions/v1/shoprenter-proxy/api/customers
The pathname will be: /api/customers (not /shoprenter-proxy/api/customers)
Claude 5 months ago
parent
commit
a83dcf00a2
1 changed files with 20 additions and 5 deletions
  1. 20 5
      supabase/functions/shoprenter-proxy/index.ts

+ 20 - 5
supabase/functions/shoprenter-proxy/index.ts

@@ -173,13 +173,30 @@ serve(async (req) => {
 
     // Extract API endpoint from URL path
     const url = new URL(req.url)
-    const pathMatch = url.pathname.match(/\/shoprenter-proxy(\/.*)$/)
 
-    if (!pathMatch || !pathMatch[1]) {
+    console.log('[ShopRenter Proxy] Received URL:', req.url)
+    console.log('[ShopRenter Proxy] URL pathname:', url.pathname)
+
+    // For Supabase Edge Functions, the pathname is just the path after the function name
+    // Example: If called as /functions/v1/shoprenter-proxy/api/customers
+    // Then req.url pathname will be: /api/customers
+    // For local testing, it might be: /shoprenter-proxy/api/customers
+
+    let apiPath = url.pathname
+
+    // If the path starts with /shoprenter-proxy/, remove that prefix
+    if (apiPath.startsWith('/shoprenter-proxy/')) {
+      apiPath = apiPath.substring('/shoprenter-proxy'.length)
+    }
+
+    // Validate that we have an API path
+    if (!apiPath || apiPath === '/') {
       return new Response(
         JSON.stringify({
           error: 'Invalid API endpoint',
-          message: 'Request path must be in format: /shoprenter-proxy/api/{endpoint}'
+          message: 'Request path must include the API endpoint (e.g., /api/customers)',
+          receivedUrl: req.url,
+          receivedPath: url.pathname
         }),
         {
           status: 400,
@@ -188,8 +205,6 @@ serve(async (req) => {
       )
     }
 
-    const apiPath = pathMatch[1]
-
     // Construct ShopRenter API URL components
     const hostname = `${shopName}.api2.myshoprenter.hu`
     const fullPath = `${apiPath}${url.search}`