function request(
  url,
  options = {
    expirys: isAntdPro(),
  }
) {
  /**
   * Produce fingerprints based on url and parameters
   * Maybe url has the same parameters
   */
  const fingerprint = url + (options.body ? JSON.stringify(options.body) : '');
  const hashcode = hash
    .sha256()
    .update(fingerprint)
    .digest('hex');

  const defaultOptions = {
    credentials: 'include',
  };
  const newOptions = { ...defaultOptions, ...options };
  const csrftoken = getCookie('csrftoken');
  
  if (
    newOptions.method === 'POST' ||
    newOptions.method === 'PUT' ||
    newOptions.method === 'DELETE'
  ) {
    if (!(newOptions.body instanceof FormData)) {
      newOptions.headers = {
        Accept: 'application/json',
        'X-CSRFToken': csrftoken,
        'Content-Type': 'application/json; charset=utf-8',
        ...newOptions.headers,
      };
      newOptions.body = JSON.stringify(newOptions.body);
    } else {
      // newOptions.body is FormData
      newOptions.headers = {
        Accept: 'application/json',
        'X-CSRFToken': csrftoken,
        ...newOptions.headers,
      };
    }
  }

  const expirys = options.expirys || 60;
  // options.expirys !== false, return the cache,
  if (options.expirys !== false) {
    const cached = sessionStorage.getItem(hashcode);
    const whenCached = sessionStorage.getItem(`${hashcode}:timestamp`);
    if (cached !== null && whenCached !== null) {
      const age = (Date.now() - whenCached) / 1000;
      if (age < expirys) {
        const response = new Response(new Blob([cached]));
        return response.json();
      }
      sessionStorage.removeItem(hashcode);
      sessionStorage.removeItem(`${hashcode}:timestamp`);
    }
  }
  return fetch(url, newOptions)
    .then(checkStatus)
    .then(cachedSave)
    .then(response => {
      // DELETE and 204 do not return data by default
      // using .json will report an error.
      if (newOptions.method === 'DELETE' || response.status === 204) {
        message.success('删除成功')
        return response.text();
      }
      return response.json();
    })
    .catch(e => {
      const status = e.name;
      if (status === 401) {
        // @HACK
        /* eslint-disable no-underscore-dangle */
        window.g_app._store.dispatch({
          type: 'login/logout',
        });
        return;
      }
      // environment should not be used
      if (status === 403) {
        router.push('/user/login');
        return;
      }
      if (status <= 504 && status >= 500) {
        return;
      }
      if (status >= 404 && status < 422) {
      }
    });
}
/**
 * Requests a URL, returning a promise.
 *
 * @param  {string} url       The URL we want to request
 * @param  {object} [options] The options we want to pass to "fetch"
 * @return {object}           An object containing either "data" or "err"
 */
Requests a URL, returning a promise.
Requests a URL returning a promise .
function request(
  url,
  options = {
    expirys: isAntdPro(),
  }
) {
  const fingerprint = url + (options.body ? JSON.stringify(options.body) : '');
  const hashcode = hash
    .sha256()
    .update(fingerprint)
    .digest('hex');
  const defaultOptions = {
    credentials: 'include',
  };
  const newOptions = { ...defaultOptions, ...options };
  const csrftoken = getCookie('csrftoken');
  if (
    newOptions.method === 'POST' ||
    newOptions.method === 'PUT' ||
    newOptions.method === 'DELETE'
  ) {
    if (!(newOptions.body instanceof FormData)) {
      newOptions.headers = {
        Accept: 'application/json',
        'X-CSRFToken': csrftoken,
        'Content-Type': 'application/json; charset=utf-8',
        ...newOptions.headers,
      };
      newOptions.body = JSON.stringify(newOptions.body);
    } else {
      newOptions.headers = {
        Accept: 'application/json',
        'X-CSRFToken': csrftoken,
        ...newOptions.headers,
      };
    }
  }
  const expirys = options.expirys || 60;
  if (options.expirys !== false) {
    const cached = sessionStorage.getItem(hashcode);
    const whenCached = sessionStorage.getItem(`${hashcode}:timestamp`);
    if (cached !== null && whenCached !== null) {
      const age = (Date.now() - whenCached) / 1000;
      if (age < expirys) {
        const response = new Response(new Blob([cached]));
        return response.json();
      }
      sessionStorage.removeItem(hashcode);
      sessionStorage.removeItem(`${hashcode}:timestamp`);
    }
  }
  return fetch(url, newOptions)
    .then(checkStatus)
    .then(cachedSave)
    .then(response => {
      if (newOptions.method === 'DELETE' || response.status === 204) {
        message.success('删除成功')
        return response.text();
      }
      return response.json();
    })
    .catch(e => {
      const status = e.name;
      if (status === 401) {
        window.g_app._store.dispatch({
          type: 'login/logout',
        });
        return;
      }
      if (status === 403) {
        router.push('/user/login');
        return;
      }
      if (status <= 504 && status >= 500) {
        return;
      }
      if (status >= 404 && status < 422) {
      }
    });
}
function request ( url , options = { expirys : isAntdPro ( ) , } ) { const fingerprint = url + ( options . body ? JSON . stringify ( options . body ) : '' ) ; const hashcode = hash . sha256 ( ) . update ( fingerprint ) . digest ( 'hex' ) ; const defaultOptions = { credentials : 'include' , } ; const newOptions = { ... defaultOptions , ... options } ; const csrftoken = getCookie ( 'csrftoken' ) ; if ( newOptions . method === 'POST' || newOptions . method === 'PUT' || newOptions . method === 'DELETE' ) { if ( ! ( newOptions . body instanceof FormData ) ) { newOptions . headers = { Accept : 'application/json' , 'X-CSRFToken' : csrftoken , 'Content-Type' : 'application/json; charset=utf-8' , ... newOptions . headers , } ; newOptions . body = JSON . stringify ( newOptions . body ) ; } else { newOptions . headers = { Accept : 'application/json' , 'X-CSRFToken' : csrftoken , ... newOptions . headers , } ; } } const expirys = options . expirys || 60 ; if ( options . expirys !== false ) { const cached = sessionStorage . getItem ( hashcode ) ; const whenCached = sessionStorage . getItem ( ` ${ hashcode } ` ) ; if ( cached !== null && whenCached !== null ) { const age = ( Date . now ( ) - whenCached ) / 1000 ; if ( age < expirys ) { const response = new Response ( new Blob ( [ cached ] ) ) ; return response . json ( ) ; } sessionStorage . removeItem ( hashcode ) ; sessionStorage . removeItem ( ` ${ hashcode } ` ) ; } } return fetch ( url , newOptions ) . then ( checkStatus ) . then ( cachedSave ) . then ( response => { if ( newOptions . method === 'DELETE' || response . status === 204 ) { message . success ( '删除成功') return response . text ( ) ; } return response . json ( ) ; } ) . catch ( e => { const status = e . name ; if ( status === 401 ) { window . g_app . _store . dispatch ( { type : 'login/logout' , } ) ; return ; } if ( status === 403 ) { router . push ( '/user/login' ) ; return ; } if ( status <= 504 && status >= 500 ) { return ; } if ( status >= 404 && status < 422 ) { } } ) ; }
Requests a URL, returning a promise.
Requests a URL returning a promise .
/**
   * Produce fingerprints based on url and parameters
   * Maybe url has the same parameters
   */ // newOptions.body is FormData // options.expirys !== false, return the cache, // DELETE and 204 do not return data by default // using .json will report an error. // @HACK /* eslint-disable no-underscore-dangle */ // environment should not be used