File tree 3 files changed +23
-3
lines changed
3 files changed +23
-3
lines changed Original file line number Diff line number Diff line change 49
49
"name" : " hello" ,
50
50
"alterEgo" : " world" ,
51
51
"id" : 10
52
+ },
53
+ {
54
+ "name" : " Updated" ,
55
+ "alterEgo" : " axios interceptors" ,
56
+ "id" : 11
52
57
}
53
58
],
54
59
"friends" : [
Original file line number Diff line number Diff line change
1
+ import axios from 'axios'
2
+
3
+ const client = axios . create ( { baseURL : 'http://localhost:4000' } )
4
+
5
+ export const request = ( { ...options } ) => {
6
+ client . defaults . headers . common . Authorization = 'Bearer token'
7
+
8
+ const onSuccess = ( response : any ) => response
9
+ const onError = ( error : Error ) => {
10
+ // optionaly catch errors and add additional logging here
11
+ return error
12
+ }
13
+
14
+ return client ( options ) . then ( onSuccess ) . catch ( onError )
15
+ }
Original file line number Diff line number Diff line change 1
1
import { useQuery , useMutation , useQueryClient } from 'react-query'
2
2
import axios from 'axios'
3
- import { SuperHero } from 'interfaces '
3
+ import { request } from 'api/Api '
4
4
5
5
interface Props {
6
6
onSuccess : ( response : any ) => void
7
7
onError : ( error : Error ) => void
8
8
}
9
9
10
10
const fetchSuperHeroes = ( ) => {
11
- return axios . get ( 'http://localhost:4000/ superheroes')
11
+ return request ( { url : '/ superheroes' } )
12
12
}
13
13
14
14
const addSuperHero = ( hero : { name : string ; alterEgo : string } ) => {
15
- return axios . post ( 'http://localhost:4000/ superheroes', hero )
15
+ return request ( { url : '/ superheroes', method : 'post' , data : hero } )
16
16
}
17
17
18
18
export const useSuperHeroesData = ( { onSuccess, onError } : Props ) => {
You can’t perform that action at this time.
0 commit comments