import debug from "debug" ; import { typeUtils } from "unmock-core" ; import { ISerializedRequest } from "unmock-core/dist/interfaces" ; import URLParse = require ( "url-parse" ) ; const debugLog = debug ( "unmock:fetch-mitm" ) ; export default ( url , methodInUpperOrLowercase , headers , body ? ) : ISerializedRequest => { const method = methodInUpperOrLowercase . toLowerCase ( ) ; debugLog ( `Serializing request to: ${ method } ${ url }` ) ; if ( ! typeUtils . isRESTMethod ( method ) ) { throw new Error ( `Unknown method: ${ method }` ) ; } const parsedUrl = new URLParse ( url , true ) ; const protocolWithoutColon = parsedUrl . protocol . replace ( ":" , "" ) ; if ( ! typeUtils . isKnownProtocol ( protocolWithoutColon ) ) { throw new Error ( `Unknown protocol: ${ protocolWithoutColon }` ) ; } const useableBody = typeof body === "string" ? body : undefined ; let maybeJson ; try { if ( useableBody ) { maybeJson = JSON . parse ( useableBody ) ; } } catch { } const path = "/" + url . replace ( "https://" , "" ) . replace ( "http://" , "" ) . split ( "/" ) . slice ( 1 ) . join ( "/" ) ; const req = { body : useableBody , bodyAsJson : maybeJson , method , headers : Object . entries ( headers ) . map ( ( [ a , b ] ) => ( { [ a . toLowerCase ( ) ] : b } ) ) . reduce ( ( a , b ) => ( { ... a , ... b } ) , { } ) , host : parsedUrl . host , path , pathname : parsedUrl . pathname , query : parsedUrl . query , protocol : protocolWithoutColon , } ; debugLog ( `Serialized request: ${ JSON . stringify ( req ) }` ) ; return req ; } ;