1.0.0 • Published 3 years ago

typesafe-functions v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

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.