UNPKG

crowdstart.js

Version:

Ecommerce SDK for JavaScript and client for Crowdstart.

2 lines (1 loc) 16.5 kB
(function(t){function e(t,n){if({}.hasOwnProperty.call(e.cache,t))return e.cache[t];if("function"==typeof n)return void e.load(t,n);var r=e.resolve(t);if(!r)throw new Error("Failed to resolve module "+t);var o={id:t,require:e,filename:t,exports:{},loaded:!1,parent:null,children:[]},i=t.slice(0,t.lastIndexOf("/")+1);return e.cache[t]=o.exports,r.call(o.exports,o,o.exports,i,t),o.loaded=!0,e.cache[t]=o.exports}e.modules={},e.cache={},e.resolve=function(t){return{}.hasOwnProperty.call(e.modules,t)?e.modules[t]:void 0},e.define=function(t,n){e.modules[t]=n},e.waiting={},e.async=function(t,n){e.modules[t]=n;for(var r;r=e.waiting[t].shift();)r(e(t))},e.load=function(t,n){var r=document.createElement("script"),o=document.getElementsByTagName("script")[0],i=e.waiting[t]=e.waiting[t]||[];i.push(n),r.type="text/javascript",r.async=!0,r.src=t,o.parentNode.insertBefore(r,o)},e.define("./api",function(t,n,r,o){var i,s,u,c,a,l;a=e("./utils"),s=a.isFunction,u=a.isString,c=a.newError,l=a.statusOk,t.exports=i=function(){function t(e){var n,r,o,i,s,u,c;if(null==e&&(e={}),!(this instanceof t))return new t(e);i=e.endpoint,o=e.debug,u=e.key,r=e.client,n=e.blueprints,this.debug=o,null==n&&(n=this.constructor.BLUEPRINTS),r?this.client=r:this.client=new this.constructor.CLIENT({debug:o,endpoint:i,key:u});for(s in n)c=n[s],this.addBlueprints(s,c)}return t.BLUEPRINTS={},t.CLIENT=null,t.prototype.addBlueprints=function(t,e){var n,r,o;null==this[t]&&(this[t]={}),r=function(e){return function(n,r){var o;return s(r)?e[t][n]=function(){return r.apply(e,arguments)}:(null==r.expects&&(r.expects=l),null==r.method&&(r.method="POST"),o=function(t,n){return e.client.request(r,t).then(function(n){var o,i;if(null!=(null!=(o=n.data)?o.error:void 0))throw c(t,n);if(!r.expects(n))throw c(t,n);return null!=r.process&&r.process.call(e,n),null!=(i=n.data)?i:n.body}).callback(n)},e[t][n]=o)}}(this);for(o in e)n=e[o],r(o,n)},t.prototype.setKey=function(t){return this.client.setKey(t)},t.prototype.setUserKey=function(t){return this.client.setUserKey(t)},t.prototype.deleteUserKey=function(){return this.client.deleteUserKey()},t.prototype.setStore=function(t){return this.storeId=t,this.client.setStore(t)},t}()}),e.define("./utils",function(t,e,n,r){e.isFunction=function(t){return"function"==typeof t},e.isString=function(t){return"string"==typeof t},e.statusOk=function(t){return 200===t.status},e.statusCreated=function(t){return 201===t.status},e.statusNoContent=function(t){return 204===t.status},e.newError=function(t,e){var n,r,o,i,s,u,c;return null==e&&(e={}),r=null!=(o=null!=e&&null!=(i=e.data)&&null!=(s=i.error)?s.message:void 0)?o:"Request failed",n=new Error(r),n.message=r,n.req=t,n.data=e.data,n.responseText=e.data,n.status=e.status,n.type=null!=(u=e.data)&&null!=(c=u.error)?c.type:void 0,n},e.updateQuery=function(t,e,n){var r,o,i;return o=new RegExp("([?&])"+e+"=.*?(&|#|$)(.*)","gi"),o.test(t)?null!=n?t.replace(o,"$1"+e+"="+n+"$2$3"):(r=t.split("#"),t=r[0].replace(o,"$1$3").replace(/(&|\?)$/,""),null!=r[1]&&(t+="#"+r[1]),t):null!=n?(i=-1!==t.indexOf("?")?"&":"?",r=t.split("#"),t=r[0]+i+e+"="+n,null!=r[1]&&(t+="#"+r[1]),t):t}}),e.define("./client/xhr",function(t,n,r,o){var i,s,u,c,a,l,f;i=e("xhr-promise-es6/lib"),i.Promise=e("broken/lib"),u=e("js-cookie/src/js.cookie"),l=e("./utils"),c=l.isFunction,a=l.newError,f=l.updateQuery,t.exports=s=function(){function t(e){return null==e&&(e={}),this instanceof t?(this.endpoint="https://api.crowdstart.com",this.key=e.key,this.debug=e.debug,e.endpoint&&this.setEndpoint(e.endpoint),void this.getUserKey()):new t(e)}return t.prototype.sessionName="crwdst",t.prototype.setEndpoint=function(t){return this.endpoint=t.replace(/\/$/,"")},t.prototype.setStore=function(t){return this.storeId=t},t.prototype.setKey=function(t){return this.key=t},t.prototype.getKey=function(){return this.userKey||this.key||this.constructor.KEY},t.prototype.getUserKey=function(){var t;return null!=(t=u.getJSON(this.sessionName))&&null!=t.userKey&&(this.userKey=t.userKey),this.userKey},t.prototype.setUserKey=function(t){return u.set(this.sessionName,{userKey:t},{expires:6048e5}),this.userKey=t},t.prototype.deleteUserKey=function(){return u.set(this.sessionName,{userKey:null},{expires:6048e5}),this.userKey},t.prototype.getUrl=function(t,e,n){return c(t)&&(t=t.call(this,e)),f(this.endpoint+t,"token",n)},t.prototype.request=function(t,e,n){var r;return null==n&&(n=this.getKey()),r={url:this.getUrl(t.url,e,n),method:t.method,data:JSON.stringify(e)},this.debug,(new i).send(r).then(function(t){return this.debug,t.data=t.responseText,t})["catch"](function(t){var n,r,o;try{t.data=null!=(o=t.responseText)?o:JSON.parse(t.xhr.responseText)}catch(r){n=r}throw n=a(e,t),this.debug,n})},t}()}),e.define("xhr-promise-es6/lib",function(n,r,o,i){var s,u,c;s=e("parse-headers/parse-headers"),c=e("object-assign"),n.exports=u=function(){function e(){}return e.DEFAULT_CONTENT_TYPE="application/x-www-form-urlencoded; charset=UTF-8",e.Promise=t.Promise,e.prototype.send=function(t){var e;return null==t&&(t={}),e={method:"GET",data:null,headers:{},async:!0,username:null,password:null},t=c({},e,t),new this.constructor.Promise(function(e){return function(n,r){var o,i,s,u,c;if(!XMLHttpRequest)return void e._handleError("browser",r,null,"browser doesn't support XMLHttpRequest");if("string"!=typeof t.url||0===t.url.length)return void e._handleError("url",r,null,"URL is a required parameter");e._xhr=c=new XMLHttpRequest,c.onload=function(){var t;e._detachWindowUnload();try{t=e._getResponseText()}catch(o){return void e._handleError("parse",r,null,"invalid JSON response")}return n({url:e._getResponseUrl(),status:c.status,statusText:c.statusText,responseText:t,headers:e._getHeaders(),xhr:c})},c.onerror=function(){return e._handleError("error",r)},c.ontimeout=function(){return e._handleError("timeout",r)},c.onabort=function(){return e._handleError("abort",r)},e._attachWindowUnload(),c.open(t.method,t.url,t.async,t.username,t.password),null==t.data||t.headers["Content-Type"]||(t.headers["Content-Type"]=e.constructor.DEFAULT_CONTENT_TYPE),s=t.headers;for(i in s)u=s[i],c.setRequestHeader(i,u);try{return c.send(t.data)}catch(a){return o=a,e._handleError("send",r,null,o.toString())}}}(this))},e.prototype.getXHR=function(){return this._xhr},e.prototype._attachWindowUnload=function(){return this._unloadHandler=this._handleWindowUnload.bind(this),window.attachEvent?window.attachEvent("onunload",this._unloadHandler):void 0},e.prototype._detachWindowUnload=function(){return window.detachEvent?window.detachEvent("onunload",this._unloadHandler):void 0},e.prototype._getHeaders=function(){return s(this._xhr.getAllResponseHeaders())},e.prototype._getResponseText=function(){var t;switch(t="string"==typeof this._xhr.responseText?this._xhr.responseText:"",this._xhr.getResponseHeader("Content-Type")){case"application/json":case"text/javascript":t=JSON.parse(t+"")}return t},e.prototype._getResponseUrl=function(){return null!=this._xhr.responseURL?this._xhr.responseURL:/^X-Request-URL:/m.test(this._xhr.getAllResponseHeaders())?this._xhr.getResponseHeader("X-Request-URL"):""},e.prototype._handleError=function(t,e,n,r){return this._detachWindowUnload(),e({reason:t,status:n||this._xhr.status,statusText:r||this._xhr.statusText,xhr:this._xhr})},e.prototype._handleWindowUnload=function(){return this._xhr.abort()},e}()}),e.define("parse-headers/parse-headers",function(t,n,r,o){var i=e("trim"),s=e("for-each"),u=function(t){return"[object Array]"===Object.prototype.toString.call(t)};t.exports=function(t){if(!t)return{};var e={};return s(i(t).split("\n"),function(t){var n=t.indexOf(":"),r=i(t.slice(0,n)).toLowerCase(),o=i(t.slice(n+1));"undefined"==typeof e[r]?e[r]=o:u(e[r])?e[r].push(o):e[r]=[e[r],o]}),e}}),e.define("trim",function(t,e,n,r){function o(t){return t.replace(/^\s*|\s*$/g,"")}e=t.exports=o,e.left=function(t){return t.replace(/^\s*/,"")},e.right=function(t){return t.replace(/\s*$/,"")}}),e.define("for-each",function(t,n,r,o){function i(t,e,n){if(!a(e))throw new TypeError("iterator must be a function");arguments.length<3&&(n=this),"[object Array]"===l.call(t)?s(t,e,n):"string"==typeof t?u(t,e,n):c(t,e,n)}function s(t,e,n){for(var r=0,o=t.length;o>r;r++)f.call(t,r)&&e.call(n,t[r],r,t)}function u(t,e,n){for(var r=0,o=t.length;o>r;r++)e.call(n,t.charAt(r),r,t)}function c(t,e,n){for(var r in t)f.call(t,r)&&e.call(n,t[r],r,t)}var a=e("is-function");t.exports=i;var l=Object.prototype.toString,f=Object.prototype.hasOwnProperty}),e.define("is-function",function(t,e,n,r){function o(t){var e=i.call(t);return"[object Function]"===e||"function"==typeof t&&"[object RegExp]"!==e||"undefined"!=typeof window&&(t===window.setTimeout||t===window.alert||t===window.confirm||t===window.prompt)}t.exports=o;var i=Object.prototype.toString}),e.define("object-assign",function(t,e,n,r){"use strict";function o(t){if(null===t||void 0===t)throw new TypeError("Object.assign cannot be called with null or undefined");return Object(t)}var i=Object.prototype.hasOwnProperty,s=Object.prototype.propertyIsEnumerable;t.exports=Object.assign||function(t,e){for(var n,r,u=o(t),c=1;c<arguments.length;c++){n=Object(arguments[c]);for(var a in n)i.call(n,a)&&(u[a]=n[a]);if(Object.getOwnPropertySymbols){r=Object.getOwnPropertySymbols(n);for(var l=0;l<r.length;l++)s.call(n,r[l])&&(u[r[l]]=n[r[l]])}}return u}}),e.define("broken/lib",function(t,n,r,o){var i,s;i=e("zousan/zousan-min"),i.suppressUncaughtRejectionError=!0,s=function(){function t(t){this.state=t.state,this.value=t.value,this.reason=t.reason}return t.prototype.isFulfilled=function(){return"fulfilled"===this.state},t.prototype.isRejected=function(){return"rejected"===this.state},t}(),i.reflect=function(t){return new i(function(e,n){return t.then(function(t){return e(new s({state:"fulfilled",value:t}))})["catch"](function(t){return e(new s({state:"rejected",reason:t}))})})},i.settle=function(t){return i.all(t.map(i.reflect))},i.prototype.callback=function(t){return"function"==typeof t&&(this.then(function(e){return t(null,e)}),this["catch"](function(e){return t(e,null)})),this},t.exports=i}),e.define("zousan/zousan-min",function(e,n,r,o){!function(t){"use strict";function n(t){if(t){var e=this;t(function(t){e.resolve(t)},function(t){e.reject(t)})}}function r(t,e){if("function"==typeof t.y)try{var n=t.y.call(s,e);t.p.resolve(n)}catch(r){t.p.reject(r)}else t.p.resolve(e)}function o(t,e){if("function"==typeof t.n)try{var n=t.n.call(s,e);t.p.resolve(n)}catch(r){t.p.reject(r)}else t.p.reject(e)}var i,s,u="fulfilled",c="rejected",a="undefined",l=function(){function t(){for(;e.length-n;)e[n](),n++,n==r&&(e.splice(0,r),n=0)}var e=[],n=0,r=1024,o=function(){if(typeof MutationObserver!==a){var e=document.createElement("div"),n=new MutationObserver(t);return n.observe(e,{attributes:!0}),function(){e.setAttribute("a",0)}}return typeof setImmediate!==a?function(){setImmediate(t)}:function(){setTimeout(t,0)}}();return function(t){e.push(t),e.length-n==1&&o()}}();n.prototype={resolve:function(t){if(this.state===i){if(t===this)return this.reject(new TypeError("Attempt to resolve promise with self"));var e=this;if(t&&("function"==typeof t||"object"==typeof t))try{var n=!0,o=t.then;if("function"==typeof o)return void o.call(t,function(t){n&&(n=!1,e.resolve(t))},function(t){n&&(n=!1,e.reject(t))})}catch(s){return void(n&&this.reject(s))}this.state=u,this.v=t,e.c&&l(function(){for(var n=0,o=e.c.length;o>n;n++)r(e.c[n],t)})}},reject:function(t){if(this.state===i){this.state=c,this.v=t;var e=this.c;e?l(function(){for(var n=0,r=e.length;r>n;n++)o(e[n],t)}):n.suppressUncaughtRejectionError||void 0}},then:function(t,e){var s=new n,c={y:t,n:e,p:s};if(this.state===i)this.c?this.c.push(c):this.c=[c];else{var a=this.state,f=this.v;l(function(){a===u?r(c,f):o(c,f)})}return s},"catch":function(t){return this.then(null,t)},"finally":function(t){return this.then(t,t)},timeout:function(t,e){e=e||"Timeout";var r=this;return new n(function(n,o){setTimeout(function(){o(Error(e))},t),r.then(function(t){n(t)},function(t){o(t)})})}},n.resolve=function(t){var e=new n;return e.resolve(t),e},n.reject=function(t){var e=new n;return e.reject(t),e},n.all=function(t){function e(e,s){"function"!=typeof e.then&&(e=n.resolve(e)),e.then(function(e){r[s]=e,o++,o==t.length&&i.resolve(r)},function(t){i.reject(t)})}for(var r=[],o=0,i=new n,s=0;s<t.length;s++)e(t[s],s);return t.length||i.resolve(r),i},typeof e!=a&&e.exports&&(e.exports=n),t.Zousan=n,n.soon=l}("undefined"!=typeof t?t:this)}),e.define("js-cookie/src/js.cookie",function(t,e,n,r){!function(n){if("function"==typeof define&&define.amd)define(n);else if("object"==typeof e)t.exports=n();else{var r=window.Cookies,o=window.Cookies=n();o.noConflict=function(){return window.Cookies=r,o}}}(function(){function t(){for(var t=0,e={};t<arguments.length;t++){var n=arguments[t];for(var r in n)e[r]=n[r]}return e}function e(n){function r(e,o,i){var s;if(arguments.length>1){if(i=t({path:"/"},r.defaults,i),"number"==typeof i.expires){var u=new Date;u.setMilliseconds(u.getMilliseconds()+864e5*i.expires),i.expires=u}try{s=JSON.stringify(o),/^[\{\[]/.test(s)&&(o=s)}catch(c){}return o=n.write?n.write(o,e):encodeURIComponent(String(o)).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent),e=encodeURIComponent(String(e)),e=e.replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent),e=e.replace(/[\(\)]/g,escape),document.cookie=[e,"=",o,i.expires&&"; expires="+i.expires.toUTCString(),i.path&&"; path="+i.path,i.domain&&"; domain="+i.domain,i.secure?"; secure":""].join("")}e||(s={});for(var a=document.cookie?document.cookie.split("; "):[],l=/(%[0-9A-Z]{2})+/g,f=0;f<a.length;f++){var p=a[f].split("="),d=p[0].replace(l,decodeURIComponent),h=p.slice(1).join("=");'"'===h.charAt(0)&&(h=h.slice(1,-1));try{if(h=n.read?n.read(h,d):n(h,d)||h.replace(l,decodeURIComponent),this.json)try{h=JSON.parse(h)}catch(c){}if(e===d){s=h;break}e||(s[d]=h)}catch(c){}}return s}return r.get=r.set=r,r.getJSON=function(){return r.apply({json:!0},[].slice.call(arguments))},r.defaults={},r.remove=function(e,n){r(e,"",t(n,{expires:-1}))},r.withConverter=e,r}return e(function(){})})}),e.define("./blueprints/browser",function(t,n,r,o){var i,s,u,c,a,l,f,p,d,h,y,v,g,w,m,x;for(h=e("./utils"),l=h.isFunction,v=h.statusCreated,g=h.statusNoContent,w=h.statusOk,y=e("./blueprints/url"),s=y.byId,m=y.storePrefixed,u=function(t){var e;return e="/"+t,{list:{url:e,method:"GET"},get:{url:s(t),method:"GET"}}},i={account:{get:{url:"/account",method:"GET"},update:{url:"/account",method:"PATCH"},exists:{url:function(t){var e,n,r;return"/account/exists/"+(null!=(e=null!=(n=null!=(r=t.email)?r:t.username)?n:t.id)?e:t)},method:"GET",process:function(t){return t.data.exists}},create:{url:"/account/create",expects:v},enable:{url:function(t){var e;return"/account/enable/"+(null!=(e=t.tokenId)?e:t)}},login:{url:"/account/login",process:function(t){return this.setUserKey(t.data.token),t}},logout:function(){return this.deleteUserKey()},reset:{url:"/account/reset"},confirm:{url:function(t){var e;return"/account/confirm/"+(null!=(e=t.tokenId)?e:t)}}},checkout:{authorize:{url:m("/checkout/authorize")},capture:{url:m(function(t){var e;return"/checkout/capture/"+(null!=(e=t.orderId)?e:t)})},charge:{url:m("/checkout/charge")},paypal:{url:m("/checkout/paypal")}},referrer:{create:{url:"/referrer",expects:v}}},d=["collection","coupon","product","variant"],x=["order","subscription"],c=function(t){return i[t]=u(t)},a=0,f=d.length;f>a;a++)p=d[a],c(p);t.exports=i}),e.define("./blueprints/url",function(t,n,r,o){var i,s;i=e("./utils").isFunction,n.storePrefixed=s=function(t){return function(e){var n;return n=i(t)?t(e):t,null!=this.storeId?"/store/"+this.storeId+n:n}},n.byId=function(t){switch(t){case"coupon":return s(function(t){var e;return"/coupon/"+(null!=(e=t.code)?e:t)});case"collection":return s(function(t){var e;return"/collection/"+(null!=(e=t.slug)?e:t)});case"product":return s(function(t){var e,n;return"/product/"+(null!=(e=null!=(n=t.id)?n:t.slug)?e:t)});case"variant":return s(function(t){var e,n;return"/variant/"+(null!=(e=null!=(n=t.id)?n:t.sku)?e:t)});default:return function(e){var n;return"/"+t+"/"+(null!=(n=e.id)?n:e)}}}}),e.define("./browser",function(n,r,o,i){var s,u;null==t.Crowdstart&&(t.Crowdstart={}),s=e("./api"),u=e("./client/xhr"),s.CLIENT=u,s.BLUEPRINTS=e("./blueprints/browser"),Crowdstart.Api=s,Crowdstart.Client=u,n.exports=Crowdstart}),e("./browser")}).call(this,this);