UNPKG

ra-data-treeql

Version:
1 lines 12.7 kB
function _arrayLikeToArray(arr,len){if(len==null||len>arr.length)len=arr.length;for(var i=0,arr2=new Array(len);i<len;i++)arr2[i]=arr[i];return arr2}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr}function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg);var value=info.value}catch(error){reject(error);return}if(info.done){resolve(value)}else{Promise.resolve(value).then(_next,_throw)}}function _asyncToGenerator(fn){return function(){var self=this,args=arguments;return new Promise(function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value)}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err)}_next(undefined)})}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}function _iterableToArrayLimit(arr,i){var _i=arr==null?null:typeof Symbol!=="undefined"&&arr[Symbol.iterator]||arr["@@iterator"];if(_i==null)return;var _arr=[];var _n=true;var _d=false;var _s,_e;try{for(_i=_i.call(arr);!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break}}catch(err){_d=true;_e=err}finally{try{if(!_n&&_i["return"]!=null)_i["return"]()}finally{if(_d)throw _e}}return _arr}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};var ownKeys1=Object.keys(source);if(typeof Object.getOwnPropertySymbols==="function"){ownKeys1=ownKeys1.concat(Object.getOwnPropertySymbols(source).filter(function(sym){return Object.getOwnPropertyDescriptor(source,sym).enumerable}))}ownKeys1.forEach(function(key){_defineProperty(target,key,source[key])})}return target}function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly){symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable})}keys.push.apply(keys,symbols)}return keys}function _objectSpreadProps(target,source){source=source!=null?source:{};if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source))}else{ownKeys(Object(source)).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key))})}return target}function _objectWithoutProperties(source,excluded){if(source==null)return{};var target=_objectWithoutPropertiesLoose(source,excluded);var key,i;if(Object.getOwnPropertySymbols){var sourceSymbolKeys=Object.getOwnPropertySymbols(source);for(i=0;i<sourceSymbolKeys.length;i++){key=sourceSymbolKeys[i];if(excluded.indexOf(key)>=0)continue;if(!Object.prototype.propertyIsEnumerable.call(source,key))continue;target[key]=source[key]}}return target}function _objectWithoutPropertiesLoose(source,excluded){if(source==null)return{};var target={};var sourceKeys=Object.keys(source);var key,i;for(i=0;i<sourceKeys.length;i++){key=sourceKeys[i];if(excluded.indexOf(key)>=0)continue;target[key]=source[key]}return target}function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_unsupportedIterableToArray(arr,i)||_nonIterableRest()}function _unsupportedIterableToArray(o,minLen){if(!o)return;if(typeof o==="string")return _arrayLikeToArray(o,minLen);var n=Object.prototype.toString.call(o).slice(8,-1);if(n==="Object"&&o.constructor)n=o.constructor.name;if(n==="Map"||n==="Set")return Array.from(n);if(n==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return _arrayLikeToArray(o,minLen)}import regeneratorRuntime from"regenerator-runtime";var p=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var $=Object.prototype.hasOwnProperty;var c=function(n,e){return p(n,"name",{value:e,configurable:!0})};var x=function(n,e){for(var a in e)p(n,a,{get:e[a],enumerable:!0})},G=function(n,e,a,t){var _iteratorNormalCompletion=true,_didIteratorError=false,_iteratorError=undefined;if(e&& typeof e=="object"|| typeof e=="function")try{var _loop=function(_iterator,_step){var r=_step.value;!$.call(n,r)&&r!==a&&p(n,r,{get:function(){return e[r]},enumerable:!(t=w(e,r))||t.enumerable})};for(var _iterator=O(e)[Symbol.iterator](),_step;!(_iteratorNormalCompletion=(_step=_iterator.next()).done);_iteratorNormalCompletion=true)_loop(_iterator,_step)}catch(err){_didIteratorError=true;_iteratorError=err}finally{try{if(!_iteratorNormalCompletion&&_iterator.return!=null){_iterator.return()}}finally{if(_didIteratorError){throw _iteratorError}}}return n};var L=function(n){return G(p({},"__esModule",{value:!0}),n)};var F={};x(F,{TreeQLDataProvider:function(){return d},default:function(){return I},formatParams:function(){return U}});module.exports=L(F);var T=require("ra-core"),C=["cs","sw","ew","eq","lt","le","ge","gt","bt","in","is"],D="q",j="search",g="filter",h=c(function(n){return C.includes(n)},"isValidFilterOperator"),A=c(function(n){return Object.entries(n).map(function(param){var _param=_slicedToArray(param,2),e=_param[0],a=_param[1];if(e===D)return[j,a];var t="eq",r=e.slice(-2);return e.slice(-3,-2)==="_"&&h(r)?(t=r,[g,[e.slice(0,-3),t,P(t,a)].filter(function(s){return!!s}).join(",")]):e.slice(-4,-2)==="_n"&&h(r)?(t=r,[g,[e.slice(0,-4),"n"+t,P(t,a)].filter(function(s){return!!s}).join(",")]):[g,"".concat(e,",eq,").concat(a)]})},"formatFilter"),P=c(function(n,e){switch(n){case"bt":if(!Array.isArray(e))throw new TypeError('Array expected as filter value for filter type "between" (bt)');return"".concat(e[0],",").concat(e[1]);case"in":if(!Array.isArray(e))throw new TypeError('Array expected as filter value for filter type "in"');return e.join(",");case"is":return null;default:return e}},"formatFilterArguments"),U=c(function(n){var e=n.filter,a=n.meta,t=_objectWithoutProperties(n,["filter","meta"]),r=new URLSearchParams(t);e&&A(e).forEach(function(param){var _param=_slicedToArray(param,2),i=_param[0],o=_param[1];return r.append(i,o)}),Object.entries(a!==null&&a!==void 0?a:{}).filter(function(param){var _param=_slicedToArray(param,1),i=_param[0];return!["order","page","filter"].includes(i)}).forEach(function(param){var _param=_slicedToArray(param,2),i=_param[0],o=_param[1];return r.append(i,o)});var s=r.toString();return s?"?"+decodeURIComponent(s):""},"formatParams"),d=function(){"use strict";function _class(e){var a=arguments.length>1&&arguments[1]!==void 0?arguments[1]:T.fetchUtils.fetchJson;_classCallCheck(this,_class);this.httpClient=a;this.apiUrl=e.endsWith("/")?e.slice(0,-1):e}_createClass(_class,[{key:"getList",value:function getList(e,a){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var t,r,s,i,o,l,y,R,u,ref,_json,m,f;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:t=a.sort;r=a.pagination;s=a.filter;i=a.meta;o=r.page;l=r.perPage;y=t.field;R=t.order;u=_this.getURL(e,{order:"".concat(y,",").concat(R),page:"".concat(o,",").concat(l),filter:s,meta:i});_ctx.next=11;return _this.httpClient(u);case 11:ref=_ctx.sent;_json=ref.json;m=_json.records;f=_json.results;return _ctx.abrupt("return",{data:m,total:f});case 16:case"end":return _ctx.stop()}},_callee)}))()}},{key:"getOne",value:function getOne(e,a){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var t,r,ref,s;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:t=a.id;r=a.meta;_ctx.next=4;return _this.httpClient(_this.getURL(e,{id:t,meta:r}));case 4:ref=_ctx.sent;s=ref.json;return _ctx.abrupt("return",{data:s});case 7:case"end":return _ctx.stop()}},_callee)}))()}},{key:"getMany",value:function getMany(e,a){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var t,r,ref,s;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:t=a.ids;r=a.meta;_ctx.next=4;return _this.httpClient(_this.getURL(e,{ids:t,meta:r}));case 4:ref=_ctx.sent;s=ref.json;return _ctx.abrupt("return",{data:Array.isArray(s)?s:[s]});case 7:case"end":return _ctx.stop()}},_callee)}))()}},{key:"getManyReference",value:function getManyReference(e,a){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var t,r,s,i,o,l,y,R,u,m,f,ref,_json,b,M;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:t=a.id;r=a.target;s=a.sort;i=a.pagination;o=a.filter;l=a.meta;y=i.page;R=i.perPage;u=s.field;m=s.order;f=_this.getURL(e,{order:"".concat(u,",").concat(m),page:"".concat(y,",").concat(R),filter:_objectSpreadProps(_objectSpread({},o),_defineProperty({},r,t)),meta:l});_ctx.next=13;return _this.httpClient(f);case 13:ref=_ctx.sent;_json=ref.json;b=_json.records;M=_json.results;return _ctx.abrupt("return",{data:b,total:M});case 18:case"end":return _ctx.stop()}},_callee)}))()}},{key:"update",value:function update(e,a){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var t,r,s,i;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:t=a.id,r=a.data,s=a.previousData,i=a.meta;_ctx.next=3;return _this.httpClient(_this.getURL(e,{id:t,meta:i}),{method:"PUT",body:JSON.stringify(r)});case 3:return _ctx.abrupt("return",(_ctx.sent,{data:_objectSpread({id:t},s,r)}));case 4:case"end":return _ctx.stop()}},_callee)}))()}},{key:"updateMany",value:function updateMany(e,a){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var t,r,s;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:t=a.ids,r=a.data,s=a.meta;_ctx.next=3;return _this.httpClient(_this.getURL(e,{ids:t,meta:s}),{method:"PUT",body:JSON.stringify(r)});case 3:return _ctx.abrupt("return",(_ctx.sent,{data:t}));case 4:case"end":return _ctx.stop()}},_callee)}))()}},{key:"create",value:function create(e,a){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var t,r,ref,s;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:t=a.data;r=a.meta;_ctx.next=4;return _this.httpClient(_this.getURL(e,{meta:r}),{method:"POST",body:JSON.stringify(t)});case 4:ref=_ctx.sent;s=ref.json;return _ctx.abrupt("return",{data:_objectSpreadProps(_objectSpread({},t),{id:s})});case 7:case"end":return _ctx.stop()}},_callee)}))()}},{key:"delete",value:function _delete(e,a){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var t,r,s;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:t=a.id,r=a.previousData,s=a.meta;_ctx.next=3;return _this.httpClient(_this.getURL(e,{id:t,meta:s}),{method:"DELETE"});case 3:return _ctx.abrupt("return",(_ctx.sent,{data:_objectSpread({id:t},r)}));case 4:case"end":return _ctx.stop()}},_callee)}))()}},{key:"deleteMany",value:function deleteMany(e,a){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var t,r;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:t=a.ids,r=a.meta;_ctx.next=3;return _this.httpClient(_this.getURL(e,{ids:t,meta:r}),{method:"DELETE"});case 3:return _ctx.abrupt("return",(_ctx.sent,{data:t}));case 4:case"end":return _ctx.stop()}},_callee)}))()}},{key:"getURL",value:function getURL(e,a){var t=a.id,r=a.ids,s=_objectWithoutProperties(a,["id","ids"]),i=t?"/".concat(t):r?"/".concat(r.join(",")):"";return"".concat(this.apiUrl,"/records/").concat(e).concat(i).concat(U(s))}}]);return _class}();c(d,"TreeQLDataProvider");var E=c(function(n,e){return new d(n,e)},"getDataProvider"),I=E;0&&(module.exports={TreeQLDataProvider:TreeQLDataProvider,formatParams:formatParams})//# sourceMappingURL=index.js.map