Skip to content

Instantly share code, notes, and snippets.

@kntmr
Created February 28, 2018 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kntmr/c233b6cda063546717e927a47f9fae5a to your computer and use it in GitHub Desktop.
Save kntmr/c233b6cda063546717e927a47f9fae5a to your computer and use it in GitHub Desktop.
API module
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