Hello, I have several Netlify Functions that are running well on my hosted netlify instance.
tl;dr Functions 2.0 function code below, run’s fine in netlify hosted instance, throws 500 error locally.
Up until this point, I have had no issues with using netlify dev
to test my functions locally, however my latest function is playing up.
On the hosted netlify instance, the function runs fine, and even when it did have issues I was able to diagnose them via the functions log (I can’t seem to find how to access the functions log on my local machine via netlify cli).
When using netlify dev, the function below throws a 500 error and nothing meaningful is logged to the console. My other functions run fine locally.
import { Context } from "@netlify/functions";
console.log('Function requested');
export default async (req: Request, context: Context) => {
console.log('Function invoked');
const urlParams = new URL(req.url).searchParams;
const authCode = urlParams.get('code');
console.log(`Auth code: ${authCode}`);
if (!authCode) {
console.log('No auth code provided');
return new Response("Authentication Code not provided", { status: 400 });
}
const encoder = new TextEncoder();
const body = new ReadableStream({
async start(controller) {
try {
console.log('Making first API call');
const response1 = await fetch('http://localhost:8888/api/auth/w/access-token', {
method: "POST",
body: JSON.stringify({ code: authCode }),
headers: {
"Content-Type": "application/json",
},
});
console.log(`First API call response: ${response1}`);
if (!response1.ok) {
console.log(`First API call error: ${response1.statusText}`);
const errorResponse = `Error: ${response1.statusText}`;
controller.enqueue(encoder.encode(errorResponse));
controller.close();
return new Response(errorResponse, { status: 400 });
}
const authorization = await response1.json();
console.log(`Access token: ${JSON.stringify(authorization)}`);
/* example:
{"access_token":"5a7c1a83efa73763bbe0a960c91e057e47cc69c268448df1eda6f3a1ad86aa3b","token_type":"bearer","scope":"authorized_user:read"}
*/
controller.enqueue(encoder.encode(JSON.stringify({ status: "Access Token received" })));
console.log('Making second API call');
const response2 = await fetch('http://localhost:8888/api/auth/w/get-authorized-user', {
method: "GET",
headers: {
"webflow-access-token": authorization.access_token,
},
});
console.log(`Second API call response: ${response2}`);
if (!response2.ok) {
console.log(`Second API call error: ${response2.statusText}`);
const errorResponse = `Error: ${response2.statusText}`;
controller.enqueue(encoder.encode(errorResponse));
controller.close();
return new Response(errorResponse, { status: 400 });
}
const wfUser = await response2.json();
console.log(`Webflow user:`, JSON.stringify(wfUser));
controller.enqueue(encoder.encode(JSON.stringify({ status: "User Info received", wfUser })));
controller.close();
} catch (error) {
console.log(`Caught error: ${error}`);
return new Response(`Error: ${error}`, { status: 400 });
}
}
});
return new Response(body, {
headers: {
"content-type": "application/json"
},
status: 200
});
};
I am now wondering if netlify cli needs to be updated in order to handle Functions 2.0