|
| 1 | +import axios from 'axios'; |
| 2 | + |
| 3 | +export default class Axios { |
| 4 | + constructor(http) { |
| 5 | + this.instance = axios.create(http); |
| 6 | + |
| 7 | + if(http.access_token) { |
| 8 | + this.instance.defaults.headers.common['Authorization'] = `Bearer ${http.access_token}`; |
| 9 | + } |
| 10 | + |
| 11 | + this.instance.interceptors.response.use( |
| 12 | + response => http.onResponse(response), |
| 13 | + error => http.onError(error), |
| 14 | + ); |
| 15 | + |
| 16 | + return this.instance; |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Head Request |
| 21 | + * @param {string} url |
| 22 | + * @param {object} config |
| 23 | + */ |
| 24 | + async head(url, config = {}) { |
| 25 | + return this.instance.head(url, config); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * GET Request |
| 30 | + * @param {string} url |
| 31 | + * @param {object} config |
| 32 | + */ |
| 33 | + async get(url, config = {}) { |
| 34 | + return this.instance.get(url, config); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * POST Request |
| 39 | + * @param {string} url |
| 40 | + * @param {object} config |
| 41 | + */ |
| 42 | + async post(url, data = {}, config = {}) { |
| 43 | + return this.instance.post(url, data, config); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * PATCH Request |
| 48 | + * @param {string} url |
| 49 | + * @param {object} config |
| 50 | + */ |
| 51 | + async patch(url, data = {}, config = {}) { |
| 52 | + return this.instance.patch(url, data, config); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * PUT Request |
| 57 | + * @param {string} url |
| 58 | + * @param {object} config |
| 59 | + */ |
| 60 | + async put(url, data = {}, config = {}) { |
| 61 | + return this.instance.put(url, data, config); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * DELETE Request |
| 66 | + * @param {string} url |
| 67 | + * @param {object} config |
| 68 | + */ |
| 69 | + async delete(url, config = {}) { |
| 70 | + return this.instance.delete(url, config); |
| 71 | + } |
| 72 | +} |
0 commit comments