get-it
Version:
Generic HTTP request library for node, browsers and workers
2 lines (1 loc) • 1.69 kB
JavaScript
import{v as e,p as r}from"./_commonjsHelpers.js";const t=["request","response","progress","error","abort"],o=["processOptions","validateOptions","interceptRequest","finalizeOptions","onRequest","onResponse","onError","onReturn","onHeaders"];function n(s,i){const u=[],a=o.reduce((e,r)=>(e[r]=e[r]||[],e),{processOptions:[r],validateOptions:[e]});function c(e){const r=t.reduce((e,r)=>(e[r]=function(){const e=/* @__PURE__ */Object.create(null);let r=0;return{publish:function(r){for(const t in e)e[t](r)},subscribe:function(t){const o=r++;return e[o]=t,function(){delete e[o]}}}}(),e),{}),o=(e=>function(r,t,...o){const n="onError"===r;let s=t;for(let t=0;t<e[r].length&&(s=(0,e[r][t])(s,...o),!n||s);t++);return s})(a),n=o("processOptions",e);o("validateOptions",n);const s={options:n,channels:r,applyMiddleware:o};let u;const c=r.request.subscribe(e=>{u=i(e,(t,n)=>((e,t,n)=>{let s=e,i=t;if(!s)try{i=o("onResponse",t,n)}catch(e){i=null,s=e}s=s&&o("onError",s,n),s?r.error.publish(s):i&&r.response.publish(i)})(t,n,e))});r.abort.subscribe(()=>{c(),u&&u.abort()});const l=o("onReturn",r,s);return l===r&&r.request.publish(s),l}return c.use=function(e){if(!e)throw new Error("Tried to add middleware that resolved to falsey value");if("function"==typeof e)throw new Error("Tried to add middleware that was a function. It probably expects you to pass options to it.");if(e.onReturn&&a.onReturn.length>0)throw new Error("Tried to add new middleware with `onReturn` handler, but another handler has already been registered for this event");return o.forEach(r=>{e[r]&&a[r].push(e[r])}),u.push(e),c},c.clone=()=>n(u,i),s.forEach(c.use),c}export{n as c};//# sourceMappingURL=createRequester.js.map