import debug from 'debug' ; import { loggerTrace } from '../utils' ; import type { Route } from 'playwright' ; import type { Request , Response , DriverRequest , Interceptor , DriverErrorCode , ExtractDriverReqResOptions , } from '../types' ; const logger = debug ( 'teremock:driver:puppeteer:request' ) ; let requestsCounter = 0 ; export function extractPlaywrightRequest ( route , options ) { const playwrightRequest = route . request ( ) ; const request = { url : playwrightRequest . url ( ) , method : playwrightRequest . method ( ) , headers : playwrightRequest . headers ( ) , body : playwrightRequest . postData ( ) , resourceType : playwrightRequest . resourceType ( ) , id : requestsCounter ++ , timestamp : options . timestamp , order : options . order , } ; playwrightRequest . timestamp = options . timestamp ; playwrightRequest . teremockRequest = request ; playwrightRequest . __meta = { request } ; logger ( `got the request ${ request . url }, sending it to teremock core` ) ; return { request , abort : async ( errorCode ? ) => await route . abort ( errorCode ) , next : async ( interceptor ) => { logger ( `continue() call` ) ; playwrightRequest . __meta . interceptor = interceptor ; loggerTrace ( `${ request . url } ← request.continue()` ) ; await route . continue ( ) ; } , respond : async ( response , interceptor ) => { logger ( `respond() call` ) ; playwrightRequest . __meta . interceptor = interceptor ; loggerTrace ( `${ request . url } ← request.respond(...)` ) ; await route . fulfill ( response ) ; } , } ; }