Skip to content

Commit

Permalink
refactor: 🏷️ update session types
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerbenw committed Dec 4, 2024
1 parent 8734597 commit 4c2a2a3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 38 deletions.
18 changes: 12 additions & 6 deletions packages/core/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ interface SessionDeliveryPayload {
notifier?: Notifier
device?: Device
app?: App
sessions?: Session[]
sessions?: Array<{
id: string
startedAt: Date
user?: User
}>
}

interface Delivery {
sendEvent(payload: EventDeliveryPayload, cb: (err?: Error | null) => void): void
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.
*
Expand All @@ -58,11 +68,7 @@ export default class ClientWithInternals<T extends Config = Config> 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

Expand Down
2 changes: 0 additions & 2 deletions packages/core/types/session.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ declare class Session {
public device?: Device;
public app?: App;

public _user?: User;

public getUser(): User;
public setUser(id?: string, email?: string, name?: string): void;
}
Expand Down
36 changes: 10 additions & 26 deletions packages/plugin-browser-session/src/session.ts
Original file line number Diff line number Diff line change
@@ -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
}
}

Expand All @@ -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
}
Expand All @@ -48,13 +32,13 @@ const sessionDelegate = {
{
id: session.id,
startedAt: session.startedAt,
user: session._user
user: session.getUser()
}
]
})
}, () => {})
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
Expand All @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions packages/plugin-browser-session/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts"],
"compilerOptions": {
"target": "ES2020"
}
"include": ["src/**/*.ts"]
}

0 comments on commit 4c2a2a3

Please sign in to comment.