UNPKG

@omgovich/firebase-functions-rate-limiter

Version:

JS/TS library that allows you to set per - time, per - user or per - anything limits for calling Firebase cloud functions

63 lines (62 loc) 3.73 kB
import * as admin from "firebase-admin"; import { FirebaseFunctionsRateLimiterConfiguration } from "./FirebaseFunctionsRateLimiterConfiguration"; import { PersistenceProviderMock } from "./persistence/PersistenceProviderMock"; import { FirestoreEquivalent } from "./types/FirestoreEquivalent"; import { RealtimeDbEquivalent } from "./types/RealtimeDbEquivalent"; export declare class FirebaseFunctionsRateLimiter { static DEFAULT_QUALIFIER: string; static withFirestoreBackend(configuration: FirebaseFunctionsRateLimiterConfiguration, firestore: admin.firestore.Firestore | FirestoreEquivalent): FirebaseFunctionsRateLimiter; static withRealtimeDbBackend(configuration: FirebaseFunctionsRateLimiterConfiguration, realtimeDb: admin.database.Database | RealtimeDbEquivalent): FirebaseFunctionsRateLimiter; static mock(configuration?: FirebaseFunctionsRateLimiterConfiguration, persistenceProviderMock?: PersistenceProviderMock): FirebaseFunctionsRateLimiter; private configurationFull; private genericRateLimiter; private debugFn; private constructor(); /** * Checks if quota is exceeded. If not — records usage time in the backend database. * The method is deprecated as it was renamed to isQuotaExceededOrRecordUsage * * @param qualifier — a string that identifies the limited resource accessor (for example the user id) * @deprecated */ isQuotaExceeded(qualifier?: string): Promise<boolean>; /** * Checks if quota is exceeded. If not — records usage time in the backend database. * * @param qualifier — a string that identifies the limited resource accessor (for example the user id) * @deprecated */ isQuotaExceededOrRecordUsage(qualifier?: string): Promise<boolean>; /** * Checks if quota is exceeded. If not — records usage time in the backend database and then * is rejected with functions.https.HttpsError (this is the type of error that can be caught when * firebase function is called directly: see https://firebase.google.com/docs/functions/callable) * The method is deprecated as it was renamed to rejectOnQuotaExceededOrRecordUsage * * @param qualifier — a string that identifies the limited resource accessor (for example the user id) * @deprecated */ rejectOnQuotaExceeded(qualifier?: string): Promise<void>; /** * Checks if quota is exceeded. If not — records usage time in the backend database and then * is rejected with functions.https.HttpsError (this is the type of error that can be caught when * firebase function is called directly: see https://firebase.google.com/docs/functions/callable) * * @param qualifier (optional) — a string that identifies the limited resource accessor (for example the user id) * @param errorFactory (optional) — when errorFactory is provided, it is used to obtain * error that is thrown in case of exceeded limit. */ rejectOnQuotaExceededOrRecordUsage(qualifier?: string, errorFactory?: (config: FirebaseFunctionsRateLimiterConfiguration.ConfigurationFull) => Error): Promise<void>; /** * Checks if quota is exceeded. If not — DOES NOT RECORD USAGE. It only checks if limit was * previously exceeded or not. * @param qualifier — a string that identifies the limited resource accessor (for example the user id) */ isQuotaAlreadyExceeded(qualifier?: string): Promise<boolean>; /** * Returns this rate limiter configuration */ getConfiguration(): FirebaseFunctionsRateLimiterConfiguration.ConfigurationFull; private constructRejectionError; private constructDebugFn; }