@ngx-resource/handler-cordova-advanced-http
Version:
Resource Handler based on cordova-advanced-http
16 lines (14 loc) • 5.99 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@ngx-resource/core"),require("@angular/core")):"function"==typeof define&&define.amd?define("@ngx-resource/handler-cordova-advanced-http",["exports","@ngx-resource/core","@angular/core"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self)["ngx-resource"]=e["ngx-resource"]||{},e["ngx-resource"]["handler-cordova-advanced-http"]={}),e.core,e.ng.core)}(this,(function(e,t,r){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */var o=function(e,t){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])})(e,t)};function n(e,t){function r(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}Object.create;Object.create;var s=function(e){function r(){var t=e.call(this)||this;return t.http=null,t.methodWithBody=["post","put","patch"],t.initDeferResolve=null,t.initDeferPromise=null,t.initDeferPromise=new Promise((function(e){t.initDeferResolve=e})),t.initHttp(),t}return n(r,e),r.prototype.initHttp=function(e){e?(this.http=e,this.resolveDeferInit()):this.initHttpPlugin()},r.prototype.handle=function(e){var r=this;if(this.initDeferPromise)return{promise:this.initDeferPromise.then((function(){return r.handle(e)})).then((function(e){return e.promise||{status:0}}))};if(!this.http)return this.createErrorResponse("Http is not defined");void 0===e.requestBodyType&&(e.requestBodyType=t.ResourceRequestBodyType.FORM_DATA),this.setDataSerializer(e.requestBodyType);var o=this.mapMethodName(e.method);if(null===o)return this.createErrorResponse("Request method is not supported");var n=e.url||"",s=e.body;return e.query&&this.prepareQuery(e.query),this.methodWithBody.indexOf(o)>-1?e.query&&(n=this.createUrlWithQuery(n,e.query)):s=e.query,{promise:new Promise((function(t,i){try{r.http[o](n,s,e.headers,t,i)}catch(e){console.error("Http plugin call failed with: "+e.message,e),i({status:-1,headers:[{error:e.message}]})}})).catch((function(t){return r.createResponse(t,e,!0)})).then((function(t){return r.createResponse(t,e)}))}},r.prototype.initHttpPlugin=function(){var e=this;this.tryToSetPlugin()?this.resolveDeferInit():document.addEventListener("deviceready",(function(){e.tryToSetPlugin()||console.warn("Can not set http plugin after device ready"),e.resolveDeferInit()}),!1)},r.prototype.tryToSetPlugin=function(){return!this.http&&cordova&&cordova.plugin&&cordova.plugin.http&&(this.http=cordova.plugin.http),!!this.http},r.prototype.resolveDeferInit=function(){this.initDeferResolve(),this.initDeferResolve=null,this.initDeferPromise=null},r.prototype.createResponse=function(e,t,r){var o=this;return void 0===r&&(r=!1),new Promise((function(n,s){var i={status:e.status,body:e.data||e.error,headers:e.headers};o.prepareResponseBody(i,t,r,n,s),r?s(i):n(i)}))},r.prototype.createErrorResponse=function(e){return{promise:Promise.reject(new Error(e))}},r.prototype.setDataSerializer=function(e){switch(e){case t.ResourceRequestBodyType.JSON:this.http.setDataSerializer("json");break;case t.ResourceRequestBodyType.FORM_DATA:this.http.setDataSerializer("urlencoded");break;case t.ResourceRequestBodyType.TEXT:this.http.setDataSerializer("utf8");break;default:return this.createErrorResponse("Supported only json, FormData or text types")}},r.prototype.mapMethodName=function(e){switch(e){case t.ResourceRequestMethod.Post:return"post";case t.ResourceRequestMethod.Get:return"get";case t.ResourceRequestMethod.Put:return"put";case t.ResourceRequestMethod.Patch:return"patch";case t.ResourceRequestMethod.Delete:return"delete";case t.ResourceRequestMethod.Head:return"head";default:return null}},r.prototype.prepareQuery=function(e){for(var t in e)e.hasOwnProperty(t)&&(e[t]=e[t].toString())},r.prototype.createUrlWithQuery=function(e,t){var r=Object.keys(t).map((function(e){return e+"="+t[e]})).join("&");return e+(-1===e.indexOf("?")?"?":"&")+encodeURI(r)},r.prototype.prepareResponseBody=function(e,r,o,n,s){if(void 0===o&&(o=!1),e.body)switch(r.responseBodyType){case t.ResourceResponseBodyType.Json:try{e.body=JSON.parse(e.body)}catch(e){}break;case t.ResourceResponseBodyType.Blob:e.body=new Blob([e.body],{type:"text/plain"});break;case t.ResourceResponseBodyType.ArrayBuffer:var i=new FileReader;return i.onload=function(){e.body=this.result,o?s(e):n(e)},i.onerror=function(){s({status:0,body:null,headers:{}})},void i.readAsArrayBuffer(new Blob([e.body],{type:"text/plain"}))}},r}(t.ResourceHandler),i=function(e){function r(){return null!==e&&e.apply(this,arguments)||this}return n(r,e),r.forRoot=function(e){return void 0===e&&(e={}),t.ResourceModule.forRoot({handler:e.handler||{provide:t.ResourceHandler,useClass:s}})},r.forChild=function(e){return void 0===e&&(e={}),t.ResourceModule.forChild({handler:e.handler||{provide:t.ResourceHandler,useClass:s}})},r}(t.ResourceModule);i.decorators=[{type:r.NgModule}],e.ResourceHandlerCordovaAdvancedHttp=s,e.ResourceModule=i,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=ngx-resource-handler-cordova-advanced-http.umd.min.js.map