ecomplus-sdk
Version:
JS SDK for E-Com Plus APIs public resources
2 lines • 7.44 kB
JavaScript
!function(){"use strict";var e=!1;"undefined"!=typeof module&&module.exports&&(e=!0);var w="api.e-com.plus",j="ioapi.ecvol.com",_="e-com.plus",x="io.ecvol.com",q="apx-search.e-com.plus",S="apx-graphs.e-com.plus",k="apx-mods.e-com.plus",u=function(){var m,s,g,y;e&&"undefined"==typeof XMLHttpRequest&&"undefined"==typeof XDomainRequest&&(g=require("https"));var c=function(e,t,o){if("function"==typeof e){var n=new Error(t);e(n,void 0===o?null:o)}y.log("E-Com Plus SDK "+"WARNING"+":\n"+t)},b=function(e,t,o,n,r){if("function"==typeof e){var s;if(o)switch(o){case j:s="/"+m+"/v1";break;case w:s="/v1";break;default:s="/api/v1"}else o=w,s="/v1";"number"!=typeof r&&(r=0),v(r,o,s+=t,n,e,t)}else c(null,"You need to specify a callback function to receive the request response")},v=function(o,e,t,n,r,s){var i;i=n?"POST":"GET";var a={};switch(e){case w:case q:case S:case k:a["X-Store-ID"]=m,a["Content-Type"]="application/json"}var c=function(){if(s)switch(e){case j:return void b(r,s,e=w,n,o);case x:return void b(r,s,e=_,n,o);case w:return void b(r,s,e=j,n,o);case _:return void b(r,s,e=x,n,o)}setTimeout(function(){v(o,e,t,n,r)},500)};if(g){var u={hostname:e,path:t,method:i,headers:a},l=g.request(u,function(e){if(503===e.statusCode&&o<3)return c(),void e.resume();var t="";e.setEncoding("utf8"),e.on("data",function(e){t+=e}),e.on("end",function(){h(e.statusCode,t,r)})});l.on("error",function(e){y.error(e),r(e,null)}),n&&l.write(JSON.stringify(n)),l.end()}else{var f;window.XDomainRequest?((f=new XDomainRequest).ontimeout=f.onerror=function(){c()},f.onload=function(){h(200,this.responseText,r)}):(f=new XMLHttpRequest).onreadystatechange=function(){4===this.readyState&&(503===this.status&&o<3?c():h(this.status,this.responseText,r))};var d="https://"+e+t;for(var p in f.open(i,d,!0),a)a.hasOwnProperty(p)&&f.setRequestHeader(p,a[p]);n?f.send(JSON.stringify(n)):f.send()}o++},h=function(e,t,o){var n,r;try{n=JSON.parse(t)}catch(e){return y.error(e),void o(e,null)}200===e?o(null,n):(r=n.hasOwnProperty("message")?n.message:"Unknown error, see response objet to more info",c(o,r,n))},n=function(e,t){var o;if("string"==typeof t){if(t.match(/^[a-f0-9]{24}$/))return!0;o="ID must be a valid 24 hexadecimal digits string, lowercase only"}else o="ID is required and must be a string";return c(e,o),!1},o=function(e,t,o){if(n(e,o)){b(e,"/"+t+"/"+o+".json","ioapi.ecvol.com")}},i=function(e,t,o,n,r,s,i){var a,c=l(o,n,r,s,i),u="/"+t+".json";""===c?a=j:u+=c,b(e,u,a)},l=function(e,t,o,n,r){var s={},i="";if("string"!=typeof r){if("number"==typeof e&&0<e&&(s.offset=e),"number"==typeof t&&0<t&&(s.limit=t),"number"==typeof o)switch(o){case 1:s.sort="name";break;case 2:s.sort="-name"}if(Array.isArray(n)){for(var a="",c=0;c<n.length;c++)"string"==typeof n[c]&&(""!==a&&(a+="%2C"),a+=n[c]);""!==a&&(s.fields=a)}for(var u in s)s.hasOwnProperty(u)&&(""!==i&&(i+="&"),i+=u+"="+s[u])}else i=r;return""!==i&&(i="?"+i),i};return{init:function(o,e,t,n){if(y="object"==typeof n&&n.hasOwnProperty("log")&&n.hasOwnProperty("error")?n:console,e&&t)m=e,s=t,"function"==typeof o&&o(null,{id:null,channel_id:null,store_id:m,store_object_id:s,default_lang:null});else if("object"==typeof location){var r="/domains/"+window.location.hostname+".json";b(function(e,t){e||(m=t.store_id,s=t.store_object_id),"function"==typeof o&&o(e,t)},r,"io.ecvol.com")}else{c(o,"It is necessary to specify the store ID as an init argument")}},storeId:function(){return m},storeObjectId:function(){return s},getStore:function(e,t){void 0===t&&s&&(t=s),o(e,"stores",t)},getProduct:function(e,t){o(e,"products",t)},getProductBySku:function(e,t){var n,o,r,s,i,a;n=e,r="SKU",s="product",i="/products.json?sku="+(o=t),a=u.getProduct,"string"==typeof o?b(function(e,t){if(e)n(e,t);else{var o=t.result;Array.isArray(o)&&"object"==typeof o[0]&&null!==o[0]?a(n,o[0]._id):c(n,"Any "+s+" found with this "+r,t)}},i):c(n,"The"+r+" is required and must be a string")},getOrder:function(e,t){o(e,"orders",t)},getCart:function(e,t){o(e,"carts",t)},getCustomer:function(e,t){o(e,"customers",t)},getApplication:function(e,t){o(e,"applications",t)},getBrand:function(e,t){o(e,"brands",t)},findBrandBySlug:function(e,t){b(e,"/brands.json?limit=1&slug="+t)},listBrands:function(e,t,o,n,r,s){i(e,"brands",t,o,n,r,s)},getCategory:function(e,t){o(e,"categories",t)},findCategoryBySlug:function(e,t){b(e,"/categories.json?limit=1&slug="+t)},listCategories:function(e,t,o,n,r,s){i(e,"categories",t,o,n,r,s)},getCollection:function(e,t){o(e,"collections",t)},findCollectionBySlug:function(e,t){b(e,"/collections.json?limit=1&slug="+t)},listCollections:function(e,t,o,n,r,s){i(e,"collections",t,o,n,r,s)},getById:o,getList:i,searchProducts:function(e,t,o,n,r,s,i,a,c,u,l){var f;if("object"==typeof l&&null!==l)f=l;else{var d=[{available:{order:"desc"}},{ad_relevance:{order:"desc"}},"_score"];switch(r){case 1:d.splice(2,0,{sales:{order:"desc"}});break;case 2:d.splice(1,0,{price:{order:"asc"}});break;case 3:d.splice(1,0,{price:{order:"desc"}});break;default:d.push({views:{order:"desc"}})}if(f={query:{bool:{filter:[{term:{visible:!0}}]}},sort:d,aggs:{brands:{terms:{field:"brands.name"}},categories:{terms:{field:"categories.name"}},specs:{nested:{path:"specs"},aggs:{grid:{terms:{field:"specs.grid",size:30},aggs:{text:{terms:{field:"specs.text"}}}}}},min_price:{min:{field:"price"}},max_price:{max:{field:"price"}},avg_price:{avg:{field:"price"}}}},"string"==typeof t&&(f.query.bool.must={multi_match:{query:t,fields:["name","keywords"]}},f.suggest={text:t,words:{term:{field:"name"}}}),"number"==typeof o&&0<o&&(f.from=o),f.size="number"==typeof n&&0<n?n:24,"object"==typeof s&&null!==s&&0<Object.keys(s).length)for(var p in s)Array.isArray(s[p])&&0<s[p].length&&f.query.bool.filter.push({nested:{path:"specs",query:{bool:{filter:[{term:{"specs.grid":p}},{terms:{"specs.text":s[p]}}]}}}});Array.isArray(i)&&0<i.length&&f.query.bool.filter.push({terms:{_id:i}});var m={brands:a,categories:c};for(var g in m){var y=m[g];if(Array.isArray(y)&&0<y.length){/^[a-f0-9]{24}$/.test(y[0])?g+="._id":g+=".name";var v={terms:{}};v.terms[g]=y,f.query.bool.filter.push(v)}}if("object"==typeof u&&null!==u&&0<Object.keys(u).length){var h={range:{price:{}}};"number"!=typeof u.min||isNaN(u.min)||(h.range.price.gte=u.min),"number"!=typeof u.max||isNaN(u.max)||(h.range.price.lte=u.max),f.query.bool.filter.push(h)}}b(e,"/items.json","apx-search.e-com.plus",f)},mapBySlug:function(r,e){var t="/"+m+"@"+e.replace(/\//g,"$")+".json";b(function(e,t){if(e)r(e,t);else{var o=t.GET;if(o)t={resource:(o=o.split("@"))[0],_id:o[1]},r(null,t);else{var n=(t={status:404,error_code:-44,message:"Page not found, invalid slug or store ID"}).message;c(r,n,t)}}},t,"iostorefront.ecvol.com")},mapByWindowUri:function(e){if("object"==typeof location){var t=window.location.pathname.slice(1);u.mapBySlug(e,t)}else{c(e,"This method is available client side only (JS on browser)")}},listRecommendedProducts:function(e,t){if(n(e,t)){b(e,"/products/"+t+"/recommended.json","apx-graphs.e-com.plus")}},listRelatedProducts:function(e,t){if(n(e,t)){b(e,"/products/"+t+"/related.json","apx-graphs.e-com.plus")}},calculateShipping:function(e,t){b(e,"/calculate_shipping.json",k,t)},listPayments:function(e,t){b(e,"/list_payments.json",k,t)},createTransaction:function(e,t){b(e,"/create_transaction.json",k,t)},checkout:function(e,t){b(e,"/@checkout.json",k,t)},http:b}}();e?module.exports=u:window.EcomIo=u}();
//# sourceMappingURL=sdk.min.js.map