|
| 1 | +// Type definitions for cronitor |
| 2 | +// Project: https://github.com/cronitorio/cronitor-js |
| 3 | +// Definitions by: Cronitor <https://cronitor.io> |
| 4 | + |
| 5 | +/// <reference types="node" /> |
| 6 | + |
| 7 | +export = Cronitor; |
| 8 | +export as namespace Cronitor; |
| 9 | + |
| 10 | +declare function Cronitor(apiKey?: string, config?: Cronitor.CronitorConfig): Cronitor.CronitorInstance; |
| 11 | + |
| 12 | +declare namespace Cronitor { |
| 13 | + // Main Cronitor instance |
| 14 | + interface CronitorInstance { |
| 15 | + _api: ApiConfig; |
| 16 | + Monitor: typeof Monitor; |
| 17 | + Event: typeof Event; |
| 18 | + path?: string; |
| 19 | + config?: any; |
| 20 | + |
| 21 | + /** |
| 22 | + * Generate a YAML config file from monitors in your Cronitor account |
| 23 | + */ |
| 24 | + generateConfig(options?: ConfigOptions): Promise<boolean>; |
| 25 | + |
| 26 | + /** |
| 27 | + * Apply/sync monitors from a YAML config file to your Cronitor account |
| 28 | + */ |
| 29 | + applyConfig(options?: ConfigOptions & { rollback?: boolean }): Promise<boolean>; |
| 30 | + |
| 31 | + /** |
| 32 | + * Validate a YAML config file without applying changes |
| 33 | + */ |
| 34 | + validateConfig(options?: ConfigOptions): Promise<boolean>; |
| 35 | + |
| 36 | + /** |
| 37 | + * Read and parse a YAML config file |
| 38 | + */ |
| 39 | + readConfig(options?: ConfigOptions & { output?: boolean }): Promise<boolean | any>; |
| 40 | + |
| 41 | + /** |
| 42 | + * Wrap a function with Cronitor telemetry (run/complete/fail pings) |
| 43 | + */ |
| 44 | + wrap<T extends (...args: any[]) => any>(key: string, callback: T): (...args: Parameters<T>) => Promise<ReturnType<T>>; |
| 45 | + |
| 46 | + /** |
| 47 | + * Wrap a cron library (node-cron or cron) to add Cronitor monitoring |
| 48 | + */ |
| 49 | + wraps(lib: any): void; |
| 50 | + |
| 51 | + /** |
| 52 | + * Schedule a job with node-cron wrapper |
| 53 | + */ |
| 54 | + schedule?(key: string, schedule: string, callback: () => void, options?: any): any; |
| 55 | + |
| 56 | + /** |
| 57 | + * Create a job with cron wrapper |
| 58 | + */ |
| 59 | + job?(key: string, schedule: string, callback: () => void): any; |
| 60 | + |
| 61 | + /** |
| 62 | + * Generate a unique series ID for grouping related pings |
| 63 | + */ |
| 64 | + newSeries(): string; |
| 65 | + } |
| 66 | + |
| 67 | + // Configuration |
| 68 | + interface CronitorConfig { |
| 69 | + apiVersion?: string; |
| 70 | + environment?: string; |
| 71 | + env?: string; |
| 72 | + timeout?: number; |
| 73 | + config?: string; |
| 74 | + region?: string; |
| 75 | + } |
| 76 | + |
| 77 | + interface ConfigOptions { |
| 78 | + path?: string; |
| 79 | + group?: string; |
| 80 | + } |
| 81 | + |
| 82 | + interface ApiConfig { |
| 83 | + key: string; |
| 84 | + version: string | null; |
| 85 | + env: string | null; |
| 86 | + pingUrl: (key: string) => string; |
| 87 | + monitorUrl: (key?: string) => string; |
| 88 | + axios: any; |
| 89 | + } |
| 90 | + |
| 91 | + // Monitor class |
| 92 | + class Monitor { |
| 93 | + key: string; |
| 94 | + data: any; |
| 95 | + |
| 96 | + constructor(key: string); |
| 97 | + |
| 98 | + /** |
| 99 | + * Create or update one or more monitors |
| 100 | + */ |
| 101 | + static put(data: MonitorConfig | MonitorConfig[], options?: PutOptions): Promise<Monitor | Monitor[]>; |
| 102 | + |
| 103 | + /** |
| 104 | + * Load monitor data from the API |
| 105 | + */ |
| 106 | + loadData(): Promise<any>; |
| 107 | + |
| 108 | + /** |
| 109 | + * Send a ping/telemetry event |
| 110 | + */ |
| 111 | + ping(params?: PingParams | string): Promise<boolean>; |
| 112 | + |
| 113 | + /** |
| 114 | + * Pause alerting for a number of hours |
| 115 | + */ |
| 116 | + pause(hours: number): Promise<boolean>; |
| 117 | + |
| 118 | + /** |
| 119 | + * Unpause alerting (alias for pause(0)) |
| 120 | + */ |
| 121 | + unpause(): Promise<boolean>; |
| 122 | + |
| 123 | + /** |
| 124 | + * Send an 'ok' ping to reset monitor to passing state |
| 125 | + */ |
| 126 | + ok(params?: PingParams): Promise<boolean>; |
| 127 | + |
| 128 | + /** |
| 129 | + * Delete the monitor |
| 130 | + */ |
| 131 | + delete(): Promise<boolean>; |
| 132 | + |
| 133 | + static readonly State: { |
| 134 | + RUN: 'run'; |
| 135 | + COMPLETE: 'complete'; |
| 136 | + FAIL: 'fail'; |
| 137 | + OK: 'ok'; |
| 138 | + }; |
| 139 | + |
| 140 | + static readonly requestType: { |
| 141 | + JSON: 'json'; |
| 142 | + YAML: 'yaml'; |
| 143 | + }; |
| 144 | + } |
| 145 | + |
| 146 | + // Monitor configuration |
| 147 | + interface MonitorConfig { |
| 148 | + type?: 'job' | 'check' | 'heartbeat'; |
| 149 | + key: string; |
| 150 | + schedule?: string; |
| 151 | + assertions?: string[]; |
| 152 | + notify?: string[]; |
| 153 | + request?: { |
| 154 | + url: string; |
| 155 | + regions?: string[]; |
| 156 | + method?: string; |
| 157 | + headers?: Record<string, string>; |
| 158 | + body?: string; |
| 159 | + timeout?: number; |
| 160 | + follow_redirects?: boolean; |
| 161 | + }; |
| 162 | + tags?: string[]; |
| 163 | + note?: string; |
| 164 | + grace_seconds?: number; |
| 165 | + realert_interval?: string; |
| 166 | + platform?: string; |
| 167 | + timezone?: string; |
| 168 | + environments?: string[]; |
| 169 | + metadata?: Record<string, any>; |
| 170 | + } |
| 171 | + |
| 172 | + interface PutOptions { |
| 173 | + rollback?: boolean; |
| 174 | + format?: 'json' | 'yaml'; |
| 175 | + } |
| 176 | + |
| 177 | + // Ping parameters |
| 178 | + interface PingParams { |
| 179 | + state?: 'run' | 'complete' | 'fail' | 'ok'; |
| 180 | + message?: string; |
| 181 | + metrics?: { |
| 182 | + duration?: number; |
| 183 | + count?: number; |
| 184 | + error_count?: number; |
| 185 | + [key: string]: number | undefined; |
| 186 | + }; |
| 187 | + series?: string; |
| 188 | + host?: string; |
| 189 | + env?: string; |
| 190 | + stamp?: number; |
| 191 | + } |
| 192 | + |
| 193 | + // Event class |
| 194 | + class Event { |
| 195 | + monitor: Monitor; |
| 196 | + intervalSeconds: number; |
| 197 | + intervalId: NodeJS.Timeout | null; |
| 198 | + |
| 199 | + constructor(key: string, options?: EventOptions); |
| 200 | + |
| 201 | + /** |
| 202 | + * Increment the tick counter |
| 203 | + */ |
| 204 | + tick(count?: number): void; |
| 205 | + |
| 206 | + /** |
| 207 | + * Increment the error counter |
| 208 | + */ |
| 209 | + error(): void; |
| 210 | + |
| 211 | + /** |
| 212 | + * Stop the event and flush remaining counts |
| 213 | + */ |
| 214 | + stop(): Promise<boolean>; |
| 215 | + |
| 216 | + /** |
| 217 | + * Stop the event and send a fail ping |
| 218 | + */ |
| 219 | + fail(message?: string | null): Promise<void>; |
| 220 | + } |
| 221 | + |
| 222 | + interface EventOptions { |
| 223 | + intervalSeconds?: number; |
| 224 | + } |
| 225 | + |
| 226 | + // Error classes |
| 227 | + class MonitorNotCreated extends Error { |
| 228 | + constructor(message?: string); |
| 229 | + } |
| 230 | + |
| 231 | + class ConfigError extends Error { |
| 232 | + constructor(message?: string); |
| 233 | + } |
| 234 | + |
| 235 | + class InvalidMonitor extends Error { |
| 236 | + constructor(message?: string); |
| 237 | + } |
| 238 | +} |
0 commit comments