Skip to content

Commit 94825b5

Browse files
committed
feat(replay): Add getReplay utility function
1 parent 1a4b6e6 commit 94825b5

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

packages/browser/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export {
2727
// eslint-disable-next-line deprecation/deprecation
2828
Replay,
2929
replayIntegration,
30+
getReplay,
3031
} from '@sentry/replay';
3132
export type {
3233
ReplayEventType,

packages/replay/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ export type {
1919
CanvasManagerOptions,
2020
} from './types';
2121

22+
export { getReplay } from './util/getReplay';
23+
2224
// TODO (v8): Remove deprecated types
2325
export * from './types/deprecated';

packages/replay/src/util/getReplay.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getClient } from '@sentry/core';
2+
import type { replayIntegration } from '../integration';
3+
4+
/**
5+
* This is a small utility to get a type-safe instance of the Replay integration.
6+
*/
7+
// eslint-disable-next-line deprecation/deprecation
8+
export function getReplay(): ReturnType<typeof replayIntegration> | undefined {
9+
const client = getClient();
10+
return (
11+
client && client.getIntegrationByName && client.getIntegrationByName<ReturnType<typeof replayIntegration>>('Replay')
12+
);
13+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { getCurrentScope } from '@sentry/core';
2+
import { replayIntegration } from '../../../src/integration';
3+
import { getReplay } from '../../../src/util/getReplay';
4+
import { TestClient, getDefaultClientOptions } from '../../utils/TestClient';
5+
6+
describe('getReplay', () => {
7+
beforeEach(() => {
8+
getCurrentScope().setClient(undefined);
9+
});
10+
11+
it('works without a client', () => {
12+
const actual = getReplay();
13+
expect(actual).toBeUndefined();
14+
});
15+
16+
it('works with a client without Replay', () => {
17+
const client = new TestClient(getDefaultClientOptions());
18+
getCurrentScope().setClient(client);
19+
20+
const actual = getReplay();
21+
expect(actual).toBeUndefined();
22+
});
23+
24+
it('works with a client with Replay', () => {
25+
const replay = replayIntegration();
26+
const client = new TestClient(
27+
getDefaultClientOptions({
28+
integrations: [replay],
29+
replaysOnErrorSampleRate: 0,
30+
replaysSessionSampleRate: 0,
31+
}),
32+
);
33+
getCurrentScope().setClient(client);
34+
client.init();
35+
36+
const actual = getReplay();
37+
expect(actual).toBe(replay);
38+
expect(replay.getReplayId()).toBe(undefined);
39+
});
40+
});

packages/replay/test/utils/TestClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function init(options: TestClientOptions): void {
3939
initAndBind(TestClient, options);
4040
}
4141

42-
export function getDefaultClientOptions(options: Partial<ClientOptions> = {}): ClientOptions {
42+
export function getDefaultClientOptions(options: Partial<TestClientOptions> = {}): ClientOptions {
4343
return {
4444
integrations: [],
4545
dsn: 'https://username@domain/123',

0 commit comments

Comments
 (0)