Last active
December 20, 2019 22:53
-
-
Save oal/c2f63d27c554b85f51d85f88eb4965c1 to your computer and use it in GitHub Desktop.
Quasar boot: Axios + Django Rest Framework
This file contains hidden or 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 axios from 'axios'; | |
export default async ({ Vue }) => { | |
let axiosInstance = axios.create({ | |
baseURL: '/api/' | |
}); | |
axiosInstance.interceptors.response.use(null, error => { | |
if(error.response.data) { | |
Object.keys(error.response.data).forEach(key => { | |
let data = error.response.data; | |
if(Array.isArray(data[key])) { | |
data[key] = data[key].join(' '); | |
} | |
}); | |
return Promise.reject(error); | |
} | |
}); | |
Vue.prototype.$axios = axiosInstance; | |
} | |
// This will result in error data looking like | |
// {"username": "This field is required"} rather than | |
// {"username": ["This field is required"]} | |
// | |
// In Quasar you can then set `this.errors = error.response.data` in the Promise reject and | |
// do <q-input ... :error="!!errors.username" :error-message="errors.username"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment