mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-28 00:06:53 +01:00
bb58a516bd
https://linear.app/unleash/issue/2-1237/explore-slack-app-addon-scalability-and-limitations Relevant document: https://linear.app/unleash/document/894e12b7-802c-4bc5-8c22-75af0e66fa4b - Implements 30s cache layer for Slack channels; - Adds error logging; - Adds respective tests; - Slight refactors and improvements for overall robustness; --------- Co-authored-by: Gastón Fournier <gaston@getunleash.io>
72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
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);
|
|
if (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: any = {},
|
|
retries: number = 1,
|
|
): Promise<Response> {
|
|
let res;
|
|
try {
|
|
res = await fetch(url, {
|
|
retry: {
|
|
retries,
|
|
},
|
|
...options,
|
|
});
|
|
return res;
|
|
} catch (e) {
|
|
const { method } = options;
|
|
this.logger.warn(
|
|
`Error querying ${url} with method ${
|
|
method || 'GET'
|
|
} status code ${e.code}`,
|
|
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>;
|
|
|
|
destroy?(): void;
|
|
}
|