Skip to content

Commit 99446c7

Browse files
committed
More type improve
1 parent 232b75b commit 99446c7

File tree

9 files changed

+29
-35
lines changed

9 files changed

+29
-35
lines changed

packages/browser/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BaseClient, getCurrentHub, getEnvelopeEndpointWithUrlEncodedAuth, Scope, SDK_VERSION } from '@sentry/core';
2-
import { AttachmentWithData,ClientOptions, Event, EventHint, Options, Severity, SeverityLevel } from '@sentry/types';
2+
import { Attachment, ClientOptions, Event, EventHint, Options, Severity, SeverityLevel } from '@sentry/types';
33
import {
44
createClientReportEnvelope,
55
dsnToString,
@@ -103,7 +103,7 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
103103
/**
104104
* @inheritDoc
105105
*/
106-
public sendEvent(event: Event, attachments?: AttachmentWithData[]): void {
106+
public sendEvent(event: Event, attachments?: Attachment[]): void {
107107
// We only want to add the sentry event breadcrumb when the user has the breadcrumb integration installed and
108108
// activated its `sentry` option.
109109
// We also do not want to use the `Breadcrumbs` class here directly, because we do not want it to be included in

packages/core/src/baseclient.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable max-lines */
22
import { Scope, Session } from '@sentry/hub';
33
import {
4-
AttachmentWithData,
4+
Attachment,
55
Client,
66
ClientOptions,
77
DataCategory,
@@ -285,7 +285,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
285285
/**
286286
* @inheritDoc
287287
*/
288-
public sendEvent(event: Event, attachments?: AttachmentWithData[]): void {
288+
public sendEvent(event: Event, attachments?: Attachment[]): void {
289289
if (this._dsn) {
290290
const env = createEventEnvelope(event, this._dsn, this._options._metadata, this._options.tunnel);
291291

@@ -670,17 +670,15 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
670670
/**
671671
* Loads attachments from scope
672672
*/
673-
protected _attachmentsFromScope(scope: Scope | undefined): AttachmentWithData[] {
673+
protected _attachmentsFromScope(scope: Scope | undefined): Attachment[] {
674674
return (
675675
scope?.getAttachments()?.reduce((acc, attachment) => {
676-
if ('path' in attachment || !('data' in attachment)) {
677-
logger.error('This SDK does not support loading attachments from file paths and no data was supplied');
678-
} else {
676+
if ('data' in attachment) {
679677
acc.push(attachment);
680678
}
681679

682680
return acc;
683-
}, [] as AttachmentWithData[]) || []
681+
}, [] as Attachment[]) || []
684682
);
685683
}
686684

packages/hub/src/scope.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable max-lines */
22
import {
3-
Attachment,
3+
AttachmentOptions,
44
Breadcrumb,
55
CaptureContext,
66
Context,
@@ -87,7 +87,7 @@ export class Scope implements ScopeInterface {
8787
protected _requestSession?: RequestSession;
8888

8989
/** Attachments */
90-
protected _attachments: Attachment[] = [];
90+
protected _attachments: AttachmentOptions[] = [];
9191

9292
/**
9393
* A place to stash data which is needed at some point in the SDK's event processing pipeline but which shouldn't get
@@ -407,15 +407,15 @@ export class Scope implements ScopeInterface {
407407
/**
408408
* @inheritDoc
409409
*/
410-
public addAttachment(attachment: Attachment): this {
410+
public addAttachment(attachment: AttachmentOptions): this {
411411
this._attachments.push(attachment);
412412
return this;
413413
}
414414

415415
/**
416416
* @inheritDoc
417417
*/
418-
public getAttachments(): Attachment[] {
418+
public getAttachments(): AttachmentOptions[] {
419419
return this._attachments;
420420
}
421421

packages/node/src/client.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BaseClient, Scope, SDK_VERSION } from '@sentry/core';
22
import { SessionFlusher } from '@sentry/hub';
3-
import { AttachmentWithData, Event, EventHint, Severity, SeverityLevel } from '@sentry/types';
3+
import { Attachment, Event, EventHint, Severity, SeverityLevel } from '@sentry/types';
44
import { basename, logger, resolvedSyncPromise } from '@sentry/utils';
55
import { existsSync, readFileSync } from 'fs';
66
import { TextEncoder } from 'util';
@@ -162,7 +162,7 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
162162
/**
163163
* @inheritDoc
164164
*/
165-
protected _attachmentsFromScope(scope: Scope | undefined): AttachmentWithData[] {
165+
protected _attachmentsFromScope(scope: Scope | undefined): Attachment[] {
166166
return (
167167
scope?.getAttachments()?.map(attachment => {
168168
if ('path' in attachment && !('data' in attachment)) {
@@ -174,9 +174,12 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
174174
attachmentType: attachment.attachmentType,
175175
};
176176
} else {
177+
const msg = `Missing attachment - file not found: ${attachment.path}`;
178+
logger.warn(msg);
179+
177180
return {
178181
filename: attachment.filename || basename(attachment.path),
179-
data: 'Missing attachment - file not found',
182+
data: msg,
180183
contentType: 'text/plain',
181184
};
182185
}

packages/node/test/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { initAndBind, SDK_VERSION } from '@sentry/core';
22
import { getMainCarrier } from '@sentry/hub';
3-
import { AttachmentWithData,Integration } from '@sentry/types';
3+
import { Attachment, Integration } from '@sentry/types';
44
import * as domain from 'domain';
55

66
import {
@@ -76,7 +76,7 @@ describe('SentryNode', () => {
7676
});
7777

7878
describe('breadcrumbs', () => {
79-
let s: jest.SpyInstance<void, [Event, AttachmentWithData[]?]>;
79+
let s: jest.SpyInstance<void, [Event, Attachment[]?]>;
8080

8181
beforeEach(() => {
8282
s = jest.spyOn(NodeClient.prototype, 'sendEvent').mockImplementation(async () => Promise.resolve({ code: 200 }));
@@ -107,7 +107,7 @@ describe('SentryNode', () => {
107107
});
108108

109109
describe('capture', () => {
110-
let s: jest.SpyInstance<void, [Event, AttachmentWithData[]?]>;
110+
let s: jest.SpyInstance<void, [Event, Attachment[]?]>;
111111

112112
beforeEach(() => {
113113
s = jest.spyOn(NodeClient.prototype, 'sendEvent').mockImplementation(async () => Promise.resolve({ code: 200 }));

packages/types/src/attachment.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
export type Attachment = AttachmentWithPath | AttachmentWithData;
1+
export type AttachmentOptions = Attachment | AttachmentFromPath;
22

3-
export interface AttachmentWithData {
3+
export interface Attachment {
44
data: string | Uint8Array;
55
filename: string;
66
contentType?: string;
77
attachmentType?: string;
88
}
99

10-
interface AttachmentWithPath {
10+
interface AttachmentFromPath {
1111
path: string;
1212
filename?: string;
1313
contentType?: string;

packages/types/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type { Attachment, AttachmentWithData } from './attachment';
1+
export type { Attachment, AttachmentOptions } from './attachment';
22
export type { Breadcrumb, BreadcrumbHint } from './breadcrumb';
33
export type { Client } from './client';
44
export type { ClientReport, Outcome, EventDropReason } from './clientreport';

packages/types/src/scope.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Attachment } from './attachment';
1+
import { AttachmentOptions } from './attachment';
22
import { Breadcrumb } from './breadcrumb';
33
import { Context, Contexts } from './context';
44
import { EventProcessor } from './eventprocessor';
@@ -164,12 +164,12 @@ export interface Scope {
164164
* Adds an attachment to the scope
165165
* @param attachment Attachment options
166166
*/
167-
addAttachment(attachment: Attachment): this;
167+
addAttachment(attachment: AttachmentOptions): this;
168168

169169
/**
170170
* Returns an array of attachments on the scope
171171
*/
172-
getAttachments(): Attachment[];
172+
getAttachments(): AttachmentOptions[];
173173

174174
/**
175175
* Clears attachments from the scope

packages/utils/src/envelope.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
AttachmentItem,
3-
AttachmentWithData,
4-
DataCategory,
5-
Envelope,
6-
EnvelopeItem,
7-
EnvelopeItemType,
8-
} from '@sentry/types';
1+
import { Attachment, AttachmentItem, DataCategory, Envelope, EnvelopeItem, EnvelopeItemType } from '@sentry/types';
92

103
import { dropUndefinedKeys } from './object';
114

@@ -93,7 +86,7 @@ function concatBuffers(buffers: Uint8Array[]): Uint8Array {
9386
* Creates attachment envelope items
9487
*/
9588
export function createAttachmentEnvelopeItem(
96-
attachment: AttachmentWithData,
89+
attachment: Attachment,
9790
textEncoder?: TextEncoderInternal,
9891
): AttachmentItem {
9992
const utf8 = textEncoder || new TextEncoder();

0 commit comments

Comments
 (0)