diff --git a/hanzo.js b/hanzo.js index 08d8439..576a65e 100644 --- a/hanzo.js +++ b/hanzo.js @@ -1338,14 +1338,12 @@ var Hanzo = (function () { useCustomerToken: true }, paymentMethod: { - create: { - url: function(x) { - return "/account/paymentmethod/" + x.type; - }, - method: POST, - expects: statusCreated, - useCustomerToken: true - } + url: function(x) { + return "/account/paymentmethod/" + x.type; + }, + method: POST, + expects: statusCreated, + useCustomerToken: true } }, cart: { diff --git a/hanzo.min.js b/hanzo.min.js index 0e090d1..cee6259 100644 --- a/hanzo.min.js +++ b/hanzo.min.js @@ -1 +1 @@ -var Hanzo=function(){"use strict";function toString(obj){return Object.prototype.toString.call(obj)}var isFunction;var isFunction$1=isFunction=function(value){var str;if(typeof window!=="undefined"&&value===window.alert){return true}str=toString(value);return str==="[object Function]"||str==="[object GeneratorFunction]"||str==="[object AsyncFunction]"};var updateParam;var statusOk=function(res){return res.status===200};var statusCreated=function(res){return res.status===201};var GET="GET";var POST="POST";var PATCH="PATCH";var newError=function(data,res,err){var message,ref,ref1,ref2,ref3,ref4;if(res==null){res={}}message=(ref=(ref1=res.data)!=null?(ref2=ref1.error)!=null?ref2.message:void 0:void 0)!=null?ref:"Request failed";if(err==null){err=new Error(message)}err.data=res.data;err.msg=message;err.req=data;err.responseText=res.data;err.status=res.status;err.type=(ref3=res.data)!=null?(ref4=ref3.error)!=null?ref4.type:void 0:void 0;return err};updateParam=function(url,key,value){var hash,re,separator;re=new RegExp("([?&])"+key+"=.*?(&|#|$)(.*)","gi");if(re.test(url)){if(value!=null){return url.replace(re,"$1"+key+"="+value+"$2$3")}else{hash=url.split("#");url=hash[0].replace(re,"$1$3").replace(/(&|\?)$/,"");if(hash[1]!=null){url+="#"+hash[1]}return url}}else{if(value!=null){separator=url.indexOf("?")!==-1?"&":"?";hash=url.split("#");url=hash[0]+separator+key+"="+value;if(hash[1]!=null){url+="#"+hash[1]}return url}else{return url}}};var updateQuery=function(url,data){var k,v;if(typeof data!=="object"){return url}for(k in data){v=data[k];url=updateParam(url,k,v)}return url};var Api;Api=function(){Api.BLUEPRINTS={};Api.CLIENT=null;function Api(opts){var blueprints,client,k,v;if(opts==null){opts={}}if(!(this instanceof Api)){return new Api(opts)}blueprints=opts.blueprints,client=opts.client;this.client=client||new this.constructor.CLIENT(opts);if(blueprints==null){blueprints=this.constructor.BLUEPRINTS}for(k in blueprints){v=blueprints[k];this.addBlueprints(k,v)}}Api.prototype.addBlueprints=function(api,blueprints){var bp,name;if(this[api]==null){this[api]={}}for(name in blueprints){bp=blueprints[name];this.addBlueprint(api,name,bp)}};Api.prototype.addBlueprint=function(api,name,bp){var method;if(isFunction$1(bp)){return this[api][name]=function(_this){return function(){return bp.apply(_this,arguments)}}(this)}if(bp.expects==null){bp.expects=statusOk}if(bp.method==null){bp.method=GET}method=function(_this){return function(data,cb){var key;key=void 0;if(bp.useCustomerToken){key=_this.client.getCustomerToken()}return _this.client.request(bp,data,key).then(function(res){var ref,ref1;if(((ref=res.data)!=null?ref.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(ref1=res.data)!=null?ref1:res.body}).callback(cb)}}(this);return this[api][name]=method};Api.prototype.setKey=function(key){return this.client.setKey(key)};Api.prototype.setCustomerToken=function(key){return this.client.setCustomerToken(key)};Api.prototype.getCustomerToken=function(){return this.client.getCustomerToken()};Api.prototype.deleteCustomerToken=function(){return this.client.deleteCustomerToken()};Api.prototype.setStore=function(id){this.storeId=id;return this.client.setStore(id)};return Api}();var Api$1=Api;var PromiseInspection;var PromiseInspection$1=PromiseInspection=function(){function PromiseInspection(arg){this.state=arg.state,this.value=arg.value,this.reason=arg.reason}PromiseInspection.prototype.isFulfilled=function(){return this.state==="fulfilled"};PromiseInspection.prototype.isRejected=function(){return this.state==="rejected"};return PromiseInspection}();var _undefined$1=void 0;var _undefinedString$1="undefined";var soon;soon=function(){var bufferSize,callQueue,cqYield,fq,fqStart;fq=[];fqStart=0;bufferSize=1024;callQueue=function(){var err;while(fq.length-fqStart){try{fq[fqStart]()}catch(error){err=error;if(typeof console!=="undefined"){console.error(err)}}fq[fqStart++]=_undefined$1;if(fqStart===bufferSize){fq.splice(0,bufferSize);fqStart=0}}};cqYield=function(){var dd,mo;if(typeof MutationObserver!==_undefinedString$1){dd=document.createElement("div");mo=new MutationObserver(callQueue);mo.observe(dd,{attributes:true});return function(){dd.setAttribute("a",0)}}if(typeof setImmediate!==_undefinedString$1){return function(){setImmediate(callQueue)}}return function(){setTimeout(callQueue,0)}}();return function(fn){fq.push(fn);if(fq.length-fqStart===1){cqYield()}}}();var soon$1=soon;var Promise$1;var STATE_FULFILLED;var STATE_PENDING;var STATE_REJECTED;var _undefined;var rejectClient;var resolveClient;_undefined=void 0;STATE_PENDING=_undefined;STATE_FULFILLED="fulfilled";STATE_REJECTED="rejected";resolveClient=function(c,arg){var err,yret;if(typeof c.y==="function"){try{yret=c.y.call(_undefined,arg);c.p.resolve(yret)}catch(error){err=error;c.p.reject(err)}}else{c.p.resolve(arg)}};rejectClient=function(c,reason){var err,yret;if(typeof c.n==="function"){try{yret=c.n.call(_undefined,reason);c.p.resolve(yret)}catch(error){err=error;c.p.reject(err)}}else{c.p.reject(reason)}};Promise$1=function(){function Promise(fn){if(fn){fn(function(_this){return function(arg){return _this.resolve(arg)}}(this),function(_this){return function(arg){return _this.reject(arg)}}(this))}}Promise.prototype.resolve=function(value){var clients,err,first,next;if(this.state!==STATE_PENDING){return}if(value===this){return this.reject(new TypeError("Attempt to resolve promise with self"))}if(value&&(typeof value==="function"||typeof value==="object")){try{first=true;next=value.then;if(typeof next==="function"){next.call(value,function(_this){return function(ra){if(first){if(first){first=false}_this.resolve(ra)}}}(this),function(_this){return function(rr){if(first){first=false;_this.reject(rr)}}}(this));return}}catch(error){err=error;if(first){this.reject(err)}return}}this.state=STATE_FULFILLED;this.v=value;if(clients=this.c){soon$1(function(_this){return function(){var c,i,len;for(i=0,len=clients.length;i");if(this.opts.debug&&(typeof console!=="undefined"&&console!==null)){return console.log.apply(console,args)}};return Client}();var Client$1=Client;var BrowserClient,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;BrowserClient=function(superClass){extend(BrowserClient,superClass);function BrowserClient(opts){BrowserClient.__super__.constructor.call(this,opts);if(!(this instanceof BrowserClient)){return new BrowserClient(opts)}this.getCustomerToken()}BrowserClient.prototype.request=function(blueprint,data,key){var opts;if(data==null){data={}}if(key==null){key=this.getKey()}opts={url:this.url(blueprint.url,data,key),method:blueprint.method};if(blueprint.method!=="GET"){opts.headers={"Content-Type":"application/json"}}if(blueprint.method==="GET"){opts.url=updateQuery(opts.url,data)}else{opts.data=JSON.stringify(data)}this.log("request",{key:key,opts:opts});return(new XhrPromise$1).send(opts).then(function(_this){return function(res){_this.log("response",res);res.data=res.responseText;return res}}(this))["catch"](function(_this){return function(res){var err,ref;try{res.data=(ref=res.responseText)!=null?ref:JSON.parse(res.xhr.responseText)}catch(error){err=error}err=newError(data,res,err);_this.log("response",res);_this.log("error",err);throw err}}(this))};return BrowserClient}(Client$1);var Client$2=BrowserClient;var sp;var storePrefixed=sp=function(u){return function(x){var url;if(isFunction$1(u)){url=u(x)}else{url=u}if(this.storeId!=null){return"/store/"+this.storeId+url}else{return url}}};var byId=function(name){switch(name){case"coupon":return sp(function(x){var ref;return"/coupon/"+((ref=x.code)!=null?ref:x)});case"collection":return sp(function(x){var ref;return"/collection/"+((ref=x.slug)!=null?ref:x)});case"product":return sp(function(x){var ref,ref1;return"/product/"+((ref=(ref1=x.id)!=null?ref1:x.slug)!=null?ref:x)});case"variant":return sp(function(x){var ref,ref1;return"/variant/"+((ref=(ref1=x.id)!=null?ref1:x.sku)!=null?ref:x)});case"site":return function(x){var ref,ref1;return"/site/"+((ref=(ref1=x.id)!=null?ref1:x.name)!=null?ref:x)};default:return function(x){var ref;return"/"+name+"/"+((ref=x.id)!=null?ref:x)}}};var blueprints,createBlueprint,fn,fn1,i,j,len,len1,marketingModels,model,models;createBlueprint=function(name){var endpoint;endpoint="/"+name;return{list:{url:endpoint,method:GET,expects:statusOk},get:{url:byId(name),method:GET,expects:statusOk}}};blueprints={library:{shopjs:{url:"/library/shopjs",method:POST,expects:statusOk}},account:{get:{url:"/account",method:GET,expects:statusOk,useCustomerToken:true},update:{url:"/account",method:PATCH,expects:statusOk,useCustomerToken:true},exists:{url:function(x){var ref,ref1,ref2;return"/account/exists/"+((ref=(ref1=(ref2=x.email)!=null?ref2:x.username)!=null?ref1:x.id)!=null?ref:x)},method:GET,expects:statusOk,process:function(res){return res.data.exists}},create:{url:"/account/create",method:POST,expects:statusCreated},enable:{url:function(x){var ref;return"/account/enable/"+((ref=x.tokenId)!=null?ref:x)},method:POST,expects:statusOk},login:{url:"/account/login",method:POST,expects:statusOk,process:function(res){this.setCustomerToken(res.data.token);return res}},logout:function(){return this.deleteCustomerToken()},reset:{url:"/account/reset",method:POST,expects:statusOk,useCustomerToken:true},updateOrder:{url:function(x){var ref,ref1;return"/account/order/"+((ref=(ref1=x.orderId)!=null?ref1:x.id)!=null?ref:x)},method:PATCH,expects:statusOk,useCustomerToken:true},confirm:{url:function(x){var ref;return"/account/confirm/"+((ref=x.tokenId)!=null?ref:x)},method:POST,expects:statusOk,useCustomerToken:true},paymentMethod:{create:{url:function(x){return"/account/paymentmethod/"+x.type},method:POST,expects:statusCreated,useCustomerToken:true}}},cart:{create:{url:"/cart",method:POST,expects:statusCreated},update:{url:function(x){var ref;return"/cart/"+((ref=x.id)!=null?ref:x)},method:PATCH,expects:statusOk},discard:{url:function(x){var ref;return"/cart/"+((ref=x.id)!=null?ref:x)+"/discard"},method:POST,expects:statusOk},set:{url:function(x){var ref;return"/cart/"+((ref=x.id)!=null?ref:x)+"/set"},method:POST,expects:statusOk}},review:{create:{url:"/review",method:POST,expects:statusCreated},get:{url:function(x){var ref;return"/review/"+((ref=x.id)!=null?ref:x)},method:GET,expects:statusOk}},checkout:{authorize:{url:storePrefixed("/checkout/authorize"),method:POST,expects:statusOk},capture:{url:storePrefixed(function(x){var ref;return"/checkout/capture/"+((ref=x.orderId)!=null?ref:x)}),method:POST,expects:statusOk},charge:{url:storePrefixed("/checkout/charge"),method:POST,expects:statusOk},paypal:{url:storePrefixed("/checkout/paypal"),method:POST,expects:statusOk}},referrer:{create:{url:"/referrer",method:POST,expects:statusCreated},get:{url:function(x){var ref;return"/referrer/"+((ref=x.id)!=null?ref:x)},method:GET,expects:statusOk}},marketing:{create:{url:"/marketing",method:POST,expects:statusCreated}}};models=["collection","coupon","product","variant","movie","watchlist","copy","media"];fn=function(model){return blueprints[model]=createBlueprint(model)};for(i=0,len=models.length;i");if(this.opts.debug&&(typeof console!=="undefined"&&console!==null)){return console.log.apply(console,args)}};return Client}();var Client$1=Client;var BrowserClient,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;BrowserClient=function(superClass){extend(BrowserClient,superClass);function BrowserClient(opts){BrowserClient.__super__.constructor.call(this,opts);if(!(this instanceof BrowserClient)){return new BrowserClient(opts)}this.getCustomerToken()}BrowserClient.prototype.request=function(blueprint,data,key){var opts;if(data==null){data={}}if(key==null){key=this.getKey()}opts={url:this.url(blueprint.url,data,key),method:blueprint.method};if(blueprint.method!=="GET"){opts.headers={"Content-Type":"application/json"}}if(blueprint.method==="GET"){opts.url=updateQuery(opts.url,data)}else{opts.data=JSON.stringify(data)}this.log("request",{key:key,opts:opts});return(new XhrPromise$1).send(opts).then(function(_this){return function(res){_this.log("response",res);res.data=res.responseText;return res}}(this))["catch"](function(_this){return function(res){var err,ref;try{res.data=(ref=res.responseText)!=null?ref:JSON.parse(res.xhr.responseText)}catch(error){err=error}err=newError(data,res,err);_this.log("response",res);_this.log("error",err);throw err}}(this))};return BrowserClient}(Client$1);var Client$2=BrowserClient;var sp;var storePrefixed=sp=function(u){return function(x){var url;if(isFunction$1(u)){url=u(x)}else{url=u}if(this.storeId!=null){return"/store/"+this.storeId+url}else{return url}}};var byId=function(name){switch(name){case"coupon":return sp(function(x){var ref;return"/coupon/"+((ref=x.code)!=null?ref:x)});case"collection":return sp(function(x){var ref;return"/collection/"+((ref=x.slug)!=null?ref:x)});case"product":return sp(function(x){var ref,ref1;return"/product/"+((ref=(ref1=x.id)!=null?ref1:x.slug)!=null?ref:x)});case"variant":return sp(function(x){var ref,ref1;return"/variant/"+((ref=(ref1=x.id)!=null?ref1:x.sku)!=null?ref:x)});case"site":return function(x){var ref,ref1;return"/site/"+((ref=(ref1=x.id)!=null?ref1:x.name)!=null?ref:x)};default:return function(x){var ref;return"/"+name+"/"+((ref=x.id)!=null?ref:x)}}};var blueprints,createBlueprint,fn,fn1,i,j,len,len1,marketingModels,model,models;createBlueprint=function(name){var endpoint;endpoint="/"+name;return{list:{url:endpoint,method:GET,expects:statusOk},get:{url:byId(name),method:GET,expects:statusOk}}};blueprints={library:{shopjs:{url:"/library/shopjs",method:POST,expects:statusOk}},account:{get:{url:"/account",method:GET,expects:statusOk,useCustomerToken:true},update:{url:"/account",method:PATCH,expects:statusOk,useCustomerToken:true},exists:{url:function(x){var ref,ref1,ref2;return"/account/exists/"+((ref=(ref1=(ref2=x.email)!=null?ref2:x.username)!=null?ref1:x.id)!=null?ref:x)},method:GET,expects:statusOk,process:function(res){return res.data.exists}},create:{url:"/account/create",method:POST,expects:statusCreated},enable:{url:function(x){var ref;return"/account/enable/"+((ref=x.tokenId)!=null?ref:x)},method:POST,expects:statusOk},login:{url:"/account/login",method:POST,expects:statusOk,process:function(res){this.setCustomerToken(res.data.token);return res}},logout:function(){return this.deleteCustomerToken()},reset:{url:"/account/reset",method:POST,expects:statusOk,useCustomerToken:true},updateOrder:{url:function(x){var ref,ref1;return"/account/order/"+((ref=(ref1=x.orderId)!=null?ref1:x.id)!=null?ref:x)},method:PATCH,expects:statusOk,useCustomerToken:true},confirm:{url:function(x){var ref;return"/account/confirm/"+((ref=x.tokenId)!=null?ref:x)},method:POST,expects:statusOk,useCustomerToken:true},paymentMethod:{url:function(x){return"/account/paymentmethod/"+x.type},method:POST,expects:statusCreated,useCustomerToken:true}},cart:{create:{url:"/cart",method:POST,expects:statusCreated},update:{url:function(x){var ref;return"/cart/"+((ref=x.id)!=null?ref:x)},method:PATCH,expects:statusOk},discard:{url:function(x){var ref;return"/cart/"+((ref=x.id)!=null?ref:x)+"/discard"},method:POST,expects:statusOk},set:{url:function(x){var ref;return"/cart/"+((ref=x.id)!=null?ref:x)+"/set"},method:POST,expects:statusOk}},review:{create:{url:"/review",method:POST,expects:statusCreated},get:{url:function(x){var ref;return"/review/"+((ref=x.id)!=null?ref:x)},method:GET,expects:statusOk}},checkout:{authorize:{url:storePrefixed("/checkout/authorize"),method:POST,expects:statusOk},capture:{url:storePrefixed(function(x){var ref;return"/checkout/capture/"+((ref=x.orderId)!=null?ref:x)}),method:POST,expects:statusOk},charge:{url:storePrefixed("/checkout/charge"),method:POST,expects:statusOk},paypal:{url:storePrefixed("/checkout/paypal"),method:POST,expects:statusOk}},referrer:{create:{url:"/referrer",method:POST,expects:statusCreated},get:{url:function(x){var ref;return"/referrer/"+((ref=x.id)!=null?ref:x)},method:GET,expects:statusOk}},marketing:{create:{url:"/marketing",method:POST,expects:statusCreated}}};models=["collection","coupon","product","variant","movie","watchlist","copy","media"];fn=function(model){return blueprints[model]=createBlueprint(model)};for(i=0,len=models.length;i "/account/paymentmethod/#{x.type}" - method: POST - expects: statusCreated - useCustomerToken: true + url: (x) -> "/account/paymentmethod/#{x.type}" + method: POST + expects: statusCreated + useCustomerToken: true # CART cart: