Skip to content

Commit

Permalink
chore: continue to enhance usage & typing
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken committed Apr 26, 2024
1 parent f2c15da commit 40ca737
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 81 deletions.
19 changes: 16 additions & 3 deletions src/discord/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Import Third-party Dependencies
import { MorphixOptions } from "@sigyn/morphix";
import { WebhookNotifierOptions, WebhookNotifier } from "@sigyn/notifiers";

// CONSTANTS
Expand All @@ -13,15 +14,27 @@ const kEmbedColor = {
info: 16777215
};

class DiscordNotifier extends WebhookNotifier<any> {
contentTemplateOptions() {
export interface DiscordEmbed {
title: string;
description: string;
color: number;
}

export interface DiscordWebhookBodyFormat {
embeds: DiscordEmbed[];
username: string;
avatar_url?: string;
}

class DiscordNotifier extends WebhookNotifier<DiscordWebhookBodyFormat> {
contentTemplateOptions(): MorphixOptions {
return {
transform: ({ key, value }) => (key === "lokiUrl" ? value : `**${value === undefined ? "unknown" : value}**`),
ignoreMissing: true
};
}

async formatWebhookBody(): Promise<any> {
async formatWebhookBody(): Promise<DiscordWebhookBodyFormat> {
if (this.data.ruleConfig?.logql) {
this.data.ruleConfig.logql = this.#formatLogQL(this.data.ruleConfig.logql);
}
Expand Down
70 changes: 22 additions & 48 deletions src/notifiers/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,29 @@ 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<MyAwesomeFormat> {
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<MyAwesomeFormat> {
const [title, content] = await Promise.all([
this.formatTitle(),
this.formatContent()
]);

return {
title,
content: content.join("\n")
}
return kCustomTemplateOptions;
}
}
```

> [!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
Expand All @@ -89,35 +80,14 @@ const kSeverityEmoji = {
};
```

You can do `this.showSeverityEmoji = false` to disable this behavior.

```ts
async formatWebhook(): Promise<any> {
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<MyAwesomeFormat> {
constructor(
options: WebhookNotifierOptions
) {
super({ ...options, showSeverityEmoji: false });
}
}
```
Expand All @@ -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 {
Expand Down
46 changes: 25 additions & 21 deletions src/notifiers/src/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -40,31 +48,25 @@ export class WebhookNotifier<T> {
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<string> {
Expand Down Expand Up @@ -105,7 +107,9 @@ export class WebhookNotifier<T> {
): Promise<httpie.RequestResponse<string>> {
return httpie.post<string>(this.webhookUrl, {
body,
headers: this.#headers
headers: {
"content-type": "application/json"
}
});
}
}
11 changes: 3 additions & 8 deletions src/slack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Import Third-party Dependencies
import { MorphixOptions } from "@sigyn/morphix";
import { WebhookNotifierOptions, WebhookNotifier } from "@sigyn/notifiers";
import { MessageAttachment } from "@slack/types";

Expand All @@ -15,15 +16,9 @@ export interface SlackWebhookBodyFormat {
}

class SlackNotifier extends WebhookNotifier<SlackWebhookBodyFormat> {
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
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/teams/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Import Third-party Dependencies
import { MorphixOptions } from "@sigyn/morphix";
import { WebhookNotifierOptions, WebhookNotifier } from "@sigyn/notifiers";

export interface TeamsWebhookBodyFormat {
Expand All @@ -7,7 +8,7 @@ export interface TeamsWebhookBodyFormat {
}

class TeamsNotifier extends WebhookNotifier<TeamsWebhookBodyFormat> {
contentTemplateOptions() {
contentTemplateOptions(): MorphixOptions {
return {
transform: ({ value, key }) => (key === "logql" || key === "lokiUrl" ? value : `**${value ?? "unknown"}**`),
ignoreMissing: true
Expand Down

0 comments on commit 40ca737

Please sign in to comment.