import { useAsyncRetry } from 'react-use' ; import { useApi , errorApiRef , configApiRef , identityApiRef } from '@backstage/core-plugin-api' ; import { LambdaData } from '../types' ; import { awsLambdaApiRef } from '../api' ; import { useCallback } from 'react' ; export function useLambda ( { lambdaName , region , } ) { const lambdaApi = useApi ( awsLambdaApiRef ) ; const errorApi = useApi ( errorApiRef ) ; const configApi = useApi ( configApiRef ) ; const identityApi = useApi ( identityApiRef ) ; const getFunctionByName = useCallback ( async ( ) => lambdaApi . getFunctionByName ( { backendUrl : configApi . getString ( 'backend.baseUrl' ) , awsRegion : region , functionName : lambdaName , token : identityApi ?. getIdToken ? await identityApi . getIdToken ( ) : undefined } ) , [ region , lambdaName ] ) ; const { loading , value : lambda , error , retry , } = useAsyncRetry < LambdaData | null > ( async ( ) => { try { const lambdaFunction = await getFunctionByName ( ) ; return lambdaFunction ; } catch ( err ) { if ( err ?. message === 'MissingBackendAwsAuthException' ) { errorApi . post ( new Error ( 'Please add aws auth backend plugin' ) ) ; return null ; } errorApi . post ( err ) ; return null ; } } , [ ] ) ; return [ { loading , lambda , error , retry , } , ] as const ; }