aboutsummaryrefslogtreecommitdiff
path: root/src/integrations/ntfy.ts
blob: cf815ed37bda8c2b5bfabef138b3911fd9f0af8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { Either, type IEither } from '@emprespresso/pengueno';
import type { NtfyConfig, StoredRequest } from '../types/index.js';

export interface NtfyNotification {
    topic: string;
    title: string;
    message: string;
    tags?: string[];
    priority?: number;
}

export async function sendNtfyNotification(config: NtfyConfig, request: StoredRequest): Promise<IEither<Error, void>> {
    if (!config.enabled || !config.server || !config.topic) {
        return Either.right(<void>undefined);
    }

    try {
        const url = `${config.server}/${config.topic}`;
        const title = `Webhook received: ${request.routeName}`;
        const message = `Method: ${request.method}\nTimestamp: ${new Date(request.timestamp).toISOString()}\nUUID: ${request.uuid}`;

        const response = await fetch(url, {
            method: 'POST',
            headers: {
                Title: title,
                Tags: 'webhook,posthook',
                Priority: '3',
            },
            body: message,
        });

        if (!response.ok) {
            return Either.left(new Error(`ntfy notification failed: ${response.statusText}`));
        }

        return Either.right(<void>undefined);
    } catch (err) {
        return Either.left(err instanceof Error ? err : new Error(String(err)));
    }
}