@datorama/akita
Version:
State Management Tailored-Made for JS Applications
2 lines • 7.82 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/common/http"),require("rxjs"),require("rxjs/operators"),require("@angular/core"),require("@datorama/akita")):"function"==typeof define&&define.amd?define("@datorama/akita/angular/playground/projects/ng-entity-service",["exports","@angular/common/http","rxjs","rxjs/operators","@angular/core","@datorama/akita"],e):e((t.datorama=t.datorama||{},t.datorama.akita=t.datorama.akita||{},t.datorama.akita.angular=t.datorama.akita.angular||{},t.datorama.akita.angular.playground=t.datorama.akita.angular.playground||{},t.datorama.akita.angular.playground.projects=t.datorama.akita.angular.playground.projects||{},t.datorama.akita.angular.playground.projects["ng-entity-service"]={}),t.ng.common.http,t.rxjs,t.rxjs.operators,t.ng.core,t.datorama.akita)}(this,function(t,o,n,s,a,c){"use strict";var i=function(t,e){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])})(t,e)};function u(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var o,n,a=r.call(t),i=[];try{for(;(void 0===e||0<e--)&&!(o=a.next()).done;)i.push(o.value)}catch(s){n={error:s}}finally{try{o&&!o.done&&(r=a["return"])&&r.call(a)}finally{if(n)throw n.error}}return i}var d={GET:"GET",POST:"POST",PUT:"PUT",PATCH:"PATCH",DELETE:"DELETE"},p=(e.prototype.dispatch=function(t){this.dispatcher.next(t)},e.decorators=[{type:a.Injectable,args:[{providedIn:"root"}]}],e.ngInjectableDef=a.defineInjectable({factory:function(){return new e},token:e,providedIn:"root"}),e);function e(){this.dispatcher=new n.Subject,this.action$=this.dispatcher.asObservable()}var l=(r.prototype.dispatch=function(t){this.dispatcher.next(t)},r.prototype.loadersFor=function(r){function o(r){return s.filter(function(t){var e=t.method;return c.isFunction(r)?r(e):e===r})}function t(t){return n.loading$.pipe(a,o(t),s.map(function(t){return t.loading}))}function e(e,t){return n.loading$.pipe(a,o(t),s.filter(function(t){return t.entityId===e}),s.map(function(t){return t.loading}))}var n=this,a=s.filter(function(t){var e=t.storeName;return!r||e===r});return{get$:t(d.GET),add$:t(d.POST),update$:t(function(t){return t===d.PUT||t===d.PATCH}),delete$:t(d.DELETE),getEntity:function(t){return e(t,d.GET)},updateEntity:function(t){return e(t,function(t){return t===d.PUT||t===d.PATCH})},deleteEntity:function(t){return e(t,d.DELETE)}}},r.decorators=[{type:a.Injectable,args:[{providedIn:"root"}]}],r.ngInjectableDef=a.defineInjectable({factory:function(){return new r},token:r,providedIn:"root"}),r);function r(){this.dispatcher=new n.Subject,this.loading$=this.dispatcher.asObservable()}var f=new a.InjectionToken("NgEntityServiceGlobalConfig"),h={httpMethods:{GET:d.GET,POST:d.POST,PATCH:d.PATCH,PUT:d.PUT,DELETE:d.DELETE}};function g(t){for(var e,r,o=[],n=1;n<arguments.length;n++)o[n-1]=arguments[n];if(!o.length)return t;var a=o.shift();if(c.isObject(t)&&c.isObject(a))for(var i in a)c.isObject(a[i])?(t[i]||Object.assign(t,((e={})[i]={},e)),g(t[i],a[i])):Object.assign(t,((r={})[i]=a[i],r));return g.apply(void 0,function s(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(u(arguments[e]));return t}([t],o))}function m(t){return c.isNumber(t)||c.isString(t)}function y(n,a){return function(t){var e=t.payload,r=t.method,o=t.successMsg;a.dispatch({type:"success",storeName:n,payload:e,method:r,successMsg:o})}}function E(n,a){return function(t){var e=t.payload,r=t.method,o=t.errorMsg;a.dispatch({type:"error",storeName:n,payload:e,method:r,errorMsg:o})}}function v(e){return s.map(function(t){return(e||{}).mapResponseFn?e.mapResponseFn(t):t})}var b,N=(function j(t,e){function r(){this.constructor=t}i(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}(T,b=c.EntityService),Object.defineProperty(T.prototype,"api",{get:function(){return(this.baseUrl||this.getConfigValue("baseUrl"))+"/"+this.resourceName},enumerable:!0,configurable:!0}),Object.defineProperty(T.prototype,"resourceName",{get:function(){return this.getConfigValue("resourceName")||this.store.storeName},enumerable:!0,configurable:!0}),T.prototype.setBaseUrl=function(t){this.baseUrl=t},T.prototype.get=function(e,t){var r,o=this,n=m(e),a=(n?t:e)||{},i=this.getHttpMethod(d.GET);return r=a.url?a.url:n?this.api+"/"+e:this.api,this.loader.dispatch({method:i,loading:!0,entityId:n?e:null,storeName:this.store.storeName}),this.http[i.toLowerCase()](r,a).pipe(v(a),s.tap(function(t){n?o.store.upsert(e,t):a.append?o.store.add(t):o.store.set(t),o.dispatchSuccess({method:i,payload:t,successMsg:a.successMsg})}),s.catchError(function(t){return o.handleError(i,t,a.errorMsg)}),s.finalize(function(){o.loader.dispatch({method:i,loading:!1,storeName:o.store.storeName})}))},T.prototype.add=function(t,e){var r=this,o=this.getHttpMethod(d.POST);return this.loader.dispatch({method:o,loading:!0,storeName:this.store.storeName}),this.http[o.toLowerCase()](this.resolveUrl(e),t,e).pipe(v(e),s.tap(function(t){r.store.add(t,e),r.dispatchSuccess({method:o,payload:t,successMsg:e&&e.successMsg})}),s.catchError(function(t){return r.handleError(o,t,e&&e.errorMsg)}),s.finalize(function(){r.loader.dispatch({method:o,loading:!1,storeName:r.store.storeName})}))},T.prototype.update=function(e,t,r){var o=this,n=r&&r.method?r.method:this.getHttpMethod(d.PUT);return this.loader.dispatch({method:n,loading:!0,entityId:e,storeName:this.store.storeName}),this.http[n.toLocaleLowerCase()](this.resolveUrl(r,e),t,r).pipe(v(r),s.tap(function(t){o.store.update(e,t),o.dispatchSuccess({method:n,payload:t,successMsg:r&&r.successMsg})}),s.catchError(function(t){return o.handleError(n,t,r&&r.errorMsg)}),s.finalize(function(){o.loader.dispatch({method:n,loading:!1,entityId:e,storeName:o.store.storeName})}))},T.prototype["delete"]=function(e,r){var o=this,n=this.getHttpMethod(d.DELETE);return this.loader.dispatch({method:n,loading:!0,entityId:e,storeName:this.store.storeName}),this.http[n.toLowerCase()](this.resolveUrl(r,e),r).pipe(v(r),s.tap(function(t){o.store.remove(e),o.dispatchSuccess({method:n,payload:t,successMsg:r&&r.successMsg})}),s.catchError(function(t){return o.handleError(n,t,r&&r.errorMsg)}),s.finalize(function(){o.loader.dispatch({method:n,loading:!1,entityId:e,storeName:o.store.storeName})}))},T.prototype.getHttpMethod=function(t){return this.mergedConfig.httpMethods[t]},T.prototype.getConfigValue=function(t){return this.constructor[t]||this.mergedConfig[t]},T.prototype.resolveUrl=function(t,e){var r=(t||{}).url;return c.isDefined(e)?r||this.api+"/"+e:r||this.api},T.prototype.handleError=function(t,e,r){return this.dispatchError({method:t,errorMsg:r,payload:e}),n.throwError(e)},T);function T(t,e){void 0===e&&(e={});var r=b.call(this)||this;return r.store=t,r.config=e,r.globalConfig={},r.http=a.inject(o.HttpClient),r.loader=a.inject(l),r.notifier=a.inject(p),r.globalConfig=a.inject(f),r.mergedConfig=g(h,r.globalConfig,e),r.dispatchSuccess=y(r.store.storeName,r.notifier),r.dispatchError=E(r.store.storeName,r.notifier),r}t.mapResponse=v,t.NgEntityService=N,t.mergeDeep=g,t.NgEntityServiceConfig=function M(e){return void 0===e&&(e={}),function(t){e.baseUrl&&(t.baseUrl=e.baseUrl),e.resourceName&&(t.resourceName=e.resourceName)}},t.NG_ENTITY_SERVICE_CONFIG=f,t.defaultConfig=h,t.HttpMethod=d,t.ofType=function(e){return s.filter(function(t){return t.type===e})},t.filterMethod=function(e){return s.filter(function(t){return t.method===e})},t.filterStore=function(e){return s.filter(function(t){return t.storeName===e})},t.NgEntityServiceNotifier=p,t.isID=m,t.NgEntityServiceLoader=l,t.successAction=y,t.errorAction=E,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=datorama-akita-angular-playground-projects-ng-entity-service.umd.min.js.map