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> { if (!config.enabled || !config.server || !config.topic) { return Either.right(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(undefined); } catch (err) { return Either.left(err instanceof Error ? err : new Error(String(err))); } }