UNPKG

mojio-js-sdk

Version:

Mojio javascript REST client.

105 lines (90 loc) 3.69 kB
// Generated by CoffeeScript 1.10.0 (function() { var HttpBrowserWrapper, HttpWrapperHelper, iHttpWrapper, bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, 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; HttpWrapperHelper = require('../helpers/HttpWrapperHelper'); iHttpWrapper = require('../interfaces/iHttpWrapper'); module.exports = HttpBrowserWrapper = (function(superClass) { var _parts, _request; extend(HttpBrowserWrapper, superClass); function HttpBrowserWrapper(token1, uri1, encoding1, requester1) { this.token = token1; this.uri = uri1 != null ? uri1 : 'https://api.moj.io/v1'; this.encoding = encoding1 != null ? encoding1 : false; this.requester = requester1 != null ? requester1 : null; this.redirect = bind(this.redirect, this); HttpBrowserWrapper.__super__.constructor.call(this); } _request = function(request, requester, callback) { var k, ref, v, xmlhttp; if ((typeof XMLHttpRequest !== "undefined" && XMLHttpRequest !== null)) { xmlhttp = new XMLHttpRequest; } else { xmlhttp = requester; } xmlhttp.open(request.method, request.url, true); ref = request.headers; for (k in ref) { v = ref[k]; xmlhttp.setRequestHeader(k, v); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState === 4) { if (xmlhttp.status >= 200 && xmlhttp.status <= 299) { return callback(null, JSON.parse(xmlhttp.responseText)); } else { return callback(xmlhttp.statusText, null); } } }; if (request.method === "GET") { return xmlhttp.send(); } else { return xmlhttp.send(request.data); } }; _parts = function(request, token, uri, encoding) { var parts; if (request.id) { request.pid = request.id; } if (request.key) { request.sid = request.key; } uri += HttpWrapperHelper._getPath(request.resource, request.pid, request.action, request.sid, request.object, request.tid); parts = HttpWrapperHelper._parse(uri, request, encoding, this.token); if (!((parts.scheme != null) || (typeof window === "undefined" || window === null))) { parts.scheme = window.location.protocol.split(':')[0]; } if (!parts.scheme || parts.scheme === 'file') { parts.scheme = 'https'; } return parts; }; HttpBrowserWrapper.prototype.url = function(request) { var parts; parts = _parts(request, this.token, this.uri, this.encoding); return parts.protocol + '//' + parts.hostname + parts.pathname + parts.params; }; HttpBrowserWrapper.prototype.request = function(request, callback) { var parts; parts = _parts(request, this.token, this.uri, this.encoding); parts.url = this.url(request); return _request(parts, this.requester, callback); }; HttpBrowserWrapper.prototype.redirect = function(request, redirectClass) { if (redirectClass == null) { redirectClass = null; } if ((redirectClass != null)) { redirectClass.redirect(this.url(request)); } else { window.location = this.url(request); } }; return HttpBrowserWrapper; })(iHttpWrapper); }).call(this); //# sourceMappingURL=HttpWrapper.js.map