jade-integration-utils
Version:
A tool made for any type of http requisitions and to manage localstorage. This package works into Angular and Ionic projects (maybe you can run this in ReactNative, MAYBE!).
16 lines (14 loc) • 16.1 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/common/http"),require("@angular/router"),require("@angular/common"),require("@angular/platform-browser")):"function"==typeof define&&define.amd?define("jade-integration-utils",["exports","@angular/core","@angular/common/http","@angular/router","@angular/common","@angular/platform-browser"],e):e((t=t||self)["jade-integration-utils"]={},t.ng.core,t.ng.common.http,t.ng.router,t.ng.common,t.ng.platformBrowser)}(this,(function(t,e,n,o,r,i){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */function s(t,e,n,o){var r,i=arguments.length,s=i<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,n):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,n,o);else for(var u=t.length-1;u>=0;u--)(r=t[u])&&(s=(i<3?r(s):i>3?r(e,n,s):r(e,n))||s);return i>3&&s&&Object.defineProperty(e,n,s),s}var u=function(){function t(){}return t.get=function(t){return this.emitters[t]||(this.emitters[t]=new e.EventEmitter),this.emitters[t]},t.emitters={},t.ɵprov=e["ɵɵdefineInjectable"]({factory:function(){return new t},token:t,providedIn:"root"}),t=s([e.Injectable({providedIn:"root"})],t)}(),p=function(){function t(){}return t.get=function(t){var e=localStorage.getItem(t);return e?JSON.parse(e):null},t.set=function(t,e){localStorage.setItem(t,JSON.stringify(e))},t.getSession=function(t){var e=sessionStorage.getItem(t);return e?JSON.parse(e):null},t.setSession=function(t,e){sessionStorage.setItem(t,JSON.stringify(e))},t.getTemp=function(){var t=sessionStorage.getItem("temp");return t?JSON.parse(t):null},t.setTemp=function(t){sessionStorage.setItem("temp",JSON.stringify(t))},t.clearTemp=function(){sessionStorage.removeItem("temp")},t.clear=function(){localStorage.clear(),sessionStorage.clear()},t.APIMaps=function(){return"AIzaSyBiQp1xNr0h8WLV_6OlbMu2E83ezjShoAk"},t.ɵprov=e["ɵɵdefineInjectable"]({factory:function(){return new t},token:t,providedIn:"root"}),t=s([e.Injectable({providedIn:"root"})],t)}(),h=function(){function t(t){this.http=t,this._url="",this._AUTH="auth",this._options={headers:{"Content-Type":"application/json","Access-Control-Allow-Origin":"*"},responseType:"json"}}return t.prototype.configure=function(t){this._url=t},t.prototype.set_token=function(t){p.set(this._AUTH,"Bearer "+t),this._options.headers.Authorization=this.get_token()?p.get(this._AUTH):null},t.prototype.get_token=function(){return p.get(this._AUTH)},t.prototype.get=function(t){return this.http.get(this._url+t,this._options).toPromise()},t.prototype.get_file=function(t){return this.options.responseType="blob",this.http.get(this._url+t,this._options).toPromise()},t.prototype.post=function(t,e){return this.http.post(this._url+t,e,this._options).toPromise()},t.prototype.formData=function(t,e){return delete this._options.headers["Content-Type"],this.http.post(this._url+t,e,this._options).toPromise()},t.prototype.put=function(t,e){return this.http.put(this._url+t,e,this._options).toPromise()},t.prototype.delete=function(t){return this.http.delete(this._url+t,this._options).toPromise()},Object.defineProperty(t.prototype,"options",{get:function(){return this._options},set:function(t){this._options=t},enumerable:!0,configurable:!0}),t.prototype.default_options=function(){this._options={headers:{"Content-Type":"application/json","Access-Control-Allow-Origin":"*"},responseType:"json"}},t.ctorParameters=function(){return[{type:n.HttpClient}]},t.ɵprov=e["ɵɵdefineInjectable"]({factory:function(){return new t(e["ɵɵinject"](n.HttpClient))},token:t,providedIn:"root"}),t=s([e.Injectable({providedIn:"root"})],t)}(),a=function(){function t(t,e){this.http=t,this.router=e,this._auth="Authentication"}return t.prototype.configureHttp=function(t){this.http.configure(t)},t.prototype.get=function(t,e){var n="";return e?(console.log(e),n="/"+t+"?"+e):n="/"+t,this.default_options(),this.auth=this.auth,this.http.get(n)},t.prototype.get_file=function(t,e){var n="";return e?(console.log(e),n="/"+t+"?"+e):n="/"+t,this.http.get_file(n)},t.prototype.get_any=function(t,e){var n="";return e?(console.log(e),n="/"+t+"?"+e):n="/"+t,this.default_options(),this.auth=this.auth,this.http.get(n)},t.prototype.getById=function(t,e){return this.default_options(),this.auth=this.auth,this.http.get("/"+e+"/"+t)},t.prototype.post=function(t,e){return this.default_options(),this.auth=this.auth,this.http.post("/"+e,t)},t.prototype.formData=function(t,e){return this.http.formData("/"+e,t)},t.prototype.put=function(t,e){return this.http.put("/"+e,t)},t.prototype.delete=function(t,e){return this.http.delete("/"+e+"/"+t)},t.prototype.set_token=function(t){this.http.set_token(t)},t.prototype.get_token=function(){return this.http.get_token()},Object.defineProperty(t.prototype,"options",{get:function(){return this.http.options},set:function(t){this.http.options=t},enumerable:!0,configurable:!0}),t.prototype.default_options=function(){this.http.default_options()},Object.defineProperty(t.prototype,"auth",{get:function(){return p.get(this._auth)},set:function(t){p.set(this._auth,t),this.set_token(t)},enumerable:!0,configurable:!0}),t.prototype.downloadFile=function(t){var e=new Blob([t]),n=window.URL.createObjectURL(e);window.open(n)},t.ctorParameters=function(){return[{type:h},{type:o.Router}]},t.ɵprov=e["ɵɵdefineInjectable"]({factory:function(){return new t(e["ɵɵinject"](h),e["ɵɵinject"](o.Router))},token:t,providedIn:"root"}),t=s([e.Injectable({providedIn:"root"})],t)}(),c=function(){function t(){}return t.GET="get",t.POST="post",t.PUT="put",t.DELETE="delete",t.OPTIONS="options",t.PATCH="patch",t}(),l=function(){function t(t,e){this._AUTH="auth",this._standard_url=t,this._xhttp=new XMLHttpRequest,e?this._headers=e:(this._headers=new Array,this._headers.push({header:"Content-Type",value:"application/json"}),this._headers.push({header:"Authorization",value:p.get(this._AUTH)}),this._headers.push({header:"Accept",value:"*/*"}),this._headers.push({header:"Access-Control-Allow-Credentials",value:"true"}),this._headers.push({header:"Access-Control-Allow-Origin",value:"http://localhost:4200"}),this._headers.push({header:"Access-Control-Allow-Methods",value:"GET, POST, PATCH, DELETE, PUT, OPTIONS"}),this._headers.push({header:"Access-Control-Allow-Headers",value:"Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"}))}return t.prototype.set_token=function(t){p.set(this._AUTH,"Bearer "+t)},t.prototype.get_token=function(){return p.get(this._AUTH)},t.prototype.get=function(t){return this._xhttp.open(c.GET,this._standard_url+t),this._send_request()},t.prototype.get_file=function(t){return this._xhttp.open(c.GET,this._standard_url+t),this._send_request()},t.prototype.post=function(t,e){return this._xhttp.open(c.POST,this._standard_url+t),this._send_request(e)},t.prototype.formData=function(t,e){return this._xhttp.open(c.POST,this._standard_url+t),this._send_request(e)},t.prototype.put=function(t,e){return this._xhttp.open(c.PUT,this._standard_url+t),this._send_request(e)},t.prototype.delete=function(t){return this._xhttp.open(c.DELETE,this._standard_url+t),this._send_request()},t.prototype._send_request=function(t,e){var n=this;void 0===t&&(t=null),void 0===e&&(e=6e4);var o=this.get_token();this._xhttp.setRequestHeader("Content-Type","application/json");for(var r=0;r<this._headers.length;r++){var i=this._headers[r];"Authorization"!=i.header&&this._xhttp.setRequestHeader(i.header,i.value),"Authorization"==i.header&&null!=o&&this._xhttp.setRequestHeader(i.header,o)}return this._xhttp.withCredentials=!!o,this._xhttp.responseType="json",new Promise((function(o,r){n._xhttp.onreadystatechange=function(){4==this.readyState&&o(this.response)},n._xhttp.send(JSON.stringify(t)),e&&setTimeout((function(){r("timeout"),this._xhttp?this._xhttp.abort():this._xhttp=new XMLHttpRequest}),e)}))},t}(),f=function(){function t(t,e){this._auth="Authentication",this.http_xhr=new l(t),this._endpoint=e}return t.prototype.get=function(t,e){var n="";return n=e?"/"+t+"?"+e:"/"+t,this.http_xhr.get(n)},t.prototype.get_file=function(t,e){var n="";return n=e?"/"+t+"?"+e:"/"+t,this.http_xhr.get_file(n)},t.prototype.get_any=function(t,e){var n="";return n=e?"/"+t+"?"+e:"/"+t,this.http_xhr.get(n)},t.prototype.getById=function(t,e){return this.http_xhr.get("/"+e+"/"+t)},t.prototype.post=function(t,e){return this.http_xhr.post("/"+e,t)},t.prototype.formData=function(t,e){return this.http_xhr.formData("/"+e,t)},t.prototype.put=function(t,e){return this.http_xhr.put("/"+e,t)},t.prototype.delete=function(t,e){return this.http_xhr.delete("/"+e+"/"+t)},t.prototype.set_token=function(t){this.http_xhr.set_token(t)},t.prototype.get_token=function(){return this.http_xhr.get_token()},Object.defineProperty(t.prototype,"auth",{get:function(){return p.get(this._auth)},set:function(t){p.set(this._auth,t),this.set_token(t)},enumerable:!0,configurable:!0}),t.prototype.downloadFile=function(t){var e=new Blob([t]),n=window.URL.createObjectURL(e);window.open(n)},t}(),d=function(){function t(t,e){var n=this,o=p.getSession("user");setTimeout((function(){null!==o&&-1==n.roles.indexOf(o.role.sigla)&&e.setStyle(t.nativeElement,"display","none")}),100)}return t.ctorParameters=function(){return[{type:e.ElementRef},{type:e.Renderer2}]},s([e.Input("checkRole")],t.prototype,"roles",void 0),t=s([e.Directive({selector:"[jiuCheckRole]"})],t)}();var _=function(){function t(t,e){this._endpoint="",this.xMan=new f(t,e),this.element_list=new Array,this.loading=!1,this._endpoint=e}return t.prototype.remove_e_query=function(t){return t=t.startsWith("&")?t.replace(t.charAt(0),""):t},t.prototype.getById=function(t,e){var n=this;this.loading=!0,this.xMan.getById(t,this._endpoint).then((function(t){n.bind_results(t),e&&e(t)})).catch((function(t){return n.showError(t)})).finally((function(){return n.loading=!1}))},t.prototype.bind_results=function(t){var e,n,o;this.page=null!==(e=null==t?void 0:t.page)&&void 0!==e?e:this.page,this.fetch=null!==(n=null==t?void 0:t.fetch)&&void 0!==n?n:this.fetch,this.itemsCount=null!==(o=null==t?void 0:t.itemsCount)&&void 0!==o?o:this.itemsCount,null!=t.objects&&(this.element_list=t.objects),this.results=t,this.get_pages_array()},t.prototype.get=function(t,e,n){var o=this;void 0===n&&(n=!1),this.loading=!0;var r=this.serialize_query(t)+(n?"&"+this.get_pagination_query():"");console.log(r),this.xMan.get(this._endpoint,r).then((function(t){o.bind_results(t),e&&e(t)})).catch((function(t){return o.showError(t)})).finally((function(){return o.loading=!1}))},t.prototype.getAll=function(t,e){var n=this;void 0===e&&(e=!1),this.loading=!0;var o=e?this.get_pagination_query():"";console.log(e),this.xMan.get(this._endpoint,o).then((function(e){n.bind_results(e),t&&t(e)})).catch((function(t){return n.showError(t)})).finally((function(){return n.loading=!1}))},t.prototype.next=function(t){this.page+=1,this.get(t,!0)},t.prototype.previous=function(t){this.page>1&&(this.page-=1),this.get(t,!0)},t.prototype.get_by_endpoint=function(t,e,n,o){var r=this;void 0===o&&(o=!1),this.loading=!0,console.log(o),this.xMan.get(t+(o?this.get_pagination_query():"")).then((function(t){r.bind_results(t),e&&e(t)})).catch((function(t){return r.showError(t)})).finally((function(){return r.loading=!1}))},t.prototype.post=function(t,e){var n=this;this.loading=!0,this.xMan.post(this.remove_resource(t),this._endpoint).then((function(t){n.results=t,e&&e(t)})).catch((function(t){return n.showError(t)})).finally((function(){return n.loading=!1}))},t.prototype.create=function(t,e){var n=this;this.loading=!0,this.xMan.post(this.remove_resource(t),this._endpoint).then((function(t){n.results=t,e&&e(t)})).catch((function(t){return n.showError(t)})).finally((function(){return n.loading=!1}))},t.prototype.update=function(t,e){var n=this;this.loading=!0,this.xMan.put(this.remove_resource(t),this._endpoint).then((function(t){n.results=t,e&&e(t)})).catch((function(t){return n.showError(t)})).finally((function(){return n.loading=!1}))},t.prototype.put=function(t,e){var n=this;this.loading=!0,this.xMan.put(t,this._endpoint).then((function(t){n.results=t,e&&e(t)})).catch((function(t){return n.showError(t)})).finally((function(){return n.loading=!1}))},t.prototype.delete=function(t,e){var n=this;this.loading=!0,this.xMan.delete(t,this._endpoint).then((function(t){t.target?e&&e(t):alert("Ocorreu um erro!")})).catch((function(t){return n.showError(t)})).finally((function(){return n.loading=!1}))},t.prototype.set_auth=function(t){this.xMan.set_token(t)},t.prototype.get_auth=function(){return this.xMan.get_token()},t.prototype.get_pages_array=function(){this.page_array=new Array;for(var t=this.itemsCount/this.fetch,e=1;e<=t;e++)this.page_array.push(e)},t.prototype.get_pagination_query=function(){return"fetch="+this.fetch+"&page="+this.page},t.prototype.show_success=function(t){alert(t||"Saved with success")},t.prototype.showError=function(t){console.log("***API ERROR REPORT***"),console.log(t),console.log("--------------------")},t.prototype.logout=function(){p.clear(),this.xMan.auth="",window.location.reload()},t.prototype.serialize_query=function(t){var e=[];for(var n in t)if("object"==typeof t[n]){for(var o in t[n])if("resource"!=n&&Object.prototype.hasOwnProperty.call(t[n],o)){t[n][o];this.serialize_query(t[n])&&e.push(encodeURIComponent(n)+"."+this.serialize_query(t[n]))}}else t.hasOwnProperty(n)&&encodeURIComponent(t[n])&&e.push(encodeURIComponent(n)+"="+encodeURIComponent(t[n]));return e.join("&")},t.prototype.auto_bind=function(t,e){for(var n in t)e.hasOwnProperty(n)&&t.hasOwnProperty(n)&&(e[n]=t[n])},t.prototype.remove_resource=function(t){for(var e in this.target=JSON.parse(JSON.stringify(t)),this.target)this.target.hasOwnProperty(e)&&("resource"!=e&&"endpoint"!=e||delete this.target[e]);return this.target},t.normalizeString=function(t){return t=(t=(t=(t=(t=(t=(t=(t=t.toLowerCase()).replace(new RegExp("[ÁÀÂÃ]","gi"),"a")).replace(new RegExp("[ÉÈÊ]","gi"),"e")).replace(new RegExp("[ÍÌÎ]","gi"),"i")).replace(new RegExp("[ÓÒÔÕ]","gi"),"o")).replace(new RegExp("[ÚÙÛ]","gi"),"u")).replace(new RegExp("[Ç]","gi"),"c")).replace(/[^a-z0-9 -]/g,"")},t.user_session="session-user",t}(),g=function(){function t(){}return t.prototype.ngOnInit=function(){},t=s([e.Component({selector:"JadeIntegrationUtilsComponent",template:""})],t)}(),y=function(){function o(e){this._injector=e,t.InjectorInstance=this._injector}return o.ctorParameters=function(){return[{type:e.Injector}]},o=s([e.NgModule({declarations:[g,d],imports:[r.CommonModule,i.BrowserModule,n.HttpClientModule],exports:[n.HttpClientModule],bootstrap:[g]})],o)}();t.CheckRoleDirective=d,t.DataService=_,t.EventEmitterService=u,t.GenericService=a,t.HttpStatusService=h,t.HttpXHRService=l,t.JadeIntegrationUtilsModule=y,t.StorageService=p,t.XHRManager=f,t.loading=function(t){return function(e,n,o){var r=o.value;return o.value=function(){for(var e=this,n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];this[t]=!0;var i=r.apply(this,n);return i?(i.next=function(n){return e[t]=!1,i._next(n)},i.error=function(n){return e[t]=!1,i._error(n)}):this[t]=!1,i},o}},t.ɵa=g,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=jade-integration-utils.umd.min.js.map