jspare-sdk
Version:
Javscript SDK for integration with JSpare Web Framework.
5 lines • 2.53 kB
JavaScript
// jspare-sdk.js 1.0.03
// http://jspare.org/framework
// (c) 2016 Jspare.org
// jspare-sdk may be freely distributed under the APACHE 2 License.
(function(){function t(t){for(k in t)this[k]=t[k]}function e(t,e,n,i,r){function s(){return"undefined"!=typeof XMLHttpRequest}function o(t){return Object.keys(t).map(function(e){return[e,t[e]].map(encodeURIComponent).join("=")}).join("&")}function u(t,e){var n="/"+t;return Object.keys(e).length&&(n+="?",n+=o(e)),n}function a(t){return"string"==typeof t||t instanceof String?t:JSON.stringify(t)}function d(t){try{return JSON.parse(t)}catch(e){return t}}s()||(XMLHttpRequest=require("xmlhttprequest").XMLHttpRequest);var p=new XMLHttpRequest;if(p.open("POST",u(t,e),!0),p.setRequestHeader("Content-type","application/json; charset=utf-8"),n)for(h in n)p.setRequestHeader(h,n[h]);var c=this;c.callback=r,p.onload=function(){if(500===this.status)return void r(new Error(this.statusText));var t={command:c,result:{status:this.status,entity:d(this.responseText)}};r&&r(null,t)},p.onerror=function(t){r&&r(t,null,this)},p.send(a(i))}function n(t,e){this.id=t,this.req={},this.parameters={},this.yieldBinds={},this.data={},this.options=e||s}var i=this,r=function(t){return t instanceof r?t:this instanceof r?void(this._wrapped=t):new r(t)};"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=r),exports.jspare=r):i.jspare=r,r.version="1.0.0";var s={};t.prototype.resume=function(){var t={"hd-tid-tkn":this.tid};e.bind(this,this.command.id,this.command.parameters,t,this,this.command.callback)()},n.prototype.options=function(t){return this.options=t,this},n.prototype.request=function(t){return this.req=t,this},n.prototype.addParameter=function(t,e){return this.parameters[t]=e,this},n.prototype.removeParameter=function(t){return delete this.parameters[t],this},n.prototype.parameters=function(t){return this.parameters=t,this},n.prototype.onYield=function(t,e){return this.yieldBinds[t]=e,this},n.prototype.execute=function(n){e.bind(this,this.id,this.parameters,{},this.req,function(e,i){if(e)return void(n&&n(e,null));if(206===i.result.status){if(!Object.keys(i.command.yieldBinds).length)throw new Error("None bind for yield registered");var r=i.result.entity;return r.command=i.command,void i.command.yieldBinds[r.bind](new t(r))}n&&n(null,i.result.entity)})()},n.prototype.call=this.execute,r.command=function(t){return new n(t)},"function"==typeof define&&define.amd&&define("jspare",[],function(){return r})}).call(this);