ars-react
Version:
Making React and Redux and React router dom easy to use and make with ARS react
85 lines (73 loc) • 2.4 kB
JavaScript
import axios from "axios";
ars.logout = {
logout_api_url: "log_out",
redirect_route: "",
set: (logout_api_url, redirect_route) => {
ars.logout.logout_api_url = logout_api_url;
ars.logout.redirect_route = redirect_route;
},
call: () => {
ars.local_user = undefined;
localStorage.removeItem("access_token");
ars.post(ars.logout.logout_api_url).then(() => {
window.location.href = ars.logout.redirect_route
});
}
}
function formatLangToStandardForm(lang){
switch (lang){
case 'en':
return 'en-US'
case 'tr':
return 'tr-TR'
}
}
ars.send_request = (method, url, data, headers, config) => {
let headerPlaceHolder = {}
if (headers) {
headerPlaceHolder = Object.assign({}, headers);
}
headerPlaceHolder["Content-Language"] = formatLangToStandardForm(ars.lang || 'tr')
return new Promise(async (resolve, err) => {
let options = {
method,
url,
data,
headers : headerPlaceHolder,
withCredentials: true,
...config
};
if (method === "get"){
options["params"] = ars.objectToSerial(Object.assign({}, data))
}
try {
resolve(await axios(options))
} catch (error) {
err(error)
}
});
};
ars.post = (url, data_to_pass, headers, config) => {
return ars.send_request("post", url, data_to_pass, headers, config);
};
ars.get = (url, data_to_pass, headers, config) => {
return ars.send_request("get", url, data_to_pass, headers, config);
};
ars.create_blob_url = (url, data_to_pass, headers) => {
return new Promise((res, err) => {
ars.get(url, data_to_pass, headers, {
responseType: 'arraybuffer'
}).then(async response => {
if (response.data) {
var file_format = "";
if (response.headers["content-type"])
file_format = response.headers["content-type"];
const blob = new Blob([response.data], {type: file_format});
const url = URL.createObjectURL(blob);
res(url);
}
}).catch(error => {
err(error);
});
})
}