2022-04-28 10:57:52 +02:00
|
|
|
import fetch from 'make-fetch-happen';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { addonDefinitionSchema } from './addon-schema';
|
|
|
|
import { IUnleashConfig } from '../types/option';
|
|
|
|
import { Logger } from '../logger';
|
2021-11-12 13:15:51 +01:00
|
|
|
import { IAddonDefinition } from '../types/model';
|
|
|
|
import { IEvent } from '../types/events';
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
export default abstract class Addon {
|
|
|
|
logger: Logger;
|
2021-01-19 10:42:45 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
_name: string;
|
|
|
|
|
|
|
|
_definition: IAddonDefinition;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
definition: IAddonDefinition,
|
|
|
|
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
|
|
|
|
) {
|
2021-01-19 10:42:45 +01:00
|
|
|
this.logger = getLogger(`addon/${definition.name}`);
|
|
|
|
const { error } = addonDefinitionSchema.validate(definition);
|
|
|
|
if (error) {
|
|
|
|
this.logger.warn(
|
|
|
|
`Could not load addon provider ${definition.name}`,
|
|
|
|
error,
|
|
|
|
);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
this._name = definition.name;
|
|
|
|
this._definition = definition;
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
get name(): string {
|
2021-01-19 10:42:45 +01:00
|
|
|
return this._name;
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
get definition(): IAddonDefinition {
|
2021-01-19 10:42:45 +01:00
|
|
|
return this._definition;
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async fetchRetry(
|
|
|
|
url: string,
|
2023-01-11 09:13:22 +01:00
|
|
|
options: any = {},
|
2021-08-12 15:04:37 +02:00
|
|
|
retries: number = 1,
|
|
|
|
): Promise<Response> {
|
2021-02-28 22:40:04 +01:00
|
|
|
let res;
|
2021-02-26 13:46:56 +01:00
|
|
|
try {
|
2022-04-28 10:57:52 +02:00
|
|
|
res = await fetch(url, {
|
|
|
|
retry: {
|
|
|
|
retries,
|
|
|
|
},
|
|
|
|
...options,
|
|
|
|
});
|
2021-02-28 22:40:04 +01:00
|
|
|
return res;
|
2022-04-28 10:57:52 +02:00
|
|
|
} catch (e) {
|
2023-01-11 09:13:22 +01:00
|
|
|
const { method } = options;
|
|
|
|
this.logger.warn(
|
|
|
|
`Error querying ${url} with method ${
|
|
|
|
method || 'GET'
|
|
|
|
} status code ${e.code}`,
|
|
|
|
e,
|
|
|
|
);
|
2022-04-28 10:57:52 +02:00
|
|
|
res = { statusCode: e.code, ok: false };
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
2021-02-28 22:40:04 +01:00
|
|
|
return res;
|
2021-01-19 10:42:45 +01:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
abstract handleEvent(event: IEvent, parameters: any): Promise<void>;
|
2023-07-20 14:37:06 +02:00
|
|
|
|
|
|
|
destroy?(): void;
|
2021-08-12 15:04:37 +02:00
|
|
|
}
|