1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/addons/webhook.ts
Christopher Kolstad 186fda1657
task: Add customHeaders as possible parameter. (#4139)
###What
Adds an optional sensitive parameter for customHeaders to all current
addons. It is sensitive because the user might be including api key headers.
2023-07-05 09:42:17 +02:00

61 lines
1.8 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;
authorization?: string;
customHeaders?: 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, authorization, customHeaders } =
parameters;
const context = {
event,
};
let body;
if (typeof bodyTemplate === 'string' && bodyTemplate.length > 1) {
body = Mustache.render(bodyTemplate, context);
} else {
body = JSON.stringify(event);
}
let extraHeaders = {};
if (typeof customHeaders === 'string' && customHeaders.length > 1) {
try {
extraHeaders = JSON.parse(customHeaders);
} catch (e) {
this.logger.warn(
`Could not parse the json in the customHeaders parameter. [${customHeaders}]`,
);
}
}
const requestOpts = {
method: 'POST',
headers: {
'Content-Type': contentType || 'application/json',
Authorization: authorization || undefined,
...extraHeaders,
},
body,
};
const res = await this.fetchRetry(url, requestOpts);
this.logger.info(
`Handled event "${event.type}". Status code: ${res.status}`,
);
}
}