ngx-endpoints
Version:
A library to dynamically load data from http endpoints / urls in angular
2 lines • 7.76 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/common/http"),require("rxjs/operators"),require("rxjs"),require("query-string"),require("moment-relativism"),require("moment")):"function"==typeof define&&define.amd?define("ngx-endpoints",["exports","@angular/core","@angular/common/http","rxjs/operators","rxjs","query-string","moment-relativism","moment"],e):e(t["ngx-endpoints"]={},t.ng.core,t.ng.common.http,t.rxjs.operators,t.rxjs,t.queryString,t.momentRelativism,t.moment_)}(this,function(t,e,o,s,a,u,c,n){"use strict";var d=function(t,e){return(d=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)};function p(e,s,a,u){return new(a||(a=Promise))(function(t,n){function r(t){try{o(u.next(t))}catch(e){n(e)}}function i(t){try{o(u["throw"](t))}catch(e){n(e)}}function o(e){e.done?t(e.value):new a(function(t){t(e.value)}).then(r,i)}o((u=u.apply(e,s||[])).next())})}function h(r,i){var o,s,a,t,u={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return t={next:e(0),"throw":e(1),"return":e(2)},"function"==typeof Symbol&&(t[Symbol.iterator]=function(){return this}),t;function e(e){return function(t){return function n(t){if(o)throw new TypeError("Generator is already executing.");for(;u;)try{if(o=1,s&&(a=2&t[0]?s["return"]:t[0]?s["throw"]||((a=s["return"])&&a.call(s),0):s.next)&&!(a=a.call(s,t[1])).done)return a;switch(s=0,a&&(t=[2&t[0],a.value]),t[0]){case 0:case 1:a=t;break;case 4:return u.label++,{value:t[1],done:!1};case 5:u.label++,s=t[1],t=[0];continue;case 7:t=u.ops.pop(),u.trys.pop();continue;default:if(!(a=0<(a=u.trys).length&&a[a.length-1])&&(6===t[0]||2===t[0])){u=0;continue}if(3===t[0]&&(!a||t[1]>a[0]&&t[1]<a[3])){u.label=t[1];break}if(6===t[0]&&u.label<a[1]){u.label=a[1],a=t;break}if(a&&u.label<a[2]){u.label=a[2],u.ops.push(t);break}a[2]&&u.ops.pop(),u.trys.pop();continue}t=i.call(r,u)}catch(e){t=[6,e],s=0}finally{o=a=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}([e,t])}}}var l=n,f=function(){function t(t){this._http=t}return t.prototype.ngOnInit=function(){},t.prototype.getData=function(r,i){return p(this,void 0,void 0,function(){var e,n;return h(this,function(t){switch(t.label){case 0:for(n in e=new o.HttpHeaders,i)e=e.append(n,i[n]);return[4,this._http.get(r,{headers:e}).pipe(s.catchError(function(t){return console.log("Cannot request data from "+r,t),a.throwError(t)})).toPromise()];case 1:return[2,t.sent()]}})})},t.prototype.convertDatesInURL=function(t,e){var n=u.parseUrl(t);for(var r in n.query)if(n.query[r].startsWith("now")){var i=c.relativism(n.query[r]);n.query[r]=l(i).format(e)}var o=u.stringify(n.query);return t=n.url+"?"+o},t.decorators=[{type:e.Injectable,args:[{providedIn:"root"}]}],t.ctorParameters=function(){return[{type:o.HttpClient}]},t.ngInjectableDef=e.defineInjectable({factory:function(){return new t(e.inject(o.HttpClient))},token:t,providedIn:"root"}),t}(),v=function b(){this.active=!0,this.requestOptions=new Object,this.live=!1,this.liveinterval=1e4,this.convertDates=!1,this.convertDatesOutputFormat=""},r=function(){function t(t,e){this.endpointservice=t,this._endpoint=e,this.data=new a.BehaviorSubject(null),this.requestPending=new a.BehaviorSubject(!1),this.isAlive=new a.BehaviorSubject(!1),this.events=new a.BehaviorSubject(""),this.running=!0,this.endpoint=e,Object.assign(this.data,new Array)}return t.prototype.requestData=function(){return p(this,void 0,void 0,function(){return h(this,function(t){switch(t.label){case 0:return[4,this.requestInternal()];case 1:return t.sent(),[2]}})})},t.prototype.requestInternal=function(){return p(this,void 0,void 0,function(){return h(this,function(t){switch(t.label){case 0:return this.requestPending.getValue()?[3,2]:(this.addEvent("requesting data from endpoint",this.endpoint),this.requestPending.next(!0),this.convertDates(this.endpoint.convertDates),[4,this.request()]);case 1:return t.sent(),this.requestPending.next(!1),[3,3];case 2:this.addEvent("a request is pending",this.endpoint),t.label=3;case 3:return[2]}})})},t.prototype.convertDates=function(t){t&&(this.endpoint.endPointUrl=this.endpointservice.convertDatesInURL(this.endpoint.endPointUrl,this.endpoint.convertDatesOutputFormat),this.addEvent("converted endpointurl",this.endpoint))},t.prototype.request=function(){return p(this,void 0,void 0,function(){var e,n;return h(this,function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,this.endpointservice.getData(this.endpoint.endPointUrl,this.endpoint.requestOptions)];case 1:return e=t.sent(),this.isAlive.getValue()||null==e||(this.isAlive.next(!0),this.addEvent("endpoint is alive",this.endpoint)),this.data.next(e),this.addEvent("updated data",this.endpoint),[3,3];case 2:return n=t.sent(),this.addEvent(n,this.endpoint),[3,3];case 3:return[2]}})})},t.prototype.addEvent=function(t,e){this.events.next(JSON.stringify({event:t,endpoint:e}))},t.decorators=[{type:e.Injectable,args:[{providedIn:"root"}]}],t.ctorParameters=function(){return[{type:f},{type:v}]},t.ngInjectableDef=e.defineInjectable({factory:function(){return new t(e.inject(f),e.inject(v))},token:t,providedIn:"root"}),t}(),i=function(r){function t(t,e){var n=r.call(this,t,e)||this;return n.endpointservice=t,n.endpoint=e,n}return function i(t,e){function n(){this.constructor=t}d(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}(t,r),t.prototype.requestData=function(){return p(this,void 0,void 0,function(){return h(this,function(t){switch(t.label){case 0:return this.running=!0,[4,this.refreshData()];case 1:return t.sent(),[2]}})})},t.prototype.sleep=function(e){return new Promise(function(t){return setTimeout(t,e)})},t.prototype.refreshData=function(){return p(this,void 0,void 0,function(){return h(this,function(t){switch(t.label){case 0:return this.running?[4,this.requestInternal()]:[3,3];case 1:return t.sent(),[4,this.sleep(this.endpoint.liveinterval)];case 2:return t.sent(),[3,0];case 3:return[2]}})})},t.decorators=[{type:e.Injectable,args:[{providedIn:"root"}]}],t.ctorParameters=function(){return[{type:f},{type:v}]},t.ngInjectableDef=e.defineInjectable({factory:function(){return new t(e.inject(f),e.inject(v))},token:t,providedIn:"root"}),t}(r),y=function(){function t(t){this.endpointservice=t,this.endpoints=new Array}return t.prototype.addEndPoint=function(e){if(!e.endPointId)throw console.log("No endPointId provided. Please attach an endpointId"),new Error("No endPointId provided. Please attach an endpointId");if(this.endpoints.find(function(t){return t.endpoint.endPointId===e.endPointId}))throw new Error("An endpoint with id "+e.endPointId+" has been already added. Please choose another id.");var t;return t=e.live?new i(this.endpointservice,e):new r(this.endpointservice,e),this.endpoints.push(t),t},t.prototype.destroyEndPoint=function(e){if(this.endpoints&&0<this.endpoints.length){var t=this.endpoints.findIndex(function(t){return t.endpoint.endPointId===e});this.endpoints[t].running=!1,this.endpoints.splice(t,1)}},t.decorators=[{type:e.Injectable,args:[{providedIn:"root"}]}],t.ctorParameters=function(){return[{type:f}]},t.ngInjectableDef=e.defineInjectable({factory:function(){return new t(e.inject(f))},token:t,providedIn:"root"}),t}(),g=function(){function t(){}return t.decorators=[{type:e.NgModule,args:[{declarations:[],imports:[o.HttpClientModule],exports:[o.HttpClientModule],providers:[f,y,r,i]}]}],t}();t.NgxEndpointsModule=g,t.NgxEndPointDataProviderService=y,t.NgxLiveEndPoint=i,t.NgxEndPoint=r,t.NgxEndpointService=f,t.NgxEndPointData=v,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=ngx-endpoints.umd.min.js.map