In my first Nuxt3 app I'm trying to setup my external API requests within the server/api folder.
With the configuration below, on a successful request everything is great. However, I am struggling to determine how to get the error response back out.
My <TestApi></TestApi> File:
<template>
<div>
<div>Check developer console!</div>
</div>
</template>
<script setup>
onMounted(async () => {
const company = await $fetch('/api/getCompany', {
async onRequestError({ request, options, error }) {
// Log error
console.log('[fetch request error]', request, error)
}
})
console.log(company)
})
// const config = useRuntimeConfig()
// console.log('Runtime config:', config.apiBase)
// if (process.server) {
// console.log('API secret:', config.apiSecret)
// }
</script>
My server/api/getCompany.ts file:
export default defineEventHandler(async (event) => {
//Defined in nuxt.config.ts
const config = useRuntimeConfig()
const baseURL = config.public.apiBase
const company = await $fetch(baseURL+'/company?bookingAppApiKey='+config.apiSecret, {
async onRequestError({ request, options, error }) {
// Log error
console.log('[fetch request error]', request, error)
}
})
return company
})
I would like to get the remote URL request response code and message out of /server/api/getCompany.ts for handling in <TestApi>.