koel/resources/assets/js/services/http.js
Fiete Börner c970481f19 add support for non root installations (#673)
* remove all absolute forward slashes to make relative urls

* set global baseUrl in common blade template

use this variable in all api calls

* fix absolute url paths in scss

set a base href in app index files for all relative resource paths

* rename baseUrl to BASE_URL
2017-10-26 15:59:53 +01:00

67 lines
1.9 KiB
JavaScript

import axios from 'axios'
import NProgress from 'nprogress'
import { event } from '@/utils'
import { ls } from '@/services'
/**
* Responsible for all HTTP requests.
*/
export const http = {
request (method, url, data, successCb = null, errorCb = null) {
method = method.toLowerCase()
axios.request({ url, data, method }).then(successCb).catch(errorCb)
},
get (url, successCb = null, errorCb = null) {
return this.request('get', url, {}, successCb, errorCb)
},
post (url, data, successCb = null, errorCb = null) {
return this.request('post', url, data, successCb, errorCb)
},
put (url, data, successCb = null, errorCb = null) {
return this.request('put', url, data, successCb, errorCb)
},
delete (url, data = {}, successCb = null, errorCb = null) {
return this.request('delete', url, data, successCb, errorCb)
},
/**
* Init the service.
*/
init () {
axios.defaults.baseURL = `${window.BASE_URL}api`
// Intercept the request to make sure the token is injected into the header.
axios.interceptors.request.use(config => {
config.headers.Authorization = `Bearer ${ls.get('jwt-token')}`
return config
})
// Intercept the response and…
axios.interceptors.response.use(response => {
NProgress.done()
// …get the token from the header or response data if exists, and save it.
const token = response.headers['Authorization'] || response.data['token']
token && ls.set('jwt-token', token)
return response
}, error => {
NProgress.done()
// Also, if we receive a Bad Request / Unauthorized error
if (error.response.status === 400 || error.response.status === 401) {
// and we're not trying to login
if (!(error.config.method === 'post' && /\/api\/me\/?$/.test(error.config.url))) {
// the token must have expired. Log out.
event.emit('logout')
}
}
return Promise.reject(error)
})
}
}