Created
January 12, 2018 22:44
-
-
Save jadehopepunk/6d99bc9a61ae9ac8652d91f40c4637fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function fetchWithHeaders(url: string, jwt: ?string = null, params: ParamType = {}) { | |
const customHeaders = params.headers || {} | |
const otherParams = omit(params, 'headers') | |
const headers = { | |
'X-Requested-With': 'XMLHttpRequest', | |
'X-CSRF-Token': window.rippleCSRF, | |
Accept: 'application/json, text/plain, */*', | |
'Content-Type': 'application/json', | |
...customHeaders | |
} | |
if (jwt) { | |
headers.Authorization = `Bearer ${jwt}` | |
} | |
const fetchParams = { ...otherParams, headers, credentials: 'same-origin' } | |
if (params.data) { | |
fetchParams.body = JSON.stringify(params.data) | |
} | |
return fetch(url, fetchParams) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment