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

fix: add multi-domain support to shoprenter-proxy #55

Claude пре 5 месеци
родитељ
комит
e319a336b6
1 измењених фајлова са 20 додато и 8 уклоњено
  1. 20 8
      supabase/functions/shoprenter-proxy/index.ts

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

@@ -2,7 +2,7 @@ import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'
 
 const corsHeaders = {
   'Access-Control-Allow-Origin': '*',
-  'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type, x-shoprenter-shop',
+  'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type, x-shoprenter-shop, x-shoprenter-domain',
   'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, OPTIONS',
 }
 
@@ -15,12 +15,14 @@ const corsHeaders = {
  *
  * Usage from n8n:
  * 1. Set Authorization header with Bearer token (from ShopRenter OAuth)
- * 2. Set X-ShopRenter-Shop header with shop name (e.g., "myshop")
- * 3. Make requests to: /shoprenter-proxy/api/{endpoint}
- * 4. The proxy will forward to: https://{shop}.shoprenter.hu/api/{endpoint}
+ * 2. Set X-ShopRenter-Shop header with shop name (e.g., "myshop" or "elektromosroller")
+ * 3. (Optional) Set X-ShopRenter-Domain header:
+ *    - "shoprenter.hu" (default) - forwards to https://{shop}.shoprenter.hu
+ *    - "oauth.app.shoprenter.net" - forwards to https://oauth.app.shoprenter.net/{shop}
+ * 4. Make requests to: /shoprenter-proxy/api/{endpoint}
  *
  * All request methods (GET, POST, PUT, PATCH, DELETE) are supported.
- * All headers (except Host) are forwarded transparently.
+ * All headers (except Host and X-ShopRenter-*) are forwarded transparently.
  * Request and response bodies are proxied as-is.
  */
 
@@ -37,7 +39,7 @@ serve(async (req) => {
       return new Response(
         JSON.stringify({
           error: 'Missing X-ShopRenter-Shop header',
-          message: 'Please provide the shop name in the X-ShopRenter-Shop header (e.g., "myshop" for myshop.shoprenter.hu)'
+          message: 'Please provide the shop name in the X-ShopRenter-Shop header (e.g., "myshop" or "elektromosroller")'
         }),
         {
           status: 400,
@@ -46,6 +48,9 @@ serve(async (req) => {
       )
     }
 
+    // Extract domain preference (defaults to shoprenter.hu)
+    const domain = req.headers.get('X-ShopRenter-Domain') || 'shoprenter.hu'
+
     // Extract API endpoint from URL path
     const url = new URL(req.url)
     const pathMatch = url.pathname.match(/\/shoprenter-proxy(\/.*)$/)
@@ -65,8 +70,15 @@ serve(async (req) => {
 
     const apiPath = pathMatch[1]
 
-    // Construct ShopRenter API URL
-    const shopRenterUrl = `https://${shopName}.shoprenter.hu${apiPath}${url.search}`
+    // Construct ShopRenter API URL based on domain
+    let shopRenterUrl: string
+    if (domain === 'oauth.app.shoprenter.net') {
+      // For OAuth API: https://oauth.app.shoprenter.net/{shop}{apiPath}
+      shopRenterUrl = `https://oauth.app.shoprenter.net/${shopName}${apiPath}${url.search}`
+    } else {
+      // For standard API: https://{shop}.shoprenter.hu{apiPath}
+      shopRenterUrl = `https://${shopName}.shoprenter.hu${apiPath}${url.search}`
+    }
 
     console.log(`[ShopRenter Proxy] Forwarding ${req.method} request to: ${shopRenterUrl}`)