performance_bar_service.js 1.2 KB
Newer Older
1 2
import Vue from 'vue';
import _ from 'underscore';
3 4
import axios from '../../lib/utils/axios_utils';

5 6
let vueResourceInterceptor;

7 8 9 10 11 12
export default class PerformanceBarService {
  static fetchRequestDetails(peekUrl, requestId) {
    return axios.get(peekUrl, { params: { request_id: requestId } });
  }

  static registerInterceptor(peekUrl, callback) {
13
    const interceptor = response => {
14
      const requestId = response.headers['x-request-id'];
15 16 17 18
      // Get the request URL from response.config for Axios, and response for
      // Vue Resource.
      const requestUrl = (response.config || response).url;
      const cachedResponse = response.headers['x-gitlab-from-cache'] === 'true';
19

20
      if (requestUrl !== peekUrl && requestId && !cachedResponse) {
21 22 23 24
        callback(requestId, requestUrl);
      }

      return response;
25 26 27 28 29 30 31
    };

    vueResourceInterceptor = (request, next) => next(interceptor);

    Vue.http.interceptors.push(vueResourceInterceptor);

    return axios.interceptors.response.use(interceptor);
32 33 34 35
  }

  static removeInterceptor(interceptor) {
    axios.interceptors.response.eject(interceptor);
36 37 38 39
    Vue.http.interceptors = _.without(
      Vue.http.interceptors,
      vueResourceInterceptor,
    );
40 41
  }
}