forked from andrebradshaw/utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandleFetch.js
More file actions
45 lines (45 loc) · 1.8 KB
/
handleFetch.js
File metadata and controls
45 lines (45 loc) · 1.8 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
async function handleFetch(url,params_obj,type){ //all arguments are required
if(params_obj && url){
var res = await fetch(url,params_obj).catch(err=> { console.log([err,url,params_obj]); return false });
if(res.status > 199 && res.status < 300){
if(type == 'json'){
var d = await res.json().catch(err=> { console.log([err,url,params_obj]); return false });
}else{
var d = await res.text().catch(err=> { console.log([err,url,params_obj]); return false });
}
return d;
}
if(res.status == 429) {
await delay(60000);
var res = await fetch(url,params_obj).catch(err=> { console.log([err,url,params_obj]); return {} });
if(res.status > 199 && res.status < 300){
if(type == 'json'){
var d = await res.json().catch(err=> { console.log([err,url,params_obj]); return false });
}else{
var d = await res.text().catch(err=> { console.log([err,url,params_obj]); return false });
}
return d;
}else{
return {download_now: true, status: res.status};
}
}
if(res.status > 499 && res.status < 900) {
await delay(3110);
var res = await fetch(url,params_obj).catch(err=> { console.log([err,url,params_obj]); return false });
if(res.status > 199 && res.status < 300){
if(type == 'json'){
var d = await res.json().catch(err=> { console.log([err,url,params_obj]); return false });
}else{
var d = await res.text().catch(err=> { console.log([err,url,params_obj]); return false });
}
return d;
}else{
return {download_now: true, status: res.status};
}
}
if(res.status > 899) {
console.log('you have been logged out');
return {download_now: true, status: res.status};
}
} else {return false;}
}