I have the following serverless function
import type { APIGatewayEvent, Context } from 'aws-lambda'
import { logger } from 'src/lib/logger'
const request = require('request')
export const handler = async (event: APIGatewayEvent, context: Context) => {
const result = await sendToApi(event)
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: { result },
}),
}
}
const sendToApi = function (event) {
return new Promise((resolve) => {
logger.info('Invoked twitter function')
const { twitter } = JSON.parse(event.body)
const options = {
method: 'GET',
url: `https://api.twitter.com/2/users/by/username/${twitter}?user.fields=profile_image_url`,
headers: {
Authorization: `Bearer ${process.env.TWITTER_BEARER_TOKEN}`,
},
contentType: 'application/json',
}
request(options, function (error, response) {
if (error) throw new Error(error)
console.log('monkeyboogers', response.body)
resolve(response.body)
})
})
}
and the following front end code that calls the function
const profile = await fetch(
`${window.location.origin}/.redwood/functions/twitter`,
{
method: 'POST',
body: JSON.stringify({ twitter: twitter }),
}
)
console.log(await profile)
setTwitter(await profile)
The problem is in the server it console logs the data just fine but in the front end it console logs the following
body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "http://localhost:8910/.redwood/functions/twitter"
and inside the body is just ReadableStream
I know I am missing some key piece, but I am not sure 100% what that is. Thanks for any help ahead of time.l I appreciate it a lot.