Created
April 29, 2019 15:25
-
-
Save pat123456/2ee5ff79602e1aab6a17ee7f2684e686 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
const http = { | |
/** | |
* Default Base URL | |
*/ | |
baseURL: 'http://localhost/.../api/', | |
/** | |
* Default URL | |
*/ | |
url: '/', | |
/** | |
* Default Method | |
*/ | |
method: 'get', | |
/** | |
* Access Token Variable | |
*/ | |
access_token: '', | |
/** | |
* Default Headers | |
*/ | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
/** | |
* Default Data | |
*/ | |
//data: {}, | |
/** | |
* Default Timout | |
*/ | |
timeout: 0, | |
/** | |
* Default With Credentials Flag | |
*/ | |
withCredentials: false, | |
/** | |
* Default Response Type | |
*/ | |
responseType: 'json', | |
/** | |
* Default Response Encoding | |
*/ | |
responseEncoding: 'utf8', | |
/** | |
* Default Validate Status Method | |
* @param {number} status | |
*/ | |
validateStatus(status) { | |
return status >= 200 && status < 300; // default | |
}, | |
/** | |
* Default Max Redirects | |
*/ | |
maxRedirects: 5, | |
/** | |
* Default Socket Path | |
*/ | |
socketPath: null, | |
/** | |
* Default Proxy | |
*/ | |
proxy: {}, | |
/** | |
* Default on Response | |
* @param {object} response | |
*/ | |
onResponse(response) { | |
return response.data; | |
}, | |
/** | |
* On 401 Unauthorised | |
* @param {object} error | |
*/ | |
onUnauthorised(error) { | |
// | |
}, | |
/** | |
* On 404 Not Found | |
* @param {object} error | |
*/ | |
onNotFound(error) { | |
// | |
}, | |
/** | |
* On 500 Server Error | |
* @param {object} error | |
*/ | |
onServerError(error) { | |
// | |
}, | |
/** | |
* On Generic Error | |
* @param {object} error | |
*/ | |
onGenericError(error) { | |
// | |
}, | |
/** | |
* On Laravel Validation Error (Or 422 Error). | |
* @param {object} error | |
*/ | |
onValidationError(error) { | |
// | |
}, | |
/** | |
* Default on Error | |
* @param {object} error | |
*/ | |
onError(error) { | |
switch (error.response.status) { | |
case 401: | |
this.onUnauthorised(error); | |
break; | |
case 404: | |
this.onNotFound(error); | |
break; | |
case 422: | |
this.onValidationError(error); | |
break; | |
case 500: | |
this.onServerError(error); | |
break; | |
default: | |
this.onGenericError(error); | |
break; | |
} | |
return Promise.reject(error); | |
}, | |
}; | |
export default { | |
axios: axios.create(http), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment