-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutils.ts
37 lines (30 loc) · 851 Bytes
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import isURL from 'validator/lib/isURL'
import env from '../../utils/env'
import logger from '../Development/logger'
const SLACK_WEBHOOK = env('SLACK_WEBHOOK', '')
if (!isURL(SLACK_WEBHOOK)) {
logger.log(`missing config SLACK_WEBHOOK`)
}
// TODO: should we use @slack/web-api?
export async function sendToSlack(body: {}) {
if (!isURL(SLACK_WEBHOOK)) {
return
}
try {
const response = await fetch(SLACK_WEBHOOK, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
})
const data = await response.text()
if (response.status >= 400) {
logger.error(`Slack bad request: ${data} (${response.status})`)
}
} catch (error) {
if (error instanceof Error) {
logger.error(`Slack service error: ` + error.message, error)
}
}
}