57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import { NotificationOptions, NotificationService } from "./service";
|
|
|
|
export interface GotifyConfig {
|
|
url: string;
|
|
token: string;
|
|
enabled: boolean;
|
|
}
|
|
|
|
/**
|
|
* Gotify Notification Service implementation.
|
|
*/
|
|
export class GotifyNotificationService implements NotificationService {
|
|
constructor(
|
|
private config: GotifyConfig,
|
|
private logger?: { error(msg: string, data?: any): void },
|
|
) {}
|
|
|
|
async notify(options: NotificationOptions): Promise<void> {
|
|
if (!this.config.enabled) return;
|
|
|
|
try {
|
|
const { title, message, priority = 4 } = options;
|
|
|
|
// Ensure we have a trailing slash for base URL, then append 'message'
|
|
const baseUrl = this.config.url.endsWith("/")
|
|
? this.config.url
|
|
: `${this.config.url}/`;
|
|
const url = new URL("message", baseUrl);
|
|
url.searchParams.set("token", this.config.token);
|
|
|
|
const response = await fetch(url.toString(), {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
title,
|
|
message,
|
|
priority,
|
|
}),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
this.logger?.error("Gotify notification failed", {
|
|
status: response.status,
|
|
error: errorText.slice(0, 100),
|
|
});
|
|
}
|
|
} catch (error) {
|
|
this.logger?.error("Gotify notification error", {
|
|
error: (error as Error).message,
|
|
});
|
|
}
|
|
}
|
|
}
|