mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-21 21:25:11 +00:00
docs: add example of typing custom useFetch
errors (#29253)
This commit is contained in:
parent
f188258016
commit
cb6cd82b8c
@ -96,6 +96,28 @@ const { data: modules } = await useAPI('/modules')
|
||||
</script>
|
||||
```
|
||||
|
||||
If you want to customize the type of any error returned, you can also do so:
|
||||
|
||||
```ts
|
||||
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
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
::note
|
||||
This example demonstrates how to use a custom `useFetch`, but the same structure is identical for a custom `useAsyncData`.
|
||||
::
|
||||
|
Loading…
Reference in New Issue
Block a user