summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/performance_bar/services/performance_bar_service.js
blob: bc71911ae358b5c2c038a4c4d51327782f71ed81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import Vue from 'vue';
import _ from 'underscore';
import axios from '../../lib/utils/axios_utils';

let vueResourceInterceptor;

export default class PerformanceBarService {
  static fetchRequestDetails(peekUrl, requestId) {
    return axios.get(peekUrl, { params: { request_id: requestId } });
  }

  static registerInterceptor(peekUrl, callback) {
    const interceptor = response => {
      const requestId = response.headers['x-request-id'];
      // 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';

      if (requestUrl !== peekUrl && requestId && !cachedResponse) {
        callback(requestId, requestUrl);
      }

      return response;
    };

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

    Vue.http.interceptors.push(vueResourceInterceptor);

    return axios.interceptors.response.use(interceptor);
  }

  static removeInterceptor(interceptor) {
    axios.interceptors.response.eject(interceptor);
    Vue.http.interceptors = _.without(
      Vue.http.interceptors,
      vueResourceInterceptor,
    );
  }
}