UNPKG

http-svc

Version:

A HTTP request service for browser and node.js

6 lines (5 loc) 34.6 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const _=require("@http-svc/middleware");function _e(r,e){var t=r==null?null:typeof Symbol!="undefined"&&r[Symbol.iterator]||r["@@iterator"];if(t!=null){var n,a,u,f,i=[],s=!0,h=!1;try{if(u=(t=t.call(r)).next,e===0){if(Object(t)!==t)return;s=!1}else for(;!(s=(n=u.call(t)).done)&&(i.push(n.value),i.length!==e);s=!0);}catch(y){h=!0,a=y}finally{try{if(!s&&t.return!=null&&(f=t.return(),Object(f)!==f))return}finally{if(h)throw a}}return i}}function be(r,e){var t=Object.keys(r);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(r);e&&(n=n.filter(function(a){return Object.getOwnPropertyDescriptor(r,a).enumerable})),t.push.apply(t,n)}return t}function W(r){for(var e=1;e<arguments.length;e++){var t=arguments[e]!=null?arguments[e]:{};e%2?be(Object(t),!0).forEach(function(n){w(r,n,t[n])}):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(t)):be(Object(t)).forEach(function(n){Object.defineProperty(r,n,Object.getOwnPropertyDescriptor(t,n))})}return r}function S(){S=function(){return e};var r,e={},t=Object.prototype,n=t.hasOwnProperty,a=Object.defineProperty||function(c,o,p){c[o]=p.value},u=typeof Symbol=="function"?Symbol:{},f=u.iterator||"@@iterator",i=u.asyncIterator||"@@asyncIterator",s=u.toStringTag||"@@toStringTag";function h(c,o,p){return Object.defineProperty(c,o,{value:p,enumerable:!0,configurable:!0,writable:!0}),c[o]}try{h({},"")}catch(c){h=function(o,p,b){return o[p]=b}}function y(c,o,p,b){var v=o&&o.prototype instanceof F?o:F,O=Object.create(v.prototype),j=new N(b||[]);return a(O,"_invoke",{value:te(c,p,j)}),O}function g(c,o,p){try{return{type:"normal",arg:c.call(o,p)}}catch(b){return{type:"throw",arg:b}}}e.wrap=y;var l="suspendedStart",R="suspendedYield",d="executing",m="completed",E={};function F(){}function k(){}function A(){}var D={};h(D,f,function(){return this});var X=Object.getPrototypeOf,C=X&&X(X(se([])));C&&C!==t&&n.call(C,f)&&(D=C);var J=A.prototype=F.prototype=Object.create(D);function M(c){["next","throw","return"].forEach(function(o){h(c,o,function(p){return this._invoke(o,p)})})}function V(c,o){function p(v,O,j,L){var B=g(c[v],c,O);if(B.type!=="throw"){var Z=B.arg,ee=Z.value;return ee&&typeof ee=="object"&&n.call(ee,"__await")?o.resolve(ee.__await).then(function(z){p("next",z,j,L)},function(z){p("throw",z,j,L)}):o.resolve(ee).then(function(z){Z.value=z,j(Z)},function(z){return p("throw",z,j,L)})}L(B.arg)}var b;a(this,"_invoke",{value:function(v,O){function j(){return new o(function(L,B){p(v,O,L,B)})}return b=b?b.then(j,j):j()}})}function te(c,o,p){var b=l;return function(v,O){if(b===d)throw new Error("Generator is already running");if(b===m){if(v==="throw")throw O;return{value:r,done:!0}}for(p.method=v,p.arg=O;;){var j=p.delegate;if(j){var L=K(j,p);if(L){if(L===E)continue;return L}}if(p.method==="next")p.sent=p._sent=p.arg;else if(p.method==="throw"){if(b===l)throw b=m,p.arg;p.dispatchException(p.arg)}else p.method==="return"&&p.abrupt("return",p.arg);b=d;var B=g(c,o,p);if(B.type==="normal"){if(b=p.done?m:R,B.arg===E)continue;return{value:B.arg,done:p.done}}B.type==="throw"&&(b=m,p.method="throw",p.arg=B.arg)}}}function K(c,o){var p=o.method,b=c.iterator[p];if(b===r)return o.delegate=null,p==="throw"&&c.iterator.return&&(o.method="return",o.arg=r,K(c,o),o.method==="throw")||p!=="return"&&(o.method="throw",o.arg=new TypeError("The iterator does not provide a '"+p+"' method")),E;var v=g(b,c.iterator,o.arg);if(v.type==="throw")return o.method="throw",o.arg=v.arg,o.delegate=null,E;var O=v.arg;return O?O.done?(o[c.resultName]=O.value,o.next=c.nextLoc,o.method!=="return"&&(o.method="next",o.arg=r),o.delegate=null,E):O:(o.method="throw",o.arg=new TypeError("iterator result is not an object"),o.delegate=null,E)}function Q(c){var o={tryLoc:c[0]};1 in c&&(o.catchLoc=c[1]),2 in c&&(o.finallyLoc=c[2],o.afterLoc=c[3]),this.tryEntries.push(o)}function re(c){var o=c.completion||{};o.type="normal",delete o.arg,c.completion=o}function N(c){this.tryEntries=[{tryLoc:"root"}],c.forEach(Q,this),this.reset(!0)}function se(c){if(c||c===""){var o=c[f];if(o)return o.call(c);if(typeof c.next=="function")return c;if(!isNaN(c.length)){var p=-1,b=function v(){for(;++p<c.length;)if(n.call(c,p))return v.value=c[p],v.done=!1,v;return v.value=r,v.done=!0,v};return b.next=b}}throw new TypeError(typeof c+" is not iterable")}return k.prototype=A,a(J,"constructor",{value:A,configurable:!0}),a(A,"constructor",{value:k,configurable:!0}),k.displayName=h(A,s,"GeneratorFunction"),e.isGeneratorFunction=function(c){var o=typeof c=="function"&&c.constructor;return!!o&&(o===k||(o.displayName||o.name)==="GeneratorFunction")},e.mark=function(c){return Object.setPrototypeOf?Object.setPrototypeOf(c,A):(c.__proto__=A,h(c,s,"GeneratorFunction")),c.prototype=Object.create(J),c},e.awrap=function(c){return{__await:c}},M(V.prototype),h(V.prototype,i,function(){return this}),e.AsyncIterator=V,e.async=function(c,o,p,b,v){v===void 0&&(v=Promise);var O=new V(y(c,o,p,b),v);return e.isGeneratorFunction(o)?O:O.next().then(function(j){return j.done?j.value:O.next()})},M(J),h(J,s,"Generator"),h(J,f,function(){return this}),h(J,"toString",function(){return"[object Generator]"}),e.keys=function(c){var o=Object(c),p=[];for(var b in o)p.push(b);return p.reverse(),function v(){for(;p.length;){var O=p.pop();if(O in o)return v.value=O,v.done=!1,v}return v.done=!0,v}},e.values=se,N.prototype={constructor:N,reset:function(c){if(this.prev=0,this.next=0,this.sent=this._sent=r,this.done=!1,this.delegate=null,this.method="next",this.arg=r,this.tryEntries.forEach(re),!c)for(var o in this)o.charAt(0)==="t"&&n.call(this,o)&&!isNaN(+o.slice(1))&&(this[o]=r)},stop:function(){this.done=!0;var c=this.tryEntries[0].completion;if(c.type==="throw")throw c.arg;return this.rval},dispatchException:function(c){if(this.done)throw c;var o=this;function p(B,Z){return O.type="throw",O.arg=c,o.next=B,Z&&(o.method="next",o.arg=r),!!Z}for(var b=this.tryEntries.length-1;b>=0;--b){var v=this.tryEntries[b],O=v.completion;if(v.tryLoc==="root")return p("end");if(v.tryLoc<=this.prev){var j=n.call(v,"catchLoc"),L=n.call(v,"finallyLoc");if(j&&L){if(this.prev<v.catchLoc)return p(v.catchLoc,!0);if(this.prev<v.finallyLoc)return p(v.finallyLoc)}else if(j){if(this.prev<v.catchLoc)return p(v.catchLoc,!0)}else{if(!L)throw new Error("try statement without catch or finally");if(this.prev<v.finallyLoc)return p(v.finallyLoc)}}}},abrupt:function(c,o){for(var p=this.tryEntries.length-1;p>=0;--p){var b=this.tryEntries[p];if(b.tryLoc<=this.prev&&n.call(b,"finallyLoc")&&this.prev<b.finallyLoc){var v=b;break}}v&&(c==="break"||c==="continue")&&v.tryLoc<=o&&o<=v.finallyLoc&&(v=null);var O=v?v.completion:{};return O.type=c,O.arg=o,v?(this.method="next",this.next=v.finallyLoc,E):this.complete(O)},complete:function(c,o){if(c.type==="throw")throw c.arg;return c.type==="break"||c.type==="continue"?this.next=c.arg:c.type==="return"?(this.rval=this.arg=c.arg,this.method="return",this.next="end"):c.type==="normal"&&o&&(this.next=o),E},finish:function(c){for(var o=this.tryEntries.length-1;o>=0;--o){var p=this.tryEntries[o];if(p.finallyLoc===c)return this.complete(p.completion,p.afterLoc),re(p),E}},catch:function(c){for(var o=this.tryEntries.length-1;o>=0;--o){var p=this.tryEntries[o];if(p.tryLoc===c){var b=p.completion;if(b.type==="throw"){var v=b.arg;re(p)}return v}}throw new Error("illegal catch attempt")},delegateYield:function(c,o,p){return this.delegate={iterator:se(c),resultName:o,nextLoc:p},this.method==="next"&&(this.arg=r),E}},e}function We(r,e){if(typeof r!="object"||!r)return r;var t=r[Symbol.toPrimitive];if(t!==void 0){var n=t.call(r,e||"default");if(typeof n!="object")return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return(e==="string"?String:Number)(r)}function qe(r){var e=We(r,"string");return typeof e=="symbol"?e:String(e)}function ue(r){return ue=typeof Symbol=="function"&&typeof Symbol.iterator=="symbol"?function(e){return typeof e}:function(e){return e&&typeof Symbol=="function"&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},ue(r)}function we(r,e,t,n,a,u,f){try{var i=r[u](f),s=i.value}catch(h){t(h);return}i.done?e(s):Promise.resolve(s).then(n,a)}function H(r){return function(){var e=this,t=arguments;return new Promise(function(n,a){var u=r.apply(e,t);function f(s){we(u,n,a,f,i,"next",s)}function i(s){we(u,n,a,f,i,"throw",s)}f(void 0)})}}function I(r,e){if(!(r instanceof e))throw new TypeError("Cannot call a class as a function")}function ge(r,e){for(var t=0;t<e.length;t++){var n=e[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(r,qe(n.key),n)}}function $(r,e,t){return e&&ge(r.prototype,e),t&&ge(r,t),Object.defineProperty(r,"prototype",{writable:!1}),r}function w(r,e,t){return e=qe(e),e in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t,r}function U(r,e){if(typeof e!="function"&&e!==null)throw new TypeError("Super expression must either be null or a function");r.prototype=Object.create(e&&e.prototype,{constructor:{value:r,writable:!0,configurable:!0}}),Object.defineProperty(r,"prototype",{writable:!1}),e&&fe(r,e)}function q(r){return q=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},q(r)}function fe(r,e){return fe=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(n,a){return n.__proto__=a,n},fe(r,e)}function P(r){if(r===void 0)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return r}function Y(r,e){if(e&&(typeof e=="object"||typeof e=="function"))return e;if(e!==void 0)throw new TypeError("Derived constructors may only return object or undefined");return P(r)}function le(r,e){return Ke(r)||_e(r,e)||ve(r,e)||Qe()}function Te(r){return Je(r)||ze(r)||ve(r)||Ve()}function Je(r){if(Array.isArray(r))return pe(r)}function Ke(r){if(Array.isArray(r))return r}function ze(r){if(typeof Symbol!="undefined"&&r[Symbol.iterator]!=null||r["@@iterator"]!=null)return Array.from(r)}function ve(r,e){if(!!r){if(typeof r=="string")return pe(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);if(t==="Object"&&r.constructor&&(t=r.constructor.name),t==="Map"||t==="Set")return Array.from(r);if(t==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return pe(r,e)}}function pe(r,e){(e==null||e>r.length)&&(e=r.length);for(var t=0,n=new Array(e);t<e;t++)n[t]=r[t];return n}function Ve(){throw new TypeError(`Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method.`)}function Qe(){throw new TypeError(`Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method.`)}function Ze(r,e){var t=typeof Symbol!="undefined"&&r[Symbol.iterator]||r["@@iterator"];if(!t){if(Array.isArray(r)||(t=ve(r))||e&&r&&typeof r.length=="number"){t&&(r=t);var n=0,a=function(){};return{s:a,n:function(){return n>=r.length?{done:!0}:{done:!1,value:r[n++]}},e:function(s){throw s},f:a}}throw new TypeError(`Invalid attempt to iterate non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method.`)}var u=!0,f=!1,i;return{s:function(){t=t.call(r)},n:function(){var s=t.next();return u=s.done,s},e:function(s){f=!0,i=s},f:function(){try{!u&&t.return!=null&&t.return()}finally{if(f)throw i}}}}var T=function(e){return"BUILT_IN_".concat(e)},x=function(e,t){var n,a=new Error(t||"Request Error: ".concat(((n=e.response)===null||n===void 0?void 0:n.status)||"unknow status"));return a.config=e.request,e.response&&(a.response=e.response,e.response.ok||(a.code=e.response.status)),a},he=function(e){return!!(e.name&&e.handler)},ie=typeof window=="undefined",xe=function(e){return toString.call(e)==="[object Array]"},ke=function(e){return e!==null&&ue(e)==="object"},je=function(e){return Object.prototype.toString.call(e)==="[object Object]"},et=function(e){return typeof FormData!="undefined"&&e instanceof FormData},tt=function(e){return toString.call(e)==="[object Date]"},Re=function(e){return encodeURIComponent(e).replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,"+").replace(/%5B/gi,"[").replace(/%5D/gi,"]")},Se=function(e,t){var n=[];Object.keys(t).forEach(function(f){var i=t[f];i===null||typeof i=="undefined"||(xe(i)?f=f+"[]":i=[i],i.forEach(function(s){tt(s)?s=s.toString():ke(s)&&(s=JSON.stringify(s)),n.push(Re(f)+"="+Re(s))}))});var a=n.join("&");if(a){var u=e.indexOf("#");u!==-1&&(e=e.slice(0,u)),e+=(e.indexOf("?")===-1?"?":"&")+a}return e},rt=function(e){var t=e.split("?"),n=le(t,2),a=n[0],u=n[1];if(u){var f=u.split("&");if(f.length){var i={},s=Ze(f),h;try{for(s.s();!(h=s.n()).done;){var y=h.value,g=y.split("="),l=le(g,2),R=l[0],d=l[1];R&&typeof d!="undefined"&&(i[R]=d)}}catch(m){s.e(m)}finally{s.f()}return{url:a,params:i}}}return{url:e}},Ae="Content-Type",nt=function(e){return(e==null?void 0:e[Ae])||(e==null?void 0:e["content-type"])},Oe=function(e,t){!t||(e[Ae]=t)},Ce=function(e,t){t||(t={});for(var n=t.decode===!1,a=t.template||document.cookie,u=n?a:decodeURIComponent(a),f=u.split(";"),i=0;i<f.length;i++){for(var s=f[i];s.charAt(0)===" ";)s=s.substring(1);var h=s.split("="),y=h[0],g=h[1];if(e===y)return g}return""},ne=function r(e,t,n,a){if(Array.isArray(t))for(var u=Te(t);u.length;){if(u.length===1){r(e,u[0],n);break}var f=u.shift();if(!je(e))if(typeof e[f]=="undefined"&&a)e[f]={};else{console.warn('The value of "'.concat(f,'" is not a record object!'));break}e=e[f]}else{var i=t;e[i]=n}};function at(r,e,t){function n(){if(typeof Reflect=="undefined"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(a){return!1}}return e=q(e),Y(r,n()?Reflect.construct(e,t||[],q(r).constructor):e.apply(r,t))}var Pe=function(){var r=H(S().mark(function e(t,n){var a,u,f,i,s,h,y,g,l,R,d,m,E;return S().wrap(function(k){for(;;)switch(k.prev=k.next){case 0:if(t.request){k.next=2;break}return k.abrupt("return",n());case 2:return a=t.config,u=a.baseURL,f=a.headers,i=a.params,s=a.data,h=a.credentials,y=h===void 0?"include":h,g=t.config.url,l=rt(g),R=l.url,d=l.params,d?(g=t.request.url=R,t.request.params=W(W({},d),i||{})):(t.request.url=g,t.request.params=W({},i||{})),u&&/^(https?:)?\/\//.test(g)===!1&&(t.request.url="".concat(u).concat(g)),t.request.headers=W({},f||{}),s&&(ue(s)==="object"?typeof FormData!="undefined"&&s instanceof FormData?(m=new FormData,E=Array.from(s.entries()),E.forEach(function(A){var D=le(A,2),X=D[0],C=D[1];m.append(X,C)}),t.request.data=m):Object.keys(s)&&(t.request.data=JSON.parse(JSON.stringify(s))):t.request.data=s),t.request.credentials=y,k.next=12,n();case 12:return k.abrupt("return",k.sent);case 13:case"end":return k.stop()}},e)}));return function(t,n){return r.apply(this,arguments)}}(),Ie=function(r){U(e,r);function e(){var t;I(this,e);for(var n=arguments.length,a=new Array(n),u=0;u<n;u++)a[u]=arguments[u];return t=at(this,e,[].concat(a)),w(P(t),"handler",Pe),w(P(t),"name",T("INIT_CTX")),t}return $(e)}(_.HttpSvcMiddleware);w(Ie,"handler",Pe);function ut(r,e,t){function n(){if(typeof Reflect=="undefined"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(a){return!1}}return e=q(e),Y(r,n()?Reflect.construct(e,t||[],q(r).constructor):e.apply(r,t))}var $e=function(){var r=H(S().mark(function e(t,n){var a,u;return S().wrap(function(i){for(;;)switch(i.prev=i.next){case 0:if(a=t.config.timeout,typeof t.config.timeout!="number"&&(a=ie?350:1e4),!(!t.abortController&&a)){i.next=11;break}return u=new AbortController,t.abortController=u,t.timeoutId=setTimeout(function(){u.abort()},a),i.next=8,n();case 8:clearTimeout(t.timeoutId),i.next=13;break;case 11:return i.next=13,n();case 13:case"end":return i.stop()}},e)}));return function(t,n){return r.apply(this,arguments)}}(),De=function(r){U(e,r);function e(){var t;return I(this,e),t=ut(this,e,[$e]),w(P(t),"name",T("TIMEOUT")),t}return $(e)}(_.HttpSvcMiddleware);w(De,"handler",$e);function it(r,e,t){function n(){if(typeof Reflect=="undefined"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(a){return!1}}return e=q(e),Y(r,n()?Reflect.construct(e,t||[],q(r).constructor):e.apply(r,t))}var He=function(){var r=H(S().mark(function e(t,n,a){var u,f,i,s,h,y,g,l;return S().wrap(function(d){for(;;)switch(d.prev=d.next){case 0:if(t.request){d.next=2;break}return d.abrupt("return",n());case 2:if(u=(a==null?void 0:a.payload)||{token:"xsrf_token"},f=u.token,i=u.params,s=u.data,h=u.headers,i||s||h)try{ie?y=Ce(f,{template:((g=t.request.headers)===null||g===void 0?void 0:g.cookie)||((l=t.request.headers)===null||l===void 0?void 0:l.Cookie)||"_",decode:!1}):y=Ce(f,{decode:!1})}catch(m){ie||console.warn(m)}return y&&(i&&ne(t.request,["params",i],y,!0),s&&(typeof t.request.data=="undefined"?ne(t.request,["data",s],y,!0):ke(t.request.data)&&(je(t.request.data)?ne(t.request,["data",s],y,!0):et(t.request.data)&&t.request.data.append(s,y))),h&&ne(t.request,["headers",h],y,!0)),d.next=8,n();case 8:return d.abrupt("return",d.sent);case 9:case"end":return d.stop()}},e)}));return function(t,n,a){return r.apply(this,arguments)}}(),Le=function(r){U(e,r);function e(t){var n;return I(this,e),n=it(this,e,[{handler:He,payload:t}]),w(P(n),"name",T("XSRF")),n}return $(e)}(_.HttpSvcMiddleware);w(Le,"handler",He);function ot(r,e,t){function n(){if(typeof Reflect=="undefined"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(a){return!1}}return e=q(e),Y(r,n()?Reflect.construct(e,t||[],q(r).constructor):e.apply(r,t))}var ce=function(e,t){return t?t.indexOf(e)>-1:!1},Ee=function(e,t,n){if(!!e.request)if(e.request.data=JSON.stringify(t),e.request.headers)n||Oe(e.request.headers,"application/json");else{var a={};Oe(a,"application/json"),e.request.headers=a}},Be=function(){var r=H(S().mark(function e(t,n,a){var u,f,i,s,h,y,g,l;return S().wrap(function(d){for(;;)switch(d.prev=d.next){case 0:if(t.request){d.next=2;break}return d.abrupt("return",n());case 2:if(u=t.request,f=u.params,i=u.headers,t.request.url=Se(t.request.url,f||{}),!t.request.data){d.next=17;break}if(s=Object.prototype.toString.call(t.request.data)!=="[object Object]",s){d.next=17;break}if(y=nt(i),g=t.request.data,!(a!=null&&(h=a.payload)!==null&&h!==void 0&&h.stringify)){d.next=14;break}return Ee(t,g,y),d.next=13,n();case 13:return d.abrupt("return",d.sent);case 14:ce("application/x-www-form-urlencoded",y)&&(t.request.data=Se("",g).slice(1)),ce("application/json",y||"application/json")&&Ee(t,g,y),ce("multipart/form-data",y)&&(l=Object.keys(g),l.length&&(t.request.data=l.reduce(function(m,E){return m.append(E,g[E]),m},new FormData)));case 17:return d.next=19,n();case 19:return d.abrupt("return",d.sent);case 20:case"end":return d.stop()}},e)}));return function(t,n,a){return r.apply(this,arguments)}}(),Fe=function(r){U(e,r);function e(){var t;return I(this,e),t=ot(this,e,[Be]),w(P(t),"name",T("BODY")),t}return $(e)}(_.HttpSvcMiddleware);w(Fe,"handler",Be);function st(r,e,t){function n(){if(typeof Reflect=="undefined"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(a){return!1}}return e=q(e),Y(r,n()?Reflect.construct(e,t||[],q(r).constructor):e.apply(r,t))}var Me=function(){var r=H(S().mark(function e(t,n){var a,u,f,i,s,h,y;return S().wrap(function(l){for(;;)switch(l.prev=l.next){case 0:if(!ie){l.next=2;break}throw new Error("When you are in a Node environment, please use server-side fetch middleware.");case 2:if(window.fetch){l.next=4;break}throw new Error("When you are in a non-modern browser, please manually polyfill fetch.");case 4:if(t.request){l.next=8;break}return l.next=7,n();case 7:return l.abrupt("return",l.sent);case 8:if(!t.response){l.next=12;break}return l.next=11,n();case 11:return l.abrupt("return",l.sent);case 12:return u=t.request,f=u.url,i=u.method,s=u.headers,h=u.credentials,y=t.request.data,l.next=16,window.fetch(f,{method:i,body:y,headers:s,credentials:h,signal:(a=t.abortController)===null||a===void 0?void 0:a.signal});case 16:return t.response=l.sent,l.next=19,n();case 19:return l.abrupt("return",l.sent);case 20:case"end":return l.stop()}},e)}));return function(t,n){return r.apply(this,arguments)}}(),ae=function(r){U(e,r);function e(){var t;I(this,e);for(var n=arguments.length,a=new Array(n),u=0;u<n;u++)a[u]=arguments[u];return t=st(this,e,[].concat(a)),w(P(t),"handler",Me),w(P(t),"name",T("FETCH")),t}return $(e)}(_.HttpSvcMiddleware);w(ae,"handler",Me);function ct(r,e,t){function n(){if(typeof Reflect=="undefined"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(a){return!1}}return e=q(e),Y(r,n()?Reflect.construct(e,t||[],q(r).constructor):e.apply(r,t))}var Ne=function(){var r=H(S().mark(function e(t,n,a){var u,f,i;return S().wrap(function(h){for(;;)switch(h.prev=h.next){case 0:return h.next=2,n();case 2:if(!((u=t.response)!==null&&u!==void 0&&u.ok)){h.next=14;break}if(typeof t.response.data!="undefined"){h.next=14;break}return h.prev=4,i=(a==null||(f=a.payload)===null||f===void 0?void 0:f.type)||"json",h.next=8,t.response[i]();case 8:t.response.data=h.sent,h.next=14;break;case 11:h.prev=11,h.t0=h.catch(4),console.warn(h.t0);case 14:case"end":return h.stop()}},e,null,[[4,11]])}));return function(t,n,a){return r.apply(this,arguments)}}(),Ue=function(r){U(e,r);function e(){var t;return I(this,e),t=ct(this,e,[Ne]),w(P(t),"name",T("RES_DATA")),t}return $(e)}(_.HttpSvcMiddleware);w(Ue,"handler",Ne);function ft(r,e,t){function n(){if(typeof Reflect=="undefined"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(a){return!1}}return e=q(e),Y(r,n()?Reflect.construct(e,t||[],q(r).constructor):e.apply(r,t))}var lt=function(){var r=H(S().mark(function e(t,n){return S().wrap(function(u){for(;;)switch(u.prev=u.next){case 0:return u.next=2,n();case 2:case"end":return u.stop()}},e)}));return function(t,n){return r.apply(this,arguments)}}(),oe=function(r){U(e,r);function e(t,n){var a;return I(this,e),a=ft(this,e),w(P(a),"name",T("SPRAY")),a.handler=function(){var u=H(S().mark(function f(i,s){return S().wrap(function(y){for(;;)switch(y.prev=y.next){case 0:return y.next=2,t(i,s,{payload:n});case 2:return y.abrupt("return",y.sent);case 3:case"end":return y.stop()}},f)}));return function(f,i){return u.apply(this,arguments)}}(),a}return $(e)}(_.HttpSvcMiddleware);w(oe,"handler",lt);function pt(r,e,t){function n(){if(typeof Reflect=="undefined"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(a){return!1}}return e=q(e),Y(r,n()?Reflect.construct(e,t||[],q(r).constructor):e.apply(r,t))}var Ye=function(){var r=H(S().mark(function e(t,n,a){var u,f,i,s,h,y,g,l,R;return S().wrap(function(m){for(;;)switch(m.prev=m.next){case 0:return f=(u=t.request)===null||u===void 0?void 0:u.function,i=(a==null?void 0:a.payload)||{times:0},s=i.times,h=i.condition,y=i.onRetry,g=function(){return!s||t.retry===s},l=function(){var E=H(S().mark(function F(k){var A,D;return S().wrap(function(C){for(;;)switch(C.prev=C.next){case 0:if(f){C.next=2;break}throw x(t,"Missing request function");case 2:if(!(g()&&!(a!=null&&(A=a.payload)!==null&&A!==void 0&&A.runtimeShortCircuit))){C.next=4;break}throw x(t);case 4:if(t.retry=t.retry?t.retry+1:1,C.prev=5,!y){C.next=11;break}return C.next=9,y(t.config,k);case 9:D=C.sent,D&&(t.config=D);case 11:return t.response&&delete t.response,C.next=14,f(t);case 14:C.next=19;break;case 16:throw C.prev=16,C.t0=C.catch(5),x(t,C.t0.message);case 19:case"end":return C.stop()}},F,null,[[5,16]])}));return function(k){return E.apply(this,arguments)}}(),m.prev=4,m.next=7,n();case 7:m.next=16;break;case 9:if(m.prev=9,m.t0=m.catch(4),!g()){m.next=13;break}throw x(t,m.t0===null||m.t0===void 0?void 0:m.t0.message);case 13:return m.next=15,l();case 15:return m.abrupt("return");case 16:if(!h){m.next=24;break}return m.next=19,h(t.response);case 19:if(R=m.sent,!R){m.next=24;break}return m.next=23,l(R);case 23:return m.abrupt("return");case 24:if(!(!t.response||!t.response.ok)){m.next=27;break}return m.next=27,l();case 27:case"end":return m.stop()}},e,null,[[4,9]])}));return function(t,n,a){return r.apply(this,arguments)}}(),de=function(r){U(e,r);function e(){var t;I(this,e);for(var n=arguments.length,a=new Array(n),u=0;u<n;u++)a[u]=arguments[u];return t=pt(this,e,[].concat(a)),w(P(t),"name",T("RETRY")),w(P(t),"handler",Ye),t}return $(e)}(_.HttpSvcMiddleware);w(de,"handler",Ye);function ht(r,e,t){function n(){if(typeof Reflect=="undefined"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(a){return!1}}return e=q(e),Y(r,n()?Reflect.construct(e,t||[],q(r).constructor):e.apply(r,t))}var yt=function(e,t){return Object.keys(t).sort().reduce(function(n,a){return n+="_".concat(a,":").concat(t[a]),n},"api:".concat(e))},Ge=function(){var r=H(S().mark(function e(t,n,a){var u,f,i,s,h,y;return S().wrap(function(l){for(;;)switch(l.prev=l.next){case 0:if(!(!t.request||t.request.method!=="GET")){l.next=4;break}return l.next=3,n();case 3:return l.abrupt("return",l.sent);case 4:if(a!=null&&a.payload){l.next=8;break}return l.next=7,n();case 7:return l.abrupt("return",l.sent);case 8:if(u=a.payload,f=u.store,i=u.extra,s=a.payload.key,!(!s||!f)){l.next=14;break}return l.next=13,n();case 13:return l.abrupt("return",l.sent);case 14:if(typeof s!="string"&&(s=yt(t.request.url,s)),h=f.get(s,i),!h){l.next=22;break}return t.response=new Response(JSON.stringify(h),{status:200,statusText:"Cache:".concat(s)}),l.next=20,n();case 20:l.next=25;break;case 22:return l.next=24,n();case 24:(y=t.response)!==null&&y!==void 0&&y.data&&f.set(s,t.response.data,i);case 25:case"end":return l.stop()}},e)}));return function(t,n,a){return r.apply(this,arguments)}}(),Xe=function(r){U(e,r);function e(t){var n;return I(this,e),n=ht(this,e,[{handler:Ge,payload:{store:t}}]),w(P(n),"name",T("CACHE")),n}return $(e)}(_.HttpSvcMiddleware);w(Xe,"handler",Ge);var me=$(function r(e){I(this,r),this.httpSvc=e});function vt(r,e,t){function n(){if(typeof Reflect=="undefined"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(a){return!1}}return e=q(e),Y(r,n()?Reflect.construct(e,t||[],q(r).constructor):e.apply(r,t))}var dt=function(r){U(e,r);function e(){return I(this,e),vt(this,e,arguments)}return $(e,[{key:"provide",value:function(){var n=this,a=arguments.length>0&&arguments[0]!==void 0?arguments[0]:{},u={inject:function(i,s){return n.inject(a,i,s),u},disable:function(i){return n.disable(a,i),u},get:function(){return a}};return u}},{key:"inject",value:function(n,a,u){n[a]=u}},{key:"disable",value:function(n,a){n[a]=W(W({},n[a]||{}),{},{disabled:!0})}},{key:"get",value:function(n,a){return n[a]||{}}}]),e}(me),G={INIT_CTX:T("INIT_CTX"),TIMEOUT:T("TIMEOUT"),BODY:T("BODY"),XSRF:T("XSRF"),FETCH:T("FETCH"),RETRY:T("RETRY"),SPRAY:T("SPRAY"),RES_DATA:T("RES_DATA"),RES_EXTRACT:T("RES_EXTRACT"),LOG:T("LOG"),CACHE:T("CACHE")},ye="$ORDER_";function mt(r,e,t){function n(){if(typeof Reflect=="undefined"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(a){return!1}}return e=q(e),Y(r,n()?Reflect.construct(e,t||[],q(r).constructor):e.apply(r,t))}var bt=function(){function r(e){I(this,r),w(this,"middlewares",[]),w(this,"middlewareConfigCtxProvider",void 0),this.control=e,this.middlewareConfigCtxProvider=this.control.httpSvc.configCtrl.provide()}return $(r,[{key:"with",value:function(t,n){return t?(typeof t=="string"?(this.middlewares.push({name:"".concat(ye).concat(t),handler:oe.handler}),n&&this.inject(t,n)):typeof t=="function"?this.middlewares.push(new oe(t,n)):he(t)&&(this.middlewares.push(t),n&&this.inject(t.name,n)),this):this}},{key:"inject",value:function(t,n){return this.middlewareConfigCtxProvider.inject(t,{payload:n}),this}},{key:"disable",value:function(t){return this.middlewareConfigCtxProvider.disable(t),this}},{key:"request",value:function(t){var n=this.control.compose(this.middlewares);return this.control.httpSvc.requestCtrl.request(t,this.middlewareConfigCtxProvider.get(),n)}}]),r}(),wt=function(r){U(e,r);function e(){var t;I(this,e);for(var n=arguments.length,a=new Array(n),u=0;u<n;u++)a[u]=arguments[u];return t=mt(this,e,[].concat(a)),w(P(t),"middlewares",[]),t}return $(e,[{key:"alreadyExistsIdx",value:function(n){return this.middlewares.findIndex(function(a){return a.name===n})}},{key:"register",value:function(n){var a=this;if(Array.isArray(n)){var u=!1;n.forEach(function(f){if(he(f)){u=!0;var i=a.alreadyExistsIdx(f.name);i>-1?a.middlewares[i]=f:a.middlewares.push(f)}}),u&&this.httpSvc.requestCtrl.reset()}}},{key:"setup",value:function(){return new bt(this)}},{key:"disable",value:function(n){return this.setup().disable(n)}},{key:"with",value:function(n,a){return this.setup().with(n,a)}},{key:"compose",value:function(n){var a=this,u=Te(this.middlewares),f=[],i=w(w(w(w({},G.RETRY,null),G.BODY,null),G.RES_DATA,null),G.TIMEOUT,null),s=Object.keys(i),h={};(n||[]).forEach(function(l){if(he(l)){var R=l.name;if(s.includes(R))i[R]||(i[R]=l);else if(R.startsWith(ye)){if(R=R.substring(ye.length),!h[R]&&!(R in i)){var d=u.findIndex(function(E){return E.name===R});if(d>-1){var m=u[d];u.splice(d,1),h[R]=m,f.push(m)}}}else G.SPRAY===R?f.push(l):h[R]||(h[R]=l,f.push(l))}});var y=[];u.forEach(function(l){s.includes(l.name)?i[l.name]||(i[l.name]=l):h[l.name]||y.push(l)}),y.push.apply(y,f);var g=[i[G.RETRY]||new de,new Ie].concat(y,[i[G.RES_DATA]||new Ue,i[G.BODY]||new Fe,i[G.TIMEOUT]||new De,this.httpSvc.fetch]);return function(){var l=H(S().mark(function R(d){var m,E,F,k,A,D,X,C;return S().wrap(function(M){for(;;)switch(M.prev=M.next){case 0:return C=function(){return C=H(S().mark(function te(K){var Q;return S().wrap(function(N){for(;;)switch(N.prev=N.next){case 0:if(K!==g.length){N.next=2;break}return N.abrupt("return");case 2:if(!(K<=E)){N.next=4;break}throw x(d,"The next() called multiple times");case 4:if(E=K,Q=g[E],Q){N.next=8;break}throw x(d,"Middleware is not exist");case 8:return N.next=10,Q.handler(d,X.bind(null,K+1),F.get(d.middleware,Q.name));case 10:case"end":return N.stop()}},te)})),C.apply(this,arguments)},X=function(te){return C.apply(this,arguments)},E=-1,F=a.httpSvc.configCtrl,k=F.get(d.middleware,"DEBUG").payload,M.next=7,X(0);case 7:if(!k){M.next=9;break}return M.abrupt("return",d);case 9:if(A=F.get(d.middleware,G.RES_EXTRACT).disabled,!(!A&&(m=d.response)!==null&&m!==void 0&&m.ok)){M.next=12;break}return M.abrupt("return",((D=d.response)===null||D===void 0?void 0:D.data)||null);case 12:return M.abrupt("return",d.response);case 13:case"end":return M.stop()}},R)}));return function(R){return l.apply(this,arguments)}}()}}]),e}(me);function gt(r,e,t){function n(){if(typeof Reflect=="undefined"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(a){return!1}}return e=q(e),Y(r,n()?Reflect.construct(e,t||[],q(r).constructor):e.apply(r,t))}var Rt=function(r){U(e,r);function e(){var t;I(this,e);for(var n=arguments.length,a=new Array(n),u=0;u<n;u++)a[u]=arguments[u];return t=gt(this,e,[].concat(a)),w(P(t),"fn",null),t}return $(e,[{key:"request",value:function(){var t=H(S().mark(function a(u){var f,i,s=arguments;return S().wrap(function(y){for(;;)switch(y.prev=y.next){case 0:return f=s.length>1&&s[1]!==void 0?s[1]:{},i=s.length>2?s[2]:void 0,i||(this.fn||this.generateRequestFunction(),i=this.fn),y.next=5,i(this.createContext(u,f,i));case 5:return y.abrupt("return",y.sent);case 6:case"end":return y.stop()}},a,this)}));function n(a){return t.apply(this,arguments)}return n}()},{key:"generateRequestFunction",value:function(){this.fn=this.httpSvc.assembleCtrl.compose()}},{key:"reset",value:function(){this.fn=null}},{key:"useAsyncRequest",get:function(){var n=this;return function(a,u){var f=n.httpSvc.create(u);a(f.request.bind(f))}}},{key:"createContext",value:function(n){var a=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{},u=arguments.length>2?arguments[2]:void 0,f=n.url,i=n.method,s={config:W({baseURL:this.httpSvc.baseURL},n),useAsyncRequest:this.useAsyncRequest,middleware:a,request:{url:f,method:(i||"get").toUpperCase(),function:u}};return s}}]),e}(me),St=function(){function r(e){if(I(this,r),w(this,"fetch",void 0),w(this,"assembleCtrl",new wt(this)),w(this,"configCtrl",new dt(this)),w(this,"requestCtrl",new Rt(this)),w(this,"baseURL","//api.domain.com"),e)if(Array.isArray(e))this.fetch=new ae,this.register(e);else{var t=e.middlewares,n=e.fetch,a=e.baseURL;this.fetch=n||new ae,this.register(t||[]),a&&(this.baseURL=a)}else this.fetch=new ae,this.register([])}return $(r,[{key:"register",value:function(t){this.assembleCtrl.register(t)}},{key:"request",value:function(t){return this.requestCtrl.request(t)}},{key:"disable",value:function(t){return this.assembleCtrl.disable(t)}},{key:"with",value:function(t,n){return this.assembleCtrl.with(t,n)}},{key:"setFetch",value:function(t){this.fetch=t,this.requestCtrl.reset()}},{key:"create",value:function(t){return new r(W({fetch:this.fetch},t||{}))}}]),r}();exports.BUILT_IN_MIDDLEWARE=G;exports.HttpService=St;exports.HttpSvcCache=Xe;exports.HttpSvcRetry=de;exports.HttpSvcSprayMiddleware=oe;exports.HttpSvcXsrf=Le; //# sourceMappingURL=index.legacy.js.map