Skip to content

Instantly share code, notes, and snippets.

@jadehopepunk
Created January 12, 2018 22:44
Show Gist options
  • Save jadehopepunk/6d99bc9a61ae9ac8652d91f40c4637fa to your computer and use it in GitHub Desktop.
Save jadehopepunk/6d99bc9a61ae9ac8652d91f40c4637fa to your computer and use it in GitHub Desktop.
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