Total Complexity | 5 |
Complexity/F | 1.67 |
Lines of Code | 29 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import axios from 'axios'; |
||
2 | import {goto} from '@sapper/app'; |
||
3 | import {TokenStorage} from './tokenStorage'; |
||
4 | import config from '../../config'; |
||
5 | |||
6 | export const client = axios.create({ |
||
7 | baseURL: config.API_URL |
||
8 | }); |
||
9 | |||
10 | client.interceptors.request.use(conf => { |
||
11 | if ('login' !== conf.url) { |
||
12 | conf.headers.Authorization = `Bearer ${TokenStorage.get()}`; |
||
13 | } |
||
14 | |||
15 | return conf; |
||
16 | }); |
||
17 | |||
18 | client.interceptors.response.use( |
||
19 | response => response, |
||
20 | error => { |
||
21 | const {responseURL} = error.request; |
||
22 | |||
23 | if (401 === error.response.status && -1 === responseURL.indexOf('login')) { |
||
24 | return goto('/login'); |
||
25 | } |
||
26 | |||
27 | return Promise.reject(error); |
||
28 | } |
||
29 | ); |
||
30 |