@@ -2,23 +2,26 @@ import type { DiscordWebhookPayload } from "./embeds";
22
33const WEBHOOK_TIMEOUT_MS = 3000 ;
44
5- export async function post ( env : { DISCORD_WEBHOOK_URL ?: string } , payload : DiscordWebhookPayload ) : Promise < void > {
5+ // Fire-and-forget. No-op when the secret is unset. Never throws —
6+ // observability must not be able to break the request path.
7+ // Returns the Discord message ID when available (for threading replies).
8+ export async function post ( env : { DISCORD_WEBHOOK_URL ?: string } , payload : DiscordWebhookPayload ) : Promise < string | undefined > {
69 const url = env . DISCORD_WEBHOOK_URL ;
7- if ( ! url ) return ;
10+ if ( ! url ) return undefined ;
811
912 let body : string ;
1013 try {
1114 body = JSON . stringify ( payload ) ;
1215 } catch ( e ) {
1316 console . error ( "[obs] webhook payload JSON.stringify failed:" , e ) ;
14- return ;
17+ return undefined ;
1518 }
1619
1720 const controller = new AbortController ( ) ;
1821 const timeout = setTimeout ( ( ) => controller . abort ( ) , WEBHOOK_TIMEOUT_MS ) ;
1922
2023 try {
21- const res = await fetch ( url , {
24+ const res = await fetch ( ` ${ url } ?wait=true` , {
2225 method : "POST" ,
2326 headers : { "Content-Type" : "application/json" } ,
2427 body,
@@ -29,7 +32,10 @@ export async function post(env: { DISCORD_WEBHOOK_URL?: string }, payload: Disco
2932 console . error (
3033 `[obs] webhook non-2xx: status=${ res . status } statusText=${ res . statusText } body=${ resBody . slice ( 0 , 300 ) } ` ,
3134 ) ;
35+ return undefined ;
3236 }
37+ const data = await res . json < { id : string } > ( ) ;
38+ return data . id ;
3339 } catch ( e ) {
3440 if ( e instanceof Error && e . name === "AbortError" ) {
3541 console . error ( `[obs] webhook timed out after ${ WEBHOOK_TIMEOUT_MS } ms` ) ;
@@ -38,6 +44,7 @@ export async function post(env: { DISCORD_WEBHOOK_URL?: string }, payload: Disco
3844 const msg = e instanceof Error ? e . message : String ( e ) ;
3945 console . error ( `[obs] webhook post failed: ${ name } : ${ msg } ` ) ;
4046 }
47+ return undefined ;
4148 } finally {
4249 clearTimeout ( timeout ) ;
4350 }
0 commit comments