Skip to content

Commit fc23402

Browse files
authored
feat(feedback): Add getFeedback utility to get typed feedback instance (#11331)
Also ensure the `feedbackIntegration` returns a properly typed response. This should make it easier to manually interact with feedbacks.
1 parent c1b4a47 commit fc23402

File tree

7 files changed

+61
-6
lines changed

7 files changed

+61
-6
lines changed

packages/browser/src/index.bundle.feedback.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This is exported so the loader does not fail when switching off Replay/Tracing
2-
import { feedbackIntegration } from '@sentry-internal/feedback';
2+
import { feedbackIntegration, getFeedback } from '@sentry-internal/feedback';
33
import {
44
addTracingExtensionsShim,
55
browserTracingIntegrationShim,
@@ -12,5 +12,6 @@ export {
1212
addTracingExtensionsShim as addTracingExtensions,
1313
replayIntegrationShim as replayIntegration,
1414
feedbackIntegration,
15+
getFeedback,
1516
};
1617
// Note: We do not export a shim for `Span` here, as that is quite complex and would blow up the bundle

packages/browser/src/index.bundle.tracing.replay.feedback.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { feedbackIntegration } from '@sentry-internal/feedback';
1+
import { feedbackIntegration, getFeedback } from '@sentry-internal/feedback';
22
import { replayIntegration } from '@sentry-internal/replay';
33
import {
44
browserTracingIntegration,
@@ -27,6 +27,7 @@ export {
2727
addTracingExtensions,
2828
startBrowserTracingNavigationSpan,
2929
startBrowserTracingPageLoadSpan,
30+
getFeedback,
3031
};
3132

3233
export * from './index.bundle.base';

packages/browser/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export { replayCanvasIntegration } from '@sentry-internal/replay-canvas';
4949

5050
export {
5151
feedbackIntegration,
52+
getFeedback,
5253
sendFeedback,
5354
} from '@sentry-internal/feedback';
5455

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { getCurrentScope } from '@sentry/core';
2+
import { getFeedback } from './getFeedback';
3+
import { feedbackIntegration } from './integration';
4+
import { mockSdk } from './mockSdk';
5+
6+
describe('getFeedback', () => {
7+
beforeEach(() => {
8+
getCurrentScope().setClient(undefined);
9+
});
10+
11+
it('works without a client', () => {
12+
const actual = getFeedback();
13+
expect(actual).toBeUndefined();
14+
});
15+
16+
it('works with a client without Feedback', () => {
17+
mockSdk({
18+
sentryOptions: {
19+
integrations: [],
20+
},
21+
});
22+
23+
const actual = getFeedback();
24+
expect(actual).toBeUndefined();
25+
});
26+
27+
it('works with a client with Feedback', () => {
28+
const feedback = feedbackIntegration();
29+
30+
mockSdk({
31+
sentryOptions: {
32+
integrations: [feedback],
33+
},
34+
});
35+
36+
const actual = getFeedback();
37+
expect(actual).toBeDefined();
38+
expect(actual === feedback).toBe(true);
39+
});
40+
});
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { getClient } from '@sentry/core';
2+
import type { feedbackIntegration } from './integration';
3+
4+
/**
5+
* This is a small utility to get a type-safe instance of the Feedback integration.
6+
*/
7+
export function getFeedback(): ReturnType<typeof feedbackIntegration> | undefined {
8+
const client = getClient();
9+
return client && client.getIntegrationByName<ReturnType<typeof feedbackIntegration>>('Feedback');
10+
}

packages/feedback/src/core/integration.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineIntegration, getClient } from '@sentry/core';
1+
import { getClient } from '@sentry/core';
22
import type { Integration, IntegrationFn } from '@sentry/types';
33
import { isBrowser, logger } from '@sentry/utils';
44
import {
@@ -45,7 +45,10 @@ interface PublicFeedbackIntegration {
4545
}
4646
export type IFeedbackIntegration = Integration & PublicFeedbackIntegration;
4747

48-
export const _feedbackIntegration = (({
48+
/**
49+
* Allow users to capture user feedback and send it to Sentry.
50+
*/
51+
export const feedbackIntegration = (({
4952
// FeedbackGeneralConfiguration
5053
id = 'sentry-feedback',
5154
showBranding = true,
@@ -279,5 +282,3 @@ export const _feedbackIntegration = (({
279282
},
280283
};
281284
}) satisfies IntegrationFn;
282-
283-
export const feedbackIntegration = defineIntegration(_feedbackIntegration);

packages/feedback/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { sendFeedback } from './core/sendFeedback';
22
export { feedbackIntegration } from './core/integration';
33
export { feedbackModalIntegration } from './modal/integration';
4+
export { getFeedback } from './core/getFeedback';
45
export { feedbackScreenshotIntegration } from './screenshot/integration';
56

67
export type { OptionalFeedbackConfiguration } from './types';

0 commit comments

Comments
 (0)