Skip to content

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

License

Notifications You must be signed in to change notification settings

ya-s-u/typesafe-functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks