mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-15 02:14:44 +00:00
docs: add example of typing custom useFetch
errors (#29253)
This commit is contained in:
parent
3331994f6a
commit
280e5923ea
@ -96,6 +96,28 @@ const { data: modules } = await useAPI('/modules')
|
|||||||
</script>
|
</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
|
::note
|
||||||
This example demonstrates how to use a custom `useFetch`, but the same structure is identical for a custom `useAsyncData`.
|
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