debian-mirror-gitlab/app/assets/javascripts/vue_shared/vue_resource_interceptor.js

36 lines
972 B
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
import Vue from 'vue';
import VueResource from 'vue-resource';
Vue.use(VueResource);
// Maintain a global counter for active requests
2017-09-10 17:25:29 +05:30
// see: spec/support/wait_for_requests.rb
2017-08-17 22:00:37 +05:30
Vue.http.interceptors.push((request, next) => {
window.activeVueResources = window.activeVueResources || 0;
window.activeVueResources += 1;
next(() => {
window.activeVueResources -= 1;
});
});
2017-09-10 17:25:29 +05:30
// Inject CSRF token and parse headers.
// New Vue Resource version uses Headers, we are expecting a plain object to render pagination
// and polling.
2017-08-17 22:00:37 +05:30
Vue.http.interceptors.push((request, next) => {
if ($.rails) {
2017-09-10 17:25:29 +05:30
request.headers.set('X-CSRF-Token', $.rails.csrfToken());
2017-08-17 22:00:37 +05:30
}
2017-09-10 17:25:29 +05:30
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-08-17 22:00:37 +05:30
});