Make a custom Vue plugin for axios

This commit is contained in:
Valentin Kaelin 2019-08-31 19:19:48 +02:00
parent 37c4315a06
commit 29134066b0
5 changed files with 41 additions and 60 deletions

View file

@ -5209,7 +5209,8 @@
"ansi-regex": { "ansi-regex": {
"version": "2.1.1", "version": "2.1.1",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"aproba": { "aproba": {
"version": "1.2.0", "version": "1.2.0",
@ -5624,7 +5625,8 @@
"safe-buffer": { "safe-buffer": {
"version": "5.1.2", "version": "5.1.2",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"safer-buffer": { "safer-buffer": {
"version": "2.1.2", "version": "2.1.2",
@ -5680,6 +5682,7 @@
"version": "3.0.1", "version": "3.0.1",
"bundled": true, "bundled": true,
"dev": true, "dev": true,
"optional": true,
"requires": { "requires": {
"ansi-regex": "^2.0.0" "ansi-regex": "^2.0.0"
} }
@ -5723,12 +5726,14 @@
"wrappy": { "wrappy": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
}, },
"yallist": { "yallist": {
"version": "3.0.3", "version": "3.0.3",
"bundled": true, "bundled": true,
"dev": true "dev": true,
"optional": true
} }
} }
}, },
@ -11150,11 +11155,6 @@
"integrity": "sha512-RRuo08A6mFye2RyLVdnODH5kyLiHANMl9EzKXZXCeMrsP4SY3nyjkQnTGlgbbVOBQuaGBMrFp9HPOJYDaVNk/w==", "integrity": "sha512-RRuo08A6mFye2RyLVdnODH5kyLiHANMl9EzKXZXCeMrsP4SY3nyjkQnTGlgbbVOBQuaGBMrFp9HPOJYDaVNk/w==",
"dev": true "dev": true
}, },
"vue-axios": {
"version": "2.1.4",
"resolved": "https://registry.npmjs.org/vue-axios/-/vue-axios-2.1.4.tgz",
"integrity": "sha512-DS8Q+WFT3i7nS0aZ/NMmTPf2yhbtlXhj4QEZmY69au/BshsGzGjC6dXaniZaPQlErP3J3Sv1HtQ4RVrXaUTkxA=="
},
"vue-eslint-parser": { "vue-eslint-parser": {
"version": "2.0.3", "version": "2.0.3",
"resolved": "https://registry.npmjs.org/vue-eslint-parser/-/vue-eslint-parser-2.0.3.tgz", "resolved": "https://registry.npmjs.org/vue-eslint-parser/-/vue-eslint-parser-2.0.3.tgz",

View file

@ -10,7 +10,6 @@
"dependencies": { "dependencies": {
"axios": "^0.18.1", "axios": "^0.18.1",
"vue": "^2.6.6", "vue": "^2.6.6",
"vue-axios": "^2.1.4",
"vue-router": "^3.0.6", "vue-router": "^3.0.6",
"vuex": "^3.1.1" "vuex": "^3.1.1"
}, },

View file

@ -1,6 +1,5 @@
import Vue from 'vue' import Vue from 'vue'
import axios from 'axios' import VueAxios from './plugins/axios'
import VueAxios from 'vue-axios'
import DotLoader from 'vue-spinner/src/DotLoader.vue' import DotLoader from 'vue-spinner/src/DotLoader.vue'
import '@/assets/css/main.css' import '@/assets/css/main.css'
@ -13,7 +12,7 @@ import router from './router'
import store from './store' import store from './store'
Vue.config.productionTip = false Vue.config.productionTip = false
Vue.use(VueAxios, axios) Vue.use(VueAxios)
Vue.component('v-icon', Icon) Vue.component('v-icon', Icon)
Vue.component('dot-loader', DotLoader) Vue.component('dot-loader', DotLoader)

View file

@ -0,0 +1,13 @@
import axiosHttp from 'axios'
export const axios = axiosHttp
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers.common['Content-Type'] = 'application/json'
axios.defaults.baseURL = process.env.NODE_ENV === 'development' ? 'http://localhost:5000/' : 'https://api.valentinkaelin.ch/'
export default {
install (Vue) {
Vue.prototype.$axios = axiosHttp
}
}

View file

@ -128,39 +128,24 @@ export default {
}, },
methods: { methods: {
apiCall() { async apiCall() {
console.log(this.$patch)
const summoner = this.summoner const summoner = this.summoner
const region = this.regionsList[this.region] const region = this.regionsList[this.region]
this.loading = true this.loading = true
this.axios({ try {
method: 'POST', const resp = await this.$axios(({ url: 'api', data: { summoner, region }, method: 'POST' }))
url: process.env.NODE_ENV === 'development' ? 'http://localhost:5000/api' : 'https://api.valentinkaelin.ch/api', if (resp.data) {
headers: { this.summonerFound = true
'Content-Type': 'application/json' this.createObject(resp.data)
}, } else {
data: { this.summonerFound = false
summoner,
region
}
})
.then(response => {
return response.data
})
.then(jsonData => {
if (jsonData) {
this.summonerFound = true
this.createObject(jsonData)
} else {
this.summonerFound = false
this.loading = false
console.log('Summoner not found')
}
})
.catch(err => {
this.loading = false this.loading = false
console.log(err) console.log('Summoner not found')
}) }
} catch (error) {
this.loading = false
console.log(error)
}
}, },
checkLocalStorage() { checkLocalStorage() {
if (localStorage[`${this.summoner}:${this.region}`]) { if (localStorage[`${this.summoner}:${this.region}`]) {
@ -260,26 +245,11 @@ export default {
this.loading = false this.loading = false
}, },
getChampionData() { async getChampionData() {
console.log('API CALL FOR CHAMPIONS') console.log('API CALL FOR CHAMPIONS')
const endpoint = 'Champion' const endpoint = 'Champion'
this.axios({ const resp = await this.$axios(({ url: 'ddragon', data: { endpoint }, method: 'POST' }))
method: 'POST', this.championsInfos = resp.data.data
url: process.env.NODE_ENV === 'development' ? 'http://localhost:5000/ddragon' : 'https://api.valentinkaelin.ch/ddragon',
headers: {
'Content-Type': 'application/json'
},
data: {
endpoint
}
})
.then(response => {
return response.data
})
.then(jsonData => {
console.log('here')
this.championsInfos = jsonData.data
})
}, },
getItemLink(id) { getItemLink(id) {
return `url('https://ddragon.leagueoflegends.com/cdn/${this.$patch}/img/item/${id === 0 ? 3637 : id}.png') no-repeat center center / contain` return `url('https://ddragon.leagueoflegends.com/cdn/${this.$patch}/img/item/${id === 0 ? 3637 : id}.png') no-repeat center center / contain`