2017-03-17 17:30:32 +00:00
|
|
|
import Vue from 'vue';
|
|
|
|
import VueResource from 'vue-resource';
|
2017-09-21 17:53:28 +00:00
|
|
|
import csrf from '../lib/utils/csrf';
|
2017-03-17 17:30:32 +00:00
|
|
|
|
|
|
|
Vue.use(VueResource);
|
2017-01-27 22:24:08 +00:00
|
|
|
|
2017-03-17 19:09:29 +00:00
|
|
|
// Maintain a global counter for active requests
|
2017-05-17 18:25:13 +00:00
|
|
|
// see: spec/support/wait_for_requests.rb
|
2017-01-27 22:24:08 +00:00
|
|
|
Vue.http.interceptors.push((request, next) => {
|
2017-03-17 19:09:29 +00:00
|
|
|
window.activeVueResources = window.activeVueResources || 0;
|
|
|
|
window.activeVueResources += 1;
|
2017-01-27 22:24:08 +00:00
|
|
|
|
2017-03-17 17:30:32 +00:00
|
|
|
next(() => {
|
2017-03-17 19:09:29 +00:00
|
|
|
window.activeVueResources -= 1;
|
2017-01-27 22:24:08 +00:00
|
|
|
});
|
|
|
|
});
|
2017-02-05 17:36:52 +00:00
|
|
|
|
2017-07-12 14:47:09 +00:00
|
|
|
// Inject CSRF token and parse headers.
|
|
|
|
// New Vue Resource version uses Headers, we are expecting a plain object to render pagination
|
|
|
|
// and polling.
|
2017-02-05 17:36:52 +00:00
|
|
|
Vue.http.interceptors.push((request, next) => {
|
2017-09-21 17:53:28 +00:00
|
|
|
request.headers.set(csrf.headerKey, csrf.token);
|
2017-07-12 14:47:09 +00:00
|
|
|
|
|
|
|
next((response) => {
|
|
|
|
// Headers object has a `forEach` property that iterates through all values.
|
|
|
|
const headers = {};
|
|
|
|
|
|
|
|
response.headers.forEach((value, key) => {
|
|
|
|
headers[key] = value;
|
|
|
|
});
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
response.headers = headers;
|
|
|
|
});
|
2017-02-05 17:36:52 +00:00
|
|
|
});
|