1.0.5 • Published 4 years ago

firebase-functions-declarations v1.0.5

Weekly downloads
1
License
ISC
Repository
-
Last release
4 years ago

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 and note that the declarations only include the function's return value. Not the data passed to it.

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

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago