Oprava pádu při chybějící konfiguraci Gotify

This commit is contained in:
Martin Berka 2023-06-16 21:12:30 +02:00
parent 09264720d1
commit 89fc27b087

View File

@ -1,6 +1,6 @@
/** Notifikace pro gotify*/ /** Notifikace pro gotify*/
import axios, {AxiosError, AxiosResponse} from 'axios'; import axios, { AxiosError, AxiosResponse } from 'axios';
import {GotifyServer, NotififaceInput, NotifikaceData, UdalostEnum} from "./types"; import { GotifyServer, NotififaceInput, NotifikaceData, UdalostEnum } from "./types";
import dotenv from 'dotenv'; import dotenv from 'dotenv';
import path from 'path'; import path from 'path';
@ -10,9 +10,12 @@ dotenv.config({ path: path.resolve(__dirname, `../.env.${ENVIRONMENT}`) });
const gotifyDataRaw = process.env.GOTIFY_SERVERS_AND_KEYS || "{}"; const gotifyDataRaw = process.env.GOTIFY_SERVERS_AND_KEYS || "{}";
const gotifyData: GotifyServer[] = JSON.parse(gotifyDataRaw); const gotifyData: GotifyServer[] = JSON.parse(gotifyDataRaw);
export const gotifyCall = async (data: NotififaceInput, gotifyServers: GotifyServer[]): Promise<any[]> => { export const gotifyCall = async (data: NotififaceInput, gotifyServers?: GotifyServer[]): Promise<any[]> => {
const urls = gotifyServers.flatMap(gotifyServer => if (!Array.isArray(gotifyServers)) {
gotifyServer.api_keys.map(apiKey => `${gotifyServer.server}/message?token=${apiKey}`)); return []
}
const urls = gotifyServers.flatMap(gotifyServer =>
gotifyServer.api_keys.map(apiKey => `${gotifyServer.server}/message?token=${apiKey}`));
const dataPayload = { const dataPayload = {
title: "Luncher", title: "Luncher",
@ -20,7 +23,7 @@ export const gotifyCall = async (data: NotififaceInput, gotifyServers: GotifySer
priority: 7, priority: 7,
}; };
const headers = {"Content-Type": "application/json"}; const headers = { "Content-Type": "application/json" };
const promises = urls.map(url => const promises = urls.map(url =>
axios.post(url, dataPayload, { headers }).then(response => { axios.post(url, dataPayload, { headers }).then(response => {
@ -42,19 +45,19 @@ export const gotifyCall = async (data: NotififaceInput, gotifyServers: GotifySer
} }
} }
// Handle unknown error without a response // Handle unknown error without a response
console.log(error,"unknown error"); console.log(error, "unknown error");
}) })
); );
return promises; return promises;
}; };
/** Zavolá notifikace na všechny konfigurované způsoby notifikace, přetížení proměných na false pro jednotlivé způsoby je vypne*/ /** Zavolá notifikace na všechny konfigurované způsoby notifikace, přetížení proměných na false pro jednotlivé způsoby je vypne*/
export const callNotifikace = async ({input, teams = true, gotify = true}: NotifikaceData) => { export const callNotifikace = async ({ input, teams = true, gotify = true }: NotifikaceData) => {
const notifications = []; const notifications = [];
if (gotify) { if (gotify) {
const gotifyPromises = await gotifyCall(input, gotifyData); const gotifyPromises = await gotifyCall(input, gotifyData);
notifications.push(...gotifyPromises); notifications.push(...gotifyPromises);
} }
/* Zatím není /* Zatím není