Skip to content

Commit 8ad8a7d

Browse files
committed
TS conversion for plugin-network-instrumentation
1 parent 48f6081 commit 8ad8a7d

4 files changed

Lines changed: 28 additions & 17 deletions

File tree

packages/plugin-network-instrumentation/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,10 @@
3737
},
3838
"peerDependencies": {
3939
"@bugsnag/core": "^8.0.0"
40+
},
41+
"scripts": {
42+
"clean": "rm -fr dist",
43+
"prebuild": "npm run clean",
44+
"build": "rollup --config rollup.config.mjs"
4045
}
4146
}
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
/**
2-
* Redact values in an object based on a list of keys
2+
* Redact values in an object based on a list of keys or patterns
33
* @param obj - The object to redact
4-
* @param keys - The keys to redact
4+
* @param keys - The keys or patterns to redact
55
* @returns A new object with redacted values
66
*/
7-
export default function redactValues<T extends Record<string, any>>(obj: T, keys: Array<keyof T>): T {
8-
const redacted: T = { ...obj }
9-
for (const key of keys) {
10-
if (Object.prototype.hasOwnProperty.call(redacted, key)) {
11-
redacted[key] = '[REDACTED]' as any
7+
export default function redactValues<T extends Record<string, any>>(obj: T, keys: (string | RegExp)[]): T {
8+
const redacted: Record<string, any> = { ...obj }
9+
for (const key of Object.keys(redacted)) {
10+
for (const pattern of keys) {
11+
if (
12+
(typeof pattern === 'string' && key === pattern) ||
13+
(pattern instanceof RegExp && pattern.test(key))
14+
) {
15+
redacted[key] = '[REDACTED]'
16+
break
17+
}
1218
}
1319
}
14-
return redacted
20+
return redacted as T
1521
}

packages/plugin-network-instrumentation/src/network-instrumentation.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ const networkInstrumentation = (
3434
onHttpError
3535
} = config
3636

37-
// Normalize httpErrorCodes to an array
38-
const normalizedStatusCodes = Array.isArray(httpErrorCodes) ? httpErrorCodes : [httpErrorCodes]
37+
// Normalize httpErrorCodes to an array of {min, max} objects
38+
const normalizedStatusCodes = (Array.isArray(httpErrorCodes) ? httpErrorCodes : [httpErrorCodes])
39+
.map(code => typeof code === 'number' ? { min: code, max: code } : code)
3940

4041
let restoreFunctions: Array<() => void> = []
4142
const plugin: Plugin = {
@@ -47,14 +48,12 @@ const networkInstrumentation = (
4748
// Auto-load request tracker if not present
4849
if (!requestTrackerPlugin) {
4950
try {
50-
// @ts-ignore
5151
const { createRequestTrackerPlugin } = require('@bugsnag/request-tracker')
5252
const trackerPlugin = createRequestTrackerPlugin([], global)
53-
// @ts-ignore
5453
client._loadPlugin(trackerPlugin)
5554
requestTrackerPlugin = client.getPlugin && client.getPlugin('requestTracker')
5655
} catch (error: any) {
57-
client._logger?.warn?.('Failed to auto-load request tracker, using direct fetch patching:', error.message)
56+
client._logger?.warn?.('Failed to auto-load request tracker, using direct fetch patching: '+ error.message)
5857
}
5958
}
6059

@@ -104,12 +103,14 @@ const networkInstrumentation = (
104103
httpMethod: method,
105104
headers: startContext.headers,
106105
params: redactValues(requestParams, client._config.redactedKeys),
107-
bodyLength: startContext.body ? startContext.body.length : undefined
106+
bodyLength: startContext.body ? startContext.body.length : undefined,
107+
body: undefined as string | undefined
108108
}
109109
const responseObj = {
110110
statusCode: endContext.status,
111111
headers: endContext.headers,
112-
bodyLength: endContext.body ? endContext.body.length : undefined
112+
bodyLength: endContext.body ? endContext.body.length : undefined,
113+
body: undefined as string | undefined
113114
}
114115

115116
// Call onHttpError callback if provided
@@ -142,7 +143,6 @@ const networkInstrumentation = (
142143
severityReason: { type: 'httpError' }
143144
}
144145

145-
// @ts-ignore
146146
const event: Event = client.Event.create(
147147
error,
148148
true,

packages/plugin-network-instrumentation/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"outDir": "dist",
66
"declarationDir": "dist/types"
77
},
8-
"include": ["src/**/*.ts", "src/lib/redact-values.ts"]
8+
"include": ["src/**/*.ts"]
99
}

0 commit comments

Comments
 (0)