Bug/fix infinite loop (#512)

* fix infinite loop with safe get method

* fix ingredients

Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
Hayden
2021-06-13 14:07:58 -08:00
committed by GitHub
parent 2dc9c8e843
commit af41b08a60
7 changed files with 88 additions and 47 deletions

View File

@@ -27,47 +27,88 @@ function defaultSuccessText(response) {
return response.statusText;
}
const apiReq = {
post: async function(url, data, getErrorText = defaultErrorText, getSuccessText) {
const response = await axios.post(url, data).catch(function(error) {
const requests = {
/**
*
* @param {*} funcCall Callable Axios Function
* @param {*} url Destination url
* @param {*} data Request Data
* @param {*} getErrorText Error Text Function
* @param {*} getSuccessText Success Text Function
* @returns Object response
*/
unsafe: async function(funcCall, url, data, getErrorText = defaultErrorText, getSuccessText) {
const response = await funcCall(url, data).catch(function(error) {
handleError(error, getErrorText);
return error;
});
return handleResponse(response, getSuccessText);
},
/**
*
* @param {*} funcCall Callable Axios Function
* @param {*} url Destination url
* @param {*} data Request Data
* @param {*} getErrorText Error Text Function
* @param {*} getSuccessText Success Text Function
* @returns Array [response, error]
*/
safe: async function(funcCall, url, data, getErrorText = defaultErrorText, getSuccessText) {
const response = await funcCall(url, data).catch(function(error) {
handleError(error, getErrorText);
return [null, error];
});
return [handleResponse(response, getSuccessText), null];
},
};
const apiReq = {
get: async function(url, getErrorText = defaultErrorText) {
return axios.get(url).catch(function(error) {
handleError(error, getErrorText);
});
},
getSafe: async function(url) {
let error = null;
const response = await axios.get(url).catch(e => {
error = e;
});
return [response, error];
},
post: async function(url, data, getErrorText = defaultErrorText, getSuccessText) {
return await requests.unsafe(axios.post, url, data, getErrorText, getSuccessText);
},
postSafe: async function(url, data, getErrorText = defaultErrorText, getSuccessText) {
return await requests.safe(axios.post, url, data, getErrorText, getSuccessText);
},
put: async function(url, data, getErrorText = defaultErrorText, getSuccessText) {
const response = await axios.put(url, data).catch(function(error) {
handleError(error, getErrorText);
return error;
});
return handleResponse(response, getSuccessText);
return await requests.unsafe(axios.put, url, data, getErrorText, getSuccessText);
},
putSafe: async function(url, data, getErrorText = defaultErrorText, getSuccessText) {
return await requests.safe(axios.put, url, data, getErrorText, getSuccessText);
},
patch: async function(url, data, getErrorText = defaultErrorText, getSuccessText) {
const response = await axios.patch(url, data).catch(function(error) {
handleError(error, getErrorText);
return error;
});
return handleResponse(response, getSuccessText);
return await requests.unsafe(axios.patch, url, data, getErrorText, getSuccessText);
},
get: async function(url, data, getErrorText = defaultErrorText) {
return axios.get(url, data).catch(function(error) {
handleError(error, getErrorText);
return error;
});
patchSafe: async function(url, data, getErrorText = defaultErrorText, getSuccessText) {
return await requests.safe(axios.patch, url, data, getErrorText, getSuccessText);
},
delete: async function(url, data, getErrorText = defaultErrorText, getSuccessText = defaultSuccessText) {
const response = await axios.delete(url, data).catch(function(error) {
handleError(error, getErrorText);
return error;
});
return handleResponse(response, getSuccessText);
return await requests.unsafe(axios.delete, url, data, getErrorText, getSuccessText);
},
async download(url) {
deleteSafe: async function(url, data, getErrorText = defaultErrorText, getSuccessText = defaultSuccessText) {
return await requests.unsafe(axios.delete, url, data, getErrorText, getSuccessText);
},
download: async function(url) {
const response = await this.get(url);
const token = response.data.fileToken;