Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Clean up ESLint warnings in packages/notifications #1194

Merged
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/notification/channel/__test__/discord.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('notificationChecker - discordNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Discord URL" is not allowed to be empty'

expect(() => {
Expand All @@ -68,7 +68,7 @@ describe('notificationChecker - discordNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Discord URL" must be a valid uri'

expect(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/notification/channel/__test__/mailgun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('notificationChecker - mailgunNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Mailgun API Key" is not allowed to be empty'

expect(() => {
Expand All @@ -48,7 +48,7 @@ describe('notificationChecker - mailgunNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Mailgun Domain" is not allowed to be empty'

expect(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/notification/channel/__test__/monika-notif.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('notificationChecker - MonikaNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Monika Notification URL" is not allowed to be empty'

expect(() => {
Expand All @@ -44,7 +44,7 @@ describe('notificationChecker - MonikaNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Monika Notification URL" must be a valid uri'

expect(() => {
Expand Down
4 changes: 1 addition & 3 deletions packages/notification/channel/__test__/pagerduty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ describe('PagerDuty notification', () => {

it('should validate PagerDuty key', () => {
// act
const error = validator.validate([
{ probeID: '65DDKmmB9mSaeE-8bMXRN' },
] as any)
const error = validator.validate([{ probeID: '65DDKmmB9mSaeE-8bMXRN' }])

// assert
expect(error.error?.message).eq('"Key" is required')
Expand Down
2 changes: 1 addition & 1 deletion packages/notification/channel/__test__/pushbullet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('notificationChecker - pushbulletNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Pushbullet token" is not allowed to be empty'

expect(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/notification/channel/__test__/sengrid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('notificationChecker - sendgridNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"SendGrid API Key" is not allowed to be empty'

expect(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/notification/channel/__test__/slack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('notificationChecker - slackNotification', () => {
data: { url: '' },
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Slack URL" is not allowed to be empty'

expect(() => {
Expand All @@ -42,7 +42,7 @@ describe('notificationChecker - slackNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Slack URL" must be a valid uri'

expect(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/notification/channel/__test__/teams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('notificationChecker - teamsNotification', () => {
data: { url: '' },
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Teams URL" is not allowed to be empty'

expect(() => {
Expand All @@ -42,7 +42,7 @@ describe('notificationChecker - teamsNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Teams URL" must be a valid uri'

expect(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/notification/channel/__test__/telegram.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('notificationChecker - telegramNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Telegram Group ID" is not allowed to be empty'

expect(() => {
Expand All @@ -47,7 +47,7 @@ describe('notificationChecker - telegramNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Telegram Bot Token" is not allowed to be empty'

expect(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/notification/channel/__test__/webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('notificationChecker - webhookNotification', () => {
data: { url: '' },
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Webhook URL" is not allowed to be empty'

expect(() => {
Expand All @@ -63,7 +63,7 @@ describe('notificationChecker - webhookNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Webhook URL" must be a valid uri'

expect(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/notification/channel/__test__/workplace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('notificationChecker - workplaceNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Workplace Thread ID" is not allowed to be empty'

expect(() => {
Expand All @@ -47,7 +47,7 @@ describe('notificationChecker - workplaceNotification', () => {
},
},
])
} catch (error) {
} catch (error: unknown) {
const message = '"Workplace Access Token" is not allowed to be empty'

expect(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/notification/channel/desktop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Joi from 'joi'
import { type as osType } from 'os'
import type { NotificationMessage } from '.'

export const validator = Joi.any()
export const validator = Joi.object()

export const send = async (
// actually do not need data property
Expand Down Expand Up @@ -90,7 +90,7 @@ export const send = async (
function psEscape(str: string): string {
let result = ''
for (const ch of str) {
if (ch.charCodeAt(0) === 39) {
if (ch.codePointAt(0) === 39) {
// single quote, escape it with another single quote
result += ch
}
syamsudotdev marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
61 changes: 32 additions & 29 deletions packages/notification/channel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE. *
**********************************************************************************/

import type { AnySchema } from 'joi'
import type { ArraySchema, ObjectSchema } from 'joi'

import * as dingtalk from './dingtalk'
import * as discord from './discord'
Expand Down Expand Up @@ -53,7 +53,7 @@ type BaseNotificationMessageMeta = {
privateIpAddress: string
publicIpAddress: string
[key: string]: unknown
monikaInstance?: any
monikaInstance?: string
version: string
}

Expand Down Expand Up @@ -89,42 +89,45 @@ export type NotificationMessage = {
| NotificationStatusUpdateMessageMeta
}

type NotificationChannel<T = any> = {
validator: AnySchema
send: (notificationData: T, message: NotificationMessage) => Promise<void>
type NotificationChannel<T = object, U = object> = {
validator: ArraySchema | ObjectSchema
send: (
notificationData: T | T[] | undefined,
message: NotificationMessage
) => Promise<void>
sendWithCustomContent?: (
notificationData: T,
customContent: T
notificationData: T | T[],
customContent: U | U[]
) => Promise<void>
additionalStartupMessage?: (notificationData: T) => string
}

export type Notification = {
id: string
type: string
data: any
data: object | undefined
}

export const channels: Record<string, NotificationChannel> = {
dingtalk,
discord,
desktop,
'google-chat': googlechat,
gotify,
instatus,
lark,
mailgun,
'monika-notif': monikaNotif,
opsgenie,
pagerduty,
pushbullet,
pushover,
sendgrid,
slack,
smtp,
teams,
telegram,
webhook,
whatsapp,
workplace,
dingtalk: dingtalk as NotificationChannel<object>,
discord: discord as NotificationChannel<object>,
desktop: desktop as NotificationChannel<unknown>,
'google-chat': googlechat as NotificationChannel<object>,
gotify: gotify as NotificationChannel<object>,
instatus: instatus as NotificationChannel<object>,
lark: lark as NotificationChannel<object>,
mailgun: mailgun as NotificationChannel<object>,
'monika-notif': monikaNotif as NotificationChannel<object>,
opsgenie: opsgenie as NotificationChannel<object>,
pagerduty: pagerduty as NotificationChannel<object>,
pushbullet: pushbullet as NotificationChannel<object>,
pushover: pushover as NotificationChannel<object>,
sendgrid: sendgrid as NotificationChannel<object>,
slack: slack as NotificationChannel<object>,
smtp: smtp as NotificationChannel<object>,
teams: teams as NotificationChannel<object>,
telegram: telegram as NotificationChannel<object>,
webhook: webhook as NotificationChannel<object>,
whatsapp: whatsapp as NotificationChannel<object>,
workplace: workplace as NotificationChannel<object>,
}
20 changes: 13 additions & 7 deletions packages/notification/channel/instatus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* SOFTWARE. *
**********************************************************************************/

import axios from 'axios'
import axios, { AxiosError } from 'axios'
import Joi from 'joi'
import {
findIncident,
Expand Down Expand Up @@ -138,10 +138,13 @@ async function createIncident(
.then((res) => res?.data?.id)

await insertIncidentToDatabase({ incidentID, probeID, status, url })
} catch (error: any) {
} catch (error: unknown) {
const axiosError = error instanceof AxiosError ? error : new AxiosError()
throw new Error(
`${error?.message}${
error?.data ? `. ${error?.response?.data?.message}` : ''
`${axiosError?.message}${
axiosError?.response?.data
? `. ${axiosError?.response?.data?.message}`
: ''
}`
)
}
Expand Down Expand Up @@ -187,10 +190,13 @@ async function updateIncident(
data,
getAxiosConfig(apiKey)
)
} catch (error: any) {
} catch (error: unknown) {
const axiosError = error instanceof AxiosError ? error : new AxiosError()
throw new Error(
`${error?.message}${
error?.data ? `. ${error?.response?.data?.message}` : ''
`${axiosError.message}${
axiosError?.response?.data
? `. ${axiosError?.response?.data?.message}`
: ''
}`
)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/notification/channel/monika-notif.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type MonikaAlertNotifDataBody = {
alert: string
url: string
time: string
monika: string
monika?: string
}

type MonikaStartAndTerminationNotifDataBody = {
Expand All @@ -43,7 +43,7 @@ type MonikaStartAndTerminationNotifDataBody = {
type MonikaStatusUpdateNotifDataBody = {
type: 'status-update'
time: string
monika: string
monika?: string
numberOfProbes: string
maxResponseTime: string
minResponseTime: string
Expand Down
2 changes: 1 addition & 1 deletion packages/notification/channel/pagerduty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export async function send(
function transformMessageToEventPayload(
routingKey: string,
message: NotificationMessage
): Record<string, any> | null {
): Record<string, string | object> | null {
// https://developer.pagerduty.com/docs/ZG9jOjExMDI5NTgw-events-api-v2-overview#pagerduty-common-event-format-pd-cef
const { meta, summary } = message
const { probeID, url, alertQuery, type, publicIpAddress } = meta
syamsudotdev marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
4 changes: 2 additions & 2 deletions packages/notification/channel/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async function sendTextMessage({
},
},
})
} catch (error) {
} catch (error: unknown) {
throw new Error(
`Something wrong with your recipient number, Please check your country code: ${recipient} ${error}`
)
Expand Down Expand Up @@ -147,7 +147,7 @@ async function loginUser({
}

return loginResp.users[0].token
} catch (error) {
} catch (error: unknown) {
throw new Error(
`Something wrong with your whatsapp config please check again. error: ${error}`
)
Expand Down
6 changes: 4 additions & 2 deletions packages/notification/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
**********************************************************************************/

import { channels, Notification, NotificationMessage } from './channel'
import { getErrorMessage } from './utils/catch-error-handler'

async function sendNotifications(
notifications: Notification[],
Expand All @@ -38,9 +39,10 @@ async function sendNotifications(
}

await channel.send(data, message)
} catch (error: any) {
} catch (error: unknown) {
const message = getErrorMessage(error)
throw new Error(
`Failed to send message using ${type}, please check your ${type} notification config.\nMessage: ${error?.message}`
`Failed to send message using ${type}, please check your ${type} notification config.\nMessage: ${message}`
)
}
})
Expand Down
Loading
Loading