Skip to content

Commit cdb2b36

Browse files
AnastasiiaSvietlovagingerbenw
authored andcommitted
Refactor @bugsnag/plugin-client-ip (#2260)
* refactor @bugsnag/plugin-client-ip * fix rollup config * update with internal client * update extended plugin interface * fix value type to unknown
1 parent 7836ce2 commit cdb2b36

6 files changed

Lines changed: 71 additions & 28 deletions

File tree

packages/plugin-client-ip/client-ip.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/plugin-client-ip/package.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
{
22
"name": "@bugsnag/plugin-client-ip",
33
"version": "8.1.1",
4-
"main": "client-ip.js",
4+
"main": "dist/client-ip.js",
5+
"types": "dist/types/client-ip.d.ts",
6+
"exports": {
7+
".": {
8+
"types": "./dist/types/client-ip.d.ts",
9+
"default": "./dist/client-ip.js",
10+
"import": "./dist/client-ip.mjs"
11+
}
12+
},
513
"description": "@bugsnag/js plugin to disable client IP from error reports",
614
"homepage": "https://www.bugsnag.com/",
715
"repository": {
@@ -12,7 +20,7 @@
1220
"access": "public"
1321
},
1422
"files": [
15-
"*.js"
23+
"dist"
1624
],
1725
"author": "Bugsnag",
1826
"license": "MIT",
@@ -21,5 +29,10 @@
2129
},
2230
"peerDependencies": {
2331
"@bugsnag/core": "^8.0.0"
32+
},
33+
"scripts": {
34+
"build": "npm run build:npm",
35+
"build:npm": "rollup --config rollup.config.npm.mjs",
36+
"clean": "rm -rf dist/*"
2437
}
2538
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import createRollupConfig from "../../.rollup/index.mjs";
2+
3+
export default createRollupConfig({
4+
input: "src/client-ip.ts",
5+
external: ["@bugsnag/core/lib/es-utils/assign"]
6+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Client, Plugin } from 'packages/core/types'
2+
import assign from '@bugsnag/core/lib/es-utils/assign'
3+
4+
interface InternalClient extends Client {
5+
_config: {
6+
collectUserIp: boolean
7+
}
8+
}
9+
10+
interface ExtendedPlugin extends Plugin {
11+
configSchema: Record<string, ValidationOption>
12+
}
13+
14+
interface ValidationOption {
15+
validate: (value: unknown) => boolean
16+
defaultValue: () => unknown
17+
message: string
18+
}
19+
20+
/*
21+
* Prevent collection of user IPs
22+
*/
23+
const plugin: ExtendedPlugin = {
24+
load: client => {
25+
if ((client as InternalClient)._config.collectUserIp) return
26+
27+
client.addOnError(event => {
28+
// If user.id is explicitly undefined, it will be missing from the payload. It needs
29+
// removing so that the following line replaces it
30+
if (event.getUser() && typeof event.getUser().id === 'undefined') {
31+
const _user = event.getUser()
32+
event.setUser('[REDACTED]', _user.email, _user.name)
33+
}
34+
event.request = assign({ clientIp: '[REDACTED]' }, event.request)
35+
})
36+
},
37+
configSchema: {
38+
collectUserIp: {
39+
defaultValue: () => true,
40+
message: 'should be true|false',
41+
validate: (value: unknown) => value === true || value === false
42+
}
43+
}
44+
}
45+
46+
export default plugin
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"include": ["src/**/*.ts"]
4+
}

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
"packages/plugin-koa",
7575
"packages/plugin-restify",
7676
"packages/node",
77-
"packages/plugin-client-ip",
7877
"packages/plugin-strip-query-string",
7978
"packages/plugin-strip-project-root",
8079
"packages/plugin-interaction-breadcrumbs",

0 commit comments

Comments
 (0)