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