added vuex store for books
parent
1b5febf169
commit
498e30e6f4
@ -0,0 +1,41 @@
|
||||
import axios from 'axios'
|
||||
|
||||
axios.defaults.baseURL = 'http://localhost:5000'
|
||||
|
||||
// To avoid adding the token to every request we make to a service we can
|
||||
// instead use an interceptor
|
||||
axios.interceptors.request.use(function (config) {
|
||||
// added for when the code is running on a server for serverside rendering
|
||||
if (typeof window === 'undefined') {
|
||||
return config
|
||||
}
|
||||
const token = window.localStorage.getItem('token')
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
|
||||
return config
|
||||
})
|
||||
|
||||
const appService = {
|
||||
getPing() {
|
||||
return new Promise((resolve) => {
|
||||
axios.get('/ping')
|
||||
.then(response => {
|
||||
resolve(response.data)
|
||||
})
|
||||
.catch(error => {alert(error)})
|
||||
})
|
||||
},
|
||||
getAllBooks() {
|
||||
return new Promise((resolve) => {
|
||||
axios.get('/books')
|
||||
.then(response => {
|
||||
resolve(response.data)
|
||||
})
|
||||
.catch(error => {alert(error)})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default appService
|
Loading…
Reference in New Issue