utils.js (751B)
1 const queryParams = (params) => { 2 return Object.keys(params) 3 .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k])) 4 .join('&') 5 } 6 7 const headers = (store) => { 8 const accessToken = store.state.oauth.token 9 if (accessToken) { 10 return {'Authorization': `Bearer ${accessToken}`} 11 } else { 12 return {} 13 } 14 } 15 16 const request = ({method = 'GET', url, params, store}) => { 17 const instance = store.state.instance.server 18 let fullUrl = `${instance}${url}` 19 20 if (method === 'GET' && params) { 21 fullUrl = fullUrl + `?${queryParams(params)}` 22 } 23 24 return window.fetch(fullUrl, { 25 method, 26 headers: headers(store), 27 credentials: 'same-origin' 28 }) 29 } 30 31 const utils = { 32 queryParams, 33 request 34 } 35 36 export default utils