I have an api route that calls another api like (openai completions, another nuxt api route or an agent). How can I make sure that when I abort the outer api, the inner api calls are aborted as well?
I'm trying to do it with event.node.req.on("close") since "aborted" is deprecated according to https://nodejs.org/api/http.html
Can't find any documentation on this and I would guess its a pretty standard usecase
See the example below
// /server/api/routeA.get.ts
export default defineEventHandler(async (event) => {
// Create an AbortController tied to the client request
const controller = new AbortController()
// If the client disconnects, abort the controller
event.node.req.on('close', () => {
controller.abort()
})
try {
// Pass the abort signal to the inner fetch
const data = await $fetch('/api/routeB', {
signal: controller.signal
})
return {
message: 'Fetched routeB',
data
}
} catch (err) {
if (err.name === 'AbortError') {
return { error: 'Request aborted' }
}
throw err
}
})```