Skip to content

Instantly share code, notes, and snippets.

@jadehopepunk
Created January 12, 2018 22:44
Show Gist options
  • Select an option

  • Save jadehopepunk/6d99bc9a61ae9ac8652d91f40c4637fa to your computer and use it in GitHub Desktop.

Select an option

Save jadehopepunk/6d99bc9a61ae9ac8652d91f40c4637fa to your computer and use it in GitHub Desktop.

Revisions

  1. jadehopepunk created this gist Jan 12, 2018.
    19 changes: 19 additions & 0 deletions fetchWithHeaders.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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)
    }