functions v1 thin wrapper for append middlewares
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!"
})
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"
}))
For functions running at least once
, use firestore to achieve exactly once
.
functions.use(idempotenceGuarantor({
firestoreCollectionName: "events" // default
}))
Report an error before timeout.
functions.use(timeoutLogger({
timing: (timeout) => timeout * 0.9 // default
}))