All files / src/apiService ApiService.ts

100% Statements 15/15
100% Branches 4/4
100% Functions 6/6
100% Lines 13/13

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            2x     10x     2x 8x     9x 9x 36x 12x 8x   8x               2x 8x   2x  
import {IApiConfig} from "../model/IApiConfig";
import {IRequestPayload} from "../model/IRequestPayload";
import {IResponsePayload} from "../model/IResponsePayload";
 
type outboundFn = (payload: IResponsePayload) => void;
 
export class ApiService {
    private outboundFn: outboundFn;
 
    constructor(apiConfig: IApiConfig, private api: Record<string, Function>) {
    }
 
    private getOutBoundFn(): outboundFn {
        return this.outboundFn;
    }
 
    getInboundFn(): (payload: IRequestPayload) => void  {
        return (payload: IRequestPayload) => {
            const {propertyToCall, args, uuid} = payload;
            if(propertyToCall && uuid && (propertyToCall in this.api)) {
                const result = this.api[propertyToCall](...args);
 
                this.getOutBoundFn()({
                    uuid: uuid,
                    returnValue: result
                });
            }
        }
    }
 
    setOutboundFn(outboundFn: (payload: IResponsePayload) => void) {
        this.outboundFn = outboundFn;
    }
}