1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/src/lib/addons/webhook.ts
Ivar Conradi Østhus d8478dd928
feat: clean up events (#1089)
Co-authored-by: Christopher Kolstad <chriswk@getunleash.ai>
2021-11-12 13:15:51 +01:00

44 lines
1.2 KiB
TypeScript

import Mustache from 'mustache';
import Addon from './addon';
import definition from './webhook-definition';
import { LogProvider } from '../logger';
import { IEvent } from '../types/events';
interface IParameters {
url: string;
bodyTemplate?: string;
contentType?: string;
}
export default class Webhook extends Addon {
constructor(args: { getLogger: LogProvider }) {
super(definition, args);
}
async handleEvent(event: IEvent, parameters: IParameters): Promise<void> {
const { url, bodyTemplate, contentType } = parameters;
const context = {
event,
};
let body;
if (typeof bodyTemplate === 'string' && bodyTemplate.length > 1) {
body = Mustache.render(bodyTemplate, context);
} else {
body = JSON.stringify(event);
}
const requestOpts = {
method: 'POST',
headers: { 'Content-Type': contentType || 'application/json' },
body,
};
const res = await this.fetchRetry(url, requestOpts);
this.logger.info(
`Handled event "${event.type}". Status code: ${res.status}`,
);
}
}