Skip to content

Instantly share code, notes, and snippets.

@mmuchin
Forked from d-levin/api-module.js
Created May 8, 2019 12:48
Show Gist options
  • Save mmuchin/8a7b1cd3bfdd49c7cc24390202723b37 to your computer and use it in GitHub Desktop.
Save mmuchin/8a7b1cd3bfdd49c7cc24390202723b37 to your computer and use it in GitHub Desktop.
Reusable Javascript ES6 API module using Axios
import axios from 'axios'
function httpRequest (method, url, request) {
return axios[method](url, request)
.then(response => Promise.resolve(response))
.catch(error => Promise.reject(error))
}
export default {
get (url, request) {
return httpRequest('get', url, request)
},
delete (url, request) {
return httpRequest('delete', url, request)
},
post (url, request) {
return httpRequest('post', url, request)
},
put (url, request) {
return httpRequest('put', url, request)
},
patch (url, request) {
return httpRequest('patch', url, request)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment