Welcome to mirror list, hosted at ThFree Co, Russian Federation.

axios_startup_calls.js « utils « lib « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a047cebc8abf1c7975c2c0369496e88bffe0db61 (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
46
47
48
49
50
51
52
53
54
55
import { isEmpty } from 'lodash';
import { mergeUrlParams } from './url_utility';

// We should probably not couple this utility to `gon.gitlab_url`
// Also, this would replace occurrences that aren't at the beginning of the string
const removeGitLabUrl = url => url.replace(gon.gitlab_url, '');

const getFullUrl = req => {
  const url = removeGitLabUrl(req.url);
  return mergeUrlParams(req.params || {}, url);
};

const setupAxiosStartupCalls = axios => {
  const { startup_calls: startupCalls } = window.gl || {};

  if (!startupCalls || isEmpty(startupCalls)) {
    return;
  }

  // TODO: To save performance of future axios calls, we can
  // remove this interceptor once the "startupCalls" have been loaded
  axios.interceptors.request.use(req => {
    const fullUrl = getFullUrl(req);

    const existing = startupCalls[fullUrl];

    if (existing) {
      // eslint-disable-next-line no-param-reassign
      req.adapter = () =>
        existing.fetchCall.then(res => {
          const fetchHeaders = {};
          res.headers.forEach((val, key) => {
            fetchHeaders[key] = val;
          });

          // eslint-disable-next-line promise/no-nesting
          return res
            .clone()
            .json()
            .then(data => ({
              data,
              status: res.status,
              statusText: res.statusText,
              headers: fetchHeaders,
              config: req,
              request: req,
            }));
        });
    }

    return req;
  });
};

export default setupAxiosStartupCalls;