-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApi.js
More file actions
76 lines (63 loc) · 2.07 KB
/
Copy pathApi.js
File metadata and controls
76 lines (63 loc) · 2.07 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {trackPromise} from "react-promise-tracker";
const isErro = (res) => {
return res && !res.loading && res.status && [200, 201, 202, 204].indexOf(res.status) === -1;
};
const isSucesso = (res) => {
return res && !res.loading && res.status && [200, 201, 202, 204].indexOf(res.status) > -1;
};
const isLoading = (res) => {
return res && res.loading;
};
const execRestRequest = async (client, request, loading) => {
const doRequest = async (request) => {
try {
return await client(request);
} catch (error) {
console.error(error);
if (error?.response?.status === 401) {
window.location.href = "/";
}
return error.response;
}
};
if (loading) {
return trackPromise(doRequest(request));
}
return doRequest(request);
};
const get = async (client, url, loading = true) => {
return execRestRequest(client, {url, method: "get"}, loading);
};
const del = (client, url, loading = true) => {
return execRestRequest(client, {url, method: "delete"}, loading);
};
const post = (client, url, data, loading = true) => {
return execRestRequest(client, {url, method: "post", data}, loading);
};
const put = (client, url, data, loading = true) => {
return execRestRequest(client, {url, method: "put", data}, loading);
};
const uploadBlob = (client, url, data, loading = true) => {
return execRestRequest(client,
{
url,
method: "post",
data,
headers: {
"Content-Type": `blob.type; boundary=${data._boundary}`,
}
}, loading);
}
const uploadFile = (client, url, data, loading = true, progress = null) => {
return execRestRequest(client,
{
url,
method: "post",
data,
headers: {
"Content-Type": "multipart/form-data"
},
onUploadProgress: progress
}, loading);
}
export {isErro, isSucesso, isLoading, get, del, post, put, uploadFile, uploadBlob, execRestRequest};