import type { NetworkScenario , NetworkAfterEvent } from './types' ; import { realFetch } from './fetch' ; import { onRun } from './eventEmitter' ; export const NOOP = ( ) => { } ; function needStringify ( input ) { if ( input === null || input === undefined ) return false ; if ( typeof input === 'number' ) return true ; if ( Array . isArray ( input ) ) return true ; return Object . getPrototypeOf ( input ) . isPrototypeOf ( Object ) ; } export const networkScenarioToResponse = ( scenario ) : Response => { const data = needStringify ( scenario . response ) ? JSON . stringify ( scenario . response ) : scenario . response ; return new Response ( data , { status : scenario . status , headers : scenario . headers , } ) ; } ; export const passRequest = async ( partialEvent , { intercept , resolveResponse , } ) => { try { const response = await realFetch ( partialEvent . request ) ; if ( ! intercept ) { resolveResponse ( response ) ; return ; } const detail = { ... partialEvent , response : response . clone ( ) , pass : ( ) => resolveResponse ( response ) , } ; onRun ( detail ) ; } catch ( error ) { if ( ! intercept ) { partialEvent . reject ( error ) ; return ; } const errorDetail = { ... partialEvent , error , pass : ( ) => partialEvent . reject ( error ) , } ; onRun ( errorDetail ) ; } } ;