src/utils/request.js
/**
 * 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) {
  const defaultOptions = {
    credentials: 'include',
  };
  const newOptions = { ...defaultOptions, ...options };
  if (newOptions.method === 'POST' || newOptions.method === 'PUT') {
    newOptions.headers = {
      Accept: 'application/json',
      'Content-Type': 'application/json; charset=utf-8',
      ...newOptions.headers,
    };
    newOptions.body = JSON.stringify(newOptions.body);
  }
  return fetch(url, newOptions)
    .then(checkStatus)
    .then(response => response.json())
    .catch(error => {
      if (error.code) {
        notification.error({
          message: error.name,
          description: error.message,
        });
      }
      if ('stack' in error && 'message' in error) {
        notification.error({
          message: `请求错误: ${url}`,
          description: error.message,
        });
      }
      return error;
    });
}
function request ( url , options ) { const defaultOptions = { credentials : 'include' , } ; const newOptions = { ... defaultOptions , ... options } ; if ( newOptions . method === 'POST' || newOptions . method === 'PUT' ) { newOptions . headers = { Accept : 'application/json' , 'Content-Type' : 'application/json; charset=utf-8' , ... newOptions . headers , } ; newOptions . body = JSON . stringify ( newOptions . body ) ; } return fetch ( url , newOptions ) . then ( checkStatus ) . then ( response => response . json ( ) ) . catch ( error => { if ( error . code ) { notification . error ( { message : error . name , description : error . message , } ) ; } if ( 'stack' in error && 'message' in error ) { notification . error ( { message : ` description : error . message , } ) ; } return error ; } ) ; }
Requests a URL, returning a promise.
Requests a URL returning a promise .