ra-data-treeql
Version:
TreeQL data provider for react-admin
1 lines • 10.2 kB
JavaScript
function _arrayLikeToArray(arr,len){if(len==null||len>arr.length)len=arr.length;for(var i1=0,arr2=new Array(len);i1<len;i1++)arr2[i1]=arr[i1];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 i2=0;i2<props.length;i2++){var descriptor=props[i2];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,i3){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(i3&&_arr.length===i3)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 i4=1;i4<arguments.length;i4++){var source=arguments[i4]!=null?arguments[i4]:{};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 _slicedToArray(arr,i5){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i5)||_unsupportedIterableToArray(arr,i5)||_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";import{fetchUtils as T}from"ra-core";var f=Object.defineProperty;var i=function(a,e){return f(a,"name",{value:e,configurable:!0})};var m=i(function(a){return Object.entries(a).map(function(param,r){var _param=_slicedToArray(param,2),e=_param[0],t=_param[1];var s="eq",n=e.slice(-2);return e.slice(-3,-2)==="_"&&U(n)?(s=n,[e.slice(0,-3),s,P(s,t)].filter(function(o){return!!o}).join(",")):"".concat(e,",eq,").concat(t)}).join("&filter=")},"formatFilter"),P=i(function(a,e){switch(a){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"),$=["cs","sw","ew","eq","lt","le","ge","gt","bt","in","is"],U=i(function(a){return $.includes(a)},"isValidFilterOperator"),b=i(function(a){return decodeURIComponent(new URLSearchParams(a).toString())},"formatParams"),c=function(){"use strict";function _class(e){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:T.fetchJson;_classCallCheck(this,_class);this.httpClient=t;this.apiUrl=e.endsWith("/")?e.slice(0,-1):e}_createClass(_class,[{key:"getList",value:function getList(e,t){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var r,s,n,o,d,p,y,l,ref,_json,R,u;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:r=t.sort;s=t.pagination;n=t.filter;o=s.page;d=s.perPage;p=r.field;y=r.order;l=_this.getURL(e,{order:"".concat(p,",").concat(y),page:"".concat(o,",").concat(d),filter:m(n)});_ctx.next=10;return _this.httpClient(l);case 10:ref=_ctx.sent;_json=ref.json;R=_json.records;u=_json.results;return _ctx.abrupt("return",{data:R,total:u});case 15:case"end":return _ctx.stop()}},_callee)}))()}},{key:"getOne",value:function getOne(e,t){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var ref,r;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:_ctx.next=2;return _this.httpClient("".concat(_this.getURL(e),"/").concat(t.id));case 2:ref=_ctx.sent;r=ref.json;return _ctx.abrupt("return",{data:r});case 5:case"end":return _ctx.stop()}},_callee)}))()}},{key:"getMany",value:function getMany(e,t){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var r,ref,s;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:r="".concat(_this.getURL(e),"/").concat(t.ids.join(","));_ctx.next=3;return _this.httpClient(r);case 3:ref=_ctx.sent;s=ref.json;return _ctx.abrupt("return",{data:Array.isArray(s)?s:[s]});case 6:case"end":return _ctx.stop()}},_callee)}))()}},{key:"getManyReference",value:function getManyReference(e,t){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var r,s,n,o,d,p,y,l,R,u,ref,_json,g,h;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:r=t.id;s=t.target;n=t.sort;o=t.pagination;d=t.filter;p=o.page;y=o.perPage;l=n.field;R=n.order;u=_this.getURL(e,{order:"".concat(l,",").concat(R),page:"".concat(p,",").concat(y),filter:m(_objectSpreadProps(_objectSpread({},d),_defineProperty({},s,r)))});_ctx.next=12;return _this.httpClient(u);case 12:ref=_ctx.sent;_json=ref.json;g=_json.records;h=_json.results;return _ctx.abrupt("return",{data:g,total:h});case 17:case"end":return _ctx.stop()}},_callee)}))()}},{key:"update",value:function update(e,t){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var r,s,n;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:r=t.id,s=t.data,n=t.previousData;_ctx.next=3;return _this.httpClient("".concat(_this.getURL(e),"/").concat(r),{method:"PUT",body:JSON.stringify(s)});case 3:return _ctx.abrupt("return",(_ctx.sent,{data:_objectSpread({},n,s)}));case 4:case"end":return _ctx.stop()}},_callee)}))()}},{key:"updateMany",value:function updateMany(e,t){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var r,s;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:r=t.ids,s=t.data;_ctx.next=3;return _this.httpClient("".concat(_this.getURL(e),"/").concat(r.join(",")),{method:"PUT",body:JSON.stringify(s)});case 3:return _ctx.abrupt("return",(_ctx.sent,{data:r}));case 4:case"end":return _ctx.stop()}},_callee)}))()}},{key:"create",value:function create(e,t){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var r,ref,s;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:r=t.data;_ctx.next=3;return _this.httpClient("".concat(_this.getURL(e)),{method:"POST",body:JSON.stringify(r)});case 3:ref=_ctx.sent;s=ref.json;return _ctx.abrupt("return",{data:_objectSpreadProps(_objectSpread({},r),{id:s})});case 6:case"end":return _ctx.stop()}},_callee)}))()}},{key:"delete",value:function _delete(e,t){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var r,s;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:r=t.id,s=t.previousData;_ctx.next=3;return _this.httpClient("".concat(_this.getURL(e),"/").concat(r),{method:"DELETE"});case 3:return _ctx.abrupt("return",(_ctx.sent,{data:s}));case 4:case"end":return _ctx.stop()}},_callee)}))()}},{key:"deleteMany",value:function deleteMany(e,t){var _this=this;return _asyncToGenerator(regeneratorRuntime.mark(function _callee(){var r;return regeneratorRuntime.wrap(function _callee$(_ctx){while(1)switch(_ctx.prev=_ctx.next){case 0:r=t.ids;_ctx.next=3;return _this.httpClient("".concat(_this.getURL(e),"/").concat(r.join(",")),{method:"DELETE"});case 3:return _ctx.abrupt("return",(_ctx.sent,{data:r}));case 4:case"end":return _ctx.stop()}},_callee)}))()}},{key:"getURL",value:function getURL(e,t){var r=t&&Object.keys(t).length>0?"?".concat(b(t)):"";return"".concat(this.apiUrl,"/records/").concat(e).concat(r)}}]);return _class}();i(c,"TreeQLDataProvider");var M=i(function(a,e){return new c(a,e)},"getDataProvider"),x=M;export{c as TreeQLDataProvider,x as default,m as formatFilter}//# sourceMappingURL=index.js.map