Skip to content

Commit fddb78c

Browse files
committed
ref(utils): drop dsn toString class method
1 parent 5289243 commit fddb78c

File tree

7 files changed

+33
-49
lines changed

7 files changed

+33
-49
lines changed

packages/browser/src/transports/base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from '@sentry/types';
1515
import {
1616
dateTimestampInSeconds,
17+
dsnToString,
1718
eventStatusFromHttpCode,
1819
getGlobalObject,
1920
logger,
@@ -116,7 +117,7 @@ export abstract class BaseTransport implements Transport {
116117

117118
const url = getEnvelopeEndpointWithUrlEncodedAuth(this._api.dsn, this._api.tunnel);
118119
// Envelope header is required to be at least an empty object
119-
const envelopeHeader = JSON.stringify({ ...(this._api.tunnel && { dsn: this._api.dsn.toString() }) });
120+
const envelopeHeader = JSON.stringify({ ...(this._api.tunnel && { dsn: dsntoString(this._api.dsn) }) });
120121
const itemHeaders = JSON.stringify({
121122
type: 'client_report',
122123
});

packages/core/src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export function getReportDialogEndpoint(
174174
const dsn = makeDsn(dsnLike);
175175
const endpoint = `${getBaseApiEndpoint(dsn)}embed/error-page/`;
176176

177-
let encodedOptions = `dsn=${dsn.toString()}`;
177+
let encodedOptions = `dsn=${dsnToString(dsn)}`;
178178
for (const key in dialogOptions) {
179179
if (key === 'dsn') {
180180
continue;

packages/core/src/request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function sessionToSentryRequest(session: Session | SessionAggregates, api
3333
const envelopeHeaders = JSON.stringify({
3434
sent_at: new Date().toISOString(),
3535
...(sdkInfo && { sdk: sdkInfo }),
36-
...(!!api.tunnel && { dsn: api.dsn.toString() }),
36+
...(!!api.tunnel && { dsn: dsnToString(api.dsn) }),
3737
});
3838
// I know this is hacky but we don't want to add `session` to request type since it's never rate limited
3939
const type: SentryRequestType = 'aggregates' in session ? ('sessions' as SentryRequestType) : 'session';
@@ -81,7 +81,7 @@ export function eventToSentryRequest(event: Event, api: APIDetails): SentryReque
8181
event_id: event.event_id,
8282
sent_at: new Date().toISOString(),
8383
...(sdkInfo && { sdk: sdkInfo }),
84-
...(!!api.tunnel && { dsn: api.dsn.toString() }),
84+
...(!!api.tunnel && { dsn: dsnToString(api.dsn) }),
8585
});
8686
const itemHeaders = JSON.stringify({
8787
type: eventType,

packages/types/src/dsn.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,3 @@ export interface DsnComponents {
2323

2424
/** Anything that can be parsed into a Dsn. */
2525
export type DsnLike = string | DsnComponents;
26-
27-
/** The Sentry Dsn, identifying a Sentry instance and project. */
28-
export interface Dsn extends DsnComponents {
29-
/**
30-
* Renders the string representation of this Dsn.
31-
*
32-
* By default, this will render the public representation without the password
33-
* component. To get the deprecated private representation, set `withPassword`
34-
* to true.
35-
*
36-
* @param withPassword When set to true, the password will be included.
37-
*/
38-
toString(withPassword?: boolean): string;
39-
}

packages/types/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export { Breadcrumb, BreadcrumbHint } from './breadcrumb';
22
export { Client } from './client';
33
export { Context, Contexts } from './context';
4-
export { Dsn, DsnComponents, DsnLike, DsnProtocol } from './dsn';
4+
export { DsnComponents, DsnLike, DsnProtocol } from './dsn';
55
export { DebugImage, DebugImageType, DebugMeta } from './debugMeta';
66
export { ExtendedError } from './error';
77
export { Event, EventHint } from './event';

packages/utils/src/dsn.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function isValidProtocol(protocol?: string): protocol is DsnProtocol {
1919
*
2020
* @param withPassword When set to true, the password will be included.
2121
*/
22-
function dsntoString(dsn: Dsn, withPassword: boolean = false): string {
22+
export function dsnToString(dsn: Dsn, withPassword: boolean = false): string {
2323
const { host, path, pass, port, projectId, protocol, publicKey } = dsn;
2424
return (
2525
`${protocol}://${publicKey}${withPassword && pass ? `:${pass}` : ''}` +
@@ -72,28 +72,30 @@ function dsnFromComponents(components: DsnComponents): Dsn {
7272
};
7373
}
7474

75-
function validateDsn(dsn: Dsn): boolean {
76-
if (isDebugBuild()) {
77-
const { port, projectId, protocol } = dsn;
75+
function validateDsn(dsn: Dsn): boolean | void {
76+
if (!isDebugBuild()) {
77+
return;
78+
}
7879

79-
const requiredComponents: ReadonlyArray<keyof DsnComponents> = ['protocol', 'publicKey', 'host', 'projectId'];
80-
requiredComponents.forEach(component => {
81-
if (!dsn[component]) {
82-
throw new SentryError(`Invalid Dsn: ${component} missing`);
83-
}
84-
});
80+
const { port, projectId, protocol } = dsn;
8581

86-
if (!projectId.match(/^\d+$/)) {
87-
throw new SentryError(`Invalid Dsn: Invalid projectId ${projectId}`);
82+
const requiredComponents: ReadonlyArray<keyof DsnComponents> = ['protocol', 'publicKey', 'host', 'projectId'];
83+
requiredComponents.forEach(component => {
84+
if (!dsn[component]) {
85+
throw new SentryError(`Invalid Dsn: ${component} missing`);
8886
}
87+
});
8988

90-
if (!isValidProtocol(protocol)) {
91-
throw new SentryError(`Invalid Dsn: Invalid protocol ${protocol}`);
92-
}
89+
if (!projectId.match(/^\d+$/)) {
90+
throw new SentryError(`Invalid Dsn: Invalid projectId ${projectId}`);
91+
}
9392

94-
if (port && isNaN(parseInt(port, 10))) {
95-
throw new SentryError(`Invalid Dsn: Invalid port ${port}`);
96-
}
93+
if (!isValidProtocol(protocol)) {
94+
throw new SentryError(`Invalid Dsn: Invalid protocol ${protocol}`);
95+
}
96+
97+
if (port && isNaN(parseInt(port, 10))) {
98+
throw new SentryError(`Invalid Dsn: Invalid port ${port}`);
9799
}
98100

99101
return true;
@@ -105,10 +107,5 @@ export function makeDsn(from: DsnLike): Dsn {
105107

106108
validateDsn(components);
107109

108-
const dsn: Dsn = {
109-
...components,
110-
toString: (withPassword?: boolean) => dsntoString(dsn, withPassword),
111-
};
112-
113-
return dsn;
110+
return components;
114111
}

packages/utils/test/dsn.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isDebugBuild } from '@sentry/utils';
22

3-
import { makeDsn } from '../src/dsn';
3+
import { makeDsn, dsnToString } from '../src/dsn';
44
import { SentryError } from '../src/error';
55

66
function testIf(condition: boolean): jest.It {
@@ -165,27 +165,27 @@ describe('Dsn', () => {
165165
describe('toString', () => {
166166
test('excludes the password by default', () => {
167167
const dsn = makeDsn('https://abc:[email protected]:1234/123');
168-
expect(dsn.toString()).toBe('https://[email protected]:1234/123');
168+
expect(dsnToString(dsn)).toBe('https://[email protected]:1234/123');
169169
});
170170

171171
test('optionally includes the password', () => {
172172
const dsn = makeDsn('https://abc:[email protected]:1234/123');
173-
expect(dsn.toString(true)).toBe('https://abc:[email protected]:1234/123');
173+
expect(dsnToString(dsn, true)).toBe('https://abc:[email protected]:1234/123');
174174
});
175175

176176
test('renders no password if missing', () => {
177177
const dsn = makeDsn('https://[email protected]:1234/123');
178-
expect(dsn.toString(true)).toBe('https://[email protected]:1234/123');
178+
expect(dsnToString(dsn, true)).toBe('https://[email protected]:1234/123');
179179
});
180180

181181
test('renders no port if missing', () => {
182182
const dsn = makeDsn('https://[email protected]/123');
183-
expect(dsn.toString()).toBe('https://[email protected]/123');
183+
expect(dsnToString(dsn)).toBe('https://[email protected]/123');
184184
});
185185

186186
test('renders the full path correctly', () => {
187187
const dsn = makeDsn('https://[email protected]/sentry/custom/installation/321');
188-
expect(dsn.toString()).toBe('https://[email protected]/sentry/custom/installation/321');
188+
expect(dsnToString(dsn)).toBe('https://[email protected]/sentry/custom/installation/321');
189189
});
190190
});
191191
});

0 commit comments

Comments
 (0)