1- import { Client , Logger , Plugin } from '@bugsnag/core'
1+ import { Client , Config , Logger , Plugin } from '@bugsnag/core'
22import includes from '@bugsnag/core/lib/es-utils/includes'
33
44const BREADCRUMB_TYPE = 'request'
55
66interface InternalClient extends Client {
77 _logger : Logger
8+ _config : Required < Config >
9+ _isBreadcrumbTypeEnabled : ( type : string ) => boolean
810}
911
10- type FetchArguments = Parameters < typeof window . fetch >
12+ type FetchArguments = Parameters < Window [ 'fetch' ] >
13+
1114/*
1215 * Leaves breadcrumbs when network requests occur
1316 */
1417export default ( _ignoredUrls = [ ] , win = window ) : Plugin => {
15- let restoreFunctions : any [ ] = [ ]
18+ let restoreFunctions : Array < ( ) => void > = [ ]
1619 const plugin : Plugin = {
1720 load : client => {
1821 const internalClient = client as InternalClient
1922
20- // @ts -expect-error isBreadcrumbTypeEnabled is private API
2123 if ( ! internalClient . _isBreadcrumbTypeEnabled ( 'request' ) ) return
2224
2325 const ignoredUrls = [
24- // @ts -expect-error _config is private API
2526 internalClient . _config . endpoints . notify ,
26- // @ts -expect-error _config is private API
2727 internalClient . _config . endpoints . sessions
2828 ] . concat ( _ignoredUrls )
2929
@@ -38,12 +38,12 @@ export default (_ignoredUrls = [], win = window): Plugin => {
3838 const requestHandlers = new WeakMap ( )
3939
4040 const originalOpen = win . XMLHttpRequest . prototype . open
41- win . XMLHttpRequest . prototype . open = function open ( method : string , url : string ) {
41+ win . XMLHttpRequest . prototype . open = function open ( method : string , url : string | URL ) {
4242 // it's possible for `this` to be `undefined`, which is not a valid key for a WeakMap
4343 if ( this ) {
4444 trackedRequests . set ( this , { method, url } )
4545 }
46- originalOpen . apply ( this , arguments as any )
46+ originalOpen . apply ( this , arguments as unknown as Parameters < typeof originalOpen > )
4747 }
4848
4949 const originalSend = win . XMLHttpRequest . prototype . send
@@ -70,7 +70,7 @@ export default (_ignoredUrls = [], win = window): Plugin => {
7070 }
7171 }
7272
73- originalSend . apply ( this , arguments as any )
73+ originalSend . apply ( this , arguments as unknown as Parameters < typeof originalSend > )
7474 }
7575
7676 if ( process . env . NODE_ENV !== 'production' ) {
@@ -131,7 +131,7 @@ export default (_ignoredUrls = [], win = window): Plugin => {
131131 // only patch it if it exists and if it is not a polyfill (patching a polyfilled
132132 // fetch() results in duplicate breadcrumbs for the same request because the
133133 // implementation uses XMLHttpRequest which is also patched)
134- // @ts -expect-error we are expecting
134+ // @ts -expect-error polyfill is not defined in the Fetch API
135135 if ( ! ( 'fetch' in win ) || win . fetch . polyfill ) return
136136
137137 const oldFetch = win . fetch
@@ -140,9 +140,9 @@ export default (_ignoredUrls = [], win = window): Plugin => {
140140 const options = args [ 1 ]
141141
142142 let method : string | undefined
143- let url : string | null = null
143+ let url : string | URL | null = null
144144
145- if ( urlOrRequest && typeof urlOrRequest === 'object' ) {
145+ if ( urlOrRequest && typeof urlOrRequest === 'object' && 'url' in urlOrRequest ) {
146146 url = urlOrRequest . url
147147 if ( options && 'method' in options ) {
148148 method = options . method
@@ -166,12 +166,7 @@ export default (_ignoredUrls = [], win = window): Plugin => {
166166 // pass through to native fetch
167167 oldFetch ( ...args )
168168 . then ( response => {
169- handleFetchSuccess (
170- response ,
171- String ( method ) ,
172- String ( url ) ,
173- getDuration ( requestStart )
174- )
169+ handleFetchSuccess ( response , method , url , getDuration ( requestStart ) )
175170 resolve ( response )
176171 } )
177172 . catch ( error => {
@@ -188,7 +183,7 @@ export default (_ignoredUrls = [], win = window): Plugin => {
188183 }
189184 }
190185
191- const handleFetchSuccess = ( response : Response , method : string , url : string , duration : number ) => {
186+ const handleFetchSuccess = ( response : Response , method : string | undefined , url : string | URL | null , duration : number ) => {
192187 const metadata = {
193188 method : String ( method ) ,
194189 status : response . status ,
0 commit comments