-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchRequest.js
45 lines (42 loc) · 1.14 KB
/
fetchRequest.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import axios from 'axios';
import API from './api';
export const fetchRequest = async ({
authorization = null,
path,
data = undefined,
method = 'POST',
}) => {
if (!authorization) {
console.error({status: false, msg: 'Please enter access token!'});
throw {status: false, msg: 'Please enter access token!'};
} else {
try {
const baseURL = API;
const instance = axios.create({
baseURL,
responseType: 'json',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${authorization}`,
},
});
try {
let response;
if (method == 'POST') {
response = await instance.post(path, data);
} else if (method == 'PUT') {
response = await instance.put(path, data);
} else if (method == 'PATCH') {
response = await instance.patch(path, JSON.stringify(data));
} else {
response = await instance.get(path);
}
return response?.data;
} catch (error) {
throw error;
} finally {
}
} catch (error) {}
}
};