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

35 lines
976 B
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
import Vue from 'vue';
import VueResource from 'vue-resource';
2018-03-17 18:26:18 +05:30
import csrf from '../lib/utils/csrf';
2017-08-17 22:00:37 +05:30
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) => {
2018-03-17 18:26:18 +05:30
request.headers.set(csrf.headerKey, csrf.token);
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
next(response => {
2017-09-10 17:25:29 +05:30
// Headers object has a `forEach` property that iterates through all values.
const headers = {};
response.headers.forEach((value, key) => {
headers[key] = value;
});
2018-11-20 20:47:30 +05:30
// eslint-disable-next-line no-param-reassign
2017-09-10 17:25:29 +05:30
response.headers = headers;
});
2017-08-17 22:00:37 +05:30
});