diff --git a/packages/browser/src/index.ts b/packages/browser/src/index.ts index 4518f0174f35..f6a5dd8f8117 100644 --- a/packages/browser/src/index.ts +++ b/packages/browser/src/index.ts @@ -27,6 +27,7 @@ export { // eslint-disable-next-line deprecation/deprecation Replay, replayIntegration, + getReplay, } from '@sentry/replay'; export type { ReplayEventType, diff --git a/packages/replay/src/index.ts b/packages/replay/src/index.ts index 6471ff9e87ce..d16fd7733c20 100644 --- a/packages/replay/src/index.ts +++ b/packages/replay/src/index.ts @@ -19,5 +19,7 @@ export type { CanvasManagerOptions, } from './types'; +export { getReplay } from './util/getReplay'; + // TODO (v8): Remove deprecated types export * from './types/deprecated'; diff --git a/packages/replay/src/util/getReplay.ts b/packages/replay/src/util/getReplay.ts new file mode 100644 index 000000000000..278505f15338 --- /dev/null +++ b/packages/replay/src/util/getReplay.ts @@ -0,0 +1,13 @@ +import { getClient } from '@sentry/core'; +import type { replayIntegration } from '../integration'; + +/** + * This is a small utility to get a type-safe instance of the Replay integration. + */ +// eslint-disable-next-line deprecation/deprecation +export function getReplay(): ReturnType | undefined { + const client = getClient(); + return ( + client && client.getIntegrationByName && client.getIntegrationByName>('Replay') + ); +} diff --git a/packages/replay/test/unit/util/getReplay.test.ts b/packages/replay/test/unit/util/getReplay.test.ts new file mode 100644 index 000000000000..7f614d4fdc33 --- /dev/null +++ b/packages/replay/test/unit/util/getReplay.test.ts @@ -0,0 +1,42 @@ +import { getCurrentScope } from '@sentry/core'; +import { replayIntegration } from '../../../src/integration'; +import { getReplay } from '../../../src/util/getReplay'; +import { getDefaultClientOptions, init } from '../../utils/TestClient'; + +describe('getReplay', () => { + beforeEach(() => { + getCurrentScope().setClient(undefined); + }); + + it('works without a client', () => { + const actual = getReplay(); + expect(actual).toBeUndefined(); + }); + + it('works with a client without Replay', () => { + init( + getDefaultClientOptions({ + dsn: 'https://dsn@ingest.f00.f00/1', + }), + ); + + const actual = getReplay(); + expect(actual).toBeUndefined(); + }); + + it('works with a client with Replay xxx', () => { + const replay = replayIntegration(); + init( + getDefaultClientOptions({ + integrations: [replay], + replaysOnErrorSampleRate: 0, + replaysSessionSampleRate: 0, + }), + ); + + const actual = getReplay(); + expect(actual).toBeDefined(); + expect(actual === replay).toBe(true); + expect(replay.getReplayId()).toBe(undefined); + }); +}); diff --git a/packages/replay/test/utils/TestClient.ts b/packages/replay/test/utils/TestClient.ts index da131aec8fd2..26a14f2a9795 100644 --- a/packages/replay/test/utils/TestClient.ts +++ b/packages/replay/test/utils/TestClient.ts @@ -39,7 +39,7 @@ export function init(options: TestClientOptions): void { initAndBind(TestClient, options); } -export function getDefaultClientOptions(options: Partial = {}): ClientOptions { +export function getDefaultClientOptions(options: Partial = {}): ClientOptions { return { integrations: [], dsn: 'https://username@domain/123',