All files / client/functions FirebaseFunction.ts

100% Statements 19/19
100% Branches 0/0
100% Functions 3/3
100% Lines 19/19

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 731x 1x 1x 1x               1x   4x                                     1x     4x 4x 4x 4x       2x 2x 2x 2x       2x 2x       2x 2x                                  
import { Result, Flattable, type ITypeBuilder } from '@stevenkellner/typescript-common-functionality';
import { type HttpsCallable, httpsCallable, type Functions } from 'firebase/functions';
import { createMacTag } from './createMacTag';
import { FunctionsError } from '../../shared/functions';
 
type FunctionCallable<Parameters, ReturnType> = HttpsCallable<{
    macTag: string;
    parameters: Flattable.Flatten<Parameters>;
}, Result.Flatten<ReturnType, FunctionsError>>;
 
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
export abstract class IFirebaseFunction<Parameters, ReturnType> {
 
    protected parameters: Parameters = null as unknown as Parameters;
 
    public abstract returnTypeBuilder: ITypeBuilder<Flattable.Flatten<ReturnType>, ReturnType>;
}
 
// istanbul ignore next
export namespace IFirebaseFunction {
 
    export type Constructor<Parameters, ReturnType> = new () => IFirebaseFunction<Parameters, ReturnType>;
 
    export class ConstructorWrapper<Parameters, ReturnType> {
 
        public constructor(
            // eslint-disable-next-line @typescript-eslint/naming-convention
            public readonly Constructor: IFirebaseFunction.Constructor<Parameters, ReturnType>
        ) {}
    }
}
 
export class FirebaseFunction<Parameters, ReturnType> {
 
    public constructor(
        private readonly functions: Functions,
        private readonly name: string,
        private readonly macKey: Uint8Array,
        private readonly returnTypeBuilder: ITypeBuilder<Flattable.Flatten<ReturnType>, ReturnType>
    ) {}
 
    public async executeWithResult(parameters: Parameters): Promise<Result<ReturnType, FunctionsError>> {
        const _function: FunctionCallable<Parameters, ReturnType> = httpsCallable(this.functions, this.name);
        const flattenParameters = Flattable.flatten(parameters);
        const macTag = createMacTag(flattenParameters, this.macKey);
        const flattenResult = await _function({
            macTag: macTag,
            parameters: flattenParameters
        });
        const resultBuilder = Result.builder(this.returnTypeBuilder, FunctionsError.builder);
        return resultBuilder.build(flattenResult.data);
    }
 
    public async execute(parameters: Parameters): Promise<ReturnType> {
        const result = await this.executeWithResult(parameters);
        return result.get();
    }
}
 
// istanbul ignore next
export namespace FirebaseFunction {
 
    export function create<Parameters, ReturnType>(
        // eslint-disable-next-line @typescript-eslint/naming-convention
        IFirebaseFunction: IFirebaseFunction.Constructor<Parameters, ReturnType>,
        functions: Functions,
        name: string,
        macKey: Uint8Array
    ): FirebaseFunction<Parameters, ReturnType> {
        return new FirebaseFunction(functions, name, macKey, new IFirebaseFunction().returnTypeBuilder);
    }
}