export default defineEventHandler(async (event) => {
// Nitro path matches the Laravel API path so we just forward it
const path = event.path
const response = await $fetch.raw(`http://my-new-api.dev/${path}`, {
// Proxy method and headers
method: event.method,
headers: event.headers,
// Prevent from throwing error so I can access the raw response and handle it
ignoreResponseError: true,
})
// Try and make our response match the one from Laravel precognition.
// This is where it fails, `statusCode` is not set correctly (again, expected since documented)
// And Response.json(response.body) does not set the body correctly (which I don't understand)
event.node.res.statusCode = response.status
response.headers.forEach((header, name) => event.node.res.setHeader(name, header))
event.node.res.statusMessage = response.statusText
return Response.json(response.body)
})
export default defineEventHandler(async (event) => {
// Nitro path matches the Laravel API path so we just forward it
const path = event.path
const response = await $fetch.raw(`http://my-new-api.dev/${path}`, {
// Proxy method and headers
method: event.method,
headers: event.headers,
// Prevent from throwing error so I can access the raw response and handle it
ignoreResponseError: true,
})
// Try and make our response match the one from Laravel precognition.
// This is where it fails, `statusCode` is not set correctly (again, expected since documented)
// And Response.json(response.body) does not set the body correctly (which I don't understand)
event.node.res.statusCode = response.status
response.headers.forEach((header, name) => event.node.res.setHeader(name, header))
event.node.res.statusMessage = response.statusText
return Response.json(response.body)
})