import { window } from 'vscode' ; import { SiteClient } from 'vscode-azureappservice' ; import { IActionContext , parseError } from 'vscode-azureextensionui' ; import { ext } from '../extensionVariables' ; import { FuncVersion } from '../FuncVersion' ; import { localize } from '../localize' ; import { FunctionTreeItemBase } from '../tree/FunctionTreeItemBase' ; import { FuncHostRequest } from '../tree/IProjectTreeItem' ; import { RemoteFunctionTreeItem } from '../tree/remoteProject/RemoteFunctionTreeItem' ; import { nonNullValue } from '../utils/nonNull' ; import { requestUtils } from '../utils/requestUtils' ; export async function executeFunction ( context , node ? ) { context . telemetry . eventVersion = 2 ; if ( ! node ) { const noItemFoundErrorMessage = localize ( 'noFunctions' , 'No functions found.' ) ; node = await ext . tree . showTreeItemPicker < FunctionTreeItemBase > ( /Function;/i , { ... context , noItemFoundErrorMessage } ) ; } const client = node instanceof RemoteFunctionTreeItem ? await node . parent . parent . site . createClient ( context ) : undefined ; const triggerBindingType = node . triggerBindingType ; context . telemetry . properties . triggerBindingType = triggerBindingType ; let functionInput = '' ; if ( ! node . isTimerTrigger ) { const prompt = localize ( 'enterRequestBody' , 'Enter request body' ) ; let value ; if ( triggerBindingType ) { const version = await node . parent . parent . getVersion ( context ) ; const templateProvider = ext . templateProvider . get ( context ) ; value = await templateProvider . tryGetSampleData ( context , version , triggerBindingType ) ; if ( value ) { value = value . replace ( /[\r\n\t]/g , ' ' ) ; value = value . replace ( / +/g , ' ' ) ; } } const data = await context . ui . showInputBox ( { prompt , value , stepName : 'requestBody' } ) ; try { functionInput = < { } > JSON . parse ( data ) ; } catch { functionInput = data ; } } let triggerRequest ; let body ; if ( node . isHttpTrigger ) { triggerRequest = nonNullValue ( await node . getTriggerRequest ( context ) , 'triggerRequest' ) ; body = functionInput ; } else { triggerRequest = await node . parent . parent . getHostRequest ( context ) ; triggerRequest . url = `${ triggerRequest . url }/admin/functions/${ node . name }` ; body = { input : functionInput } ; } let responseText ; await node . runWithTemporaryDescription ( context , localize ( 'executing' , 'Executing...' ) , async ( ) => { const headers = { } ; if ( client ) { headers [ 'x-functions-key' ] = ( await client . listHostKeys ( ) ) . masterKey ; } try { responseText = ( await requestUtils . sendRequestWithExtTimeout ( context , { method : 'POST' , ... triggerRequest , headers , body } ) ) . bodyAsText ; } catch ( error ) { if ( ! client && parseError ( error ) . errorType === 'ECONNREFUSED' ) { context . errorHandling . suppressReportIssue = true ; throw new Error ( localize ( 'failedToConnect' , 'Failed to connect. Make sure your project is [running locally](https://aka.ms/AA76v2d).' ) ) ; } else { context . telemetry . maskEntireErrorMessage = true ; throw error ; } } } ) ; const message = responseText ? localize ( 'executedWithResponse' , 'Executed function "{0}". Response: "{1}"' , node . name , responseText ) : localize ( 'executed' , 'Executed function "{0}"' , node . name ) ; void window . showInformationMessage ( message ) ; }