UNPKG

@klogt/intercept

Version:

The lightweight HTTP interception library built for modern Node.js testing. MSW-inspired simplicity meets native `fetch` performance.

2 lines 958 B
import{HttpResponse as e,headersToObject as t,resolveStrategy as n}from"./response-tF45mQBS.js";function r(){let r=null,i=e=>{try{return new URL(e.url)}catch{return new URL(e.url,`http://origin.invalid`)}},a=async(t,n)=>{let i=r;return i?i(t,n):e.json({error:`No original fetch available for passthrough`},{status:500})};return{attach(o){r||(r=globalThis.fetch??null,globalThis.fetch=async(r,s)=>{let c=new Request(r,s),l=await o.tryHandle(c);if(l.matched)return l.res;let u=i(c),d=n(o.getOptions().onUnhandledRequest??void 0,{request:c,url:u});if(d===`warn`)return o.logUnhandled(`warn`,c,u,o.getRegisteredHandlers()),a(r,s);if(d===`bypass`)return a(r,s);o.logUnhandled(`error`,c,u,o.getRegisteredHandlers());let f={method:c.method,url:u.toString(),headers:t(c.headers)};return e.json({error:`Unhandled request`,details:f},{status:501})})},detach(){r&&(globalThis.fetch=r,r=null)}}}export{r as createFetchAdapter}; //# sourceMappingURL=fetch-D6Tw5Yh6.js.map