aboutsummaryrefslogtreecommitdiff
path: root/src/requests.ts
blob: e1f82b44053534e0a59485129c6abe8186311c1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import axios from 'axios';
import _ from 'lodash';

const baseURL = process.env.NODE_ENV === 'production'
  ? 'https://which-api.herokuapp.com'
  : 'http://localhost:3030';

const requests = axios.create({ baseURL });

requests.interceptors.request.use(config => {
  const token = localStorage.getItem('token');
  return _.set(config, 'headers.Authorization', token);
});

requests.interceptors.response.use(response => response, error => {
  if (error.message === 'Request failed with status code 401' && localStorage.getItem('token')) {
    localStorage.setItem('shouldClear', 'true');
    window.location.reload();
  }
  return Promise.reject(error);
});

export const {
  get, post, put, patch
} = requests;
export default requests;