UNPKG

@commercetools/api-request-builder

Version:

Helper functions to construct API requests URI for the commercetools platform in a declarative way.

3 lines (2 loc) 18.4 kB
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self).commercetoolsApiRequestBuilder={})}(this,(function(e){"use strict";function r(e){var r=function(e,r){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var n=t.call(e,r||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(e)}(e,"string");return"symbol"==typeof r?r:r+""}function t(e,t,n){return(t=r(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function n(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function o(e){for(var r=1;r<arguments.length;r++){var o=null!=arguments[r]?arguments[r]:{};r%2?n(Object(o),!0).forEach((function(r){t(e,r,o[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):n(Object(o)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(o,r))}))}return e}var s="create",i="update",a="delete",u="query",c="queryOne",p="queryExpand",h="queryLocation",f="search",d="projection",y="suggest",l=Object.freeze({__proto__:null,create:s,update:i,del:a,query:u,queryOne:c,queryExpand:p,queryLocation:h,search:f,projection:d,suggest:y}),m={login:{type:"login",endpoint:"/login",features:[s]},cartDiscounts:{type:"cart-discounts",endpoint:"/cart-discounts",features:[s,i,a,u,c,p]},carts:{type:"carts",endpoint:"/carts",features:[s,i,a,u,c,p]},categories:{type:"categories",endpoint:"/categories",features:[s,i,a,u,c,p]},channels:{type:"channels",endpoint:"/channels",features:[s,i,a,u,c,p]},customerGroups:{type:"customer-groups",endpoint:"/customer-groups",features:[s,i,a,u,c,p]},customers:{type:"customers",endpoint:"/customers",features:[s,i,a,u,c,p]},customersPassword:{type:"customers-password",endpoint:"/customers/password",features:[s]},customersPasswordToken:{type:"customers-password-token",endpoint:"/customers/password-token",features:[s,c]},customersPasswordReset:{type:"customers-password-reset",endpoint:"/customers/password/reset",features:[s]},customersEmailVerificationToken:{type:"customers-email-verification-token",endpoint:"/customers/email-token",features:[s,c]},customersEmailVerification:{type:"customers-email-verification",endpoint:"/customers/email/confirm",features:[s]},customObjects:{type:"custom-objects",endpoint:"/custom-objects",features:[s,i,a,u,c,p]},discountCodes:{type:"discount-codes",endpoint:"/discount-codes",features:[s,i,a,u,c,p]},extensions:{type:"extensions",endpoint:"/extensions",features:[s,i,a,u,c,p]},inventory:{type:"inventory",endpoint:"/inventory",features:[s,i,a,u,c,p]},messages:{type:"messages",endpoint:"/messages",features:[u,c,p]},myActiveCart:{type:"my-carts",endpoint:"/me/active-cart",features:[c]},myCarts:{type:"my-carts",endpoint:"/me/carts",features:[s,i,a,u,c,p]},myOrders:{type:"my-orders",endpoint:"/me/orders",features:[s,i,a,u,c,p]},orders:{type:"orders",endpoint:"/orders",features:[s,i,a,u,c,p]},orderEdits:{type:"orders-edits",endpoint:"/orders/edits",features:[s,i,a,u,c,p]},orderImport:{type:"orderImport",endpoint:"/orders/import",features:[s,u]},payments:{type:"payments",endpoint:"/payments",features:[s,i,a,u,c,p]},productDiscounts:{type:"product-discounts",endpoint:"/product-discounts",features:[s,i,a,u,c,p]},productProjections:{type:"product-projections",endpoint:"/product-projections",features:[u,c,p,d]},productProjectionsSearch:{type:"product-projections-search",endpoint:"/product-projections/search",features:[f,c,p,d]},productProjectionsSuggest:{type:"product-projections-suggest",endpoint:"/product-projections/suggest",features:[f,y,c,d]},products:{type:"products",endpoint:"/products",features:[s,i,a,u,c,p]},productTypes:{type:"product-types",endpoint:"/product-types",features:[s,i,a,u,c,p]},project:{type:"project",endpoint:"/",features:[i,u]},reviews:{type:"reviews",endpoint:"/reviews",features:[s,i,a,u,c,p]},shippingMethods:{type:"shipping-methods",endpoint:"/shipping-methods",features:[s,i,a,u,c,p,h]},shoppingLists:{type:"shopping-lists",endpoint:"/shopping-lists",features:[s,i,a,u,c,p]},states:{type:"states",endpoint:"/states",features:[s,i,a,u,c,p]},stores:{type:"stores",endpoint:"/stores",features:[s,i,a,u,c,p]},subscriptions:{type:"subscriptions",endpoint:"/subscriptions",features:[s,i,a,u,c,p]},taxCategories:{type:"tax-categories",endpoint:"/tax-categories",features:[s,i,a,u,c,p]},types:{type:"types",endpoint:"/types",features:[s,i,a,u,c,p]},zones:{type:"zones",endpoint:"/zones",features:[s,i,a,u,c,p]}};function g(){this.params.expand=[],this.params.key=null,this.features.includes(c)&&(this.params.id=null),this.features.includes(u)&&(this.params.pagination={page:null,perPage:null,sort:[],withTotal:null},this.params.query={operator:"and",where:[]}),this.features.includes(f)&&(this.params.pagination={page:null,perPage:null,sort:[],withTotal:null},this.params.search={facet:[],filter:[],filterByQuery:[],filterByFacets:[],fuzzy:!1,fuzzyLevel:0,markMatchingVariants:!1,text:null}),this.features.includes(h)&&(this.params.location={currency:"",country:"",state:""}),this.features.includes(y)&&(this.params.searchKeywords=[])}var w=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)};function b(e){var r=this,t=["expand","id","key","customerId","cartId","sort","page","perPage","staged","priceCurrency","priceCountry","priceCustomerGroup","priceChannel","text","fuzzy","fuzzyLevel","markMatchingVariants","facet","filter","filterByQuery","filterByFacets","searchKeywords","where","whereOperator","version","country","currency","state","dataErasure","withTotal","applyOrderEditTo","container","orderNumber"];Object.keys(e).forEach((function(e){if(!t.includes(e))throw new Error('Unknown key "'.concat(e,'"'))})),e.expand&&e.expand.forEach((function(e){r.expand(e)})),w(e,"id")&&this.byId(e.id),w(e,"key")&&!w(e,"container")&&this.byKey(e.key),w(e,"customerId")&&this.byCustomerId(e.customerId),w(e,"cartId")&&this.byCartId(e.cartId),w(e,"country")&&this.byCountry(e.country),w(e,"currency")&&this.byCurrency(e.currency),w(e,"state")&&this.byState(e.state),e.sort&&e.sort.forEach((function(e){r.sort(e.by,"asc"===e.direction)})),w(e,"page")&&this.page(e.page),w(e,"perPage")&&this.perPage(e.perPage),w(e,"staged")&&this.staged(e.staged),w(e,"priceCurrency")&&this.priceCurrency(e.priceCurrency),w(e,"priceCountry")&&this.priceCountry(e.priceCountry),w(e,"priceCustomerGroup")&&this.priceCustomerGroup(e.priceCustomerGroup),w(e,"priceChannel")&&this.priceChannel(e.priceChannel),w(e,"orderNumber")&&this.byOrderNumber(e.orderNumber),e.text&&this.text(e.text.value,e.text.language),e.fuzzy&&this.fuzzy(),w(e,"fuzzyLevel")&&this.fuzzyLevel(e.fuzzyLevel),e.markMatchingVariants&&this.markMatchingVariants(),e.facet&&e.facet.forEach((function(e){r.facet(e)})),e.filter&&e.filter.forEach((function(e){r.filter(e)})),e.filterByQuery&&e.filterByQuery.forEach((function(e){r.filterByQuery(e)})),e.filterByFacets&&e.filterByFacets.forEach((function(e){r.filterByFacets(e)})),e.searchKeywords&&e.searchKeywords.forEach((function(e){r.searchKeywords(e.value,e.language)})),e.where&&e.where.forEach((function(e){r.where(e)})),w(e,"whereOperator")&&this.whereOperator(e.whereOperator),w(e,"version")&&this.withVersion(e.version),w(e,"dataErasure")&&this.withFullDataErasure(),w(e,"withTotal")&&this.withTotal(e.withTotal),w(e,"applyOrderEditTo")&&this.applyOrderEditTo(e.applyOrderEditTo),w(e,"container")&&w(e,"key")&&this.byContainerAndKey(e.container,e.key)}function v(e){if("number"!=typeof e)throw new Error("A resource version is missing or invalid");return this.params.version=e,this}function E(){return this.params.dataErasure="dataErasure=true",this}function C(e){if("string"!=typeof e)throw new Error("A resource orderEditId is missing or invalid");return this.params.applyOrderEditTo=e,this}var O=Object.freeze({__proto__:null,where:function(e){if(!e)throw new Error("Required argument for `where` is missing");var r=encodeURIComponent(e);return this.params.query.where.push(r),this},whereOperator:function(e){if(!e)throw new Error("Required argument for `whereOperator` is missing");if("and"!==e&&"or"!==e)throw new Error("Required argument for `whereOperator` is invalid, allowed values are (`and`, `or`)");return this.params.query.operator=e,this}});var j=Object.freeze({__proto__:null,byId:function(e){if(!e)throw new Error("Required argument for `byId` is missing");if(this.params.key)throw new Error("A key for this resource has already been set. You cannot use both `byKey` and `byId`.");if(this.params.customerId)throw new Error("A customerId for this resource has already been set. You cannot use both `byId` and `byCustomerId`.");if(this.params.cartId)throw new Error("A cartId for this resource has already been set. You cannot use both `byId` and `byCartId`.");return this.params.id=e,this},byKey:function(e){if(!e)throw new Error("Required argument for `byKey` is missing");if(this.params.id)throw new Error("An ID for this resource has already been set. You cannot use both `byId` and `byKey`.");return this.params.key=e,this},byCustomerId:function(e){if(!e)throw new Error("Required argument for `byCustomerId` is missing");if(this.params.id)throw new Error("An ID for this resource has already been set. You cannot use both `byId` and `byCustomerId`.");return this.params.customerId=e,this},byOrderNumber:function(e){if("string"!=typeof e)throw new Error("Required argument for `byOrderNumber` is missing or invalid");return this.params.orderNumber=e,this},byCartId:function(e){if(!e)throw new Error("Required argument for `byCartId` is missing");if(this.params.id)throw new Error("An ID for this resource has already been set. You cannot use both `byId` and `byCartId`.");return this.params.cartId=e,this},byContainerAndKey:function(e,r){if("string"!=typeof e||"string"!=typeof r)throw new Error("Required `container` or `key` argument for `byContainerAndKey` needs to be a string");return this.params.container=e,this.params.key=r,this}});var z=Object.freeze({__proto__:null,byCountry:function(e){if(!e)throw new Error("Required argument for `byCountry` is missing");return this.params.location.country=e,this},byCurrency:function(e){if(!e)throw new Error("Required argument for `byCurrency` is missing");if(!this.params.location.country)throw new Error("A `country` for this resource has not been set. You must set the country in order to use the `byCurrency` method.");return this.params.location.currency=e,this},byState:function(e){if(!e)throw new Error("Required argument for `byState` is missing");if(!this.params.location.country)throw new Error("A `country` for this resource has not been set. You must set the country in order to use the `byState` method.");return this.params.location.state=e,this}});var I=Object.freeze({__proto__:null,expand:function(e){if(!e)throw new Error("Required argument for `expand` is missing");var r=encodeURIComponent(e);return this.params.expand.push(r),this}});var q=Object.freeze({__proto__:null,sort:function(e){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(!e)throw new Error("Required argument for `sort` is missing");var t=r?"asc":"desc",n=encodeURIComponent("".concat(e," ").concat(t));return this.params.pagination.sort.push(n),this},page:function(e){if("number"!=typeof e&&!e)throw new Error("Required argument for `page` is missing or invalid");if("number"!=typeof e||e<1)throw new Error("Required argument for `page` must be a number >= 1");return this.params.pagination.page=e,this},perPage:function(e){if("number"!=typeof e&&!e)throw new Error("Required argument for `perPage` is missing or invalid");if("number"!=typeof e||e<0)throw new Error("Required argument for `perPage` must be a number >= 0");return this.params.pagination.perPage=e,this},withTotal:function(){var e=!(arguments.length>0&&void 0!==arguments[0])||arguments[0];if("boolean"!=typeof e)throw new Error("Required argument for `withTotal` is missing or invalid");return this.params.pagination.withTotal=e,this}});var R=Object.freeze({__proto__:null,staged:function(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];return this.params.staged=e,this},priceCurrency:function(e){return this.params.priceCurrency=e,this},priceCountry:function(e){return this.params.priceCountry=e,this},priceCustomerGroup:function(e){return this.params.priceCustomerGroup=e,this},priceChannel:function(e){return this.params.priceChannel=e,this}});var k=Object.freeze({__proto__:null,searchKeywords:function(e,r){if(!e||!r)throw new Error("Required arguments for `searchKeywords` are missing");return this.params.searchKeywords.push({lang:r,value:encodeURIComponent(e)}),this}});var _=Object.freeze({__proto__:null,text:function(e,r){if(!e||!r)throw new Error("Required arguments for `text` are missing");return this.params.search.text={lang:r,value:encodeURIComponent(e)},this},fuzzy:function(){return this.params.search.fuzzy=!0,this},fuzzyLevel:function(e){if(null==e)throw new Error("Required argument for `fuzzyLevel` is missing");return this.params.search.fuzzyLevel=e,this},markMatchingVariants:function(){return this.params.search.markMatchingVariants=!0,this},facet:function(e){if(!e)throw new Error("Required argument for `facet` is missing");var r=encodeURIComponent(e);return this.params.search.facet.push(r),this},filter:function(e){if(!e)throw new Error("Required argument for `filter` is missing");var r=encodeURIComponent(e);return this.params.search.filter.push(r),this},filterByQuery:function(e){if(!e)throw new Error("Required argument for `filterByQuery` is missing");var r=encodeURIComponent(e);return this.params.search.filterByQuery.push(r),this},filterByFacets:function(e){if(!e)throw new Error("Required argument for `filterByFacets` is missing");var r=encodeURIComponent(e);return this.params.search.filterByFacets.push(r),this}}),P=["type","endpoint","features"];function x(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";if(!e)throw new Error("Cannot create a service without its definition.");if(P.forEach((function(r){if(!e[r])throw new Error("Definition is missing required parameter ".concat(r,"."))})),!Array.isArray(e.features)||!e.features.length)throw new Error("Definition requires `features` to be a non empty array.");if(!r)throw new Error("No project defined. Please enter a project key");var t=e.type,n=e.endpoint,s=e.features;return function(e){var r=arguments.length>1&&void 0!==arguments[1]&&arguments[1],t={};return Object.keys(e).forEach((function(n){Object.defineProperty(t,n,{value:e[n],enumerable:!!r||"function"==typeof e[n]})})),t}(o(o({type:t,features:s,params:{id:null,expand:[],pagination:{page:null,perPage:null,sort:[],withTotal:null},location:{currency:"",country:"",state:""},query:{operator:"and",where:[]},searchKeywords:[]},withVersion:v,withFullDataErasure:E,applyOrderEditTo:C},s.reduce((function(e,r){return r===u?o(o(o({},e),O),q):r===c?o(o({},e),j):r===h?o(o({},e),z):r===p?o(o({},e),I):r===f?o(o(o(o({},e),_),q),{},{params:{expand:[],searchKeywords:[],pagination:{page:null,perPage:null,sort:[],withTotal:null},search:{facet:[],filter:[],filterByQuery:[],filterByFacets:[],fuzzy:!1,fuzzyLevel:0,markMatchingVariants:!1,text:null}}}):r===y?o(o(o(o({},e),_),q),k):r===d?o(o({},e),R):e}),{})),{},{build:function(){var e,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{withProjectKey:!0,applyOrderEdit:!1},o=t.withProjectKey,s=t.applyOrderEdit,i=function(e){if(!e)throw new Error("Missing options object to build query string.");var r=e.query,t=e.pagination,n=e.search,o=e.expand,s=e.staged,i=e.priceCurrency,a=e.priceCountry,u=e.priceCustomerGroup,c=e.priceChannel,p=e.searchKeywords,h=e.version,f=e.customerId,d=e.cartId,y=e.location,l=e.dataErasure,m=e.applyOrderEditTo,g=[];if(f&&g.push("customerId=".concat(f)),d&&g.push("cartId=".concat(d)),"boolean"==typeof s&&g.push("staged=".concat(s.toString())),i&&g.push("priceCurrency=".concat(i)),a&&g.push("priceCountry=".concat(a)),u&&g.push("priceCustomerGroup=".concat(u)),c&&g.push("priceChannel=".concat(c)),o&&o.length&&(g=g.concat(o.map((function(e){return"expand=".concat(e)})))),r){var w=r.operator,b=r.where.join(encodeURIComponent(" ".concat(w," ")));b&&g.push("where=".concat(b))}if(y){var v=y.country,E=y.currency,C=y.state;v&&g.push("country=".concat(v)),E&&g.push("currency=".concat(E)),C&&g.push("state=".concat(C))}if(t){var O=t.page,j=t.perPage,z=t.sort,I=t.withTotal;if("number"==typeof j&&g.push("limit=".concat(j)),O){var q=(j||20)*(O-1);g.push("offset=".concat(q))}z&&z.length&&(g=g.concat(z.map((function(e){return"sort=".concat(e)})))),"boolean"==typeof I&&g.push("withTotal=".concat(String(I)))}if(n){var R=n.text,k=n.fuzzy,_=n.fuzzyLevel,P=n.markMatchingVariants,x=n.facet,T=n.filter,K=n.filterByQuery,B=n.filterByFacets;R&&g.push("text.".concat(R.lang,"=").concat(R.value)),k&&g.push("fuzzy=true"),_&&g.push("fuzzyLevel=".concat(_)),g.push("markMatchingVariants=".concat(P.toString())),x.forEach((function(e){return g.push("facet=".concat(e))})),T.forEach((function(e){return g.push("filter=".concat(e))})),K.forEach((function(e){return g.push("filter.query=".concat(e))})),B.forEach((function(e){return g.push("filter.facets=".concat(e))}))}return p&&p.forEach((function(e){return g.push("searchKeywords.".concat(e.lang,"=").concat(e.value))})),h&&g.push("version=".concat(h)),l&&g.push(l),m&&g.push("/".concat(m,"/apply")),g.join("&")}(this.params),a=(o?"/".concat(r):"")+n+((e=this.params).id?"/".concat(e.id):e.orderNumber?"/order-number=".concat(e.orderNumber):e.key&&!e.container?"/key=".concat(e.key):e.key&&e.container?"/".concat(e.container,"/").concat(e.key):"")+(i&&!s?"?".concat(i):i);return g.call(this),a},parse:function(e){return b.call(this,e),this}}))}e.createRequestBuilder=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=o(o({},m),e.customServices);return Object.keys(r).reduce((function(n,s){return o(o({},n),{},t({},s,x(r[s],e.projectKey)))}),{})},e.features=l,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=commercetools-api-request-builder.umd.min.js.map