mirror of
https://github.com/unraid/api.git
synced 2025-12-31 05:29:48 -06:00
refactor(api): directly accept importance level in UnraidLocalNotifier
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import Mustache from 'mustache';
|
||||
import { type LooseObject } from '@app/core/types';
|
||||
import { type NotificationIni } from '../types/states/notification';
|
||||
|
||||
export type NotifierLevel = 'info' | 'warn' | 'error';
|
||||
|
||||
export type NotifierOptions = Partial<{
|
||||
level: NotifierLevel;
|
||||
importance?: NotificationIni['importance'];
|
||||
helpers?: Record<string, unknown>;
|
||||
template?: string;
|
||||
}>;
|
||||
|
||||
39
api/src/core/notifiers/unraid-local.ts
Normal file
39
api/src/core/notifiers/unraid-local.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { logger } from '@app/core/log';
|
||||
import { Notifier, type NotifierSendOptions, type NotifierOptions } from '@app/core/notifiers/notifier';
|
||||
import { execa } from 'execa';
|
||||
|
||||
type ValidLocalLevels = 'alert' | 'warning' | 'normal';
|
||||
|
||||
export class UnraidLocalNotifier extends Notifier {
|
||||
private convertNotifierLevel(level: NotifierOptions['level']): ValidLocalLevels {
|
||||
switch (level) {
|
||||
case 'error':
|
||||
return 'alert';
|
||||
case 'warn':
|
||||
return 'warning';
|
||||
case 'info':
|
||||
return 'normal';
|
||||
default:
|
||||
return 'normal';
|
||||
}
|
||||
}
|
||||
|
||||
constructor(options: NotifierOptions = {}) {
|
||||
super(options);
|
||||
|
||||
this.level = options.importance ?? this.convertNotifierLevel(options.level ?? 'info');
|
||||
this.template = options.template ?? '{{ message }}';
|
||||
}
|
||||
|
||||
async send(options: NotifierSendOptions) {
|
||||
const { title, data } = options;
|
||||
const { level } = this;
|
||||
|
||||
const template = this.render(data);
|
||||
try {
|
||||
await execa('/usr/local/emhttp/webGui/scripts/notify', ['-i', `${level}`, '-s', 'Unraid API', '-d', `${template}`, '-e', `${title}`]);
|
||||
} catch (error: unknown) {
|
||||
logger.warn(`Error sending unraid notification: ${error instanceof Error ? error.message : 'No Error Information'}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user