function createMessageReceivedFunc()

in src/lambda/commands/invokeLambda.ts [106:187]


function createMessageReceivedFunc({
    fn,
    outputChannel,
    ...restParams
}: {
    // TODO: Consider passing lambdaClient: LambdaClient
    fn: LambdaFunctionNode // TODO: Replace w/ invokeParams: {functionArn: string} // or Lambda.Types.InvocationRequest
    outputChannel: vscode.OutputChannel
    onPostMessage(message: any): Thenable<boolean>
}) {
    const logger: Logger = getLogger()

    return async (message: CommandMessage) => {
        switch (message.command) {
            case 'promptForFile': {
                const fileLocations = await vscode.window.showOpenDialog({
                    openLabel: 'Open',
                })

                if (!fileLocations || fileLocations.length === 0) {
                    return undefined
                }

                try {
                    const fileContent = readFileSync(fileLocations[0].fsPath, { encoding: 'utf8' })
                    restParams.onPostMessage({
                        command: 'loadedSample',
                        sample: fileContent,
                        selectedFile: fileLocations[0].path,
                    })
                } catch (e) {
                    getLogger().error('readFileSync: Failed to read file at path %O', fileLocations[0].fsPath, e)
                    vscode.window.showErrorMessage((e as Error).message)
                }
                return
            }
            case 'sampleRequestSelected': {
                logger.info(`Requesting ${message.value}`)
                const sampleUrl = `${sampleRequestPath}${message.value}`

                const sample = (await new HttpResourceFetcher(sampleUrl, { showUrl: true }).get()) ?? ''

                logger.debug(`Retrieved: ${sample}`)

                restParams.onPostMessage({ command: 'loadedSample', sample: sample })

                return
            }
            case 'invokeLambda':
                logger.info('invoking lambda function with the following payload:')
                logger.info(String(message.value))

                outputChannel.show()
                outputChannel.appendLine('Loading response...')

                try {
                    if (!fn.configuration.FunctionArn) {
                        throw new Error(`Could not determine ARN for function ${fn.configuration.FunctionName}`)
                    }
                    const client: LambdaClient = globals.toolkitClientBuilder.createLambdaClient(fn.regionCode)
                    const funcResponse = await client.invoke(fn.configuration.FunctionArn, message.value)
                    const logs = funcResponse.LogResult ? Buffer.from(funcResponse.LogResult, 'base64').toString() : ''
                    const payload = funcResponse.Payload ? funcResponse.Payload : JSON.stringify({})

                    outputChannel.appendLine(`Invocation result for ${fn.configuration.FunctionArn}`)
                    outputChannel.appendLine('Logs:')
                    outputChannel.appendLine(logs)
                    outputChannel.appendLine('')
                    outputChannel.appendLine('Payload:')
                    outputChannel.appendLine(payload.toString())
                    outputChannel.appendLine('')
                } catch (e) {
                    const error = e as Error
                    outputChannel.appendLine(`There was an error invoking ${fn.configuration.FunctionArn}`)
                    outputChannel.appendLine(error.toString())
                    outputChannel.appendLine('')
                }

                return
        }
    }
}