aboutsummaryrefslogtreecommitdiff
path: root/src/services/new_api/utils.js
blob: 078f392fc03d3690fce09646ab97aca58f50e710 (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
const queryParams = (params) => {
  return Object.keys(params)
    .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
    .join('&')
}

const headers = (store) => {
  const accessToken = store.state.oauth.token
  if (accessToken) {
    return {'Authorization': `Bearer ${accessToken}`}
  } else {
    return {}
  }
}

const request = ({method = 'GET', url, params, store}) => {
  const instance = store.state.instance.server
  let fullUrl = `${instance}${url}`

  if (method === 'GET' && params) {
    fullUrl = fullUrl + `?${queryParams(params)}`
  }

  return window.fetch(fullUrl, {
    method,
    headers: headers(store),
    credentials: 'same-origin'
  })
}

const utils = {
  queryParams,
  request
}

export default utils