UNPKG

crowdstart.js

Version:

Ecommerce SDK for JavaScript and client for Crowdstart.

110 lines (98 loc) 3.12 kB
// Generated by CoffeeScript 1.10.0 var NodeClient, Promise, XhrClient, fs, newError, request, extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, hasProp = {}.hasOwnProperty; Promise = require('broken'); fs = require('fs'); request = require('request'); XhrClient = require('./xhr'); newError = require('../utils').newError; module.exports = NodeClient = (function(superClass) { extend(NodeClient, superClass); function NodeClient(opts) { if (opts == null) { opts = {}; } if (!(this instanceof NodeClient)) { return new NodeClient(opts); } this.endpoint = 'https://api.crowdstart.com'; this.key = opts.key, this.debug = opts.debug; if (NodeClient.ENDPOINT) { this.setEndpoint(NodeClient.ENDPOINT); } if (NodeClient.DEBUG) { this.debug = true; } if (opts.endpoint) { this.setEndpoint(opts.endpoint); } } NodeClient.prototype.request = function(blueprint, data, key) { var opts, ref; if (key == null) { key = this.getKey(); } opts = { url: this.getUrl(blueprint.url, data, key), method: blueprint.method, headers: (ref = blueprint.headers) != null ? ref : {}, followAllRedirects: true }; if ((opts.method === 'POST') || (opts.method === 'PATCH')) { opts.json = data; } else { opts.json = true; } if (this.debug) { console.log('--REQUEST--'); console.log(opts); } return new Promise((function(_this) { return function(resolve, reject) { var req; req = request(opts, function(err, res) { var ref1; if (res != null) { if (_this.debug) { console.log('--RESPONSE--'); console.log({ status: res.statusCode, body: res.body }); } res.status = res.statusCode; res.data = res.body; } if ((err != null) || (res.status > 308) || (((ref1 = res.data) != null ? ref1.error : void 0) != null)) { err = newError(opts, res); if (_this.debug) { console.log('ERROR:'); console.log({ message: err.message, status: err.status, type: err.type }); } return reject(err); } return resolve({ url: opts.url, req: opts, res: res, data: res.data, responseText: res.data, status: res.status, statusText: res.statusText, headers: res.headers }); }); if (blueprint.upload != null) { return (blueprint.upload.call(_this, data)).pipe(req); } }; })(this)); }; return NodeClient; })(XhrClient); //# sourceMappingURL=node.js.map