import URL from 'url-parser-lite';
import { isAppbase } from '../utils/index';
/**
 * Returns an instance of Appbase client
 * @param {Object} config To configure properties
 * @param {String} config.url
 * @param {String} config.app
 * @param {String} config.credentials
 * @param {String} config.username
 * @param {String} config.password
 * @param {Boolean} config.enableTelemetry
 * @param {Object} config.mongodb
 * A callback function which will be invoked before a fetch request made
 */
function AppBase(config) {
  const {
    auth = null,
    host = '',
    path = '',
    protocol = '',
  } = URL(config.url || '');
  let url = host + path;

  // Validate config and throw appropriate error
  if (typeof url !== 'string' || url === '') {
    throw new Error('URL not present in options.');
  }
if (!config.mongodb) {
 if (typeof config.app !== 'string' || config.app === '') {
    throw new Error('App name is not present in options.');
  }
}
  if (typeof protocol !== 'string' || protocol === '') {
    throw new Error(
      'Protocol is not present in url. URL should be of the form https://scalr.api.appbase.io',
    );
  }
  // Parse url
  if (url.slice(-1) === '/') {
    url = url.slice(0, -1);
  }
  let credentials = auth || null;
  /**
   * Credentials can be provided as a part of the URL,
   * as username, password args or as a credentials argument directly */
  if (typeof config.credentials === 'string' && config.credentials !== '') {
    // eslint-disable-next-line
    credentials = config.credentials;
  } else if (
    typeof config.username === 'string'
    && config.username !== ''
    && typeof config.password === 'string'
    && config.password !== ''
  ) {
    credentials = `${config.username}:${config.password}`;
  }

  if (isAppbase(url) && !config.mongodb && credentials === null) {
    throw new Error(
      'Authentication information is not present. Did you add credentials?',
    );
  }
  this.mongodb = config.mongodb;
  this.url = url;
  this.protocol = protocol;
  this.app = config.app;
  this.credentials = credentials;
  this.headers = {
    'X-Search-Client': 'Appbase JS',
  };
  if (config.enableTelemetry === false) {
    Object.assign(this.headers, {
      'X-Enable-Telemetry': config.enableTelemetry,
    });
  }
}
export default AppBase;

@@ -630,26 +630,25 @@
  let
+ %7B
  url 
-= host + path
+%7D = config
 ;%0A%0A 
@@ -802,156 +802,8 @@
   %7D%0A
-if (!config.mongodb) %7B%0A if (typeof config.app !== 'string' %7C%7C config.app === '') %7B%0A    throw new Error('App name is not present in options.');%0A  %7D%0A%7D
 %0A  i
@@ -984,16 +984,101 @@
  );%0A  %7D%0A
+%0A  let credentials = auth %7C%7C null;%0A  if (!config.mongodb) %7B%0A    url = host + path;%0A  
   // Par
@@ -1084,16 +1084,18 @@
 rse url%0A
+  
   if (ur
@@ -1117,24 +1117,26 @@
  '/') %7B%0A    
+ 
+ 
 url = url.sl
@@ -1151,45 +1151,315 @@
 1);%0A
+  
   %7D%0A  
-let credentials = auth %7C%7C null;
+  if (typeof config.app !== 'string' %7C%7C config.app === '') %7B%0A      throw new Error('App name is not present in options.');%0A    %7D%0A    if (isAppbase(url) && credentials === null) %7B%0A      throw new Error(%0A        'Authentication information is not present. Did you add credentials?',%0A      );%0A    %7D%0A  %7D
 %0A  /
@@ -1967,183 +1967,8 @@
  %7D%0A%0A
-  if (isAppbase(url) && !config.mongodb && credentials === null) %7B%0A    throw new Error(%0A      'Authentication information is not present. Did you add credentials?',%0A    );%0A  %7D
 %0A  t

refactor constructor code for validations