Skip to content

Commit

Permalink
update types and exports
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerbenw committed Oct 31, 2024
1 parent cca05c0 commit 1298756
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
8 changes: 4 additions & 4 deletions packages/plugin-network-breadcrumbs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 14 additions & 19 deletions packages/plugin-network-breadcrumbs/src/network-breadcrumbs.ts
Original file line number Diff line number Diff line change
@@ -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<Config>
_isBreadcrumbTypeEnabled: (type: string) => boolean
}

type FetchArguments = Parameters<typeof window.fetch>
type FetchArguments = Parameters<Window['fetch']>

/*
* 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)

Expand All @@ -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<typeof originalOpen>)
}

const originalSend = win.XMLHttpRequest.prototype.send
Expand All @@ -70,7 +70,7 @@ export default (_ignoredUrls = [], win = window): Plugin => {
}
}

originalSend.apply(this, arguments as any)
originalSend.apply(this, arguments as unknown as Parameters<typeof originalSend>)
}

if (process.env.NODE_ENV !== 'production') {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 => {
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-network-breadcrumbs/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts"],
"compilerOptions": {
"target": "ES2020",
"types": ["node"]
}
}
4 changes: 4 additions & 0 deletions packages/react-native/window.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Implement basic Window type for plugin-network-breadcrumbs
declare interface Window {
readonly fetch: unknown
}

0 comments on commit 1298756

Please sign in to comment.