Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
AnastasiiaSvietlova authored and gingerbenw committed Oct 30, 2024
1 parent 0706701 commit cca05c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-network-breadcrumbs/rollup.config.npm.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import createRollupConfig from "../../.rollup/index.mjs";

export default createRollupConfig({
input: "src/console-breadcrumbs.ts",
input: "src/network-breadcrumbs.ts",
external: ["@bugsnag/core/lib/es-utils/includes"]
});
28 changes: 15 additions & 13 deletions packages/plugin-network-breadcrumbs/src/network-breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const BREADCRUMB_TYPE = 'request'
interface InternalClient extends Client {
_logger: Logger
}

type FetchArguments = Parameters<typeof window.fetch>
/*
* Leaves breadcrumbs when network requests occur
*/
Expand Down Expand Up @@ -129,15 +131,16 @@ 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
if (!('fetch' in win) || win.fetch.polyfill) return

const oldFetch = win.fetch
win.fetch = function fetch () {
const urlOrRequest = arguments[0]
const options = arguments[1]
win.fetch = function fetch (...args: FetchArguments) {
const urlOrRequest = args[0]
const options = args[1]

let method
let url = null
let method: string | undefined
let url: string | null = null

if (urlOrRequest && typeof urlOrRequest === 'object') {
url = urlOrRequest.url
Expand All @@ -160,20 +163,19 @@ export default (_ignoredUrls = [], win = window): Plugin => {
return new Promise((resolve, reject) => {
const requestStart = new Date()

// const [urlOrRequest, options] = arguments
// pass through to native fetch
oldFetch(...arguments)
oldFetch(...args)
.then(response => {
handleFetchSuccess(
response,
method,
url,
String(method),
String(url),
getDuration(requestStart)
)
resolve(response)
})
.catch(error => {
handleFetchError(method, url, getDuration(requestStart))
handleFetchError(String(method), String(url), getDuration(requestStart))
reject(error)
})
})
Expand All @@ -186,7 +188,7 @@ export default (_ignoredUrls = [], win = window): Plugin => {
}
}

const handleFetchSuccess = (response, method, url, duration) => {
const handleFetchSuccess = (response: Response, method: string, url: string, duration: number) => {
const metadata = {
method: String(method),
status: response.status,
Expand All @@ -201,7 +203,7 @@ export default (_ignoredUrls = [], win = window): Plugin => {
}
}

const handleFetchError = (method, url, duration) => {
const handleFetchError = (method: string, url: string, duration: number) => {
internalClient.leaveBreadcrumb('fetch() error', { method: String(method), url: String(url), duration: duration }, BREADCRUMB_TYPE)
}
}
Expand All @@ -217,4 +219,4 @@ export default (_ignoredUrls = [], win = window): Plugin => {
return plugin
}

const getDuration = (startTime): number => startTime && new Date() as unknown as number - startTime
const getDuration = (startTime: Date): number => startTime && Number(new Date()) - Number(startTime)

0 comments on commit cca05c0

Please sign in to comment.