1.4.133 • Published 6 months ago

@stevenkellner/firebase-function v1.4.133

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

Firebase Function

Overview

A TypeScript library designed to simplify the creation and deployment of Firebase functions. It provides a structured and type-safe way to define Firebase functions, requests, and schedules.

Table of Contents

Features

  • TypeScript Support: Leverage the power of TypeScript for type safety and better development experience.
  • Firebase Functions: Easily create and manage Firebase functions.
  • Firebase Requests: Define and handle Firebase HTTPS requests.
  • Firebase Schedules: Schedule and manage Firebase scheduled functions.

Installation

To install the package, use npm:

npm install @stevenkellner/firebase-function

Usage

Creating Firease Function / Request / Schedule

To create a Firebase function, request, or schedule, use the respective builder methods provided by the library. Here is an example:

import { FirebaseFunction, FirebaseRequest, FirebaseSchedule } from '@stevenkellner/firebase-function/admin';
import type { ITypeBuilder } from '@stevenkellner/typescript-common-functionality';

export class MyFirebaseFunction extends FirebaseFunction<MyParameters, MyReturnType> {

    public parametersBuilder: ITypeBuilder<Flatten<MyParameters>, MyParameters> = // Type builder to build parameters

    public constructor(userId: string | null) {
        super();
    }

    public async execute(parameters: MyParameters): Promise<MyReturnType> {
        // Function execution logic here
    }
}

export class MyFirebaseRequest extends FirebaseRequest<MyParameters, MyReturnType> {

    public parametersBuilder: ITypeBuilder<Flatten<MyParameters>, MyParameters> = // Type builder to build parameters

    public constructor() {
        super();
    }

    public async execute(parameters: MyParameters): Promise<MyReturnType> {
        // Request execution logic here
    }
}

export class MyFirebaseSchedule extends FirebaseSchedule {

    public constructor() {
        super();
    }

    public async execute(): Promise<void> {
        // Schedule execution login here
    }
}

Creating Firebase Functions Strukture

To create Firebase functions, use the createFirebaseFunctions method:

import { createFirebaseFunctions } from '@stevenkellner/firebase-function/admin';

const firebaseFunctions = createFirebaseFunctions(builder => ({
    function: builder.function(MyFirebaseFunction),
    requests: {
        request: builder.request(MyFirebaseRequest)
    },
    schedule: builder.schedule(MyFirebaseSchedule, "0 0 1 1 *", "europe/berlin")
}));

Providing Firebase Functions

To provide Firebase functions, use the provideFirebaseFunctions method:

import { provideFirebaseFunctions } from '@stevenkellner/firebase-function/admin';

const macKey = // My private message authenication key
const regions = ['europe-west1'];

const runnableFunctions = provideFirebaseFunctions(firebaseFunctions, macKey, regions);

Call Function on Client

To call a Firebase function on the client side, use the httpsCallable method from the Firebase SDK. Here is an example:

import type { ITypeBuilder } from '@stevenkellner/typescript-common-functionality';
import { createFirebaseFunctions, IFirebaseFunction, IFirebaseRequest } from '../../client';
import { initializeApp } from 'firebase/app';
import { getFunctions } from 'firebase/functions';

export class MyFirebaseFunction extends IFirebaseFunction<MyParameters, MyReturnType> {

    public readonly returnTypeBuilder: ITypeBuilder<Flatten<MyReturnType>, MyReturnType> = // Type builder to build return type
}

export class MyFirebaseRequest extends IFirebaseRequest<MyParameters, MyReturnType> {

    public readonly returnTypeBuilder: ITypeBuilder<Flatten<MyReturnType>, MyReturnType> = // Type builder to build return type
}

initializeApp({
    apiKey: // Your FIREBASE_API_KEY,
    authDomain: // Your FIREBASE_AUTH_DOMAIN,
    databaseURL: // Your FIREBASE_DATABASE_URL,
    projectId: // Your FIREBASE_PROJECT_ID,
    storageBucket: // Your FIREBASE_STORAGE_BUCKET,
    messagingSenderId: // Your FIREBASE_MESSAGING_SENDER_ID,
    appId: // Your FIREBASE_APP_ID
});
const functions = getFunctions(undefined, 'europe-west1');

const macKey = // My private message authenication key
const firebaseFunctions = createFirebaseFunctions(/* Your base url to firebase functions */, 'europe-west1', macKey, builder => ({
    function: builder.function(MyFirebaseFunction),
    requests: {
        request: builder.request(MyFirebaseRequest)
    }
}));

Testing

To run tests, use the following command:

npm run test

To generate a coverage report, use:

npm run test:coverage

and view it with

npm run coverage:report

Continuous Integration

Build, Test and Lint

Build, test and lint Node.js Package

On each push and pull request, the CI runs the build, test and lint script to verify the package.

Coverage Report

Run Test with Coverage Report and upload it to Github Pages

The coverage report is generated automatically by the CI on each push and pull request. You can view the latest coverage report here.

Publish to npm

Publish Node.js Package

The library is published automatically as a npm package and a github release is created by the CI on each new release tag. The npm package can be found here, you can install it with npm install @stevenkellner/firebase-function.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

1.4.133

6 months ago

1.4.132

6 months ago

1.4.131

6 months ago

1.4.130

6 months ago

1.4.129

6 months ago

1.4.128

6 months ago

1.4.127

6 months ago

1.4.126

6 months ago

1.4.125

6 months ago

1.4.124

6 months ago

1.4.123

6 months ago

1.4.121

6 months ago