UNPKG

4.13 kBJavaScriptView Raw
1var T=t=>{if(/^https?/.test(t))return t;let r=window.location.protocol+"//"+window.location.host;return t[0]==="/"?r+t:r+window.location.pathname+"/"+t},E=t=>[...t.entries()].reduce((r,[l,a])=>(r[l]=a,r),{}),L=t=>new URL(t),P=(t,r,l=null)=>t.reduce((a,w)=>w.call(l,a),r);function j({namespace:t=globalThis||window,fetch:r=t.fetch}={}){if(t==null&&(t=globalThis||window),typeof t!="object")throw new Error("[xfetch-hook] - `namespace` should be an object");if(typeof r!="function")throw new Error("[xfetch-hook] - `fetch` should be a function");if(typeof r.onRequest=="function")return r.stopIntercepting;let l="fetch",a=[];async function w(...e){let f=[],p=[],c=new Request(...e),y=null,b=null,R=null,x=null;for(let h of a){let s=await h({request:c,fetch:r,get url(){return R||(R=L(c.url))},get headers(){return x||(x=E(c.headers))}});!s||(s.request instanceof Request&&(c=s.request),s.response instanceof Response&&(y=s.response),typeof s.listen=="function"&&f.push(s.listen),typeof s.as=="string"&&(b=s.as,typeof s.transformResponse=="function"&&p.push(s.transformResponse)))}async function m(h){let s=h,g=b&&typeof h[b]=="function",i=await(g&&h.clone()[b]());if(p.length>0){let n=P(p,i);typeof n!="string"&&(n=JSON.stringify(n)),s=new Response(n,{status:h.status,statusText:h.statusText,headers:h.headers})}return f.forEach(n=>{let d=s.clone();g?d[b]().then(n.bind(null,d)):n(d)}),s}return y?m(y):r(c).then(m)}Object.defineProperty(w,"name",{value:l,configurable:!0});function H(){t[l]=w}H();function q(){t[l]=r}function M(e){a=a.filter(f=>f!==e)}function o(e){a.push(e)}return w.stopProxying=q,w.onRequest=function(f){if(typeof f!="function")throw new Error("[xfetch-hook] - `onRequest`: argument must be a function");let p=()=>M(f);return a.find(y=>y===f)||o(f),p},q}function X({namespace:t=globalThis||window}={}){if(t==null&&(t=globalThis||window),typeof t!="object")throw new Error("[xfetch-hook] - `namespace` should be an object");let r=t.XMLHttpRequest;if(typeof r!="function")throw new Error("[xfetch-hook] - `XMLHttpRequest` should be a function.");let l=[];function a(){let o=new r,e=this,f,p,c,y,b,R,x,m,h=[],s=[];this.onreadystatechange=null,o.onreadystatechange=function(){if(c&&this.readyState==4){let i=o.responseType;try{e.response=m||(m=P(h,o.response,e)),(i===""||i==="text")&&(e.responseText=o.responseText),s.forEach(n=>n.call(e,e.response,e))}catch(n){console.warn("Error in proxied xfetch-hook package",n.message)}}if(e.onreadystatechange)return e.onreadystatechange()},["status","statusText","readyState","responseXML","upload"].forEach(function(i){Object.defineProperty(e,i,{get:function(){return o[i]}})}),["ontimeout, timeout","withCredentials","onload","onerror","onprogress","responseType"].forEach(function(i){Object.defineProperty(e,i,{get:function(){return o[i]},set:function(n){i==="onload"&&typeof n=="function"&&(n=n.bind(e)),o[i]=n}})}),["addEventListener","abort","getAllkeyResponseHeaders","getAllResponseHeaders","getResponseHeader","overrideMimeType"].forEach(function(i){Object.defineProperty(e,i,{value:function(){return o[i].apply(o,arguments)}})});let g=new Headers;e.setRequestHeader=function(n,d){g.append(n,d)},e.open=function(n,d,u,U,O){f=n,p=new URL(T(d)),c=u!==!1,y=U,b=O,o.open(f,p.toString(),c,y,b)},e.send=async function(n){if(R=n,c){for(let d of l){let u=await d({method:f,url:p,body:R,get headers(){return x||(x=E(g))}});!u||(u.method&&(f=u.method),u.url&&(p=u.url.href||u.url),u.body!==void 0&&(R=u.body),u.headers instanceof Headers&&(g=u.headers),typeof u.transformResponse=="function"&&h.push(u.transformResponse),typeof u.listen=="function"&&s.push(u.listen))}o.open(f,p,c,y,b);for(let d in g.entries())o.setRequestHeader(d[0],d[1]);o.send(R)}}}Object.defineProperty(a,"name",{value:"XMLHttpRequest",configurable:!0});function w(){t.XMLHttpRequest=a}w();function H(){t.XMLHttpRequest=r}function q(o){l=l.filter(e=>e!==o)}function M(o){l.push(o)}return a.stopProxying=H,a.onRequest=function(e){if(typeof e!="function")throw new Error("[onRequest] - Argument must be a function");let f=()=>q(e);return l.find(c=>c===e)||M(e),f},H}export{j as fetchHook,X as xhrHook};