'What is the best way to invoke a firebase https callable function from within another https callable function

I wanted to know the best practice for invoking a callable (or "onCall") Firebase function from another Firebase callable function, where each of the callable functions are endpoints

I've seen workarounds that use a common Node function that both Firebase functions would invoke, but I want to avoid this.

I am specifically invoking another callable function within the first callable function so that I can monitor each function separately.

I am using the onCall http functions rather than the onRequest http functions to avoid testing and writing middleware.

The method I am using right now is as follows:

// first function invoked
export const firstFunction = https.onCall( async (data) => {
    msg = data.message;
    const firebaseConfig = {
       apiKey: "...",
       authDomain: "...",
       projectId: "...",
       storageBucket: "...",
       messagingSenderId: "...",
       appId: "...",
       measurementId: "...",
    };
   
    const app = initializeApp(firebaseConfig);
    const functions = getFunctions(app, "us-central1");
    connectFunctionsEmulator(functions, "localhost", 5001);
    
    const callingFunction = httpsCallable(functions, "sendEmail");
    const result = await callingFunction(msg);
    console.log( result );
}
// function called by the first function
export const sendEmail = https.onCall( async (data) => {
    sgMail.send( data );
    return { sucess: true };
}

Is there a way I can invoke the callable function without having to initialize the app and use the getFunctions() method?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source