UNPKG

okanjo

Version:

Integrate your application with the Okanjo API.

130 lines (112 loc) 3.7 kB
/* * Date: 1/26/16 11:59 AM * * ---- * * (c) Okanjo Partners Inc * https://okanjo.com * support@okanjo.com * * https://github.com/okanjo/okanjo-nodejs * * ---- * * TL;DR? see: http://www.tldrlegal.com/license/mit-license * * The MIT License (MIT) * Copyright (c) 2013 Okanjo Partners Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is furnished to do * so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ var Provider = require('../src/provider'), Query = require('../src/query'); /** * SDK Base * @param {object} [config] Client options * @namespace * @constructor */ function Client(config) { // Allow client to be initialized as: var api = new Client("api_key"); if (typeof config === "string") { config = { key: config }; } else { config = config || {}; } this.config = config; // Connect the right default provider based on runtime context if (typeof config.provider === "function") { // Context is provided in the config - use the constructor as-is this.provider = new config.provider(this); } else { // Detect context if (process.browser) { // Running in browser - default to proxy mode //this.provider = new (require('../src/providers/jquery_provider'))(this); this.provider = new (require('../src/providers/fetch_provider'))(this); } else { // Running in Node - Use the HTTP provider by default to make real requests this.provider = new (require('../src/providers/http_provider'))(this); } } // Attach resources for (var i = 0; i < Client.resourceBinders.length; i++) { Client.resourceBinders[i](this); } } /** * SDK Version */ Client.Version = '%%OKANJO_VERSION%%'; /** * Expose the Provider base class * @type {Provider} */ Client.Provider = Provider; /** * Expose the Query base class * @type {Query} */ Client.Query = Query; /** * Routes a request through the client's registered transport provider * @param {object} spec - Query specifications * @param {function} callback * @return {Query} - A compiled query, ready to rock and roll, or be modified and executed yourself */ Client.prototype._makeRequest = function(spec, callback) { // Build the query var query = new Query(this.config, spec); // Compile the query this.provider.compile(query); // If we have a callback, execute the request if (callback) { query.execute(callback); } // Return the query for reuse or manual execution return query; }; /** * Container for resource binder functions * @type {Array} */ Client.resourceBinders = []; module.exports = Client;