/*!
* 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;
|