@haensl/firebase
v1.0.0
Published
Utilities around firebase admin SDK.
Readme
@haensl/firebase
Useful utitlities for working with Firbase Admin SDK.
Installation
npm i --save @haensl/firebaseUsage
The module exports a set of utility functions useful in Firebase Admin SDK contexts
getUserIdToken()- generate a user JWT.init()- initialize the Firebase app.isDuplicateEmailError()- check for duplicate email error.isNotFoundError()- check for not found error.isInvalidDeviceTokenError()- check for invalid device token error.message()- compile a Firebase Cloud Message
getUserIdToken: async (userId: string) => string
Returns a promise that resolves with a Firebase ID Bearer token (JWT) with subject set to the given userId.
init: () => void
Initializes the Firebase app and SDK and throws if the initialization fails.
isDuplicateEmailError: (Error) => boolean
Checks whether the given error is a duplicate sign up e-mail error.
isInvalidDeviceTokenError: (Error) => boolean
Checks whether the given error relates to an invalid device token.
isNotFoundError: (Error) => boolean
Checks whether the given error is a user not found error.
message: ({ category: string, data: object, body: string, locArgs: [string], locKey: string, tokens: [string], title: string, titleLocKey: string, titleLocArgs: [string] }) => object
Compiles a Firebae push notification from the given input.
Parameters
category: string
The push notification category.
data: object
Data payload to send with the notification.
body: string
Notificaion body. Expects localized, i.e. user-facing string. Prefe locKey if possible.
locKey: string
The localized string key to set as the message body.
locArgs: [string]
Formatting arguments to locKey.
tokens: [string]
An array of Firebase device tokens to send the notification to.
title: string
Notification title. Expects localized, i.e. user-facing string. Prefer titleLocKey if possible.
titleLocKey: string
The localized string key to set as the message title.
titleLocArgs: [string]
Formatting arguments to locKey.
Returns
The message compiled for handing to Firebase Messaging APIs.
Example
const firebase = require('@haensl/firebase');
const { getMessaging } = require('firebase-admin/messaging');
const sendMessage = async (user) => {
firebase.init();
const message = firebase.message({
category: 'yo-mama-jokes',
data: {
payload: 'too large to handle'
},
tokens: [
'user-device-token'
],
titleLocKey: 'YoMamaSoPhat'
});
await getMessaging().sendMulticast(message);
};
