From b255da4ca2f893c02a5f51cb0f58feb557b5964d Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Wed, 30 Oct 2024 11:56:57 +0000 Subject: [PATCH] chore: :label: fix types and remove usage of ts-expect-error --- packages/core/lib/es-utils/reduce.d.ts | 2 +- .../src/console-breadcrumbs.ts | 38 ++++++++++++------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/core/lib/es-utils/reduce.d.ts b/packages/core/lib/es-utils/reduce.d.ts index f77c6a8dc..4a468b0a7 100644 --- a/packages/core/lib/es-utils/reduce.d.ts +++ b/packages/core/lib/es-utils/reduce.d.ts @@ -1 +1 @@ -export default function reduce(arr: T[], fn: (accum: any, arg: any, item: T) => any, accum: any): any +export default function reduce(arr: T[], fn: (accum: any, item: T, index: number, arr: T[]) => any, accum: any): any diff --git a/packages/plugin-console-breadcrumbs/src/console-breadcrumbs.ts b/packages/plugin-console-breadcrumbs/src/console-breadcrumbs.ts index 06ab7930f..39ba7b609 100644 --- a/packages/plugin-console-breadcrumbs/src/console-breadcrumbs.ts +++ b/packages/plugin-console-breadcrumbs/src/console-breadcrumbs.ts @@ -1,18 +1,29 @@ -import { Plugin } from '@bugsnag/core' +import { Client, Config, Plugin } from '@bugsnag/core' import filter from '@bugsnag/core/lib/es-utils/filter' import map from '@bugsnag/core/lib/es-utils/map' import reduce from '@bugsnag/core/lib/es-utils/reduce' +type ConsoleMethod = 'log' | 'debug' | 'info' | 'warn' | 'error' + +interface ClientWithInternals extends Client { + _config: Required + _isBreadcrumbTypeEnabled: (type: ConsoleMethod) => boolean +} + +type ConsoleWithRestore = Console & { + [key in ConsoleMethod]: typeof console[ConsoleMethod] & { _restore: () => void } +} + /* * Leaves breadcrumbs when console log methods are called */ const plugin: Plugin = { - load: client => { - // @ts-expect-error _config is private API - const isDev = /^(local-)?dev(elopment)?$/.test(client._config.releaseStage) + load: (client) => { + const internalClient = client as ClientWithInternals + + const isDev = /^(local-)?dev(elopment)?$/.test(internalClient._config.releaseStage) - // @ts-expect-error isBreadcrumbTypeEnabled is private API - if (isDev || !client._isBreadcrumbTypeEnabled('log')) return + if (isDev || !internalClient._isBreadcrumbTypeEnabled('log')) return map(CONSOLE_LOG_METHODS, method => { const original = console[method] @@ -48,8 +59,7 @@ const plugin: Plugin = { ) original.apply(console, args) } - // @ts-expect-error _restore is added to be able to remove our monkey patched code - console[method]._restore = () => { + (console as ConsoleWithRestore)[method]._restore = () => { console[method] = original } }) @@ -57,17 +67,17 @@ const plugin: Plugin = { } if (process.env.NODE_ENV !== 'production') { - plugin.destroy = () => + plugin.destroy = () => { + const consoleWithRestore = console as ConsoleWithRestore CONSOLE_LOG_METHODS.forEach(method => { - // @ts-expect-error _restore is added to be able to remove our monkey patched code - if (typeof console[method]._restore === 'function') { - // @ts-expect-error _restore is added to be able to remove our monkey patched code - console[method]._restore() + if (typeof consoleWithRestore[method]._restore === 'function') { + consoleWithRestore[method]._restore() } }) + } } -const CONSOLE_LOG_METHODS: Array<'log' | 'debug' | 'info' | 'warn' | 'error'> = filter( +const CONSOLE_LOG_METHODS: ConsoleMethod[] = filter( ['log', 'debug', 'info', 'warn', 'error'], method => typeof console !== 'undefined' && typeof console[method] === 'function'