N
Nuxt4mo ago
Mole

Import FetchError type

Hi! What is the current method of importing FetchError type? Ref: https://discord.com/channels/473401852243869706/1224809697689862156 Ref: https://github.com/nuxt/nuxt/issues/26628 I do not think FetchError should be imported from ofetch. Using error type from package may fail instanceof. It is also explicitly recommended to use $fetch instead of importing from ofetch.
No description
3 Replies
kapa.ai
kapa.ai4mo ago
Hi @Mole. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I'm sorry, but the knowledge sources provided do not explicitly document the recommended way to import the FetchError type in Nuxt 3, nor do they clarify whether you should import it from ofetch or another source. The available documentation and examples show usage of FetchError from ofetch in custom composables, such as:
import type { FetchError } from 'ofetch'
import type { FetchError } from 'ofetch'
This is demonstrated in the official Nuxt recipe for creating a custom useFetch composable, where FetchError is imported from ofetch and used as the error type parameter for useFetch:
import type { FetchError } from 'ofetch'
import type { UseFetchOptions } from 'nuxt/app'

interface CustomError {
message: string
statusCode: number
}

export function useAPI<T>(
url: string | (() => string),
options?: UseFetchOptions<T>,
) {
return useFetch<T, FetchError<CustomError>>(url, {
...options,
$fetch: useNuxtApp().$api
})
}
import type { FetchError } from 'ofetch'
import type { UseFetchOptions } from 'nuxt/app'

interface CustomError {
message: string
statusCode: number
}

export function useAPI<T>(
url: string | (() => string),
options?: UseFetchOptions<T>,
) {
return useFetch<T, FetchError<CustomError>>(url, {
...options,
$fetch: useNuxtApp().$api
})
}
Custom useFetch in Nuxt However, the sources do not address the concern about instanceof checks or provide an alternative to importing FetchError from ofetch. If you need strict instanceof compatibility with errors thrown by $fetch in Nuxt, the documentation does not clarify whether importing FetchError from ofetch is sufficient or if there is a Nuxt-specific type you should use. If you require further clarification, you may need to consult the Nuxt core team or check for updates in the official documentation or GitHub issues, as the current knowledge sources do not provide a definitive answer. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4mo ago
<@522852263979384852> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Mole
MoleOP4mo ago
Surprisingly good response for AI. Does not answer the question tho :)

Did you find this page helpful?