feat: strict config type

pull/47/head
神代綺凛 2024-07-07 16:54:45 +08:00
parent e99d919bd7
commit 037bd7756d
3 changed files with 50 additions and 13 deletions

View File

@ -1,4 +1,6 @@
const pageConfig = {
import type { PageConfig, UptimeConfig } from './uptime.types'
const pageConfig: PageConfig = {
// Title for your status page
title: "lyc8503's Status Page",
// Links shown at the header of your status page, could set `highlight` to `true`
@ -9,7 +11,7 @@ const pageConfig = {
],
}
const workerConfig = {
const workerConfig: UptimeConfig = {
// Write KV at most every 3 minutes unless the status changed.
kvWriteCooldownMinutes: 3,
// Define all your monitors here
@ -61,12 +63,12 @@ const workerConfig = {
notification: {
// [Optional] apprise API server URL
// if not specified, no notification will be sent
appriseApiServer: "https://apprise.example.com/notify",
appriseApiServer: 'https://apprise.example.com/notify',
// [Optional] recipient URL for apprise, refer to https://github.com/caronc/apprise
// if not specified, no notification will be sent
recipientUrl: "tgram://bottoken/ChatID",
recipientUrl: 'tgram://bottoken/ChatID',
// [Optional] timezone used in notification messages, default to "Etc/GMT"
timeZone: "Asia/Shanghai",
timeZone: 'Asia/Shanghai',
// [Optional] grace period in minutes before sending a notification
// notification will be sent only if the monitor is down for N continuous checks after the initial failure
// if not specified, notification will be sent immediately
@ -83,7 +85,6 @@ const workerConfig = {
) => {
// This callback will be called when there's a status change for any monitor
// Write any Typescript code here
// This will not follow the grace period settings and will be called immediately when the status changes
// You need to handle the grace period manually if you want to implement it
},

View File

@ -1,4 +1,5 @@
type MonitorState = {
export type MonitorState = {
version: number
lastUpdate: number
overallUp: number
overallDown: number
@ -28,7 +29,7 @@ type MonitorState = {
>
}
type MonitorTarget = {
export type MonitorTarget = {
id: string
name: string
method: string // "TCP_PING" or Http Method (e.g. GET, POST, OPTIONS, etc.)
@ -45,4 +46,39 @@ type MonitorTarget = {
responseKeyword?: string
}
export type { MonitorState, MonitorTarget }
export type UptimeConfig = {
kvWriteCooldownMinutes: number
monitors: MonitorTarget[]
notification?: {
appriseApiServer?: string
recipientUrl?: string
timeZone?: string
gracePeriod?: number
}
callbacks?: {
onStatusChange?: (
env: any,
monitor: any,
isUp: boolean,
timeIncidentStart: number,
timeNow: number,
reason: string
) => Promise<any>
onIncident?: (
env: any,
monitor: any,
timeIncidentStart: number,
timeNow: number,
reason: string
) => Promise<any>
}
}
export type PageConfig = {
title: string
links: Array<{
link: string
label: string
highlight?: boolean
}>
}

View File

@ -58,7 +58,7 @@ export default {
timeNow: number,
reason: string
) => {
if (workerConfig.notification?.appriseApiServer && workerConfig.notification?.recipientUrl) {
if (workerConfig.notification?.appriseApiServer && workerConfig.notification.recipientUrl) {
const notification = formatStatusChangeNotification(
monitor,
isUp,
@ -191,7 +191,7 @@ export default {
}
console.log('Calling config onStatusChange callback...')
await workerConfig.callbacks.onStatusChange(
await workerConfig.callbacks?.onStatusChange?.(
env,
monitor,
true,
@ -257,7 +257,7 @@ export default {
if (monitorStatusChanged) {
console.log('Calling config onStatusChange callback...')
await workerConfig.callbacks.onStatusChange(
await workerConfig.callbacks?.onStatusChange?.(
env,
monitor,
false,
@ -273,7 +273,7 @@ export default {
try {
console.log('Calling config onIncident callback...')
await workerConfig.callbacks.onIncident(
await workerConfig.callbacks?.onIncident?.(
env,
monitor,
currentIncident.start[0],