@sd-angular/core
Version:
Sd Angular Core Lib
2 lines • 14.7 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("@angular/common/http"),require("rxjs"),require("rxjs/operators"),require("object-hash"),require("@sd-angular/core/cache"),require("uuid")):"function"==typeof define&&define.amd?define("@sd-angular/core/api",["exports","@angular/core","@angular/common/http","rxjs","rxjs/operators","object-hash","@sd-angular/core/cache","uuid"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self)["sd-angular"]=e["sd-angular"]||{},e["sd-angular"].core=e["sd-angular"].core||{},e["sd-angular"].core.api={}),e.ng.core,e.ng.common.http,e.rxjs,e.rxjs.operators,e["object-hash"],e["sd-angular"].core.cache,e.uuid)}(this,(function(e,t,r,n,o,i,s,c){"use strict";function u(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=u(i),l=new t.InjectionToken("api.configuration"),d=a.default({origin:location.origin,key:"ed4a2ffc-af43-41fb-9ac9-970f43d99571"}),h=function(){function e(e,t){var r=this;this.configuration=e,this.cacheService=t,this.set=function(e){(0,r.cacheService.create(d,{type:"local",hours:240}).set)(e)},this.clear=function(){(0,r.cacheService.create(d,{type:"local",hours:240}).remove)()},this.customHeaders=function(e){var t,n;return(null===(n=null===(t=r.configuration)||void 0===t?void 0:t.customHeaders)||void 0===n?void 0:n.call(t,e))||null}}return Object.defineProperty(e.prototype,"token",{get:function(){return(0,this.cacheService.create(d,{type:"local",hours:240}).get)()},enumerable:!1,configurable:!0}),e}();h.ɵprov=t.ɵɵdefineInjectable({factory:function(){return new h(t.ɵɵinject(l,8),t.ɵɵinject(s.SdCacheService))},token:h,providedIn:"root"}),h.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}],h.ctorParameters=function(){return[{type:void 0,decorators:[{type:t.Inject,args:[l]},{type:t.Optional}]},{type:s.SdCacheService}]};var p=function(){function e(e,t){this.configuration=e,this.tokenService=t}return e.prototype.intercept=function(e,t){var i,s,c,u,a=this;(null===(i=this.configuration)||void 0===i?void 0:i.tokenHeader)&&this.tokenService.token&&(e=e.clone({headers:e.headers.set(this.configuration.tokenHeader,this.tokenService.token)})),e.url.startsWith("http")||(e=e.clone({url:((null===(s=this.configuration)||void 0===s?void 0:s.host)||location.host)+e.url})),e.body instanceof FormData&&(e=e.clone({headers:e.headers.delete("Content-Type")}));var l=null===(u=null===(c=this.configuration)||void 0===c?void 0:c.beforeRemote)||void 0===u?void 0:u.call(c,e);return l instanceof Promise?n.from(l).pipe(o.switchMap((function(){return t.handle(e)})),o.map((function(e){var t,n;return e instanceof r.HttpResponse&&(null===(n=null===(t=a.configuration)||void 0===t?void 0:t.afterRemote)||void 0===n||n.call(t,e)),e})),o.catchError((function(e){var t,r;return null===(r=null===(t=a.configuration)||void 0===t?void 0:t.afterRemote)||void 0===r||r.call(t,e),n.throwError(e)}))):t.handle(e).pipe(o.map((function(e){var t,n;return e instanceof r.HttpResponse&&(null===(n=null===(t=a.configuration)||void 0===t?void 0:t.afterRemote)||void 0===n||n.call(t,e)),e})),o.catchError((function(e){var t,r;return null===(r=null===(t=a.configuration)||void 0===t?void 0:t.afterRemote)||void 0===r||r.call(t,e),n.throwError(e)})))},e}();p.decorators=[{type:t.Injectable}],p.ctorParameters=function(){return[{type:void 0,decorators:[{type:t.Inject,args:[l]},{type:t.Optional}]},{type:h}]};var f=function(){};f.decorators=[{type:t.NgModule,args:[{imports:[r.HttpClientModule],exports:[r.HttpClientModule],providers:[{provide:r.HTTP_INTERCEPTORS,useClass:p,multi:!0}]}]}];function v(e,t,r,n){return new(r||(r=Promise))((function(o,i){function s(e){try{u(n.next(e))}catch(e){i(e)}}function c(e){try{u(n.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,c)}u((n=n.apply(e,t||[])).next())}))}function b(e,t){var r,n,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;s;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,n=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){s=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){s.label=i[1];break}if(6===i[0]&&s.label<o[1]){s.label=o[1],o=i;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(i);break}o[2]&&s.ops.pop(),s.trys.pop();continue}i=t.call(e,s)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}}Object.create;function y(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function g(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,o,i=r.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(n=i.next()).done;)s.push(n.value)}catch(e){o={error:e}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(o)throw o.error}}return s}var m,j,w,S;Object.create;function k(e,t,r,n){if("a"===r&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?n:"a"===r?n.call(e):n?n.value:t.get(e)}var I=function(){function e(e,t,r,i){var s=this;this.httpClient=e,this.configuration=t,this.cacheService=r,this.tokenService=i,this.uploadId="U118fefb1-3085-4641-8cb8-f7fe5c28f509",m.set(this,12e4),j.set(this,2e3),w.set(this,{}),this.translate=function(e){var t;return(null===(t=s.configuration)||void 0===t?void 0:t.translate(e))||e},this.post=function(e,t,r,i){return v(s,void 0,void 0,(function(){var s,u,l,d,h=this;return b(this,(function(p){if(e=!1===(null==i?void 0:i.encodeURI)?e:encodeURI(e),r=r||this.tokenService.customHeaders({url:e}),l=c.v4(),t instanceof FormData||(l=a.default({url:e,headers:r,body:t})),d=(null==i?void 0:i.timeout)||k(this,m),(!k(this,w)[l]||Date.addMiliseconds(k(this,w)[l].createdDate,k(this,j))<new Date)&&(k(this,w)[l]={createdDate:new Date,subject:new n.Subject},(null==i?void 0:i.responseType)&&"json"!==i.responseType?this.httpClient.post(e,t,{observe:"body",headers:r,responseType:null==i?void 0:i.responseType}).pipe(o.timeout(d)).subscribe((function(e){k(h,w)[l].response={status:"success",result:e},k(h,w)[l].subject.next(e)}),(function(e){k(h,w)[l].response={status:"error",result:e},k(h,w)[l].subject.error(e)})):this.httpClient.post(e,t,{observe:"response",headers:r}).pipe(o.timeout(d)).subscribe((function(e){e.ok?(k(h,w)[l].response={status:"success",result:e.body},k(h,w)[l].subject.next(e.body)):(k(h,w)[l].response={status:"error",result:e.body},k(h,w)[l].subject.error(e.body))}),(function(e){k(h,w)[l].response={status:"error",result:e},k(h,w)[l].subject.error(e)}))),null===(s=k(this,w)[l].response)||void 0===s?void 0:s.status){if("success"===(null===(u=k(this,w)[l].response)||void 0===u?void 0:u.status))return[2,k(this,w)[l].response.result];throw k(this,w)[l].response.result}return[2,k(this,w)[l].subject.pipe(o.take(1)).toPromise()]}))}))},this.postCache=function(e,t,r){return v(s,void 0,void 0,(function(){var n,o,i,s,c;return b(this,(function(u){switch(u.label){case 0:return n=(null==r?void 0:r.headers)||this.tokenService.customHeaders({url:e}),o=this.cacheService.create({url:e,headers:n,body:t},{type:null==r?void 0:r.cacheType,hours:null==r?void 0:r.cacheHours}),i=o.get,s=o.set,(0,o.has)()?[3,2]:[4,this.post(e,t,null==r?void 0:r.headers,{timeout:5e3})];case 1:return c=u.sent(),s(c),[2,c];case 2:return[2,i()]}}))}))},this.put=function(e,t,r){return v(s,void 0,void 0,(function(){var n=this;return b(this,(function(i){switch(i.label){case 0:return r=r||this.tokenService.customHeaders({url:e}),[4,new Promise((function(i,s){n.httpClient.put(encodeURI(e),t,{observe:"response",headers:r}).pipe(o.timeout(k(n,m))).toPromise().then((function(e){e.ok?i(e.body):s(e.body)})).catch((function(e){s(e)}))}))];case 1:return[2,i.sent()]}}))}))},this.patch=function(e,t,r){return v(s,void 0,void 0,(function(){var n=this;return b(this,(function(i){switch(i.label){case 0:return r=r||this.tokenService.customHeaders({url:e}),[4,new Promise((function(i,s){n.httpClient.patch(encodeURI(e),t,{observe:"response",headers:r}).pipe(o.timeout(k(n,m))).toPromise().then((function(e){e.ok?i(e.body):s(e.body)})).catch((function(e){s(e)}))}))];case 1:return[2,i.sent()]}}))}))},S.set(this,(function(e){var t,r,n=[];try{for(var o=y(Object.entries(e)),i=o.next();!i.done;i=o.next()){var s=g(i.value,2),c=s[0],u=s[1];null!=u&&""!==u&&""!==(null==u?void 0:u.toString())&&n.push(encodeURIComponent(c)+"="+encodeURIComponent(u))}}catch(e){t={error:e}}finally{try{i&&!i.done&&(r=o.return)&&r.call(o)}finally{if(t)throw t.error}}return"?"+(null==n?void 0:n.join("&"))})),this.getWithParams=function(e,t,r){return v(s,void 0,void 0,(function(){return b(this,(function(n){switch(n.label){case 0:return[4,this.get(""+e+k(this,S).call(this,t),null==r?void 0:r.headers,Object.assign(Object.assign({},r),{encodeURI:!1}))];case 1:return[2,n.sent()]}}))}))},this.getCacheWithParams=function(e,t,r,n){return v(s,void 0,void 0,(function(){return b(this,(function(o){switch(o.label){case 0:return[4,this.getCache(""+e+k(this,S).call(this,t),Object.assign(Object.assign({},n),{headers:r,encodeURI:!1}))];case 1:return[2,o.sent()]}}))}))},this.get=function(e,t,r){return v(s,void 0,void 0,(function(){var i,s,c,u,l,d=this;return b(this,(function(h){if(t=t||this.tokenService.customHeaders({url:e}),c=!1===(null==r?void 0:r.encodeURI)?e:encodeURI(e),u=a.default({encodedUrl:c,headers:t}),l=(null==r?void 0:r.timeout)||k(this,m),(!k(this,w)[u]||Date.addMiliseconds(k(this,w)[u].createdDate,k(this,j))<new Date)&&(k(this,w)[u]={createdDate:new Date,subject:new n.Subject},(null==r?void 0:r.responseType)&&"json"!==r.responseType?this.httpClient.get(c,{observe:"body",headers:t,responseType:null==r?void 0:r.responseType}).pipe(o.timeout(l)).subscribe((function(e){k(d,w)[u].response={status:"success",result:e},k(d,w)[u].subject.next(e)}),(function(e){k(d,w)[u].response={status:"error",result:e},k(d,w)[u].subject.error(e)})):this.httpClient.get(c,{observe:"response",headers:t}).pipe(o.timeout(l)).subscribe((function(e){e.ok?(k(d,w)[u].response={status:"success",result:e.body},k(d,w)[u].subject.next(e.body)):(k(d,w)[u].response={status:"error",result:e.body},k(d,w)[u].subject.error(e.body))}),(function(e){k(d,w)[u].response={status:"error",result:e},k(d,w)[u].subject.error(e)}))),null===(i=k(this,w)[u].response)||void 0===i?void 0:i.status){if("success"===(null===(s=k(this,w)[u].response)||void 0===s?void 0:s.status))return[2,k(this,w)[u].response.result];throw k(this,w)[u].response.result}return[2,k(this,w)[u].subject.pipe(o.take(1)).toPromise()]}))}))},this.getCache=function(e,t){return v(s,void 0,void 0,(function(){var r,n,o,i,s,c;return b(this,(function(u){switch(u.label){case 0:return r=(null==t?void 0:t.headers)||this.tokenService.customHeaders({url:e}),n=!1===(null==t?void 0:t.encodeURI)?e:encodeURI(e),o=this.cacheService.create({encodedUrl:n,headers:r},{type:null==t?void 0:t.cacheType,hours:null==t?void 0:t.cacheHours}),i=o.get,s=o.set,(0,o.has)()&&!(null==t?void 0:t.reload)?[3,2]:[4,this.get(n,null==t?void 0:t.headers,{encodeURI:!1,timeout:5e3})];case 1:return c=u.sent(),s(c),[2,c];case 2:return[2,i()]}}))}))},this.delete=function(e,t){return v(s,void 0,void 0,(function(){var r=this;return b(this,(function(n){switch(n.label){case 0:return t=t||this.tokenService.customHeaders({url:e}),[4,new Promise((function(n,i){r.httpClient.delete(encodeURI(e),{observe:"response",headers:t}).pipe(o.timeout(k(r,m))).toPromise().then((function(e){e.ok?n(e.body):i(e.body)})).catch((function(e){i(e)}))}))];case 1:return[2,n.sent()]}}))}))},this.uploadSingle=function(e,t){return SdUtility.upload(t).then((function(t){return s.upload(e,t)}))},this.upload=function(e,t){return v(s,void 0,void 0,(function(){var r,n;return b(this,(function(o){switch(o.label){case 0:if(-1===t.name.lastIndexOf("."))throw r=this.translate("Invalid file extension"),new Error(r);return(n=new FormData).append("file",t,t.name),[4,this.post(e,n)];case 1:return[2,o.sent()]}}))}))}}return Object.defineProperty(e.prototype,"http",{get:function(){return this.httpClient},enumerable:!1,configurable:!0}),e}();m=new WeakMap,j=new WeakMap,w=new WeakMap,S=new WeakMap,I.ɵprov=t.ɵɵdefineInjectable({factory:function(){return new I(t.ɵɵinject(r.HttpClient),t.ɵɵinject(l,8),t.ɵɵinject(s.SdCacheService),t.ɵɵinject(h))},token:I,providedIn:"root"}),I.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}],I.ctorParameters=function(){return[{type:r.HttpClient},{type:void 0,decorators:[{type:t.Inject,args:[l]},{type:t.Optional}]},{type:s.SdCacheService},{type:h}]};var C=function(e,t){var r=this;this.httpClient=e,this.tokenService=t,this.get=function(e,t){return t=Object.assign(Object.assign({},t),{headers:t.headers||r.tokenService.customHeaders({url:e})}),r.httpClient.get(e,t)},this.post=function(e,t,n){return n=Object.assign(Object.assign({},n),{headers:n.headers||r.tokenService.customHeaders({url:e})}),r.httpClient.post(e,t,n)},this.put=function(e,t,n){return n=Object.assign(Object.assign({},n),{headers:n.headers||r.tokenService.customHeaders({url:e})}),r.httpClient.put(e,t,n)},this.delete=function(e,t){return t=Object.assign(Object.assign({},t),{headers:t.headers||r.tokenService.customHeaders({url:e})}),r.httpClient.delete(e,t)},this.request=function(e,t,n){return n=Object.assign(Object.assign({},n),{headers:n.headers||r.tokenService.customHeaders({url:t})}),r.httpClient.request(e,t,n)}};C.ɵprov=t.ɵɵdefineInjectable({factory:function(){return new C(t.ɵɵinject(r.HttpClient),t.ɵɵinject(h))},token:C,providedIn:"root"}),C.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}],C.ctorParameters=function(){return[{type:r.HttpClient},{type:h}]},e.API_CONFIG=l,e.SdApiModule=f,e.SdApiService=I,e.SdHttpInterceptor=p,e.SdHttpService=C,e.SdTokenService=h,e.TOKEN=d,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=sd-angular-core-api.umd.min.js.map