Skip to content

Latest commit

 

History

History
64 lines (46 loc) · 1.71 KB

README.md

File metadata and controls

64 lines (46 loc) · 1.71 KB

typesafe-functions

npm version

A library of typescript interfaces that extend existing firebase functions classes, adding type safety and a better autocomplete experience.

Depends on typesafe-node-firestore.

Installation

yarn add typesafe-functions --dev

Usage

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
});

License

typesafe-functions is MIT licensed.