All files / src/lib/addons addon.ts

88.24% Statements 15/17
66.67% Branches 2/3
100% Functions 4/4
88.24% Lines 15/17

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 6363x 63x           63x                     594x 594x 594x             594x 594x       588x       646x                 4x 4x           3x   1x   1x            
import fetch from 'make-fetch-happen';
import { addonDefinitionSchema } from './addon-schema';
import { IUnleashConfig } from '../types/option';
import { Logger } from '../logger';
import { IAddonDefinition } from '../types/model';
import { IEvent } from '../types/events';
 
export default abstract class Addon {
    logger: Logger;
 
    _name: string;
 
    _definition: IAddonDefinition;
 
    constructor(
        definition: IAddonDefinition,
        { getLogger }: Pick<IUnleashConfig, 'getLogger'>,
    ) {
        this.logger = getLogger(`addon/${definition.name}`);
        const { error } = addonDefinitionSchema.validate(definition);
        Iif (error) {
            this.logger.warn(
                `Could not load addon provider ${definition.name}`,
                error,
            );
            throw error;
        }
        this._name = definition.name;
        this._definition = definition;
    }
 
    get name(): string {
        return this._name;
    }
 
    get definition(): IAddonDefinition {
        return this._definition;
    }
 
    async fetchRetry(
        url: string,
        options = {},
        retries: number = 1,
    ): Promise<Response> {
        let res;
        try {
            res = await fetch(url, {
                retry: {
                    retries,
                },
                ...options,
            });
            return res;
        } catch (e) {
            res = { statusCode: e.code, ok: false };
        }
        return res;
    }
 
    // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
    abstract handleEvent(event: IEvent, parameters: any): Promise<void>;
}