Last active
August 28, 2020 09:20
-
-
Save vanpav/99958bd1b583df4b6c42290515d28bdd to your computer and use it in GitHub Desktop.
Small wrapper around Axios lib for post requests in Django
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 Promise from 'promise-polyfill'; | |
window.Promise = window.Promise || Promise; | |
import axios from 'axios'; | |
import qs from 'qs'; | |
const DJANGO_DEFAULTS = { | |
xsrfCookieName: 'csrftoken', | |
xsrfHeaderName: 'X-CSRFToken', | |
headers: { | |
'X-Requested-With': 'XMLHttpRequest' | |
} | |
}; | |
export const http = (config) => { | |
this.axios = axios.create({ | |
...DJANGO_DEFAULTS, | |
...config | |
}); | |
let fn = (config) => { | |
return this.axios.request({ | |
...config, | |
data: qs.stringify(config.data || {}) | |
}); | |
}; | |
['delete', 'get', 'head'].forEach(method => { | |
fn[method] = this.axios[method]; | |
}); | |
['post', 'put', 'patch'].forEach(method => { | |
fn[method] = (url, data, config) => ( | |
this.axios[method](url, qs.stringify(data), config) | |
); | |
}); | |
return fn; | |
}; | |
export default http(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment