|
| 1 | +const { instances } = require("../../config/dato/instances"); |
| 2 | + |
| 3 | +// Change to exports.handler format |
| 4 | +exports.handler = async (event, context) => { |
| 5 | + try { |
| 6 | + // Find the current instance |
| 7 | + const currentInstance = instances.find( |
| 8 | + (instance) => instance.name === process.env.DATO_INSTANCE_CURRENT |
| 9 | + ); |
| 10 | + |
| 11 | + if (!currentInstance) { |
| 12 | + return createErrorResponse("Current instance not found", 500); |
| 13 | + } |
| 14 | + |
| 15 | + const apiUrl = `${currentInstance.apiUrl}/api`; |
| 16 | + |
| 17 | + if (!apiUrl) { |
| 18 | + return createErrorResponse("API_URL is not configured", 500); |
| 19 | + } |
| 20 | + |
| 21 | + // Get the path and query parameters from the request URL |
| 22 | + const eventUrl = new URL(event.rawUrl || `https://${event.headers.host}${event.path}`); |
| 23 | + const path = eventUrl.pathname.replace('/.netlify/functions/api', ''); |
| 24 | + |
| 25 | + // Create the target URL with the path |
| 26 | + const targetUrl = new URL(path, apiUrl); |
| 27 | + |
| 28 | + // Copy all query parameters from the original request URL |
| 29 | + for (const [key, value] of eventUrl.searchParams.entries()) { |
| 30 | + targetUrl.searchParams.set(key, value); |
| 31 | + } |
| 32 | + |
| 33 | + // Also check event.queryStringParameters as a fallback for compatibility |
| 34 | + const params = event.queryStringParameters || {}; |
| 35 | + Object.keys(params).forEach(key => { |
| 36 | + // Only set if not already set from URL search params |
| 37 | + if (!targetUrl.searchParams.has(key)) { |
| 38 | + targetUrl.searchParams.set(key, params[key]); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + // Convert to string for the fetch request |
| 43 | + const targetUrlString = targetUrl.toString(); |
| 44 | + |
| 45 | + // Prepare headers to forward |
| 46 | + const headers = {}; |
| 47 | + |
| 48 | + // Forward relevant headers from the original request |
| 49 | + if (event.headers) { |
| 50 | + const headersToForward = [ |
| 51 | + 'authorization', |
| 52 | + 'content-type', |
| 53 | + 'accept', |
| 54 | + 'user-agent', |
| 55 | + 'x-requested-with' |
| 56 | + ]; |
| 57 | + |
| 58 | + for (const header of headersToForward) { |
| 59 | + if (event.headers[header]) { |
| 60 | + headers[header] = event.headers[header]; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Prepare fetch options |
| 66 | + const fetchOptions = { |
| 67 | + method: event.httpMethod || 'GET', |
| 68 | + headers |
| 69 | + }; |
| 70 | + |
| 71 | + // Forward request body for POST, PUT, PATCH methods |
| 72 | + if (['POST', 'PUT', 'PATCH'].includes(fetchOptions.method) && event.body) { |
| 73 | + fetchOptions.body = event.body; |
| 74 | + } |
| 75 | + |
| 76 | + // Log request details (only in development) |
| 77 | + if (process.env.NODE_ENV !== 'production') { |
| 78 | + console.log(`Proxying ${fetchOptions.method} request to: ${targetUrlString}`); |
| 79 | + } |
| 80 | + |
| 81 | + console.log('[TARGET URL]', targetUrlString); |
| 82 | + |
| 83 | + // Fetch the target URL and properly handle its response |
| 84 | + const response = await fetch(targetUrlString, fetchOptions); |
| 85 | + |
| 86 | + const json = await response.json(); |
| 87 | + |
| 88 | + console.log('[RESPONSE]', json); |
| 89 | + console.log('[RESPONSE STATUS]', response.status); |
| 90 | + |
| 91 | + // Return in the format expected by Netlify Functions |
| 92 | + return { |
| 93 | + statusCode: response.status, |
| 94 | + body: JSON.stringify(json), |
| 95 | + headers: { |
| 96 | + 'Content-Type': 'application/json', |
| 97 | + } |
| 98 | + }; |
| 99 | + } catch (error) { |
| 100 | + console.error("Error proxying request:", error); |
| 101 | + return createErrorResponse("Error proxying request to API", 500); |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +// Helper function to create error responses (updated for Netlify Functions format) |
| 106 | +function createErrorResponse(message, statusCode) { |
| 107 | + return { |
| 108 | + statusCode, |
| 109 | + body: JSON.stringify({ error: message }), |
| 110 | + headers: { |
| 111 | + "Content-Type": "application/json", |
| 112 | + "Access-Control-Allow-Origin": "*" |
| 113 | + } |
| 114 | + }; |
| 115 | +} |
| 116 | + |
| 117 | +// The path configuration is handled differently in netlify.toml for traditional functions |
0 commit comments