diff --git a/packages/core/client.d.ts b/packages/core/client.d.ts index d869ee50b..b4fc339f9 100644 --- a/packages/core/client.d.ts +++ b/packages/core/client.d.ts @@ -24,7 +24,11 @@ interface SessionDeliveryPayload { notifier?: Notifier device?: Device app?: App - sessions?: Session[] + sessions?: Array<{ + id: string + startedAt: Date + user?: User + }> } interface Delivery { @@ -32,6 +36,12 @@ interface Delivery { sendSession(session: SessionDeliveryPayload, cb: (err?: Error | null) => void): void } +export interface SessionDelegate { + startSession: (client: ClientWithInternals, session: Session) => ClientWithInternals + pauseSession: (client: ClientWithInternals) => void + resumeSession: (client: ClientWithInternals) => ClientWithInternals +} + /** * Extend the public type definitions with internal declarations. * @@ -58,11 +68,7 @@ export default class ClientWithInternals extends Clie _session: Session | null _pausedSession: Session | null - _sessionDelegate: { - startSession: (client: ClientWithInternals, session: Session) => ClientWithInternals - pauseSession: (client: ClientWithInternals) => void - resumeSession: (client: ClientWithInternals) => ClientWithInternals - } + _sessionDelegate: SessionDelegate _addOnSessionPayload: (cb: (sessionPayload: Session) => void) => void diff --git a/packages/plugin-browser-session/src/session.ts b/packages/plugin-browser-session/src/session.ts index 8036e84d4..8dab60296 100644 --- a/packages/plugin-browser-session/src/session.ts +++ b/packages/plugin-browser-session/src/session.ts @@ -1,30 +1,14 @@ -import { Client, Logger, Plugin, Session } from '@bugsnag/core' +import { Plugin, Session } from '@bugsnag/core' +import ClientWithInternals, { Notifier } from '@bugsnag/core/client' import includes from '@bugsnag/core/lib/es-utils/includes' -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 +interface InternalClient extends ClientWithInternals { + _notifier?: Notifier } const plugin: Plugin = { load: client => { - const internalClient = client as InternalClient - internalClient._sessionDelegate = sessionDelegate + (client as InternalClient)._sessionDelegate = sessionDelegate } } @@ -35,7 +19,7 @@ const sessionDelegate = { sessionClient._pausedSession = null // exit early if the current releaseStage is not enabled - if (sessionClient._config.enabledReleaseStages !== null && !includes(sessionClient._config.enabledReleaseStages, sessionClient._config.releaseStage)) { + if (sessionClient._config.enabledReleaseStages && !includes(sessionClient._config.enabledReleaseStages, sessionClient._config.releaseStage)) { sessionClient._logger.warn('Session not sent due to releaseStage/enabledReleaseStages configuration') return sessionClient } @@ -51,10 +35,10 @@ const sessionDelegate = { user: session._user } ] - }) + }, () => {}) return sessionClient }, - resumeSession: (client: { _session: any, _pausedSession: null, startSession: () => any }) => { + resumeSession: (client: InternalClient) => { // Do nothing if there's already an active session if (client._session) { return client @@ -71,7 +55,7 @@ const sessionDelegate = { // Otherwise start a new session return client.startSession() }, - pauseSession: (client: { _pausedSession: any, _session: null }) => { + pauseSession: (client: InternalClient) => { client._pausedSession = client._session client._session = null }