import { safeParse } from "secure-json-parse" ; import { HeadersShorthands } from "../context" ; param request export function proxyRequest ( request ) { let _arrayBuffer ; let _formData ; let _json ; let _text ; let _get = ( name ) => request . headers . get ( name ) ; let _set = ( name , value ) => request . headers . set ( name , value ) ; let _has = ( name ) => request . headers . has ( name ) ; return new Proxy ( request , { get ( target , property , receiver ) { if ( property === "arrayBuffer" ) { if ( _arrayBuffer !== undefined ) { return ( ) => _arrayBuffer ; } return ( ) => _arrayBuffer = target . arrayBuffer ( ) ; } else if ( property === "formData" ) { if ( _formData !== undefined ) { return ( ) => _formData ; } return ( ) => _formData = target . formData ( ) ; } else if ( property === "json" ) { if ( _json !== undefined ) { return ( ) => _json ; } return ( reviver ? ) => _json = target . text ( ) . then ( ( text ) => safeParse ( text , reviver ) ) ; } else if ( property === "text" ) { if ( _text !== undefined ) { return ( ) => _text ; } return ( ) => _text = target . text ( ) ; } else if ( property === "get" ) { return _get ; } else if ( property === "set" ) { return _set ; } else if ( property === "has" ) { return _has ; } return ( target as any ) [ property ] ; } , } ) as Request & HeadersShorthands ; }