mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-06 00:07:44 +01:00
5a3bb1ffc3
Lots of work here, mostly because I didn't want to turn off the `noImplicitAnyLet` lint. This PR tries its best to type all the untyped lets biome complained about (Don't ask me how many hours that took or how many lints that was >200...), which in the future will force test authors to actually type their global variables setup in `beforeAll`. --------- Co-authored-by: Gastón Fournier <gaston@getunleash.io>
61 lines
1.8 KiB
TypeScript
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: string | undefined;
|
|
|
|
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}`,
|
|
);
|
|
}
|
|
}
|