All files / client/functions firebaseFunctions.ts

100% Statements 19/19
100% Branches 4/4
100% Functions 5/5
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 511x 1x   1x   1x   1x           4x             4x       1x           4x 4x 4x 4x     1x               16x 4x 12x 4x 12x    
import { FirebaseFunction, IFirebaseFunction } from './FirebaseFunction';
import { FirebaseRequest, IFirebaseRequest } from './FirebaseRequest';
import type { FirebaseFunctions, RunnableFirebaseFunctions } from './FirebaseFunctionsType';
import { mapRecord } from '@stevenkellner/typescript-common-functionality';
import type { SupportedRegion } from 'firebase-functions/options';
import { type Functions as _Functions, getFunctions } from 'firebase/functions';
 
export class FirebaseFunctionBuilder {
 
    public function<Parameters, ReturnType>(
        // eslint-disable-next-line @typescript-eslint/naming-convention
        Constructor: IFirebaseFunction.Constructor<Parameters, ReturnType>
    ): IFirebaseFunction.ConstructorWrapper<Parameters, ReturnType> {
        return new IFirebaseFunction.ConstructorWrapper<Parameters, ReturnType>(Constructor);
    }
 
    public request<Parameters, ReturnType>(
        // eslint-disable-next-line @typescript-eslint/naming-convention
        Constructor: IFirebaseRequest.Constructor<Parameters, ReturnType>
    ): IFirebaseRequest.ConstructorWrapper<Parameters, ReturnType> {
        return new IFirebaseRequest.ConstructorWrapper<Parameters, ReturnType>(Constructor);
    }
}
 
export function createFirebaseFunctions<Functions extends FirebaseFunctions>(
    requestBaseUrl: string,
    region: SupportedRegion,
    macKey: Uint8Array,
    create: (builder: FirebaseFunctionBuilder) => Functions
): RunnableFirebaseFunctions<Functions> {
    const builder = new FirebaseFunctionBuilder();
    const firebaseFunctions = create(builder);
    const functions = getFunctions(undefined, region);
    return _create(functions, firebaseFunctions, requestBaseUrl, region, macKey, '');
}
 
export function _create<Functions extends FirebaseFunctions>(
    functions: _Functions,
    firebaseFunctions: Functions,
    requestBaseUrl: string,
    region: SupportedRegion,
    macKey: Uint8Array,
    name: string
): RunnableFirebaseFunctions<Functions> {
    if (firebaseFunctions instanceof IFirebaseFunction.ConstructorWrapper)
        return FirebaseFunction.create(firebaseFunctions.Constructor, functions, name, macKey) as RunnableFirebaseFunctions<Functions>;
    if (firebaseFunctions instanceof IFirebaseRequest.ConstructorWrapper)
        return FirebaseRequest.create(firebaseFunctions.Constructor, requestBaseUrl, region, name, macKey) as RunnableFirebaseFunctions<Functions>;
    return mapRecord(firebaseFunctions as Record<string, FirebaseFunctions>, (firebaseFunctions, key) => _create(functions, firebaseFunctions, requestBaseUrl, region, macKey, name === '' ? key : `${name}-${key}`)) as RunnableFirebaseFunctions<Functions>;
}