Skip to content

Commit 6510955

Browse files
committed
Correct broken file api.mjs
1 parent 4f09ded commit 6510955

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

netlify/functions/api.mjs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ exports.handler = async (event, context) => {
7979
}
8080
}
8181

82-
// Also check event.queryStringParameters as a fallback for compatibility
82+
// Also check event.queryStringParameters as a fallback
8383
const params = event.queryStringParameters || {};
8484
Object.keys(params).forEach(key => {
85-
// Only set if not already set from URL search params
8685
if (!targetUrl.searchParams.has(key)) {
8786
targetUrl.searchParams.set(key, params[key]);
8887
}
@@ -105,8 +104,12 @@ exports.handler = async (event, context) => {
105104
];
106105

107106
for (const header of headersToForward) {
108-
if (event.headers[header]) {
109-
headers[header] = event.headers[header];
107+
// Netlify lowercases header names, so check both cases
108+
const headerKey = Object.keys(event.headers).find(
109+
h => h.toLowerCase() === header.toLowerCase()
110+
);
111+
if (headerKey && event.headers[headerKey]) {
112+
headers[header] = event.headers[headerKey];
110113
}
111114
}
112115
}
@@ -165,8 +168,19 @@ exports.handler = async (event, context) => {
165168
}
166169
};
167170
} catch (error) {
171+
// Enhanced error logging
168172
console.error("Error proxying request:", error);
169-
return createErrorResponse("Error proxying request to API", 500);
173+
console.error("Error stack:", error.stack);
174+
console.error("Error details:", JSON.stringify({
175+
message: error.message,
176+
name: error.name,
177+
cause: error.cause
178+
}, null, 2));
179+
180+
return createErrorResponse(
181+
`Error proxying request to API: ${ error.message }`,
182+
500
183+
);
170184
}
171185
};
172186

@@ -181,3 +195,5 @@ function createErrorResponse(message, statusCode) {
181195
}
182196
};
183197
}
198+
199+
// The path configuration is handled differently in netlify.toml for traditional functions

0 commit comments

Comments
 (0)