发送通知
发送到一个目标
Section titled “发送到一个目标”一次性投递可以使用 send():
import { send } from 'statocysts'
await send(process.env.SLACK_TARGET!, { title: 'Deployment complete', body: 'Version 0.14.0 is now live.',})send() 会在本地校验目标 URL 和提供方专属格式,然后尝试投递。本地校验错误保持原样;请求准备或传输失败会抛出 NotificationDeliveryError。
需要向同一组通知目标多次投递时,可以创建通知器:
import { createNotifier } from 'statocysts'
const operations = createNotifier([ process.env.SLACK_TARGET!, process.env.TELEGRAM_TARGET!,])
await operations.send({ title: 'Deployment started' })await operations.send({ title: 'Deployment complete' })通知目标必须唯一。创建通知器时,通知运行时会拒绝空目标列表、非法 URL、重复的标准化 URL、不支持的协议和无效的提供方专属 URL 组成部分。创建过程不会发起远程请求。
处理部分失败
Section titled “处理部分失败”所有通知目标都会被并发尝试。批次完成后,可以检查每个失败目标:
import { createNotifier, NotificationDeliveryError } from 'statocysts'
try { await operations.send({ title: 'Service recovered' })}catch (error) { if (error instanceof NotificationDeliveryError) { console.error(`${error.failureCount} deliveries failed`)
for (const failure of error.failures) { console.error(failure.target, failure.cause) } } else { throw error }}有关校验和投递错误的边界,请阅读错误处理。
配置通知提供方
Section titled “配置通知提供方”如果选项不适合放在通知目标中,可以调用具名通知提供方:
import { telegram } from 'statocysts'
await telegram.send( process.env.TELEGRAM_TARGET!, { title: 'Deployment complete' }, { apiBaseUrl: 'https://api.telegram.org', fetchOptions: { timeout: 5000 }, },)直接调用通知提供方时,原始校验或传输错误不会被包装成 NotificationDeliveryError。