diff --git a/src/discord/src/index.ts b/src/discord/src/index.ts index 80576ed..4bd63e4 100644 --- a/src/discord/src/index.ts +++ b/src/discord/src/index.ts @@ -1,4 +1,5 @@ // Import Third-party Dependencies +import { MorphixOptions } from "@sigyn/morphix"; import { WebhookNotifierOptions, WebhookNotifier } from "@sigyn/notifiers"; // CONSTANTS @@ -13,15 +14,27 @@ const kEmbedColor = { info: 16777215 }; -class DiscordNotifier extends WebhookNotifier { - contentTemplateOptions() { +export interface DiscordEmbed { + title: string; + description: string; + color: number; +} + +export interface DiscordWebhookBodyFormat { + embeds: DiscordEmbed[]; + username: string; + avatar_url?: string; +} + +class DiscordNotifier extends WebhookNotifier { + contentTemplateOptions(): MorphixOptions { return { transform: ({ key, value }) => (key === "lokiUrl" ? value : `**${value === undefined ? "unknown" : value}**`), ignoreMissing: true }; } - async formatWebhookBody(): Promise { + async formatWebhookBody(): Promise { if (this.data.ruleConfig?.logql) { this.data.ruleConfig.logql = this.#formatLogQL(this.data.ruleConfig.logql); } diff --git a/src/notifiers/docs/usage.md b/src/notifiers/docs/usage.md index b74100d..65be9bf 100644 --- a/src/notifiers/docs/usage.md +++ b/src/notifiers/docs/usage.md @@ -46,31 +46,20 @@ export function execute( You can use `formatTitle()` & `formatContent()` to get title & content formatted with template data. Theses functions uses `@sigyn/morphix` and you can customise the options of boths: ```ts +import { MorphixOptions } from "@sigyn/morphix"; + +const kCustomTemplateOptions: MorphixOptions = { + transform: ({ value }) => (value === undefined ? "unknown" : value), + ignoreMissing: true +}; + class MyAwesomeWebhookNotifier extends WebhookNotifier { contentTemplateOptions() { - return { - transform: ({ value }) => (value === undefined ? "unknown" : value), - ignoreMissing: true - } + return kCustomTemplateOptions; } titleTemplateOptions() { - return { - transform: ({ value }) => (value === undefined ? "unknown" : value), - ignoreMissing: true - } - } - - async formatWebhookBody(): Promise { - const [title, content] = await Promise.all([ - this.formatTitle(), - this.formatContent() - ]); - - return { - title, - content: content.join("\n") - } + return kCustomTemplateOptions; } } ``` @@ -78,6 +67,8 @@ class MyAwesomeWebhookNotifier extends WebhookNotifier { > [!NOTE] > The `contentTemplateOptions` & `titleTemplateOptions` above are the default values. +--- + By default, `showSeverityEmoji` is truthy: this option add an emoji before the title depending the alert **severity**. ```ts @@ -89,35 +80,14 @@ const kSeverityEmoji = { }; ``` -You can do `this.showSeverityEmoji = false` to disable this behavior. - -```ts -async formatWebhook(): Promise { - this.showSeverityEmoji = false; - - const [title, content] = await Promise.all([ - this.formatTitle(), - this.formatContent() - ]); - - return { - title, - content: content.join("\n") - } -} -``` - -You can also disable it in the constructor +But you can disable it by providing the constructor options `showSeverityEmoji` to false. ```ts -class MyAwesomeWebhookNotifier extends WebhookNotifier { - // directly set the property to false - showSeverityEmoji = false; - - constructor(options: WebhookNotifierOptions) { - super(options); - // or - this.showSeverityEmoji = false; +class MyAwesomeWebhookNotifier extends WebhookNotifier { + constructor( + options: WebhookNotifierOptions + ) { + super({ ...options, showSeverityEmoji: false }); } } ``` @@ -132,8 +102,12 @@ You can see implementation examples with our notifiers: ```ts export interface WebhookNotifierOptions { webhookUrl: string; - data: WebhookData; + data: WebhookData;s template: SigynInitializedTemplate; + /** + * @default true + */ + showSeverityEmoji?: boolean; } export interface WebhookData { diff --git a/src/notifiers/src/webhook.ts b/src/notifiers/src/webhook.ts index cf4006a..bb53d96 100644 --- a/src/notifiers/src/webhook.ts +++ b/src/notifiers/src/webhook.ts @@ -3,7 +3,7 @@ import { SigynInitializedTemplate, NotifierFormattedSigynRule } from "@sigyn/config"; -import { morphix } from "@sigyn/morphix"; +import { morphix, MorphixOptions } from "@sigyn/morphix"; import * as httpie from "@myunisoft/httpie"; // CONSTANTS @@ -13,11 +13,19 @@ const kSeverityEmoji = { warning: "⚠️", info: "📢" }; +const kDefaultTemplateOptions: MorphixOptions = { + transform: ({ value }) => (value === undefined ? "unknown" : value), + ignoreMissing: true +}; export interface WebhookNotifierOptions { webhookUrl: string; data: WebhookData; template: SigynInitializedTemplate; + /** + * @default true + */ + showSeverityEmoji?: boolean; } export interface WebhookData { @@ -40,31 +48,25 @@ export class WebhookNotifier { webhookUrl: string; data: WebhookData; template: SigynInitializedTemplate; - showSeverityEmoji = true; - - #headers: httpie.RequestOptions["headers"]; - #defaultTemplateOptions = { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - transform: ({ value, key }) => (value === undefined ? "unknown" : value), - ignoreMissing: true - }; + showSeverityEmoji: boolean; - constructor(options: WebhookNotifierOptions) { - this.webhookUrl = options.webhookUrl; - this.data = JSON.parse(JSON.stringify((options.data))); - this.template = JSON.parse(JSON.stringify((options.template))); + constructor( + options: WebhookNotifierOptions + ) { + const { webhookUrl, data, template, showSeverityEmoji = true } = options; - this.#headers = { - "content-type": "application/json" - }; + this.webhookUrl = webhookUrl; + this.data = JSON.parse(JSON.stringify((data))); + this.template = JSON.parse(JSON.stringify((template))); + this.showSeverityEmoji = showSeverityEmoji; } - contentTemplateOptions() { - return this.#defaultTemplateOptions; + contentTemplateOptions(): MorphixOptions { + return kDefaultTemplateOptions; } - titleTemplateOptions() { - return this.#defaultTemplateOptions; + titleTemplateOptions(): MorphixOptions { + return kDefaultTemplateOptions; } formatTitle(): Promise { @@ -105,7 +107,9 @@ export class WebhookNotifier { ): Promise> { return httpie.post(this.webhookUrl, { body, - headers: this.#headers + headers: { + "content-type": "application/json" + } }); } } diff --git a/src/slack/src/index.ts b/src/slack/src/index.ts index 362d3ac..fd9c745 100644 --- a/src/slack/src/index.ts +++ b/src/slack/src/index.ts @@ -1,4 +1,5 @@ // Import Third-party Dependencies +import { MorphixOptions } from "@sigyn/morphix"; import { WebhookNotifierOptions, WebhookNotifier } from "@sigyn/notifiers"; import { MessageAttachment } from "@slack/types"; @@ -15,15 +16,9 @@ export interface SlackWebhookBodyFormat { } class SlackNotifier extends WebhookNotifier { - contentTemplateOptions() { + contentTemplateOptions(): MorphixOptions { return { - transform: ({ value, key }) => { - if (key === "logql" || key === "lokiUrl") { - return value; - } - - return `*${value ?? "unknown"}*`; - }, + transform: ({ value, key }) => (key === "logql" || key === "lokiUrl" ? value : `*${value ?? "unknown"}*`), ignoreMissing: true }; } diff --git a/src/teams/src/index.ts b/src/teams/src/index.ts index 310147a..d99a42c 100644 --- a/src/teams/src/index.ts +++ b/src/teams/src/index.ts @@ -1,4 +1,5 @@ // Import Third-party Dependencies +import { MorphixOptions } from "@sigyn/morphix"; import { WebhookNotifierOptions, WebhookNotifier } from "@sigyn/notifiers"; export interface TeamsWebhookBodyFormat { @@ -7,7 +8,7 @@ export interface TeamsWebhookBodyFormat { } class TeamsNotifier extends WebhookNotifier { - contentTemplateOptions() { + contentTemplateOptions(): MorphixOptions { return { transform: ({ value, key }) => (key === "logql" || key === "lokiUrl" ? value : `**${value ?? "unknown"}**`), ignoreMissing: true