Skip to content

Commit ea7b909

Browse files
committed
clean up attachment code
1 parent 22dad05 commit ea7b909

File tree

7 files changed

+32
-48
lines changed

7 files changed

+32
-48
lines changed

packages/core/src/envelope.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import type {
1111
SessionAggregates,
1212
SessionEnvelope,
1313
SessionItem,
14-
TextEncoderInternal,
1514
} from '@sentry/types';
1615
import {
1716
createAttachmentEnvelopeItem,
@@ -96,11 +95,10 @@ export function createEventEnvelope(
9695
*/
9796
export function createAttachmentEnvelope(
9897
event: Event,
99-
attachment: Attachment,
98+
attachments: Attachment[],
10099
dsn?: DsnComponents,
101100
metadata?: SdkMetadata,
102101
tunnel?: string,
103-
textEncoder?: TextEncoderInternal,
104102
): EventEnvelope {
105103
const sdkInfo = getSdkMetadataForEnvelopeHeader(metadata);
106104
enhanceEventWithSdkInfo(event, metadata && metadata.sdk);
@@ -113,6 +111,9 @@ export function createAttachmentEnvelope(
113111
// of this `delete`, lest we miss putting it back in the next time the property is in use.)
114112
delete event.sdkProcessingMetadata;
115113

116-
const attachmentItem: AttachmentItem = createAttachmentEnvelopeItem(attachment, textEncoder);
117-
return createEnvelope<EventEnvelope>(envelopeHeaders, [attachmentItem]);
114+
const attachmentItems: AttachmentItem[] = [];
115+
for (const attachment of attachments || []) {
116+
attachmentItems.push(createAttachmentEnvelopeItem(attachment));
117+
}
118+
return createEnvelope<EventEnvelope>(envelopeHeaders, attachmentItems);
118119
}

packages/feedback/src/sendFeedback.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { getLocationHref } from '@sentry/utils';
22

33
import { FEEDBACK_API_SOURCE } from './constants';
4-
import type { Screenshot, SendFeedbackOptions } from './types';
4+
import type { SendFeedbackOptions } from './types';
55
import { sendFeedbackRequest } from './util/sendFeedbackRequest';
6+
import type { Attachment } from '@sentry/types';
67

78
interface SendFeedbackParams {
89
message: string;
@@ -18,7 +19,7 @@ interface SendFeedbackParams {
1819
export function sendFeedback(
1920
{ name, email, message, source = FEEDBACK_API_SOURCE, url = getLocationHref() }: SendFeedbackParams,
2021
options: SendFeedbackOptions = {},
21-
screenshots: Screenshot[] = [],
22+
screenshots: Attachment[] = [],
2223
): ReturnType<typeof sendFeedbackRequest> {
2324
if (!message) {
2425
throw new Error('Unable to submit feedback with empty message');

packages/feedback/src/types/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Primitive } from '@sentry/types';
1+
import type { Attachment, Primitive } from '@sentry/types';
22

33
import type { ActorComponent } from '../widget/Actor';
44
import type { DialogComponent } from '../widget/Dialog';
@@ -21,7 +21,7 @@ export interface SendFeedbackOptions {
2121
* Should include replay with the feedback?
2222
*/
2323
includeReplay?: boolean;
24-
screenshots?: Screenshot[];
24+
screenshots?: Attachment[];
2525
}
2626

2727
/**
@@ -343,9 +343,3 @@ export interface FeedbackWidget {
343343
closeDialog: () => void;
344344
removeDialog: () => void;
345345
}
346-
347-
export interface Screenshot {
348-
filename: string;
349-
data: Uint8Array;
350-
contentType: string;
351-
}

packages/feedback/src/util/handleFeedbackSubmit.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { TransportMakeRequestResponse } from '@sentry/types';
1+
import type { Attachment, TransportMakeRequestResponse } from '@sentry/types';
22
import { logger } from '@sentry/utils';
33

44
import { FEEDBACK_WIDGET_SOURCE } from '../constants';
55
import { DEBUG_BUILD } from '../debug-build';
66
import { sendFeedback } from '../sendFeedback';
7-
import type { FeedbackFormData, Screenshot, SendFeedbackOptions } from '../types';
7+
import type { FeedbackFormData, SendFeedbackOptions } from '../types';
88
import type { DialogComponent } from '../widget/Dialog';
99

1010
/**
@@ -14,7 +14,7 @@ import type { DialogComponent } from '../widget/Dialog';
1414
export async function handleFeedbackSubmit(
1515
dialog: DialogComponent | null,
1616
feedback: FeedbackFormData,
17-
screenshots?: Screenshot[],
17+
screenshots?: Attachment[],
1818
options?: SendFeedbackOptions,
1919
): Promise<TransportMakeRequestResponse | void> {
2020
if (!dialog) {

packages/feedback/src/util/sendFeedbackRequest.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { createEventEnvelope, getClient, withScope, createAttachmentEnvelope } from '@sentry/core';
2-
import type { FeedbackEvent, TransportMakeRequestResponse } from '@sentry/types';
2+
import type { Attachment, FeedbackEvent, TransportMakeRequestResponse } from '@sentry/types';
33

44
import { FEEDBACK_API_SOURCE, FEEDBACK_WIDGET_SOURCE } from '../constants';
5-
import type { Screenshot, SendFeedbackData, SendFeedbackOptions } from '../types';
5+
import type { SendFeedbackData, SendFeedbackOptions } from '../types';
66
import { prepareFeedbackEvent } from './prepareFeedbackEvent';
77

88
/**
@@ -11,7 +11,7 @@ import { prepareFeedbackEvent } from './prepareFeedbackEvent';
1111
export async function sendFeedbackRequest(
1212
{ feedback: { message, email, name, source, url } }: SendFeedbackData,
1313
{ includeReplay = true }: SendFeedbackOptions = {},
14-
screenshots: Screenshot[],
14+
screenshots: Attachment[],
1515
): Promise<void | TransportMakeRequestResponse> {
1616
const client = getClient();
1717
const transport = client && client.getTransport();
@@ -56,19 +56,6 @@ export async function sendFeedbackRequest(
5656

5757
const envelope = createEventEnvelope(feedbackEvent, dsn, client.getOptions()._metadata, client.getOptions().tunnel);
5858

59-
let attachment_envelope;
60-
for (const attachment of screenshots || []) {
61-
attachment_envelope = createAttachmentEnvelope(
62-
feedbackEvent,
63-
attachment,
64-
dsn,
65-
client.getOptions()._metadata,
66-
client.getOptions().tunnel,
67-
// eslint-disable-next-line @sentry-internal/sdk/no-optional-chaining
68-
client.getOptions().transportOptions && client.getOptions().transportOptions?.textEncoder,
69-
);
70-
}
71-
7259
let response: void | TransportMakeRequestResponse;
7360

7461
try {
@@ -96,7 +83,15 @@ export async function sendFeedbackRequest(
9683
throw new Error('Unable to send Feedback');
9784
}
9885

99-
if (attachment_envelope) {
86+
if (screenshots) {
87+
const attachment_envelope = createAttachmentEnvelope(
88+
feedbackEvent,
89+
screenshots,
90+
dsn,
91+
client.getOptions()._metadata,
92+
client.getOptions().tunnel,
93+
);
94+
10095
try {
10196
response = await transport.send(attachment_envelope);
10297
} catch (err) {

packages/feedback/src/widget/Form.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import type {
2-
FeedbackComponent,
3-
FeedbackFormData,
4-
FeedbackInternalOptions,
5-
FeedbackTextConfiguration,
6-
Screenshot,
7-
} from '../types';
1+
import type { Attachment } from '@sentry/types';
2+
import type { FeedbackComponent, FeedbackFormData, FeedbackInternalOptions, FeedbackTextConfiguration } from '../types';
83
import { SubmitButton } from './SubmitButton';
94
import { createElement } from './util/createElement';
105
import * as ScreenshotIntegration from '@sentry-internal/feedback-screenshot';
@@ -27,7 +22,7 @@ export interface FormComponentProps
2722
*/
2823
defaultEmail: string;
2924
onCancel?: (e: Event) => void;
30-
onSubmit?: (feedback: FeedbackFormData, screenshots?: Screenshot[]) => void;
25+
onSubmit?: (feedback: FeedbackFormData, screenshots?: Attachment[]) => void;
3126
}
3227

3328
interface FormComponent extends FeedbackComponent<HTMLFormElement> {

packages/feedback/src/widget/createWidget.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getCurrentScope } from '@sentry/core';
22
import { logger } from '@sentry/utils';
3-
4-
import type { FeedbackFormData, FeedbackInternalOptions, FeedbackWidget, Screenshot } from '../types';
3+
import type { Attachment } from '@sentry/types';
4+
import type { FeedbackFormData, FeedbackInternalOptions, FeedbackWidget } from '../types';
55
import { handleFeedbackSubmit } from '../util/handleFeedbackSubmit';
66
import type { ActorComponent } from './Actor';
77
import { Actor } from './Actor';
@@ -83,7 +83,7 @@ export function createWidget({
8383
* Handler for when the feedback form is completed by the user. This will
8484
* create and send the feedback message as an event.
8585
*/
86-
async function _handleFeedbackSubmit(feedback: FeedbackFormData, screenshots?: Screenshot[]): Promise<void> {
86+
async function _handleFeedbackSubmit(feedback: FeedbackFormData, screenshots?: Attachment[]): Promise<void> {
8787
if (!dialog) {
8888
return;
8989
}
@@ -210,8 +210,6 @@ export function createWidget({
210210
}
211211
} catch (err) {
212212
// TODO: Error handling?
213-
logger.error(err);
214-
console.log(err);
215213
}
216214
}
217215

0 commit comments

Comments
 (0)