all files / src/ main.js

28.57% Statements 2/7
4.55% Branches 1/22
0% Functions 0/1
28.57% Lines 2/7
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                                                                                                                         
/*!
 * Sheetrock
 * Quickly connect to, query, and lazy-load data from Google Sheets.
 * https://chriszarate.github.io/sheetrock/
 * License: MIT
 */
 
import Options from './lib/options';
import Request from './lib/request';
import Response from './lib/response';
 
import { defaults } from './lib/config';
import environment from './lib/env';
import transport from './lib/transport'; // Shimmed with 'transport-browser' in browser.
 
const version = '1.1.0';
 
function sheetrock(userOptions = {}, data = null) {
  let options = {
    user: userOptions,
  };
  let request;
  let response;
 
  try {
    options = new Options(Object.assign({ target: this }, userOptions), !!data);
    request = new Request(options);
    response = new Response(request);
 
    if (data) {
      response.loadData(data);
    } else {
      transport(request.url, response.loadData);
    }
  } catch (error) {
    if (error.name === 'SheetrockError') {
      if (response && response.request && response.request.update) {
        response.request.update({ failed: true });
      }
 
      // Call the user's callback function.
      if (options && options.user && options.user.callback) {
        options.user.callback(error, options, response);
        return this;
      }
    }
 
    // If we're still here, rethrow the error.
    throw error;
  }
 
  return this;
}
 
Object.assign(sheetrock, { defaults, environment, version });
 
// If jQuery is available as a global, register as a plugin.
Iif (environment.jquery) {
  window.jQuery.fn.sheetrock = sheetrock;
}
 
export default sheetrock;