@@ -93,6 +93,22 @@ type TelegramFetchInput = Parameters<NonNullable<ApiClientOptions["fetch"]>>[0];
9393type TelegramFetchInit = Parameters < NonNullable < ApiClientOptions [ "fetch" ] > > [ 1 ] ;
9494type GlobalFetchInput = Parameters < typeof globalThis . fetch > [ 0 ] ;
9595type GlobalFetchInit = Parameters < typeof globalThis . fetch > [ 1 ] ;
96+ type TelegramCompatFetch = (
97+ input : TelegramFetchInput ,
98+ init ?: TelegramFetchInit ,
99+ ) => ReturnType < typeof globalThis . fetch > ;
100+
101+ function asTelegramClientFetch (
102+ fetchImpl : TelegramCompatFetch ,
103+ ) : NonNullable < ApiClientOptions [ "fetch" ] > {
104+ return fetchImpl as NonNullable < ApiClientOptions [ "fetch" ] > ;
105+ }
106+
107+ function asTelegramCompatFetch (
108+ fetchImpl : NonNullable < ApiClientOptions [ "fetch" ] > ,
109+ ) : TelegramCompatFetch {
110+ return fetchImpl as TelegramCompatFetch ;
111+ }
96112
97113function readRequestUrl ( input : TelegramFetchInput ) : string | null {
98114 if ( typeof input === "string" ) {
@@ -101,9 +117,8 @@ function readRequestUrl(input: TelegramFetchInput): string | null {
101117 if ( input instanceof URL ) {
102118 return input . toString ( ) ;
103119 }
104- if ( typeof input === "object" && input !== null && "url" in input ) {
105- const url = ( input as { url ?: unknown } ) . url ;
106- return typeof url === "string" ? url : null ;
120+ if ( input instanceof Request ) {
121+ return input . url ;
107122 }
108123 return null ;
109124}
@@ -163,23 +178,22 @@ export function createTelegramBot(opts: TelegramBotOptions) {
163178 const shouldProvideFetch = Boolean ( telegramTransport . fetch ) ;
164179 // grammY's ApiClientOptions types still track `node-fetch` types; Node 22+ global fetch
165180 // (undici) is structurally compatible at runtime but not assignable in TS.
166- const fetchForClient = telegramTransport . fetch as unknown as NonNullable <
167- ApiClientOptions [ " fetch" ]
168- > ;
181+ const fetchForClient = telegramTransport . fetch
182+ ? asTelegramClientFetch ( telegramTransport . fetch )
183+ : undefined ;
169184
170185 // Wrap fetch so polling requests cannot hang indefinitely on a wedged network path,
171186 // and so shutdown still aborts in-flight Telegram API requests immediately.
172187 let finalFetch = shouldProvideFetch ? fetchForClient : undefined ;
173188 if ( finalFetch || opts . fetchAbortSignal ) {
174- const baseFetch =
175- finalFetch ?? ( globalThis . fetch as unknown as NonNullable < ApiClientOptions [ "fetch" ] > ) ;
189+ const baseFetch = finalFetch ?? asTelegramClientFetch ( globalThis . fetch ) ;
176190 // Cast baseFetch to global fetch to avoid node-fetch ↔ global-fetch type divergence;
177191 // they are runtime-compatible (the codebase already casts at every fetch boundary).
178- const callFetch = baseFetch as unknown as typeof globalThis . fetch ;
192+ const callFetch = asTelegramCompatFetch ( baseFetch ) ;
179193 // Use manual event forwarding instead of AbortSignal.any() to avoid the cross-realm
180194 // AbortSignal issue in Node.js (grammY's signal may come from a different module context,
181195 // causing "signals[0] must be an instance of AbortSignal" errors).
182- finalFetch = ( ( input : TelegramFetchInput , init ?: TelegramFetchInit ) => {
196+ finalFetch = ( input : TelegramFetchInput , init ?: TelegramFetchInit ) => {
183197 const controller = new AbortController ( ) ;
184198 const abortWith = ( signal : AbortSignal ) => controller . abort ( signal . reason ) ;
185199 const shutdownSignal = opts . fetchAbortSignal ;
@@ -196,9 +210,9 @@ export function createTelegramBot(opts: TelegramBotOptions) {
196210 }
197211 if ( init ?. signal ) {
198212 if ( init . signal . aborted ) {
199- abortWith ( init . signal as unknown as AbortSignal ) ;
213+ abortWith ( init . signal ) ;
200214 } else {
201- onRequestAbort = ( ) => abortWith ( init . signal as AbortSignal ) ;
215+ onRequestAbort = ( ) => abortWith ( init . signal ) ;
202216 init . signal . addEventListener ( "abort" , onRequestAbort ) ;
203217 }
204218 }
@@ -220,11 +234,11 @@ export function createTelegramBot(opts: TelegramBotOptions) {
220234 init . signal . removeEventListener ( "abort" , onRequestAbort ) ;
221235 }
222236 } ) ;
223- } ) as unknown as NonNullable < ApiClientOptions [ "fetch" ] > ;
237+ } ;
224238 }
225239 if ( finalFetch ) {
226240 const baseFetch = finalFetch ;
227- finalFetch = ( ( input : TelegramFetchInput , init ?: TelegramFetchInit ) => {
241+ finalFetch = ( input : TelegramFetchInput , init ?: TelegramFetchInit ) => {
228242 return Promise . resolve ( baseFetch ( input , init ) ) . catch ( ( err : unknown ) => {
229243 try {
230244 tagTelegramNetworkError ( err , {
@@ -237,7 +251,7 @@ export function createTelegramBot(opts: TelegramBotOptions) {
237251 }
238252 throw err ;
239253 } ) ;
240- } ) as unknown as NonNullable < ApiClientOptions [ "fetch" ] > ;
254+ } ;
241255 }
242256
243257 const timeoutSeconds =
0 commit comments