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 = { export type PageConfig = {
title?: string title?: string
links?: PageConfigLink[] links?: PageConfigLink[]
@ -39,12 +41,12 @@ export type MonitorTarget = {
checkProxyFallback?: boolean checkProxyFallback?: boolean
} }
export type WorkerConfig = { export type WorkerConfig<TEnv = Env> = {
kvWriteCooldownMinutes: number kvWriteCooldownMinutes: number
passwordProtection?: string passwordProtection?: string
monitors: MonitorTarget[] monitors: MonitorTarget[]
notification?: Notification notification?: Notification
callbacks?: Callbacks callbacks?: Callbacks<TEnv>
} }
export type Notification = { export type Notification = {
@ -55,22 +57,22 @@ export type Notification = {
skipNotificationIds?: string[] skipNotificationIds?: string[]
} }
export type Callbacks = { export type Callbacks<TEnv = Env> = {
onStatusChange: ( onStatusChange?: (
env: any, env: TEnv,
monitor: any, monitor: MonitorTarget,
isUp: boolean, isUp: boolean,
timeIncidentStart: number, timeIncidentStart: number,
timeNow: number, timeNow: number,
reason: string reason: string
) => Promise<any> ) => Promise<any> | any
onIncident: ( onIncident?: (
env: any, env: TEnv,
monitor: any, monitor: MonitorTarget,
timeIncidentStart: number, timeIncidentStart: number,
timeNow: number, timeNow: number,
reason: string reason: string
) => Promise<any> ) => Promise<any> | any
} }
export type MonitorState = { export type MonitorState = {

View File

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