You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.0 KiB
JavaScript
41 lines
1.0 KiB
JavaScript
7 years ago
|
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
|