-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
4605afc
commit af49fb0
Showing
6 changed files
with
71 additions
and
28 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import createRollupConfig from "../../.rollup/index.mjs"; | ||
|
||
export default createRollupConfig({ | ||
input: "src/client-ip.ts", | ||
external: ["@bugsnag/core/lib/es-utils/assign"] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Client, Plugin } from 'packages/core/types' | ||
import assign from '@bugsnag/core/lib/es-utils/assign' | ||
|
||
interface InternalClient extends Client { | ||
_config: { | ||
collectUserIp: boolean | ||
} | ||
} | ||
|
||
interface ExtendedPlugin extends Plugin { | ||
configSchema: Record<string, ValidationOption> | ||
} | ||
|
||
interface ValidationOption { | ||
validate: (value: unknown) => boolean | ||
defaultValue: () => unknown | ||
message: string | ||
} | ||
|
||
/* | ||
* Prevent collection of user IPs | ||
*/ | ||
const plugin: ExtendedPlugin = { | ||
load: client => { | ||
if ((client as InternalClient)._config.collectUserIp) return | ||
|
||
client.addOnError(event => { | ||
// If user.id is explicitly undefined, it will be missing from the payload. It needs | ||
// removing so that the following line replaces it | ||
if (event.getUser() && typeof event.getUser().id === 'undefined') { | ||
const _user = event.getUser() | ||
event.setUser('[REDACTED]', _user.email, _user.name) | ||
} | ||
event.request = assign({ clientIp: '[REDACTED]' }, event.request) | ||
}) | ||
}, | ||
configSchema: { | ||
collectUserIp: { | ||
defaultValue: () => true, | ||
message: 'should be true|false', | ||
validate: (value: unknown) => value === true || value === false | ||
} | ||
} | ||
} | ||
|
||
export default plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src/**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters