UNPKG

@hoangnam.io/qa-tools

Version:

Logging, Error handling, Notifying for Express codebase

30 lines (23 loc) 938 B
const axios = require("axios"); const { default: z } = require("zod"); const schema = z.object({ discord: z.object({ url: z.string() }).optional(), telegram: z.object({ url: z.string(), chatId: z.string() }).optional(), }); function sendNotifyToDiscord(config, message) { return axios.post(config.url, { content: message }); } function sendNotifyToTelegram(config, message) { return axios.post(config.url, { chat_id: config.chatId, text: message }); } async function sendNotification(options, textMessage) { const results = []; if (options.discord) results.push(sendNotifyToDiscord(options.discord, textMessage)); if (options.telegram) results.push(sendNotifyToTelegram(options.telegram, textMessage)); return Promise.allSettled(results); } function getNotifier(options) { schema.parse(options); return { send: async (textMessage) => sendNotification(options, textMessage) }; } module.exports = { getNotifier };