From 97819cbbcc675de9b4cebaf4f38bd0fbd47c1400 Mon Sep 17 00:00:00 2001 From: AnastasiiaSvietlova <119674359+AnastasiiaSvietlova@users.noreply.github.com> Date: Fri, 15 Nov 2024 13:07:47 +0100 Subject: [PATCH] Convert @bugsnag/plugin-simple-throttle to TypeScript (#2242) * update rollup configuration * convert plugin-simple-throttle to typescript * fix: int-range --------- Co-authored-by: Ben Wilson --- packages/core/lib/validators/int-range.d.ts | 4 ++++ packages/plugin-simple-throttle/package.json | 18 +++++++++++++++--- .../rollup.config.npm.mjs | 5 +++++ .../{throttle.js => src/throttle.ts} | 13 +++++++++---- .../test/throttle.test.ts | 2 +- packages/plugin-simple-throttle/tsconfig.json | 7 +++++++ .../rollup.config.npm.mjs | 2 +- tsconfig.json | 1 - 8 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 packages/core/lib/validators/int-range.d.ts create mode 100644 packages/plugin-simple-throttle/rollup.config.npm.mjs rename packages/plugin-simple-throttle/{throttle.js => src/throttle.ts} (63%) create mode 100644 packages/plugin-simple-throttle/tsconfig.json diff --git a/packages/core/lib/validators/int-range.d.ts b/packages/core/lib/validators/int-range.d.ts new file mode 100644 index 0000000000..6aa36f90a7 --- /dev/null +++ b/packages/core/lib/validators/int-range.d.ts @@ -0,0 +1,4 @@ +export default function intRange( + min?: number, + max?: number +): (value: T) => boolean diff --git a/packages/plugin-simple-throttle/package.json b/packages/plugin-simple-throttle/package.json index bb89c0145f..a54dd8da8a 100644 --- a/packages/plugin-simple-throttle/package.json +++ b/packages/plugin-simple-throttle/package.json @@ -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": { @@ -12,9 +20,8 @@ "access": "public" }, "files": [ - "*.js" + "dist" ], - "scripts": {}, "author": "Bugsnag", "license": "MIT", "devDependencies": { @@ -22,5 +29,10 @@ }, "peerDependencies": { "@bugsnag/core": "^8.0.0" + }, + "scripts": { + "build": "npm run build:npm", + "build:npm": "rollup --config rollup.config.npm.mjs", + "clean": "rm -rf dist/*" } } 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..17c25ef1a6 --- /dev/null +++ b/packages/plugin-simple-throttle/rollup.config.npm.mjs @@ -0,0 +1,5 @@ +import createRollupConfig from "../../.rollup/index.mjs"; + +export default createRollupConfig({ + input: "src/throttle.ts" +}); diff --git a/packages/plugin-simple-throttle/throttle.js b/packages/plugin-simple-throttle/src/throttle.ts similarity index 63% rename from packages/plugin-simple-throttle/throttle.js rename to packages/plugin-simple-throttle/src/throttle.ts index 3de502ec61..b2aa3e3b81 100644 --- a/packages/plugin-simple-throttle/throttle.js +++ b/packages/plugin-simple-throttle/src/throttle.ts @@ -1,10 +1,10 @@ -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 @@ -12,7 +12,9 @@ module.exports = { // 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 } @@ -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 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/tsconfig.json b/packages/plugin-simple-throttle/tsconfig.json new file mode 100644 index 0000000000..9478c51300 --- /dev/null +++ b/packages/plugin-simple-throttle/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "include": ["src/**/*.ts"], + "compilerOptions": { + "target": "ES2020" + } +} diff --git a/packages/plugin-window-onerror/rollup.config.npm.mjs b/packages/plugin-window-onerror/rollup.config.npm.mjs index d5a4a7c3d4..356d458f94 100644 --- a/packages/plugin-window-onerror/rollup.config.npm.mjs +++ b/packages/plugin-window-onerror/rollup.config.npm.mjs @@ -2,4 +2,4 @@ import createRollupConfig from '../../.rollup/index.mjs' export default createRollupConfig({ input: 'src/onerror.ts' -}) \ No newline at end of file +}) diff --git a/tsconfig.json b/tsconfig.json index a154bd5ea1..ca9e6527a6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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",