Browse Source

feat: add configurable API version support to shoprenter-proxy #56

- Added X-ShopRenter-API-Version header to support both api.myshoprenter.hu and api2.myshoprenter.hu
- Default to 'api' (api.myshoprenter.hu) to match most common usage
- Added validation for API version parameter
- Updated CORS headers to include new header
- Updated function documentation with usage instructions
Claude 5 months ago
parent
commit
8f8f29208a
1 changed files with 25 additions and 3 deletions
  1. 25 3
      supabase/functions/shoprenter-proxy/index.ts

+ 25 - 3
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, x-shoprenter-token',
+  'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type, x-shoprenter-shop, x-shoprenter-token, x-shoprenter-api-version',
   'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, OPTIONS',
 }
 
@@ -17,7 +17,10 @@ const corsHeaders = {
  * 1. Set X-ShopRenter-Token header with access token (from ShopRenter OAuth)
  *    - Use X-ShopRenter-Token instead of Authorization to avoid Supabase JWT validation
  * 2. Set X-ShopRenter-Shop header with shop name (e.g., "elektromosroller")
- * 3. Make requests to: /shoprenter-proxy/api/{endpoint}
+ * 3. (Optional) Set X-ShopRenter-API-Version header to specify API endpoint:
+ *    - "api" for api.myshoprenter.hu (default)
+ *    - "api2" for api2.myshoprenter.hu
+ * 4. Make requests to: /shoprenter-proxy/api/{endpoint}
  *
  * All request methods (GET, POST, PUT, PATCH, DELETE) are supported.
  * Request and response bodies are proxied as-is.
@@ -274,10 +277,29 @@ serve(async (req) => {
       )
     }
 
+    // Get API version from header (default to 'api' for api.myshoprenter.hu)
+    const apiVersion = req.headers.get('X-ShopRenter-API-Version') || 'api'
+
+    // Validate API version
+    if (apiVersion !== 'api' && apiVersion !== 'api2') {
+      return new Response(
+        JSON.stringify({
+          error: 'Invalid X-ShopRenter-API-Version header',
+          message: 'API version must be either "api" or "api2"',
+          receivedValue: apiVersion
+        }),
+        {
+          status: 400,
+          headers: { ...corsHeaders, 'Content-Type': 'application/json' }
+        }
+      )
+    }
+
     // Construct ShopRenter API URL components
-    const hostname = `${shopName}.api2.myshoprenter.hu`
+    const hostname = `${shopName}.${apiVersion}.myshoprenter.hu`
     const fullPath = `${apiPath}${url.search}`
 
+    console.log(`[ShopRenter Proxy] Using API version: ${apiVersion}`)
     console.log(`[ShopRenter Proxy] Forwarding ${req.method} request to: https://${hostname}${fullPath}`)
 
     // Prepare headers for ShopRenter API request