Skip to content

Commit ac7cb33

Browse files
committed
feat(replay): Add getReplay utility function (#10510)
As pointed out here, and I also did notice that myself, it is not super nice - as you need to provide a generic integration to `getIntegrationByName`, which is annoying to do in a type safe way, esp. if you want to avoid deprecations: ```ts const client = getClient(); const replay = client && client.getIntegrationByName && client.getIntegrationByName<ReturnType<typeof replayIntegration>>('Replay'); ``` So IMHO a small utility `Sentry.getReplay()` is not unreasonable for this 🤔
1 parent 613a1aa commit ac7cb33

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { getCurrentScope } from '@sentry/core';
2+
import { replayIntegration } from '../../../src/integration';
3+
import { getReplay } from '../../../src/util/getReplay';
4+
import { getDefaultClientOptions, init } 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+
init(
18+
getDefaultClientOptions({
19+
dsn: 'https://[email protected]/1',
20+
}),
21+
);
22+
23+
const actual = getReplay();
24+
expect(actual).toBeUndefined();
25+
});
26+
27+
it('works with a client with Replay xxx', () => {
28+
const replay = replayIntegration();
29+
init(
30+
getDefaultClientOptions({
31+
integrations: [replay],
32+
replaysOnErrorSampleRate: 0,
33+
replaysSessionSampleRate: 0,
34+
}),
35+
);
36+
37+
const actual = getReplay();
38+
expect(actual).toBeDefined();
39+
expect(actual === replay).toBe(true);
40+
expect(replay.getReplayId()).toBe(undefined);
41+
});
42+
});

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)