|
| 1 | +import { z } from "zod"; |
| 2 | + |
| 3 | +// Payload for the BetterStack status-page webhook. The endpoint is unsigned, so |
| 4 | +// the route auths via a shared secret in the URL. |
| 5 | + |
| 6 | +// BetterStack sends ids as numbers; accept either and normalize to string. |
| 7 | +const IdSchema = z.union([z.string(), z.number()]).transform((v) => String(v)); |
| 8 | + |
| 9 | +export const IncidentUpdateSchema = z.object({ |
| 10 | + id: IdSchema, |
| 11 | + status_report_id: IdSchema.optional(), |
| 12 | + body: z.string().nullish(), |
| 13 | + created_at: z.string().nullish(), |
| 14 | + updated_at: z.string().nullish(), |
| 15 | +}); |
| 16 | + |
| 17 | +export const IncidentWebhookSchema = z.object({ |
| 18 | + event_type: z.string(), |
| 19 | + page: z |
| 20 | + .object({ |
| 21 | + id: IdSchema.optional(), |
| 22 | + status_indicator: z.string().nullish(), |
| 23 | + status_description: z.string().nullish(), |
| 24 | + }) |
| 25 | + .optional(), |
| 26 | + // Optional so non-incident callbacks (maintenance/component) parse and are |
| 27 | + // ignored instead of 400ing. |
| 28 | + incident: z |
| 29 | + .object({ |
| 30 | + id: IdSchema, |
| 31 | + name: z.string().nullish(), |
| 32 | + created_at: z.string().nullish(), |
| 33 | + updated_at: z.string().nullish(), |
| 34 | + shortlink: z.string().nullish(), |
| 35 | + incident_updates: z.array(IncidentUpdateSchema).default([]), |
| 36 | + }) |
| 37 | + .optional(), |
| 38 | +}); |
| 39 | + |
| 40 | +export type IncidentWebhook = z.infer<typeof IncidentWebhookSchema>; |
| 41 | + |
| 42 | +export const NormalizedIncidentUpdateSchema = z.object({ |
| 43 | + incidentId: z.string(), |
| 44 | + updateId: z.string(), |
| 45 | + name: z.string(), |
| 46 | + statusIndicator: z.string(), |
| 47 | + body: z.string(), |
| 48 | + shortlink: z.string().nullable(), |
| 49 | + updatedAt: z.string().nullable(), |
| 50 | +}); |
| 51 | + |
| 52 | +export type NormalizedIncidentUpdate = { |
| 53 | + incidentId: string; |
| 54 | + /** The specific update id — our idempotency key. */ |
| 55 | + updateId: string; |
| 56 | + name: string; |
| 57 | + /** operational | degraded | downtime | maintenance */ |
| 58 | + statusIndicator: string; |
| 59 | + body: string; |
| 60 | + shortlink: string | null; |
| 61 | + updatedAt: string | null; |
| 62 | +}; |
| 63 | + |
| 64 | +/** Only published "incident" events notify customers, not monitor auto-alerts. */ |
| 65 | +export function isCustomerNotifiableEvent(payload: IncidentWebhook): boolean { |
| 66 | + return payload.event_type === "incident" && !!payload.incident; |
| 67 | +} |
| 68 | + |
| 69 | +/** Reduce the webhook to its most recent update, or null if there are none. */ |
| 70 | +export function normalizeIncidentUpdate(payload: IncidentWebhook): NormalizedIncidentUpdate | null { |
| 71 | + if (!payload.incident) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + const updates = payload.incident.incident_updates; |
| 76 | + if (updates.length === 0) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + // Sort by created_at so we don't rely on BetterStack's ordering. |
| 81 | + const mostRecent = [...updates].sort((a, b) => { |
| 82 | + const aTime = a.created_at ? Date.parse(a.created_at) : 0; |
| 83 | + const bTime = b.created_at ? Date.parse(b.created_at) : 0; |
| 84 | + return bTime - aTime; |
| 85 | + })[0]; |
| 86 | + |
| 87 | + return { |
| 88 | + incidentId: payload.incident.id, |
| 89 | + updateId: mostRecent.id, |
| 90 | + name: payload.incident.name?.trim() || "Service incident", |
| 91 | + statusIndicator: payload.page?.status_indicator?.trim() || "downtime", |
| 92 | + body: mostRecent.body?.trim() || "", |
| 93 | + shortlink: payload.incident.shortlink?.trim() || null, |
| 94 | + updatedAt: mostRecent.created_at ?? payload.incident.updated_at ?? null, |
| 95 | + }; |
| 96 | +} |
0 commit comments