Skip to content

Commit

Permalink
feat: Add support for all methods with webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
simenandre committed Aug 30, 2021
1 parent 4c0a3b0 commit bce20a0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 44 deletions.
42 changes: 6 additions & 36 deletions src/methods/api.ts
@@ -1,4 +1,5 @@
import { WebClient } from '@slack/web-api';
import { parseLog } from '../parse-log';
import type { LogEntry, SlackApiOptions, SlackMethod } from '../types';

export class SlackApiMethod implements SlackMethod {
Expand All @@ -9,41 +10,10 @@ export class SlackApiMethod implements SlackMethod {
}

async send(entry: LogEntry): Promise<void> {
if (entry.jsonPayload?.slack) {
await this.client.chat.postMessage({
channel: this.config.defaultChannel,
...entry.jsonPayload.slack,
});
return;
}

if (
entry.operation?.producer === 'github.com/bjerkio/nestjs-slack@v1' &&
typeof entry.jsonPayload?.message !== 'string'
) {
await this.client.chat.postMessage({
channel: this.config.defaultChannel,
...entry.jsonPayload.message.slack,
});
return;
}

if (
entry.jsonPayload?.message &&
typeof entry.jsonPayload?.message === 'string'
) {
await this.client.chat.postMessage({
channel: this.config.defaultChannel,
text: entry.jsonPayload?.message,
});
return;
}

if (entry.textPayload) {
await this.client.chat.postMessage({
channel: this.config.defaultChannel,
text: entry.textPayload,
});
}
const parsedLog = await parseLog(entry);
await this.client.chat.postMessage({
channel: this.config.defaultChannel,
...parsedLog,
});
}
}
11 changes: 3 additions & 8 deletions src/methods/webhook.ts
@@ -1,4 +1,5 @@
import axios, { AxiosError } from 'axios';
import { parseLog } from '../parse-log';
import type { LogEntry, SlackMethod, SlackWebhookOptions } from '../types';

export type WebhookResponse = 'ok' | string;
Expand All @@ -7,14 +8,8 @@ export class SlackWebhookMethod implements SlackMethod {
constructor(private readonly config: SlackWebhookOptions) {}

async send(entry: LogEntry): Promise<void> {
if (entry.jsonPayload?.slack) {
await this.sendRequest(entry.jsonPayload.slack);
return;
}

await this.sendRequest({
text: entry.jsonPayload?.message ?? entry.textPayload,
});
const parsedLog = await parseLog(entry);
await this.sendRequest(parsedLog);
}

private async sendRequest(data: unknown): Promise<void> {
Expand Down
29 changes: 29 additions & 0 deletions src/parse-log.ts
@@ -0,0 +1,29 @@
import { LogEntry, SlackMessageRequest } from './types';

export async function parseLog(entry: LogEntry): Promise<SlackMessageRequest> {
if (entry.jsonPayload?.slack) {
return entry.jsonPayload.slack;
}

if (
entry.operation?.producer === 'github.com/bjerkio/nestjs-slack@v1' &&
typeof entry.jsonPayload?.message !== 'string'
) {
return entry.jsonPayload?.message?.slack;
}

if (
entry.jsonPayload?.message &&
typeof entry.jsonPayload?.message === 'string'
) {
return {
text: entry.jsonPayload?.message,
};
}

if (entry.textPayload) {
return {
text: entry.textPayload,
};
}
}

0 comments on commit bce20a0

Please sign in to comment.