Skip to content

Instantly share code, notes, and snippets.

@cezar-popa
Forked from d-levin/api-module.js
Created December 23, 2021 09:08
Show Gist options
  • Save cezar-popa/3fa86cb6865057100bbd614cde1e32e8 to your computer and use it in GitHub Desktop.
Save cezar-popa/3fa86cb6865057100bbd614cde1e32e8 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