aboutsummaryrefslogtreecommitdiff
path: root/src/requests.ts
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-11-14 23:11:06 +0300
committereug-vs <eug-vs@keemail.me>2020-11-14 23:11:06 +0300
commitf96e6174fe90f3d2b1e3016861017cfcc25a6ddc (patch)
tree74baaa47fc42e11c48c2e82c2ce3ae788d9b303c /src/requests.ts
parentb978e6a4958cb543bd73259ec6d43e98dfb7ae18 (diff)
downloadfamcs-kit-f96e6174fe90f3d2b1e3016861017cfcc25a6ddc.tar.gz
feat: add APIClient
Diffstat (limited to 'src/requests.ts')
-rw-r--r--src/requests.ts27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/requests.ts b/src/requests.ts
new file mode 100644
index 0000000..368ae40
--- /dev/null
+++ b/src/requests.ts
@@ -0,0 +1,27 @@
+import axios from 'axios';
+import _ from 'lodash';
+
+const baseURL = process.env.NODE_ENV === 'production'
+ ? 'https://bsu-fantom.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.removeItem('remember');
+ window.location.reload();
+ }
+ return Promise.reject(error);
+});
+
+export const {
+ get, post, put, patch
+} = requests;
+export default requests;
+