UNPKG

2.29 kBJavaScriptView Raw
1var xfetch=xfetch||{};xfetch.fetchHook=(()=>{var w=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var _=Object.prototype.hasOwnProperty;var A=e=>w(e,"__esModule",{value:!0});var D=(e,t)=>{for(var n in t)w(e,n,{get:t[n],enumerable:!0})},N=(e,t,n,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of M(t))!_.call(e,r)&&(n||r!=="default")&&w(e,r,{get:()=>t[r],enumerable:!(s=k(t,r))||s.enumerable});return e};var O=(e=>(t,n)=>e&&e.get(t)||(n=N(A({}),t,1),e&&e.set(t,n),n))(typeof WeakMap!="undefined"?new WeakMap:0);var v={};D(v,{default:()=>P});var q=e=>[...e.entries()].reduce((t,[n,s])=>(t[n]=s,t),{}),m=e=>new URL(e),E=(e,t,n=null)=>e.reduce((s,r)=>r.call(n,s),t);function P({namespace:e=globalThis||window,fetch:t=e.fetch}={}){if(e==null&&(e=globalThis||window),typeof e!="object")throw new Error("[xfetch-hook] - `namespace` should be an object");if(typeof t!="function")throw new Error("[xfetch-hook] - `fetch` should be a function");if(typeof t.onRequest=="function")return t.stopIntercepting;let n="fetch",s=[];async function r(...a){let f=[],l=[],c=new Request(...a),p=null,d=null,y=null,R=null;for(let u of s){let o=await u({request:c,fetch:t,get url(){return y||(y=m(c.url))},get headers(){return R||(R=q(c.headers))}});!o||(o.request instanceof Request&&(c=o.request),o.response instanceof Response&&(p=o.response),typeof o.listen=="function"&&f.push(o.listen),typeof o.as=="string"&&(d=o.as,typeof o.transformResponse=="function"&&l.push(o.transformResponse)))}async function x(u){let o=u,g=d&&typeof u[d]=="function",j=await(g&&u.clone()[d]());if(l.length>0){let i=E(l,j);typeof i!="string"&&(i=JSON.stringify(i)),o=new Response(i,{status:u.status,statusText:u.statusText,headers:u.headers})}return f.forEach(i=>{let h=o.clone();g?h[d]().then(i.bind(null,h)):i(h)}),o}return p?x(p):t(c).then(x)}Object.defineProperty(r,"name",{value:n,configurable:!0});function U(){e[n]=r}U();function b(){e[n]=t}function T(a){s=s.filter(f=>f!==a)}function H(a){s.push(a)}return r.stopProxying=b,r.onRequest=function(f){if(typeof f!="function")throw new Error("[xfetch-hook] - `onRequest`: argument must be a function");let l=()=>T(f);return s.find(p=>p===f)||H(f),l},b}return O(v);})();
2xfetch.fetchHook = xfetch.fetchHook.default;