UNPKG

13 kBJavaScriptView Raw
1"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=require("tslib"),r=require("js-magic"),n=(t=require("axios"))&&"object"==typeof t&&"default"in t?t.default:t,o=require("@magna_shogun/catch-decorator");function i(t,e,r,n,o,i,a){try{var s=t[i](a),u=s.value}catch(t){return void r(t)}s.done?e(u):Promise.resolve(u).then(n,o)}function a(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var a=t.apply(e,r);function s(t){i(a,n,o,s,u,"next",t)}function u(t){i(a,n,o,s,u,"throw",t)}s(void 0)}))}}function s(){return(s=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function u(t,e){if(null==t)return{};var r,n,o={},i=Object.keys(t);for(n=0;n<i.length;n++)e.indexOf(r=i[n])>=0||(o[r]=t[r]);return o}var c,l=(function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function s(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{s({},"")}catch(t){s=function(t,e,r){return t[e]=r}}function u(t,e,r,n){var o=Object.create((e&&e.prototype instanceof h?e:h).prototype),i=new q(n||[]);return o._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var s=R(a,r);if(s){if(s===l)continue;return s}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=c(t,e,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}(t,r,i),o}function c(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=u;var l={};function h(){}function p(){}function f(){}var d={};d[o]=function(){return this};var v=Object.getPrototypeOf,g=v&&v(v(E([])));g&&g!==e&&r.call(g,o)&&(d=g);var y=f.prototype=h.prototype=Object.create(d);function m(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function w(t,e){var n;this._invoke=function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,s){var u=c(t[o],t,i);if("throw"!==u.type){var l=u.arg,h=l.value;return h&&"object"==typeof h&&r.call(h,"__await")?e.resolve(h.__await).then((function(t){n("next",t,a,s)}),(function(t){n("throw",t,a,s)})):e.resolve(h).then((function(t){l.value=t,a(l)}),(function(t){return n("throw",t,a,s)}))}s(u.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function R(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,R(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=c(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,l;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function x(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function b(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function q(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(x,this),this.reset(!0)}function E(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return i.next=i}}return{next:L}}function L(){return{value:void 0,done:!0}}return p.prototype=y.constructor=f,f.constructor=p,p.displayName=s(f,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===p||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,f):(t.__proto__=f,s(t,a,"GeneratorFunction")),t.prototype=Object.create(y),t},t.awrap=function(t){return{__await:t}},m(w.prototype),w.prototype[i]=function(){return this},t.AsyncIterator=w,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new w(u(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},m(y),s(y,a,"Generator"),y[o]=function(){return this},y.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=E,q.prototype={constructor:q,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(b),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var s=r.call(i,"catchLoc"),u=r.call(i,"finallyLoc");if(s&&u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),l},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),b(r),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;b(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:E(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),l}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}}(c={exports:{}}),c.exports),h=function(){function t(t,e,r){this.noThrowStrategies=["reject","silent"],this.logger=t,this.httpRequestErrorService=e,this.strategy=r}return t.prototype.process=function(t){this.logger&&this.logger.warn&&this.logger.warn("API ERROR",t);var e=t;if("string"==typeof t&&(e=new Error(t)),this.httpRequestErrorService&&(void 0!==this.httpRequestErrorService.process?this.httpRequestErrorService.process(e):"function"==typeof this.httpRequestErrorService&&this.httpRequestErrorService(e)),!this.noThrowStrategies.includes(this.strategy))throw e},t}();exports.HttpRequestHandler=function(){function t(t){var e=t.baseURL,r=void 0===e?"":e,o=t.timeout,i=void 0===o?null:o,a=t.strategy,c=void 0===a?null:a,l=t.flattenResponse,h=void 0===l?null:l,p=t.logger,f=void 0===p?null:p,d=t.httpRequestErrorService,v=void 0===d?null:d,g=u(t,["baseURL","timeout","strategy","flattenResponse","logger","httpRequestErrorService"]);this.timeout=3e4,this.strategy="silent",this.flattenResponse=!0,this.timeout=null!==i?i:this.timeout,this.strategy=null!==c?c:this.strategy,this.flattenResponse=null!==h?h:this.flattenResponse,this.logger=f||global.console||window.console||null,this.httpRequestErrorService=v,this.requestInstance=n.create(s({},g,{baseURL:r,timeout:this.timeout}))}var e=t.prototype;return e.getInstance=function(){return this.requestInstance},e.interceptRequest=function(t){this.getInstance().interceptors.request.use(t)},e.post=function(t,e,r){return void 0===e&&(e=null),void 0===r&&(r=null),this.handleRequest({type:"post",url:t,data:e,config:r})},e.get=function(t,e,r){return void 0===e&&(e=null),void 0===r&&(r=null),this.handleRequest({type:"get",url:t,data:e,config:r})},e.put=function(t,e,r){return void 0===e&&(e=null),void 0===r&&(r=null),this.handleRequest({type:"put",url:t,data:e,config:r})},e.delete=function(t,e,r){return void 0===e&&(e=null),void 0===r&&(r=null),this.handleRequest({type:"delete",url:t,data:e,config:r})},e.patch=function(t,e,r){return void 0===e&&(e=null),void 0===r&&(r=null),this.handleRequest({type:"patch",url:t,data:e,config:r})},e.head=function(t,e,r){return void 0===e&&(e=null),void 0===r&&(r=null),this.handleRequest({type:"head",url:t,data:e,config:r})},e.processRequestError=function(t){return new h(this.logger,this.httpRequestErrorService,this.strategy).process(t)},e.handleRequest=function(){var t=a(l.mark((function t(e){var r,n,o,i,a,u;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:a=null,u=s({},u=(void 0===(i=e.config)?null:i)||{},((r={url:e.url,method:n=e.type})["get"===n||"head"===n?"params":"data"]=(void 0===(o=e.data)?null:o)||{},r)),t.t0=this.strategy,t.next="silent"===t.t0?8:"reject"===t.t0?22:33;break;case 8:return t.prev=8,t.next=11,this.requestInstance.request(u);case 11:a=t.sent,t.next=21;break;case 14:return t.prev=14,t.t1=t.catch(8),this.processRequestError(t.t1),t.next=19,new Promise((function(){return null}));case 19:return t.abrupt("return",a=t.sent);case 21:return t.abrupt("break",36);case 22:return t.prev=22,t.next=25,this.requestInstance.request(u);case 25:a=t.sent,t.next=32;break;case 28:return t.prev=28,t.t2=t.catch(22),this.processRequestError(t.t2),t.abrupt("return",Promise.reject(t.t2));case 32:return t.abrupt("break",36);case 33:return t.next=35,this.requestInstance.request(u);case 35:a=t.sent;case 36:return t.abrupt("return",this.processResponseData(a));case 37:case"end":return t.stop()}}),t,this,[[8,14],[22,28]])})));return function(e){return t.apply(this,arguments)}}(),e.processResponseData=function(t){return t.data?this.flattenResponse?"object"==typeof t.data&&t.data.data&&1===Object.keys(t.data).length?t.data.data:t.data:t:null},t}(),exports.HttpRequestHandler=e.__decorate([o.CatchAll((function(t,e){return e.processRequestError(t)}))],exports.HttpRequestHandler),exports.ApiHandler=function(){function t(t){var e=t.apiUrl,r=t.apiEndpoints,n=t.timeout,o=void 0===n?null:n,i=t.strategy,a=void 0===i?null:i,c=t.flattenResponse,l=void 0===c?null:c,h=t.logger,p=void 0===h?null:h,f=t.httpRequestErrorService,d=void 0===f?null:f,v=u(t,["apiUrl","apiEndpoints","timeout","strategy","flattenResponse","logger","httpRequestErrorService"]);this.apiUrl="",this.apiUrl=e,this.apiEndpoints=r,this.logger=p,this.httpRequestHandler=new exports.HttpRequestHandler(s({},v,{baseURL:this.apiUrl,timeout:o,strategy:a,flattenResponse:l,logger:p,httpRequestErrorService:d}))}var e=t.prototype;return e.getInstance=function(){return this.httpRequestHandler.getInstance()},e.__get=function(t){return t in this?this[t]:this.apiEndpoints[t]?this.handleRequest.bind(this,t):this.handleNonImplemented.bind(this,t)},e.handleRequest=function(){var t=a(l.mark((function t(){var e,r,n,o,i,a,u=arguments;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=(u.length<=1?void 0:u[1])||{},n=(u.length<=2?void 0:u[2])||{},o=(u.length<=3?void 0:u[3])||{},i=(e=this.apiEndpoints[u.length<=0?void 0:u[0]]).url.replace(/:[a-z]+/gi,(function(t){return n[t.substr(1)]?n[t.substr(1)]:t})),a=s({},r),t.next=10,this.httpRequestHandler[e.method](i,a,o);case 10:return t.abrupt("return",t.sent);case 12:case"end":return t.stop()}}),t,this)})));return function(){return t.apply(this,arguments)}}(),e.handleNonImplemented=function(t){return this.logger&&this.logger.log&&this.logger.log(t+" endpoint not implemented."),Promise.resolve(null)},t}(),exports.ApiHandler=e.__decorate([r.applyMagic],exports.ApiHandler),exports.HttpRequestErrorHandler=h,exports.createApiFetcher=function(t){return new exports.ApiHandler(t)};
2//# sourceMappingURL=axios-multi-api.cjs.production.min.js.map