Skip to content

Commit 9510771

Browse files
authored
fix(replay): Fix type for replayCanvasIntegration (#11995)
`snapshot` is a public API. I think the other methods we do not need to expose (?). With this, usage will be: ```ts const canvas = getClient()?.getIntegrationByName<ReturnType<typeof replayCanvasIntegration>>('ReplayCanvas'); await canvas.snapshot(); ``` Closes #11971
1 parent bd9ead6 commit 9510771

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

packages/replay-canvas/src/canvas.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import type { CanvasManagerInterface, CanvasManagerOptions } from '@sentry-internal/replay';
22
import { CanvasManager } from '@sentry-internal/rrweb';
33
import { defineIntegration } from '@sentry/core';
4-
import type { IntegrationFn } from '@sentry/types';
4+
import type { Integration, IntegrationFn } from '@sentry/types';
5+
6+
interface ReplayCanvasIntegration extends Integration {
7+
snapshot: (canvasElement?: HTMLCanvasElement) => Promise<void>;
8+
}
59

610
interface ReplayCanvasOptions {
711
enableManualSnapshot?: boolean;
@@ -107,9 +111,11 @@ export const _replayCanvasIntegration = ((options: Partial<ReplayCanvasOptions>
107111
canvasManager.snapshot(canvasElement);
108112
},
109113
};
110-
}) satisfies IntegrationFn;
114+
}) satisfies IntegrationFn<ReplayCanvasIntegration>;
111115

112116
/**
113117
* Add this in addition to `replayIntegration()` to enable canvas recording.
114118
*/
115-
export const replayCanvasIntegration = defineIntegration(_replayCanvasIntegration);
119+
export const replayCanvasIntegration = defineIntegration(
120+
_replayCanvasIntegration,
121+
) as IntegrationFn<ReplayCanvasIntegration>;

packages/replay-canvas/test/canvas.test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CanvasManager } from '@sentry-internal/rrweb';
2-
import { _replayCanvasIntegration } from '../src/canvas';
2+
import { _replayCanvasIntegration, replayCanvasIntegration } from '../src/canvas';
33

44
jest.mock('@sentry-internal/rrweb');
55

@@ -86,3 +86,15 @@ it('enforces a max canvas size', () => {
8686
}),
8787
);
8888
});
89+
90+
it('has correct types', () => {
91+
const rc = replayCanvasIntegration();
92+
93+
expect(typeof rc.snapshot).toBe('function');
94+
const res = rc.snapshot();
95+
expect(res).toBeInstanceOf(Promise);
96+
97+
// Function signature is correctly typed
98+
const res2 = rc.snapshot(document.createElement('canvas'));
99+
expect(res2).toBeInstanceOf(Promise);
100+
});

0 commit comments

Comments
 (0)