diff --git a/packages/plugin-simple-throttle/package.json b/packages/plugin-simple-throttle/package.json index bb89c0145f..9a9b25b104 100644 --- a/packages/plugin-simple-throttle/package.json +++ b/packages/plugin-simple-throttle/package.json @@ -2,6 +2,14 @@ "name": "@bugsnag/plugin-simple-throttle", "version": "8.1.1", "main": "throttle.js", + "types": "dist/types/throttle.d.ts", + "exports": { + ".": { + "types": "./dist/types/throttle.d.ts", + "default": "./dist/throttle.js", + "import": "./dist/throttle.mjs" + } + }, "description": "@bugsnag/js plugin to prevent too many events from being sent", "homepage": "https://www.bugsnag.com/", "repository": { @@ -14,7 +22,11 @@ "files": [ "*.js" ], - "scripts": {}, + "scripts": { + "build": "npm run build:npm", + "build:npm": "rollup --config rollup.config.npm.mjs", + "clean": "rm -rf dist/*" + }, "author": "Bugsnag", "license": "MIT", "devDependencies": { diff --git a/packages/plugin-simple-throttle/rollup.config.npm.mjs b/packages/plugin-simple-throttle/rollup.config.npm.mjs new file mode 100644 index 0000000000..bb2a0d724e --- /dev/null +++ b/packages/plugin-simple-throttle/rollup.config.npm.mjs @@ -0,0 +1,6 @@ +import createRollupConfig from '../../.rollup/index.mjs' + +export default createRollupConfig({ + input: 'src/throttle.ts', + external: ['@bugsnag/core/lib/validators/int-range'] +}) diff --git a/packages/plugin-simple-throttle/src/throttle.ts b/packages/plugin-simple-throttle/src/throttle.ts new file mode 100644 index 0000000000..1220c51995 --- /dev/null +++ b/packages/plugin-simple-throttle/src/throttle.ts @@ -0,0 +1,49 @@ +import { Client, Config, Logger, Plugin } from '@bugsnag/core' +import intRange from '@bugsnag/core/lib/validators/int-range' + +interface ThrottlePlugin extends Plugin { + configSchema: { + [key: string]: { + defaultValue: () => unknown + message: string + validate: (value: unknown) => boolean + } + } +} + +interface InternalClient extends Client { + _config: Config & { maxEvents: number } + _logger: Logger +} + +/* + * Throttles and dedupes events + */ + +const plugin: ThrottlePlugin = { + load: (client) => { + // track sent events for each init of the plugin + let n = 0 + + // add onError hook + client.addOnError((event) => { + // have max events been sent already? + if (n >= (client as InternalClient)._config.maxEvents) { + (client as InternalClient)._logger.warn(`Cancelling event send due to maxEvents per session limit of ${(client as InternalClient)._config.maxEvents} being reached`) + return false + } + n++ + }) + + client.resetEventCount = () => { n = 0 } + }, + configSchema: { + maxEvents: { + defaultValue: () => 10, + message: 'should be a positive integer ≤100', + validate: val => intRange(1, 100)(val) + } + } +} + +export default plugin diff --git a/packages/plugin-simple-throttle/test/throttle.test.ts b/packages/plugin-simple-throttle/test/throttle.test.ts index 32589456f4..29d27a5bc1 100644 --- a/packages/plugin-simple-throttle/test/throttle.test.ts +++ b/packages/plugin-simple-throttle/test/throttle.test.ts @@ -1,4 +1,4 @@ -import plugin from '../' +import plugin from '../src/throttle' import Client from '@bugsnag/core/client' diff --git a/packages/plugin-simple-throttle/throttle.js b/packages/plugin-simple-throttle/throttle.js deleted file mode 100644 index 3de502ec61..0000000000 --- a/packages/plugin-simple-throttle/throttle.js +++ /dev/null @@ -1,31 +0,0 @@ -const intRange = require('@bugsnag/core/lib/validators/int-range') - -/* - * Throttles and dedupes events - */ - -module.exports = { - load: (client) => { - // track sent events for each init of the plugin - let n = 0 - - // add onError hook - client.addOnError((event) => { - // have max events been sent already? - if (n >= client._config.maxEvents) { - client._logger.warn(`Cancelling event send due to maxEvents per session limit of ${client._config.maxEvents} being reached`) - return false - } - n++ - }) - - client.resetEventCount = () => { n = 0 } - }, - configSchema: { - maxEvents: { - defaultValue: () => 10, - message: 'should be a positive integer ≤100', - validate: val => intRange(1, 100)(val) - } - } -} diff --git a/packages/plugin-simple-throttle/tsconfig.json b/packages/plugin-simple-throttle/tsconfig.json new file mode 100644 index 0000000000..09758ceb4c --- /dev/null +++ b/packages/plugin-simple-throttle/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "../../tsconfig.json", + "include": ["src/**/*.ts"] +} + \ No newline at end of file