about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/util/api.js
blob: c59a24518c406c4ca54c698d5f67431f9d2eae8c (plain) (blame)
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
import axios from 'axios';
import ready from './ready';
import LinkHeader from 'http-link-header';

export const getLinks = response => {
  const value = response.headers.link;

  if (!value) {
    return { refs: [] };
  }

  return LinkHeader.parse(value);
};

let csrfHeader = {};

function setCSRFHeader() {
  const csrfToken = document.querySelector('meta[name=csrf-token]');
  if (csrfToken) {
    csrfHeader['X-CSRF-Token'] = csrfToken.content;
  }
}

ready(setCSRFHeader);

export default getState => axios.create({
  headers: Object.assign(csrfHeader, getState ? {
    'Authorization': `Bearer ${getState().getIn(['meta', 'access_token'], '')}`,
  } : {}),

  transformResponse: [function (data) {
    try {
      return JSON.parse(data);
    } catch(Exception) {
      return data;
    }
  }],
});