Passed
Push — master ( 085900...85d83b )
by Mathieu
227:05 queued 225:41
created

client/src/utils/axios.js   A

Complexity

Total Complexity 5
Complexity/F 1.67

Size

Lines of Code 29
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 17
mnd 2
bc 2
fnc 3
dl 0
loc 29
rs 10
bpm 0.6666
cpm 1.6666
noi 0
c 0
b 0
f 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