Skip to content

ishowta/firebase-functions-middleware

Repository files navigation

firebase functions middleware

functions v1 thin wrapper for append middlewares

how to use

import {
    Functions,
    parameterLogger,
    timeoutLogger,
    idempotenceGuarantor
} from 'firebase-functions-middleware'
import { getFirestore, initializeFirestore } from 'firebase-admin/firestore';
import { getApps, initializeApp } from 'firebase-admin/app';

if (getApps().length === 0) {
    const firebase = initializeApp();
    initializeFirestore(firebase);
}

const app = new Functions()
app.use(parameterLogger())
app.use(idempotenceGuarantor(getFirestore))
app.use(timeoutLogger())
app.use(({ functionType, options, parameters, next }) => {
    switch (functionType) {
        case 'https.onCall': {
            const [data, context] = parameters;
            // ...
            return next(data, context);
        }
        default:
            return next(...parameters);
    }
});
app.useDeployment(({ options }) => ({
    // for reduce cold start latency
    memory: '1GB',
    ...options,
}));

export const functions = app.builder
import { functions } from '../functions'

export const helloWorld = functions().https.onCall(() => {
    return "Hello world!"
})

parameterLogger

Logging data, contexts and response.

Note:

  • Some data will be filtered
  • Sensitive data may be displayed
functions.use(parameterLogger({
    target: {
        data: true,
        contexts: true,
        response: true
    },
    level: "DEBUG"
}))

idempotenceGuarantor

For functions running at least once, use firestore to achieve exactly once.

functions.use(idempotenceGuarantor({
    firestoreCollectionName: "events" // default
}))

timeoutLogger

Report an error before timeout.

functions.use(timeoutLogger({
    timing: (timeout) => timeout * 0.9 // default
}))