All files / src/Connector ABaseConnector.ts

94.73% Statements 18/19
60% Branches 3/5
100% Functions 5/5
94.73% Lines 18/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 5616x 16x   16x   16x   16x   192x 192x           12x             12x 12x 12x       12x         4x       12x 12x 12x   12x         12x                
import AConnector from '@orchesty/nodejs-sdk/dist/lib/Connector/AConnector';
import { HttpMethods } from '@orchesty/nodejs-sdk/dist/lib/Transport/HttpMethods';
import ProcessDto from '@orchesty/nodejs-sdk/dist/lib/Utils/ProcessDto';
import FormData from 'form-data';
 
export const SUCCESS = 'SUCCESS';
 
export default abstract class ABaseConnector<T, K> extends AConnector {
 
    public constructor(private readonly useInForm = false) {
        super();
    }
 
    protected abstract getMethod(): string;
 
    public async processAction(dto: ProcessDto<T>): Promise<ProcessDto<K>> {
        const req = await this.getApplication().getRequestDto(
            dto,
            await this.getApplicationInstallFromProcess(dto, this.useInForm ? null : true),
            HttpMethods.POST,
            undefined,
            this.prepareBody(this.getMethod(), await this.getParameters(dto)),
        );
        const resp = await this.getSender().send<IResponse>(req, [200]);
        const { status, ...jsonBody } = resp.getJsonBody();
        Iif (status !== SUCCESS) {
            throw new Error(`Request failed. Reason: ${resp.getBody()}`);
        }
 
        return this.processOutputData(dto, jsonBody);
    }
 
    // eslint-disable-next-line @typescript-eslint/require-await
    protected async getParameters(_dto: ProcessDto<T>): Promise<object> {
        return [];
    }
 
    protected prepareBody(method: string, parameters: object): FormData {
        const body = new FormData();
        body.append('method', method);
        body.append('parameters', JSON.stringify(parameters));
 
        return body;
    }
 
    // eslint-disable-next-line @typescript-eslint/require-await
    protected async processOutputData(dto: ProcessDto<T>, jsonBody: object): Promise<ProcessDto<K>> {
        return dto.setNewJsonData(jsonBody as K);
    }
 
}
 
export interface IResponse {
    status: string;
}