@klogt/intercept
Version:
The lightweight HTTP interception library built for modern Node.js testing. MSW-inspired simplicity meets native `fetch` performance.
2 lines • 2.25 kB
JavaScript
const e=`[@klogt/intercept]`;function t(){return!!(process?.env&&(process.env.VITEST||process.env.JEST_WORKER_ID||process.env.NODE_ENV===`test`))}function n(){return t()?`error`:`warn`}function r(e){return/^https?:\/\//i.test(e.trim())}function i(e){let t=new URL(e),n=t.protocol.toLowerCase(),r=t.host.toLowerCase();return`${n}//${r}${t.pathname}${t.search}${t.hash}`}function a(t,n){if(r(t))return new URL(t);if(n)return new URL(t,n);throw Error(`${e} Received a relative request URL "${t}" but no intercept.origin(...) is set for this test. Call intercept.origin("https://api.example.com") in beforeAll/beforeEach or use an absolute URL.`)}const o=e=>Object.fromEntries(e.entries()),s=async e=>{try{return await e.clone().json()}catch{return}},c=e=>e.replace(/\/+/g,`/`).replace(/\/$/g,``).split(`/`).filter(Boolean);function l(e){if(e===`/*`)return{re:/^\/(.*)$/i,keys:[`_star`],isStar:!0};let t=[],n=c(e).map(e=>e.startsWith(`:`)?(t.push(e.slice(1)),`([^/]+)`):e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)).join(`/`),r=`^/${n}$`;return{re:new RegExp(r,`i`),keys:t,isStar:!1}}function u(e,t){let n=e.re.exec(t);if(!n)return null;let r={};for(let t=0;t<e.keys.length;t++){let i=e.keys[t];if(i===void 0)continue;let a=n[t+1]??``;try{r[i]=decodeURIComponent(a)}catch{r[i]=a}}return r}function d(e,t){if(typeof e==`function`){let n=e(t);return n===`warn`||n===`error`||n===`bypass`?n:`warn`}return e??`warn`}function f(t){let n=new URL(t);if(n.pathname!==`/`||n.search||n.hash)throw Error(`${e} Expected an origin without path/query/hash, e.g. "https://api.example.com". Got: "${t}"`);let r=`${n.protocol.toLowerCase()}//${n.host.toLowerCase()}`;if(r.endsWith(`//`))throw Error(`${e} Invalid origin: "${t}"`);return r}var p=class extends Response{constructor(e,t){super(e??null,t)}static json(e,t={}){let n=new Headers(t.headers);return n.has(`content-type`)||n.set(`content-type`,`application/json`),new Response(JSON.stringify(e),{...t,headers:n})}};export{p as HttpResponse,e as INTERCEPT_LOG_PREFIX,l as compilePattern,n as getDefaultOnUnhandledRequest,o as headersToObject,r as isAbsoluteUrl,u as matchPattern,i as normalizeAbsoluteUrl,d as resolveStrategy,a as toRequestUrl,s as tryJson,f as validateOrigin};
//# sourceMappingURL=response-tF45mQBS.js.map