A library of typescript interfaces that extend existing firebase functions classes, adding type safety and a better autocomplete experience.
Depends on typesafe-node-firestore.
yarn add typesafe-functions --dev
You most likely want to import TypedDocumentBuilder
and define functions document.
import * as functions from 'firebase-functions';
import { TypedDocumentBuilder } from 'typesafe-functions';
interface Author {
penName: string;
shortBiography: string;
posts: string[];
};
interface Params {
authorId: string;
};
const document = functions.firestore.document('authors/{authorId}') as TypedDocumentBuilder<Author, Params>;
And trigger your typesafe firestore events.
document.onWrite((change, context) => {
console.log(change.before.data()) //=> Author | undefined
console.log(change.after.data()) //=> Author | undefined
console.log(context.params.authorId) //=> string
});
document.onChange((change, context) => {
console.log(change.before.data()) //=> Author | undefined
console.log(change.after.data()) //=> Author | undefined
console.log(context.params.authorId) //=> string
});
document.onWrite((snapshot, context) => {
console.log(snapshot.data()) //=> Author
console.log(context.params.authorId) //=> string
});
document.onDelete((snapshot, context) => {
console.log(snapshot.data()) //=> Author
console.log(context.params.authorId) //=> string
});
typesafe-functions is MIT licensed.