Skip to content

Commit

Permalink
Convert @bugsnag/plugin-simple-throttle to TypeScript (#2242)
Browse files Browse the repository at this point in the history
* update rollup configuration

* convert plugin-simple-throttle to typescript

* fix: int-range

---------

Co-authored-by: Ben Wilson <[email protected]>
  • Loading branch information
AnastasiiaSvietlova and gingerbenw authored Nov 15, 2024
1 parent eade9df commit 97819cb
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/core/lib/validators/int-range.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default function intRange<T>(
min?: number,
max?: number
): (value: T) => boolean
18 changes: 15 additions & 3 deletions packages/plugin-simple-throttle/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"name": "@bugsnag/plugin-simple-throttle",
"version": "8.1.1",
"main": "throttle.js",
"main": "dist/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": {
Expand All @@ -12,15 +20,19 @@
"access": "public"
},
"files": [
"*.js"
"dist"
],
"scripts": {},
"author": "Bugsnag",
"license": "MIT",
"devDependencies": {
"@bugsnag/core": "^8.1.1"
},
"peerDependencies": {
"@bugsnag/core": "^8.0.0"
},
"scripts": {
"build": "npm run build:npm",
"build:npm": "rollup --config rollup.config.npm.mjs",
"clean": "rm -rf dist/*"
}
}
5 changes: 5 additions & 0 deletions packages/plugin-simple-throttle/rollup.config.npm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import createRollupConfig from "../../.rollup/index.mjs";

export default createRollupConfig({
input: "src/throttle.ts"
});
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const intRange = require('@bugsnag/core/lib/validators/int-range')

import { Plugin } from '@bugsnag/core'
import intRange from '@bugsnag/core/lib/validators/int-range'
/*
* Throttles and dedupes events
*/

module.exports = {
const plugin: Plugin = {
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?
// @ts-expect-error _config is private API
if (n >= client._config.maxEvents) {
// @ts-expect-error _config is private API
client._logger.warn(`Cancelling event send due to maxEvents per session limit of ${client._config.maxEvents} being reached`)
return false
}
Expand All @@ -21,11 +23,14 @@ module.exports = {

client.resetEventCount = () => { n = 0 }
},
// @ts-expect-error _config is private API
configSchema: {
maxEvents: {
defaultValue: () => 10,
message: 'should be a positive integer ≤100',
validate: val => intRange(1, 100)(val)
validate: (val: unknown): val is number => intRange(1, 100)(val)
}
}
}

export default plugin
2 changes: 1 addition & 1 deletion packages/plugin-simple-throttle/test/throttle.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import plugin from '../'
import plugin from '../src/throttle'

import Client from '@bugsnag/core/client'

Expand Down
7 changes: 7 additions & 0 deletions packages/plugin-simple-throttle/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts"],
"compilerOptions": {
"target": "ES2020"
}
}
2 changes: 1 addition & 1 deletion packages/plugin-window-onerror/rollup.config.npm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import createRollupConfig from '../../.rollup/index.mjs'

export default createRollupConfig({
input: 'src/onerror.ts'
})
})
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
"packages/plugin-strip-query-string",
"packages/plugin-strip-project-root",
"packages/plugin-interaction-breadcrumbs",
"packages/plugin-simple-throttle",
"packages/plugin-intercept",
"packages/plugin-node-unhandled-rejection",
"packages/plugin-node-in-project",
Expand Down

0 comments on commit 97819cb

Please sign in to comment.