diff --git a/packages/plugin-network-breadcrumbs/package.json b/packages/plugin-network-breadcrumbs/package.json index 4a87cdb17f..c6f0cc9a1a 100644 --- a/packages/plugin-network-breadcrumbs/package.json +++ b/packages/plugin-network-breadcrumbs/package.json @@ -2,12 +2,12 @@ "name": "@bugsnag/plugin-network-breadcrumbs", "version": "8.1.1", "main": "dist/network-breadcrumbs.js", - "types": "dist/types/throttle.d.ts", + "types": "dist/types/network-breadcrumbs.d.ts", "exports": { ".": { - "types": "./dist/types/throttle.d.ts", - "default": "./dist/throttle.js", - "import": "./dist/throttle.mjs" + "types": "./dist/types/network-breadcrumbs.d.ts", + "default": "./dist/network-breadcrumbs.js", + "import": "./dist/network-breadcrumbs.mjs" } }, "description": "@bugsnag/js plugin to record browser requests as breadcrumbs", diff --git a/packages/plugin-network-breadcrumbs/src/network-breadcrumbs.ts b/packages/plugin-network-breadcrumbs/src/network-breadcrumbs.ts index a54a7958a0..87d0490fea 100644 --- a/packages/plugin-network-breadcrumbs/src/network-breadcrumbs.ts +++ b/packages/plugin-network-breadcrumbs/src/network-breadcrumbs.ts @@ -1,29 +1,29 @@ -import { Client, Logger, Plugin } from '@bugsnag/core' +import { Client, Config, Logger, Plugin } from '@bugsnag/core' import includes from '@bugsnag/core/lib/es-utils/includes' const BREADCRUMB_TYPE = 'request' interface InternalClient extends Client { _logger: Logger + _config: Required + _isBreadcrumbTypeEnabled: (type: string) => boolean } -type FetchArguments = Parameters +type FetchArguments = Parameters + /* * Leaves breadcrumbs when network requests occur */ export default (_ignoredUrls = [], win = window): Plugin => { - let restoreFunctions: any[] = [] + let restoreFunctions: Array<() => void> = [] const plugin: Plugin = { load: client => { const internalClient = client as InternalClient - // @ts-expect-error isBreadcrumbTypeEnabled is private API if (!internalClient._isBreadcrumbTypeEnabled('request')) return const ignoredUrls = [ - // @ts-expect-error _config is private API internalClient._config.endpoints.notify, - // @ts-expect-error _config is private API internalClient._config.endpoints.sessions ].concat(_ignoredUrls) @@ -38,12 +38,12 @@ export default (_ignoredUrls = [], win = window): Plugin => { const requestHandlers = new WeakMap() const originalOpen = win.XMLHttpRequest.prototype.open - win.XMLHttpRequest.prototype.open = function open (method: string, url: string) { + win.XMLHttpRequest.prototype.open = function open (method: string, url: string | URL) { // it's possible for `this` to be `undefined`, which is not a valid key for a WeakMap if (this) { trackedRequests.set(this, { method, url }) } - originalOpen.apply(this, arguments as any) + originalOpen.apply(this, arguments as unknown as Parameters) } const originalSend = win.XMLHttpRequest.prototype.send @@ -70,7 +70,7 @@ export default (_ignoredUrls = [], win = window): Plugin => { } } - originalSend.apply(this, arguments as any) + originalSend.apply(this, arguments as unknown as Parameters) } if (process.env.NODE_ENV !== 'production') { @@ -131,7 +131,7 @@ export default (_ignoredUrls = [], win = window): Plugin => { // only patch it if it exists and if it is not a polyfill (patching a polyfilled // fetch() results in duplicate breadcrumbs for the same request because the // implementation uses XMLHttpRequest which is also patched) - // @ts-expect-error we are expecting + // @ts-expect-error polyfill is not defined in the Fetch API if (!('fetch' in win) || win.fetch.polyfill) return const oldFetch = win.fetch @@ -140,9 +140,9 @@ export default (_ignoredUrls = [], win = window): Plugin => { const options = args[1] let method: string | undefined - let url: string | null = null + let url: string | URL | null = null - if (urlOrRequest && typeof urlOrRequest === 'object') { + if (urlOrRequest && typeof urlOrRequest === 'object' && 'url' in urlOrRequest) { url = urlOrRequest.url if (options && 'method' in options) { method = options.method @@ -166,12 +166,7 @@ export default (_ignoredUrls = [], win = window): Plugin => { // pass through to native fetch oldFetch(...args) .then(response => { - handleFetchSuccess( - response, - String(method), - String(url), - getDuration(requestStart) - ) + handleFetchSuccess(response, method, url, getDuration(requestStart)) resolve(response) }) .catch(error => { @@ -188,7 +183,7 @@ export default (_ignoredUrls = [], win = window): Plugin => { } } - const handleFetchSuccess = (response: Response, method: string, url: string, duration: number) => { + const handleFetchSuccess = (response: Response, method: string | undefined, url: string | URL | null, duration: number) => { const metadata = { method: String(method), status: response.status, diff --git a/packages/plugin-network-breadcrumbs/test/network-breadcrumbs.test.ts b/packages/plugin-network-breadcrumbs/test/network-breadcrumbs.test.ts index 512be6bfa6..9cdda0fc87 100644 --- a/packages/plugin-network-breadcrumbs/test/network-breadcrumbs.test.ts +++ b/packages/plugin-network-breadcrumbs/test/network-breadcrumbs.test.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ -import plugin from '../src/network-breadcrumbs' +import plugin from '../' import Client from '@bugsnag/core/client' import { Config } from '@bugsnag/core' diff --git a/packages/plugin-network-breadcrumbs/tsconfig.json b/packages/plugin-network-breadcrumbs/tsconfig.json index 4fe20b6abd..7346c3eb52 100644 --- a/packages/plugin-network-breadcrumbs/tsconfig.json +++ b/packages/plugin-network-breadcrumbs/tsconfig.json @@ -2,7 +2,6 @@ "extends": "../../tsconfig.json", "include": ["src/**/*.ts"], "compilerOptions": { - "target": "ES2020", "types": ["node"] } } diff --git a/packages/react-native/window.d.ts b/packages/react-native/window.d.ts new file mode 100644 index 0000000000..c9749a6b9d --- /dev/null +++ b/packages/react-native/window.d.ts @@ -0,0 +1,4 @@ +// Implement basic Window type for plugin-network-breadcrumbs +declare interface Window { + readonly fetch: unknown +}