import {IApiConfig} from "../model/IApiConfig"; import {IRequestPayload} from "../model/IRequestPayload"; import {IResponsePayload, IResponsePayloadEnum} from "../model/IResponsePayload"; type outboundFn = (payload: IResponsePayload) => void; export class ApiService { private outboundFn: outboundFn; private name; constructor(apiConfig: IApiConfig, private api: Record) { this.name = apiConfig.name; } private getOutBoundFn(): outboundFn { return this.outboundFn; } private async callApiProp(prop: string, args: unknown[]): Promise { return this.api[prop](...args); } getInboundFn(): (payload: IRequestPayload) => void { return async (payload: IRequestPayload) => { const {propertyToCall, args, uuid, name} = payload; if(name === this.name) { let responsePayload: IResponsePayload = { uuid, type: IResponsePayloadEnum.RESOLVED, returnValue: undefined }; try { await this.callApiProp(propertyToCall, args).then((resolvedValue: unknown) => { responsePayload.type = IResponsePayloadEnum.RESOLVED; responsePayload.returnValue = resolvedValue; }).catch((rejectedValue: unknown) => { responsePayload.type = IResponsePayloadEnum.REJECTED; responsePayload.returnValue = rejectedValue; }); } catch (e) { const typedError = e as Error; responsePayload.type = IResponsePayloadEnum.ERROR; responsePayload.returnValue = typedError?.message; } this.getOutBoundFn()(responsePayload); } } } setOutboundFn(outboundFn: (payload: IResponsePayload) => void) { this.outboundFn = outboundFn; } }