Skip to content

Commit

Permalink
fix(axios-instance): ensure defaults.headers[method] is defined
Browse files Browse the repository at this point in the history
supporting redaxios
  • Loading branch information
leomp12 committed Mar 10, 2021
1 parent a1b1605 commit 39b6c24
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/lib/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ const instance = axios.create({
timeout: 60000
})
// always JSON for request with body data
const { defaults } = instance
;['post', 'patch', 'put'].forEach(method => {
if (!instance.defaults.headers) {
instance.defaults.headers = {}
if (!defaults.headers) {
defaults.headers = {
[method]: {}
}
} else if (!defaults.headers[method]) {
defaults.headers[method] = {}
}
instance.defaults.headers[method]['Content-Type'] = 'application/json'
defaults.headers[method]['Content-Type'] = 'application/json'
})

export default instance

0 comments on commit 39b6c24

Please sign in to comment.