Openstatus sdk www.openstatus.dev
at main 201 lines 5.7 kB view raw
1/** 2 * @module 3 * 4 * Official Node.js SDK for OpenStatus - the open-source monitoring platform. 5 * 6 * @example Basic usage 7 * ```typescript 8 * import { openstatus, Periodicity, Region } from "@openstatus/sdk-node"; 9 * 10 * const headers = { 11 * "x-openstatus-key": `Bearer ${process.env.OPENSTATUS_API_KEY}`, 12 * }; 13 * 14 * // List all monitors 15 * const { httpMonitors, tcpMonitors, dnsMonitors } = 16 * await openstatus.monitor.v1.MonitorService.listMonitors({}, { headers }); 17 * 18 * // Create an HTTP monitor 19 * const { monitor } = await openstatus.monitor.v1.MonitorService.createHTTPMonitor({ 20 * monitor: { 21 * name: "My API", 22 * url: "https://api.example.com/health", 23 * periodicity: Periodicity.PERIODICITY_1M, 24 * regions: [Region.FLY_AMS, Region.FLY_IAD, Region.FLY_SYD], 25 * active: true, 26 * }, 27 * }, { headers }); 28 * ``` 29 */ 30 31import type { Client } from "@connectrpc/connect"; 32import { createClient } from "@connectrpc/connect"; 33import { createConnectTransport } from "@connectrpc/connect-node"; 34import { MonitorService } from "./gen/openstatus/monitor/v1/service_pb.ts"; 35import { HealthService } from "./gen/openstatus/health/v1/health_pb.ts"; 36 37// Re-export monitor types 38export type { 39 Headers, 40 HTTPMonitor, 41 OpenTelemetryConfig, 42} from "./gen/openstatus/monitor/v1/http_monitor_pb.ts"; 43 44export type { TCPMonitor } from "./gen/openstatus/monitor/v1/tcp_monitor_pb.ts"; 45 46export type { DNSMonitor } from "./gen/openstatus/monitor/v1/dns_monitor_pb.ts"; 47 48// Re-export assertion types 49export type { 50 BodyAssertion, 51 HeaderAssertion, 52 RecordAssertion, 53 StatusCodeAssertion, 54} from "./gen/openstatus/monitor/v1/assertions_pb.ts"; 55 56// Re-export assertion comparator enums 57export { 58 NumberComparator, 59 StringComparator, 60 RecordComparator, 61} from "./gen/openstatus/monitor/v1/assertions_pb.ts"; 62 63// Re-export enums 64export { HTTPMethod } from "./gen/openstatus/monitor/v1/http_monitor_pb.ts"; 65 66export { Periodicity, Region } from "./gen/openstatus/monitor/v1/monitor_pb.ts"; 67 68export { MonitorStatus } from "./gen/openstatus/monitor/v1/monitor_pb.ts"; 69 70// Re-export request/response types 71export type { 72 CreateDNSMonitorRequest, 73 CreateDNSMonitorResponse, 74 CreateHTTPMonitorRequest, 75 CreateHTTPMonitorResponse, 76 CreateTCPMonitorRequest, 77 CreateTCPMonitorResponse, 78 UpdateDNSMonitorRequest, 79 UpdateDNSMonitorResponse, 80 UpdateHTTPMonitorRequest, 81 UpdateHTTPMonitorResponse, 82 UpdateTCPMonitorRequest, 83 UpdateTCPMonitorResponse, 84 DeleteMonitorRequest, 85 DeleteMonitorResponse, 86 GetMonitorStatusRequest, 87 GetMonitorStatusResponse, 88 GetMonitorSummaryRequest, 89 GetMonitorSummaryResponse, 90 ListMonitorsRequest, 91 ListMonitorsResponse, 92 RegionStatus, 93 TriggerMonitorRequest, 94 TriggerMonitorResponse, 95} from "./gen/openstatus/monitor/v1/service_pb.ts"; 96 97export { TimeRange } from "./gen/openstatus/monitor/v1/service_pb.ts"; 98 99// Re-export health types 100export type { 101 CheckRequest, 102 CheckResponse, 103} from "./gen/openstatus/health/v1/health_pb.ts"; 104 105export { CheckResponse_ServingStatus as ServingStatus } from "./gen/openstatus/health/v1/health_pb.ts"; 106 107/** 108 * Default OpenStatus API URL. 109 */ 110const DEFAULT_API_URL = "https://api.openstatus.dev/rpc"; 111 112/** 113 * Creates a Connect RPC transport configured for the OpenStatus API. 114 */ 115const transport = createConnectTransport({ 116 baseUrl: process.env.OPENSTATUS_API_URL ?? DEFAULT_API_URL, 117 httpVersion: "2", 118}); 119 120/** 121 * OpenStatus API client interface. 122 * 123 * Provides access to Monitor and Health services. 124 */ 125export interface OpenStatusClient { 126 /** 127 * Monitor service namespace (v1). 128 */ 129 monitor: { 130 v1: { 131 /** 132 * MonitorService provides CRUD and operational commands for monitors. 133 * 134 * Methods: 135 * - `createHTTPMonitor` - Create a new HTTP monitor 136 * - `createTCPMonitor` - Create a new TCP monitor 137 * - `createDNSMonitor` - Create a new DNS monitor 138 * - `updateHTTPMonitor` - Update an existing HTTP monitor 139 * - `updateTCPMonitor` - Update an existing TCP monitor 140 * - `updateDNSMonitor` - Update an existing DNS monitor 141 * - `listMonitors` - List all monitors 142 * - `triggerMonitor` - Trigger an immediate check 143 * - `deleteMonitor` - Delete a monitor 144 * - `getMonitorStatus` - Get status of all regions for a monitor 145 * - `getMonitorSummary` - Get aggregated metrics for a monitor 146 */ 147 MonitorService: Client<typeof MonitorService>; 148 }; 149 }; 150 /** 151 * Health service namespace (v1). 152 */ 153 health: { 154 v1: { 155 /** 156 * HealthService provides health check endpoints. 157 * 158 * Methods: 159 * - `check` - Check API health status 160 */ 161 HealthService: Client<typeof HealthService>; 162 }; 163 }; 164} 165 166/** 167 * OpenStatus SDK client. 168 * 169 * Provides access to the OpenStatus API for managing monitors and checking service health. 170 * 171 * @example 172 * ```typescript 173 * import { openstatus, Periodicity } from "@openstatus/sdk-node"; 174 * 175 * // Check API health (no auth required) 176 * const { status } = await openstatus.health.v1.HealthService.check({}); 177 * 178 * // Create a monitor (auth required) 179 * const headers = { "x-openstatus-key": `Bearer ${process.env.OPENSTATUS_API_KEY}` }; 180 * const { monitor } = await openstatus.monitor.v1.MonitorService.createHTTPMonitor({ 181 * monitor: { 182 * name: "My Website", 183 * url: "https://example.com", 184 * periodicity: Periodicity.PERIODICITY_1M, 185 * active: true, 186 * }, 187 * }, { headers }); 188 * ``` 189 */ 190export const openstatus: OpenStatusClient = { 191 monitor: { 192 v1: { 193 MonitorService: createClient(MonitorService, transport), 194 }, 195 }, 196 health: { 197 v1: { 198 HealthService: createClient(HealthService, transport), 199 }, 200 }, 201};