UNPKG

crowdstart.js

Version:

Ecommerce SDK for JavaScript and client for Crowdstart.

104 lines (91 loc) 2.67 kB
// Generated by CoffeeScript 1.10.0 var Api, isFunction, isString, newError, ref, statusOk; ref = require('./utils'), isFunction = ref.isFunction, isString = ref.isString, newError = ref.newError, statusOk = ref.statusOk; module.exports = Api = (function() { Api.BLUEPRINTS = {}; Api.CLIENT = null; function Api(opts) { var blueprints, client, debug, endpoint, k, key, v; if (opts == null) { opts = {}; } if (!(this instanceof Api)) { return new Api(opts); } endpoint = opts.endpoint, debug = opts.debug, key = opts.key, client = opts.client, blueprints = opts.blueprints; this.debug = debug; if (blueprints == null) { blueprints = this.constructor.BLUEPRINTS; } if (client) { this.client = client; } else { this.client = new this.constructor.CLIENT({ debug: debug, endpoint: endpoint, key: key }); } for (k in blueprints) { v = blueprints[k]; this.addBlueprints(k, v); } } Api.prototype.addBlueprints = function(api, blueprints) { var bp, fn, name; if (this[api] == null) { this[api] = {}; } fn = (function(_this) { return function(name, bp) { var method; if (isFunction(bp)) { return _this[api][name] = function() { return bp.apply(_this, arguments); }; } if (bp.expects == null) { bp.expects = statusOk; } if (bp.method == null) { bp.method = 'POST'; } method = function(data, cb) { return _this.client.request(bp, data).then(function(res) { var ref1, ref2; if (((ref1 = res.data) != null ? ref1.error : void 0) != null) { throw newError(data, res); } if (!bp.expects(res)) { throw newError(data, res); } if (bp.process != null) { bp.process.call(_this, res); } return (ref2 = res.data) != null ? ref2 : res.body; }).callback(cb); }; return _this[api][name] = method; }; })(this); for (name in blueprints) { bp = blueprints[name]; fn(name, bp); } }; Api.prototype.setKey = function(key) { return this.client.setKey(key); }; Api.prototype.setUserKey = function(key) { return this.client.setUserKey(key); }; Api.prototype.deleteUserKey = function() { return this.client.deleteUserKey(); }; Api.prototype.setStore = function(id) { this.storeId = id; return this.client.setStore(id); }; return Api; })(); //# sourceMappingURL=api.js.map