Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

docs(api): add example for fetch interceptors #7180

Merged
merged 7 commits into from
Sep 5, 2022
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions docs/content/3.api/1.composables/use-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This composable provides a convenient wrapper around [`useAsyncData`](/api/compo

## Type

```ts [Signature]
```ts [Signature]
pi0 marked this conversation as resolved.
Show resolved Hide resolved
function useFetch(
url: string | Request | Ref<string | Request> | () => string | Request,
options?: UseFetchOptions<DataT>
Expand Down Expand Up @@ -69,12 +69,31 @@ By default, Nuxt waits until a `refresh` is finished before it can be executed a
## Example

```ts
const { data, pending, error, refresh } = await useFetch(
'https://api.nuxtjs.dev/mountains',
{
const { data, pending, error, refresh } = await useFetch('https://api.nuxtjs.dev/mountains',{
pick: ['title']
})
```

Using [interceptors](https://github.com/unjs/ohmyfetch#%EF%B8%8F-interceptors):

```ts
const { data, pending, error, refresh } = await useFetch('/api/auth/login', {
onRequest({ request, options }) {
// Set the request headers
options.headers = options.headers || {}
options.headers.authorization = '...'
},
onRequestError({ request, options, error }) {
// Handle the request errors
},
onResponse({ request, response, options }) {
// Process the response data
return response._data
},
onResponseError({ request, response, options }) {
// Pandle the response errors
}
)
})
```

:ReadMore{link="/guide/features/data-fetching"}
Expand Down