aboutsummaryrefslogtreecommitdiff
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
parentb978e6a4958cb543bd73259ec6d43e98dfb7ae18 (diff)
downloadfamcs-kit-f96e6174fe90f3d2b1e3016861017cfcc25a6ddc.tar.gz
feat: add APIClient
-rw-r--r--package-lock.json18
-rw-r--r--package.json3
-rw-r--r--src/hooks/APIClient.ts17
-rw-r--r--src/requests.ts27
-rw-r--r--src/types.ts12
5 files changed, 77 insertions, 0 deletions
diff --git a/package-lock.json b/package-lock.json
index 1508f80..58e70f4 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -2135,6 +2135,11 @@
"resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz",
"integrity": "sha1-7ihweulOEdK4J7y+UnC86n8+ce4="
},
+ "@types/lodash": {
+ "version": "4.14.165",
+ "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.165.tgz",
+ "integrity": "sha512-tjSSOTHhI5mCHTy/OOXYIhi2Wt1qcbHmuXD1Ha7q70CgI/I71afO4XtLb/cVexki1oVYchpul/TOuu3Arcdxrg=="
+ },
"@types/minimatch": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz",
@@ -4858,6 +4863,11 @@
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz",
"integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak="
},
+ "dequal": {
+ "version": "2.0.2",
+ "resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.2.tgz",
+ "integrity": "sha512-q9K8BlJVxK7hQYqa6XISGmBZbtQQWVXSrRrWreHC94rMt1QL/Impruc+7p2CYSYuVIUr+YCt6hjrs1kkdJRTug=="
+ },
"des.js": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/des.js/-/des.js-1.0.1.tgz",
@@ -13598,6 +13608,14 @@
}
}
},
+ "swr": {
+ "version": "0.3.8",
+ "resolved": "https://registry.npmjs.org/swr/-/swr-0.3.8.tgz",
+ "integrity": "sha512-EHRlaqoBtHsB2wOB+dQJ74DrZvaRGu4BaIQrhkD+/rj8/UGo2iQXN+rCcYnV7/VAreBJBmm9+lDkwZmUqWEkKA==",
+ "requires": {
+ "dequal": "2.0.2"
+ }
+ },
"symbol-tree": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/symbol-tree/-/symbol-tree-3.2.4.tgz",
diff --git a/package.json b/package.json
index 6c7717f..47c634c 100644
--- a/package.json
+++ b/package.json
@@ -3,13 +3,16 @@
"version": "0.0.0",
"dependencies": {
"@material-ui/core": "^4.11.0",
+ "@types/lodash": "^4.14.165",
"@types/node": "^12.0.0",
"@types/react": "^16.9.53",
"@types/react-dom": "^16.9.8",
+ "lodash": "^4.17.20",
"react": "^17.0.1",
"react-benzin": "^4.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.0",
+ "swr": "^0.3.8",
"typescript": "^4.0.3"
},
"scripts": {
diff --git a/src/hooks/APIClient.ts b/src/hooks/APIClient.ts
new file mode 100644
index 0000000..d3c4542
--- /dev/null
+++ b/src/hooks/APIClient.ts
@@ -0,0 +1,17 @@
+import useSWR, { responseInterface } from 'swr';
+import { get } from '../requests';
+import { User, Event } from '../types';
+
+type Response<T> = responseInterface<T, Error>;
+
+const fetcher = (endpoint: string) => get(endpoint).then(response => response.data);
+
+
+export const useUser = (id: string): Response<User> => {
+ return useSWR(id && `/users/${id}`, fetcher);
+};
+
+export const useEvents = (): Response<Event[]> => {
+ return useSWR(`/events`, fetcher);
+};
+
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;
+
diff --git a/src/types.ts b/src/types.ts
new file mode 100644
index 0000000..dc429a4
--- /dev/null
+++ b/src/types.ts
@@ -0,0 +1,12 @@
+interface Base {
+ _id: string;
+ createdAt: string;
+ updatedAt: string;
+}
+
+export interface User extends Base {
+ username: string;
+}
+
+export interface Event extends Base {}
+