Skip to content

feat(replay): Add getReplay utility function #10510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
Replay,
replayIntegration,
getReplay,
} from '@sentry/replay';
export type {
ReplayEventType,
Expand Down
2 changes: 2 additions & 0 deletions packages/replay/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ export type {
CanvasManagerOptions,
} from './types';

export { getReplay } from './util/getReplay';

// TODO (v8): Remove deprecated types
export * from './types/deprecated';
13 changes: 13 additions & 0 deletions packages/replay/src/util/getReplay.ts
Original file line number Diff line number Diff line change
@@ -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<typeof replayIntegration> | undefined {
const client = getClient();
return (
client && client.getIntegrationByName && client.getIntegrationByName<ReturnType<typeof replayIntegration>>('Replay')
);
}
42 changes: 42 additions & 0 deletions packages/replay/test/unit/util/getReplay.test.ts
Original file line number Diff line number Diff line change
@@ -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://[email protected]/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);
});
});
2 changes: 1 addition & 1 deletion packages/replay/test/utils/TestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function init(options: TestClientOptions): void {
initAndBind(TestClient, options);
}

export function getDefaultClientOptions(options: Partial<ClientOptions> = {}): ClientOptions {
export function getDefaultClientOptions(options: Partial<TestClientOptions> = {}): ClientOptions {
return {
integrations: [],
dsn: 'https://username@domain/123',
Expand Down