import { platform } from '../Environment' ; import { MethodHandle , System_String , System_Array } from '../Platform/Platform' ; const httpClientAssembly = 'Microsoft.AspNetCore.Blazor' ; const httpClientNamespace = `${ httpClientAssembly }.Http` ; const httpClientTypeName = 'WebAssemblyHttpMessageHandler' ; let receiveResponseMethod ; let allocateArrayMethod ; export const internalFunctions = { sendAsync , } ; async function sendAsync ( id , body , jsonFetchArgs ) { let response ; let responseData ; const fetchOptions = JSON . parse ( platform . toJavaScriptString ( jsonFetchArgs ) ) ; const requestInit = Object . assign ( convertToRequestInit ( fetchOptions . requestInit ) , fetchOptions . requestInitOverrides ) ; if ( body ) { requestInit . body = platform . toUint8Array ( body ) ; } try { response = await fetch ( fetchOptions . requestUri , requestInit ) ; responseData = await response . arrayBuffer ( ) ; } catch ( ex ) { dispatchErrorResponse ( id , ex . toString ( ) ) ; return ; } dispatchSuccessResponse ( id , response , responseData ) ; } function convertToRequestInit ( blazorRequestInit ) { return { credentials : blazorRequestInit . credentials , method : blazorRequestInit . method , headers : blazorRequestInit . headers . map ( item => [ item . name , item . value ] ) } ; } function dispatchSuccessResponse ( id , response , responseData ) { const responseDescriptor = { statusCode : response . status , statusText : response . statusText , headers : [ ] , } ; response . headers . forEach ( ( value , name ) => { responseDescriptor . headers . push ( { name : name , value : value } ) ; } ) ; if ( ! allocateArrayMethod ) { allocateArrayMethod = platform . findMethod ( httpClientAssembly , httpClientNamespace , httpClientTypeName , 'AllocateArray' ) ; } const dotNetArray = platform . callMethod ( allocateArrayMethod , null , [ platform . toDotNetString ( responseData . byteLength . toString ( ) ) ] ) as System_Array < any > ; const array = platform . toUint8Array ( dotNetArray ) ; array . set ( new Uint8Array ( responseData ) ) ; dispatchResponse ( id , platform . toDotNetString ( JSON . stringify ( responseDescriptor ) ) , dotNetArray , null ) ; } function dispatchErrorResponse ( id , errorMessage ) { dispatchResponse ( id , null , null , platform . toDotNetString ( errorMessage ) ) ; } function dispatchResponse ( id , responseDescriptor , responseData , errorMessage ) { if ( ! receiveResponseMethod ) { receiveResponseMethod = platform . findMethod ( httpClientAssembly , httpClientNamespace , httpClientTypeName , 'ReceiveResponse' ) ; } platform . callMethod ( receiveResponseMethod , null , [ platform . toDotNetString ( id . toString ( ) ) , responseDescriptor , responseData , errorMessage , ] ) ; } interface FetchOptions { requestUri : string ; requestInit : BlazorRequestInit ; requestInitOverrides : RequestInit ; } interface BlazorRequestInit { credentials : string ; headers : Header [ ] ; method : string ; } interface ResponseDescriptor { statusCode : number ; statusText : string ; headers : Header [ ] ; } interface Header { name : string ; value : string ; }