1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/addons/webhook.js
Ivar Conradi Østhus 17c8fe7710 feat: Introduce addon framework
fixes: #587

Co-authored-by: Christopher Kolstad <chriswk@getunleash.ai>
2021-02-05 15:20:00 +01:00

40 lines
990 B
JavaScript

'use strict';
const Mustache = require('mustache');
const Addon = require('./addon');
const definition = require('./webhook-definition');
class Webhook extends Addon {
constructor(args) {
super(definition, args);
}
async handleEvent(event, parameters) {
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}`,
);
}
}
module.exports = Webhook;