text: import { URL } from '@lvchengbin/url';
  import ajax from './ajax';
  import lc from './localcache';
  import { mergeParams } from './utils';
  import assign from '@lvchengbin/kit/src/object-assign';
  
  function resJSON( xhr ) {
      return /^application\/json;/i.test( xhr.getResponseHeader( 'content-type' ) );
  }
  
  function request( url, options = {} ) {
  
      if( options.auth ) {
          if( !options.headers ) {
              options.headers = {};
          }
          const username = options.auth.username || '';
          const password = options.auth.password || '';
          options.headers.Authorization = 'Basic ' + btoa( username + ':' + password );
      }
  
      options.xhr || ( options.xhr =  new XMLHttpRequest() );
  
      return ajax( url, options ).then( response => {
          const status = response.status;
  
          if( status < 200 || status >= 300 ) {
              throw response;
          }
  
          if( options.fullResponse ) {
              return response;
          }
  
          if( options.rawBody ) {
              return response.body;
          }
  
          if( resJSON( options.xhr ) || options.type === 'json' ) {
              return response.json();
          }
  
          return response.body;
      } );
  }
  
  function get( url, options = {} ) {
  
      const {
          cache = false,
          fullResponse = false,
          rawBody = false,
          localcache = false
      } = options;
  
      options = assign( {}, options, {
          method : 'GET'
      } );
  
      url = new URL( url, location.href );
      mergeParams( url.searchParams, options.params ); 
  
      options.params = {};
  
      if( cache === false ) {
          options.params[ '_' + +new Date ] = '_';
      }
  
      if( !localcache ) {
          return request( url, options );
      }
  
      return lc.get( url, localcache ).catch( () => {
  
          options.fullResponse = true;
  
          options.xhr || ( options.xhr = new XMLHttpRequest() );
  
          return request( url, options ).then( response => {
  
              const isJSON = ( resJSON( options.xhr ) || options.type === 'json' );
  
              if( !localcache.mime ) {
                  if( isJSON ) {
                      localcache.mime = 'application/json';
                  } else {
                      localcache.mime = response.headers[ 'Content-Type' ] || 'text/plain';
                  }
              }
  
              return lc.set( url.toString(), response.body, localcache ).then( () => {
  
                  if( fullResponse ) {
                      return response;
                  }
  
                  if( rawBody ) {
                      return response.body;
                  }
  
                  if( isJSON ) {
                      return response.json();
                  }
  
                  return response.body;
              } );
          } );
      } );
  }
  
  function post( url, options = {} ) {
      options.method = 'POST';
  
      const { contentType = true } = options;
  
      if( !options.headers ) {
          options.headers = {};
      }
  
      if( contentType && !options.headers[ 'Content-Type' ] ) {
          options.headers[ 'Content-Type' ] = 'application/x-www-form-urlencoded; charset=UTF-8';
      }
  
      return request( url, options );
  }
  
  export { request, get, post };
  