formatted
This commit is contained in:
@@ -5,38 +5,42 @@ import axios from "axios";
|
||||
const defaultOptions = {
|
||||
data: {},
|
||||
requestOptions: {
|
||||
headers: {}
|
||||
headers: {},
|
||||
},
|
||||
notification: {
|
||||
show: true,
|
||||
success: false,
|
||||
failed: true
|
||||
failed: true,
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
const useRequest = (initOptions) => {
|
||||
const _options = Object.assign({}, defaultOptions, initOptions);
|
||||
|
||||
return async (url = '', method = 'get', options) => {
|
||||
return async (url = "", method = "get", options) => {
|
||||
const mergedOptions = Object.assign({}, _options, options);
|
||||
|
||||
try {
|
||||
const response = await axios({
|
||||
url,
|
||||
method,
|
||||
data: method === 'get' ? null : mergedOptions.data,
|
||||
data: method === "get" ? null : mergedOptions.data,
|
||||
withCredentials: true,
|
||||
...mergedOptions.requestOptions
|
||||
...mergedOptions.requestOptions,
|
||||
});
|
||||
successRequest(mergedOptions.notification.show && mergedOptions.notification.success, 'request_data');
|
||||
successRequest(mergedOptions.notification.show && mergedOptions.notification.success, "request_data");
|
||||
return response;
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
errorResponse(error.response, mergedOptions.notification.show && mergedOptions.notification.failed, 'request_data');
|
||||
errorResponse(
|
||||
error.response,
|
||||
mergedOptions.notification.show && mergedOptions.notification.failed,
|
||||
"request_data"
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export default useRequest
|
||||
export default useRequest;
|
||||
|
||||
Reference in New Issue
Block a user