Estoy usando las funciones de la nube de Firebase (node.js) para mi aplicación y quiero crear un contenedor compartido para todos mis puntos finales que manejará los errores y tendrá algunos controles.
¿Es posible crear una función contenedora para las funciones de la nube y luego exponerla como un nuevo punto final?
Ejemplo:
// normal function that works const helloWorld = functions.https.onRequest((req, res) => { functions.logger.log("hello world!"); return res.end(); }) // the wrapper function that handles errors and shared logic const functionsWrapper = async ({allowedMethods, handler}) => functions.https.onRequest(async (req, res) => { try { if (allowedMethods) { if (!allowedMethods.includes(req.method.toLowerCase())) { return res.status(405).end(); } } const result = await handler; return res.json(result); } catch (error) { //handling errors here } } ); // the function I want to wrap and then expose as an endpoint const anotherFunc = functionsWrapper({ allowedMethods: ['get'], async handler() { return {message: 'I am inside functionsWrapper'} } }) // exposing the functions module.exports = { helloWorld, // helloWorld can be called and gets an endpoint anotherFunc // anotherFunc does not get an endpoint }
Creo que Firebase encuentra dónde se exportan functions.https.onRequest y luego lo expone como un punto final, pero ¿cómo puedo exponer 'anotherFunc'?
O en resumen cómo exponer:
const endpoint = () => functions.htpps.onRequest((req, res) => {})
¡Gracias!