All files / src index.js

100% Statements 25/25
100% Branches 21/21
100% Functions 2/2
100% Lines 25/25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67                                6x     11x 11x 11x       11x 4x 2x       11x 9x 9x     2x 1x       11x 11x 10x 9x   2x     10x 5x 5x 4x     10x     6x     6x 6x        
/*!
 * Sheetrock
 * Quickly connect to, query, and lazy-load data from Google Sheets.
 * https://chriszarate.github.io/sheetrock/
 * License: MIT
 */
 
/* global window */
 
import Options from './lib/options';
import Request from './lib/request';
import Response from './lib/response';
 
import { defaults } from './lib/config';
import transport from './lib/transport'; // Shimmed with 'transport-browser' in browser.
 
const version = '1.1.0';
 
function sheetrock(userOptions = {}, data = null) {
  let options = null;
  let request = null;
  let response = null;
 
  // Call the user's callback function or rethrow error.
  function handleError(error) {
    if (error && error.name === 'SheetrockError') {
      if (request && request.update) {
        request.update({ failed: true });
      }
    }
 
    if (userOptions.callback) {
      userOptions.callback(error, options, response);
      return;
    }
 
    if (error) {
      throw error;
    }
  }
 
  try {
    options = new Options(Object.assign({ target: this }, userOptions), !!data);
    request = new Request(options);
    response = new Response(request);
  } catch (error) {
    handleError(error);
  }
 
  if (data) {
    response.loadData(data, handleError);
  } else if (options && request && response) {
    transport(response, handleError);
  }
 
  return this;
}
 
Object.assign(sheetrock, { defaults, version });
 
// If jQuery is available as a global, register as a plugin.
try {
  window.jQuery.fn.sheetrock = sheetrock;
} catch (ignore) { /* empty */ }
 
export default sheetrock;