Skip to content

A simple tool that generates declaration files for your Firebase Functions

Notifications You must be signed in to change notification settings

dutzi/firebase-functions-declarations

Repository files navigation

firebase-functions-declarations

This simple tool creates declaration files (.d.ts) for your Firebase Functions.

When calling a firebase function you have no guarantee for the functions return value. This tool tries solving that.

When using this tool, instead of writing:

const x = (await firebase.functions().httpsCallable('myFunction')(someData)).data

And not knowing what x is, you write:

import { myFunction } from './firebase-functions';

// ...

const x = await myFunction(someData);

And x will be typed to be whatever myFunction returns!

Important: Read the prerequisites.

Why?

So that in your app's code you can enjoy type safety when working with functions.

How it works

Using tsc (on functions/src/index.ts), this tool creates a declaration files for each one of your functions.

It then creates an index.ts, where per-each Firebase Function you declared, an exported function is created, that function uses firebase.functions().httpCallback to dispatch a call to the Firebase function and the function's return value type is set to be the return value type of the Firebase Function.

How to set it up

Inside your project's root folder, run:

yarn add firebase-functions-declarations

Then add the following script to your package.json's scripts:

  "scripts": {
    "create-functions-declarations": "createFunctionsDeclarations --output ./src/firebase-functions"
  }

Where ./src/firebase-functions is the path where you want the declarations and index files created.

Prerequisites

Because of several limitations, for this tool to work you have to declare your Firebase Function to be a module that export defaults the return value of functions.https.onCall and also exports a function named impl that is the function passed to functions.https.onCall.

For example:

// ... import statements

export async function impl(
  data: any,
  context: functions.https.CallableContext,
) {
  // firebase function body...
}

export default functions.https.onCall(impl);

About

A simple tool that generates declaration files for your Firebase Functions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published