2018-05-09 12:01:36 +05:30
|
|
|
import Vue from 'vue';
|
|
|
|
import _ from 'underscore';
|
|
|
|
import axios from '../../lib/utils/axios_utils';
|
2018-12-23 12:14:25 +05:30
|
|
|
import { parseBoolean } from '~/lib/utils/common_utils';
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
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 => {
|
2018-12-13 13:39:08 +05:30
|
|
|
const [fireCallback, requestId, requestUrl] = PerformanceBarService.callbackParams(
|
|
|
|
response,
|
|
|
|
peekUrl,
|
|
|
|
);
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
if (fireCallback) {
|
2018-05-09 12:01:36 +05:30
|
|
|
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);
|
2018-12-13 13:39:08 +05:30
|
|
|
Vue.http.interceptors = _.without(Vue.http.interceptors, vueResourceInterceptor);
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
static callbackParams(response, peekUrl) {
|
|
|
|
const requestId = response.headers && 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 apiRequest = requestUrl && requestUrl.match(/^\/api\//);
|
2018-12-23 12:14:25 +05:30
|
|
|
const cachedResponse =
|
|
|
|
response.headers && parseBoolean(response.headers['x-gitlab-from-cache']);
|
2018-11-20 20:47:30 +05:30
|
|
|
const fireCallback = requestUrl !== peekUrl && requestId && !apiRequest && !cachedResponse;
|
|
|
|
|
|
|
|
return [fireCallback, requestId, requestUrl];
|
|
|
|
}
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|