Open xgqfrms opened 5 years ago
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description fetchAPIs
* @augments
* @example
* @link
*
*/
const getToken = (url = ``, debug = false) => {
let log = console.log;
let error = console.error;
if (url) {
return fetch(url, {
method: "GET",
cors: "no",
})
.then(res => res.json())
.then(json => {
if (debug) {
log(`JSON =`, JSON.stringify(json, null, 4));
}
let Token = ``;
let {
Result,
Code,
} = json;
if (Code === 200) {
Token = Result[0].Token;
DD_Token = Token;
} else {
DD_Token = "";
}
})
.catch(err => {
error(`fetch error`, err);
});
} else {
error(`fetch url is empty!`);
return ``;
}
};
const fetchPOST = (options = {}, debug = false) => {
let log = console.log;
let error = console.error;
let {
url,
token,
} = options;
let parmas = {
"Type": "attention",
"Batch": 0,
"Token": token,
};
return fetch(url, {
method: "POST",
cors: "no",
body: JSON.stringify(parmas),
})
.then(res => res.json())
.then(json => {
if (!debug) {
log(`JSON =`, JSON.stringify(json, null, 4));
}
})
.catch(err => {
error(`fetch error`, err);
});
};
const fetchGET = (options = {}, debug = false) => {
let log = console.log;
let error = console.error;
let {
url,
token,
} = options;
const URL = `${url}?token=${token}`;
return fetch(URL, {
method: "GET",
cors: "no",
})
.then(res => res.json())
.then(json => {
if (!debug) {
log(`JSON =`, JSON.stringify(json, null, 4));
}
})
.catch(err => {
error(`fetch error`, err);
});
};
const fetchAPIs = {
getToken,
fetchPOST,
fetchGET,
};
export default fetchAPIs;
export {
getToken,
fetchPOST,
fetchGET,
};
Fetch API & Async Await