mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-19 09:55:53 +00:00
docs(api): add example for fetch interceptors (#7180)
Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
parent
27c11884b4
commit
74db9d525e
@ -72,12 +72,31 @@ If you have not fetched data on the server (for example, with `server: false`),
|
|||||||
## Example
|
## Example
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
const { data, pending, error, refresh } = await useFetch(
|
const { data, pending, error, refresh } = await useFetch('https://api.nuxtjs.dev/mountains',{
|
||||||
'https://api.nuxtjs.dev/mountains',
|
|
||||||
{
|
|
||||||
pick: ['title']
|
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"}
|
:ReadMore{link="/guide/features/data-fetching"}
|
||||||
|
Loading…
Reference in New Issue
Block a user