From 5769a52f0cd29ce64a6b2279ecf2e35ec7f7225c Mon Sep 17 00:00:00 2001 From: keito <131662659+mst-mkt@users.noreply.github.com> Date: Thu, 7 Aug 2025 19:47:19 +0900 Subject: [PATCH] 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 --- types/config.ts | 24 +++++++++++++----------- worker/src/index.ts | 6 +++--- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/types/config.ts b/types/config.ts index 69afa29..83c39a6 100644 --- a/types/config.ts +++ b/types/config.ts @@ -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 = { kvWriteCooldownMinutes: number passwordProtection?: string monitors: MonitorTarget[] notification?: Notification - callbacks?: Callbacks + callbacks?: Callbacks } export type Notification = { @@ -55,22 +57,22 @@ export type Notification = { skipNotificationIds?: string[] } -export type Callbacks = { - onStatusChange: ( - env: any, - monitor: any, +export type Callbacks = { + onStatusChange?: ( + env: TEnv, + monitor: MonitorTarget, isUp: boolean, timeIncidentStart: number, timeNow: number, reason: string - ) => Promise - onIncident: ( - env: any, - monitor: any, + ) => Promise | any + onIncident?: ( + env: TEnv, + monitor: MonitorTarget, timeIncidentStart: number, timeNow: number, reason: string - ) => Promise + ) => Promise | any } export type MonitorState = { diff --git a/worker/src/index.ts b/worker/src/index.ts index e34b926..f9b4680 100644 --- a/worker/src/index.ts +++ b/worker/src/index.ts @@ -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],