summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/performance_bar/services/performance_bar_service.js
blob: 3ebfaa87a4e5e300ba35a7b0564d6a6b02ccde74 (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
42
43
44
45
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) {
    vueResourceInterceptor = (request, next) => {
      next(response => {
        const requestId = response.headers['x-request-id'];
        const requestUrl = response.url;

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

    Vue.http.interceptors.push(vueResourceInterceptor);

    return axios.interceptors.response.use(response => {
      const requestId = response.headers['x-request-id'];
      const requestUrl = response.config.url;

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

      return response;
    });
  }

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