Created
February 28, 2018 10:33
-
-
Save kntmr/c233b6cda063546717e927a47f9fae5a to your computer and use it in GitHub Desktop.
API module
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 debug = process.env.NODE_ENV !== 'production' | |
const onSuccess = (resp) => { | |
if (debug) { | |
console.log(' << ' + JSON.stringify(resp.data)) | |
} | |
return Promise.resolve(resp.data) | |
} | |
const onError = () => { | |
throw new Error('API error.') | |
} | |
export default { | |
get: (url, params) => { | |
if (debug) { | |
console.log('GET ' + url + ' >> ' + JSON.stringify(params)) | |
} | |
return axios.get(url, { params: params }).then(onSuccess).catch(onError) | |
}, | |
post: (url, params) => { | |
if (debug) { | |
console.log('POST ' + url + ' >> ' + JSON.stringify(params)) | |
} | |
return axios.post(url, params).then(onSuccess).catch(onError) | |
}, | |
put: (url, params) => { | |
if (debug) { | |
console.log('PUT ' + url + ' >> ' + JSON.stringify(params)) | |
} | |
return axios.put(url, params).then(onSuccess).catch(onError) | |
}, | |
delete: (url, params) => { | |
if (debug) { | |
console.log('DELETE ' + url + ' >> ' + JSON.stringify(params)) | |
} | |
return axios.delete(url, { params: params }).then(onSuccess).catch(onError) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment