1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- function checkStatus(response) {
- if (response.status >= 200 && response.status < 300) {
- return response;
- }
- const error = new Error(response.statusText);
- error.response = response;
- throw error;
- }
- function parseJSON(response) {
- return response.json();
- }
- function checkAPIDatas(data) {
- console.log('data',data)
- if (!data.success) {
- const code = data;
- }
- return data
- }
- function checkAPIError(err) {
- const code = err.response.code;
- const status = err.response.status;
-
-
- return {
- err
- };
- }
- const request = async (url, options,callBack) => {
- if (options) {
- const method = options.method.toUpperCase();
- if (method === 'POST' || method === 'PATCH' || method === 'DELETE' || method === 'PUT') {
- options.headers = {
- 'Content-Type': 'application/json',
- 'token': await storage.load({
- key: 'token'
- })
- };
- } else {
- options.headers = {
- 'token': await storage.load({
- key: 'token'
- })
- };
- }
- }
- return fetch(url, options)
- .then(checkStatus)
- .then(parseJSON)
- .then(checkAPIDatas)
- .catch(checkAPIError);
- }
- export default request
|