fix: improve types for callback-related config (#135)

* fix: allow partial callbacks in config types

* fix: allow non-async callback functions in config types

* fix: specify type of monitor argument in callback type

* fix: use generics type for env argument in callback type
pull/140/head^2
keito 2025-08-07 19:47:19 +09:00 committed by GitHub
parent 7156fd83f2
commit 5769a52f0c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 14 deletions

View File

@ -1,3 +1,5 @@
import type { Env } from '../worker/src'
export type PageConfig = {
title?: string
links?: PageConfigLink[]
@ -39,12 +41,12 @@ export type MonitorTarget = {
checkProxyFallback?: boolean
}
export type WorkerConfig = {
export type WorkerConfig<TEnv = Env> = {
kvWriteCooldownMinutes: number
passwordProtection?: string
monitors: MonitorTarget[]
notification?: Notification
callbacks?: Callbacks
callbacks?: Callbacks<TEnv>
}
export type Notification = {
@ -55,22 +57,22 @@ export type Notification = {
skipNotificationIds?: string[]
}
export type Callbacks = {
onStatusChange: (
env: any,
monitor: any,
export type Callbacks<TEnv = Env> = {
onStatusChange?: (
env: TEnv,
monitor: MonitorTarget,
isUp: boolean,
timeIncidentStart: number,
timeNow: number,
reason: string
) => Promise<any>
onIncident: (
env: any,
monitor: any,
) => Promise<any> | any
onIncident?: (
env: TEnv,
monitor: MonitorTarget,
timeIncidentStart: number,
timeNow: number,
reason: string
) => Promise<any>
) => Promise<any> | any
}
export type MonitorState = {

View File

@ -179,7 +179,7 @@ const Worker = {
}
console.log('Calling config onStatusChange callback...')
await workerConfig.callbacks?.onStatusChange(
await workerConfig.callbacks?.onStatusChange?.(
env,
monitor,
true,
@ -250,7 +250,7 @@ const Worker = {
if (monitorStatusChanged) {
console.log('Calling config onStatusChange callback...')
await workerConfig.callbacks?.onStatusChange(
await workerConfig.callbacks?.onStatusChange?.(
env,
monitor,
false,
@ -266,7 +266,7 @@ const Worker = {
try {
console.log('Calling config onIncident callback...')
await workerConfig.callbacks?.onIncident(
await workerConfig.callbacks?.onIncident?.(
env,
monitor,
currentIncident.start[0],