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

Commit

Permalink
docs(api): add example for fetch interceptors (#7180)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <[email protected]>
  • Loading branch information
HomWang and pi0 authored Sep 5, 2022
1 parent 27c1188 commit 74db9d5
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions docs/content/3.api/1.composables/use-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,31 @@ If you have not fetched data on the server (for example, with `server: false`),
## 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

0 comments on commit 74db9d5

Please sign in to comment.