Closed hjyssg closed 3 years ago
import _ from "underscore";
import 'whatwg-fetch';
const Sender = {};
_.resHandle = function (res) {
if (res.status === 200 || res.status === 304) {
return res.json();
}
//need to name res.res
console.error('[failed]', res.status, res.statusText);
res.failed = true;
debugger
res.text();
return res;
};
// going to remove simplePost
// Sender.simplePost = function (api, body, callback) {
// fetch(api, {
// method: 'POST',
// headers: {
// Accept: 'application/json',
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify(body)
// })
// .then(res => {
// if(!(res.status === 200 || res.status === 304)){
// res.failed = true;
// }
// callback(res);
// });
// };
Sender.post = function (api, body, callback) {
const pp = fetch(api, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(body)
})
.then(_.resHandle);
if(callback){
pp.then(callback);
}else{
return pp;
}
};
Sender.get = function (api, callback) {
const pp = fetch(api)
.then(_.resHandle);
if(callback){
pp.then(callback);
}else{
return pp;
}
};
//going to remove this lsdir
Sender.lsDir = function (body, callback) {
Sender.post('/api/lsDir', body, callback);
};
export default Sender;