From 8734597f5af324bca54314caf32cb19f8cbe4858 Mon Sep 17 00:00:00 2001 From: AnastasiiaSvietlova Date: Fri, 22 Nov 2024 11:56:33 +0100 Subject: [PATCH] refactor @bugsnag/plugin-browser-session --- packages/core/types/session.d.ts | 2 + packages/plugin-browser-session/package.json | 17 ++++++++- .../rollup.config.npm.mjs | 6 +++ .../{session.js => src/session.ts} | 38 ++++++++++++++++--- .../test/session.test.ts | 2 +- packages/plugin-browser-session/tsconfig.json | 7 ++++ tsconfig.json | 1 - 7 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 packages/plugin-browser-session/rollup.config.npm.mjs rename packages/plugin-browser-session/{session.js => src/session.ts} (53%) create mode 100644 packages/plugin-browser-session/tsconfig.json diff --git a/packages/core/types/session.d.ts b/packages/core/types/session.d.ts index 73ec509c1a..760cd2b9ef 100644 --- a/packages/core/types/session.d.ts +++ b/packages/core/types/session.d.ts @@ -7,6 +7,8 @@ declare class Session { public device?: Device; public app?: App; + public _user?: User; + public getUser(): User; public setUser(id?: string, email?: string, name?: string): void; } diff --git a/packages/plugin-browser-session/package.json b/packages/plugin-browser-session/package.json index 8dc129f399..230b0b0b08 100644 --- a/packages/plugin-browser-session/package.json +++ b/packages/plugin-browser-session/package.json @@ -1,7 +1,15 @@ { "name": "@bugsnag/plugin-browser-session", "version": "8.1.1", - "main": "session.js", + "main": "dist/session.js", + "types": "dist/types/session.d.ts", + "exports": { + ".": { + "types": "./dist/types/session.d.ts", + "default": "./dist/session.js", + "import": "./dist/session.mjs" + } + }, "description": "@bugsnag/js plugin to enable session tracking in browsers", "homepage": "https://www.bugsnag.com/", "repository": { @@ -12,7 +20,7 @@ "access": "public" }, "files": [ - "*.js" + "dist" ], "author": "Bugsnag", "license": "MIT", @@ -21,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-browser-session/rollup.config.npm.mjs b/packages/plugin-browser-session/rollup.config.npm.mjs new file mode 100644 index 0000000000..e8a18d9a91 --- /dev/null +++ b/packages/plugin-browser-session/rollup.config.npm.mjs @@ -0,0 +1,6 @@ +import createRollupConfig from "../../.rollup/index.mjs"; + +export default createRollupConfig({ + input: "src/session.ts", + external: ["@bugsnag/core/lib/es-utils/includes"] +}); diff --git a/packages/plugin-browser-session/session.js b/packages/plugin-browser-session/src/session.ts similarity index 53% rename from packages/plugin-browser-session/session.js rename to packages/plugin-browser-session/src/session.ts index fdee3c102d..8036e84d43 100644 --- a/packages/plugin-browser-session/session.js +++ b/packages/plugin-browser-session/src/session.ts @@ -1,11 +1,35 @@ -const includes = require('@bugsnag/core/lib/es-utils/includes') +import { Client, Logger, Plugin, Session } from '@bugsnag/core' +import includes from '@bugsnag/core/lib/es-utils/includes' -module.exports = { - load: client => { client._sessionDelegate = sessionDelegate } +interface SessionDelegate { + startSession: (client: InternalClient, session: Session) => InternalClient + resumeSession: (client: { _session: any, _pausedSession: null, startSession: () => any}) => any + pauseSession: (client: { _pausedSession: any, _session: null }) => void +} + +interface InternalClient extends Client { + _config: { + enabledReleaseStages: string[] | null + releaseStage: string + releaseStages: string[] + } + _delivery: any + _logger: Logger + _notifier: any + _session: Session + _sessionDelegate: SessionDelegate + _pausedSession: any +} + +const plugin: Plugin = { + load: client => { + const internalClient = client as InternalClient + internalClient._sessionDelegate = sessionDelegate + } } const sessionDelegate = { - startSession: (client, session) => { + startSession: (client: InternalClient, session: Session) => { const sessionClient = client sessionClient._session = session sessionClient._pausedSession = null @@ -30,7 +54,7 @@ const sessionDelegate = { }) return sessionClient }, - resumeSession: (client) => { + resumeSession: (client: { _session: any, _pausedSession: null, startSession: () => any }) => { // Do nothing if there's already an active session if (client._session) { return client @@ -47,8 +71,10 @@ const sessionDelegate = { // Otherwise start a new session return client.startSession() }, - pauseSession: (client) => { + pauseSession: (client: { _pausedSession: any, _session: null }) => { client._pausedSession = client._session client._session = null } } + +export default plugin diff --git a/packages/plugin-browser-session/test/session.test.ts b/packages/plugin-browser-session/test/session.test.ts index 70a41c2c53..c1369afcf4 100644 --- a/packages/plugin-browser-session/test/session.test.ts +++ b/packages/plugin-browser-session/test/session.test.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/no-non-null-assertion */ -import plugin from '../' +import plugin from '../src/session' import Client, { EventDeliveryPayload } from '@bugsnag/core/client' import EventWithInternals from '@bugsnag/core/event' diff --git a/packages/plugin-browser-session/tsconfig.json b/packages/plugin-browser-session/tsconfig.json new file mode 100644 index 0000000000..9478c51300 --- /dev/null +++ b/packages/plugin-browser-session/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "include": ["src/**/*.ts"], + "compilerOptions": { + "target": "ES2020" + } +} diff --git a/tsconfig.json b/tsconfig.json index 4c498ea7d9..e24fa50c19 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -82,7 +82,6 @@ "packages/plugin-node-device", "packages/plugin-node-surrounding-code", "packages/plugin-node-uncaught-exception", - "packages/plugin-browser-session", "packages/browser" ], "exclude": [