1
1
import { DsnComponents , DsnLike , DsnProtocol } from '@sentry/types' ;
2
2
3
3
import { SentryError } from './error' ;
4
+ import { logger } from './logger' ;
4
5
5
6
/** Regular expression used to parse a Dsn. */
6
7
const DSN_REGEX = / ^ (?: ( \w + ) : ) \/ \/ (?: ( \w + ) (?: : ( \w + ) ) ? @ ) ( [ \w . - ] + ) (?: : ( \d + ) ) ? \/ ( .+ ) / ;
@@ -12,8 +13,10 @@ const ERROR_MESSAGE = 'Invalid Dsn';
12
13
export class Dsn implements DsnComponents {
13
14
/** Protocol used to connect to Sentry. */
14
15
public protocol ! : DsnProtocol ;
15
- /** Public authorization key. */
16
+ /** Public authorization key (deprecated, renamed to publicKey) . */
16
17
public user ! : string ;
18
+ /** Public authorization key. */
19
+ public publicKey ! : string ;
17
20
/** Private authorization key (deprecated, optional). */
18
21
public pass ! : string ;
19
22
/** Hostname of the Sentry instance. */
@@ -46,9 +49,9 @@ export class Dsn implements DsnComponents {
46
49
* @param withPassword When set to true, the password will be included.
47
50
*/
48
51
public toString ( withPassword : boolean = false ) : string {
49
- const { host, path, pass, port, projectId, protocol, user } = this ;
52
+ const { host, path, pass, port, projectId, protocol, publicKey } = this ;
50
53
return (
51
- `${ protocol } ://${ user } ${ withPassword && pass ? `:${ pass } ` : '' } ` +
54
+ `${ protocol } ://${ publicKey } ${ withPassword && pass ? `:${ pass } ` : '' } ` +
52
55
`@${ host } ${ port ? `:${ port } ` : '' } /${ path ? `${ path } /` : path } ${ projectId } `
53
56
) ;
54
57
}
@@ -61,7 +64,7 @@ export class Dsn implements DsnComponents {
61
64
throw new SentryError ( ERROR_MESSAGE ) ;
62
65
}
63
66
64
- const [ protocol , user , pass = '' , host , port = '' , lastPath ] = match . slice ( 1 ) ;
67
+ const [ protocol , publicKey , pass = '' , host , port = '' , lastPath ] = match . slice ( 1 ) ;
65
68
let path = '' ;
66
69
let projectId = lastPath ;
67
70
@@ -78,13 +81,20 @@ export class Dsn implements DsnComponents {
78
81
}
79
82
}
80
83
81
- this . _fromComponents ( { host, pass, path, projectId, port, protocol : protocol as DsnProtocol , user } ) ;
84
+ this . _fromComponents ( { host, pass, path, projectId, port, protocol : protocol as DsnProtocol , publicKey } ) ;
82
85
}
83
86
84
87
/** Maps Dsn components into this instance. */
85
88
private _fromComponents ( components : DsnComponents ) : void {
89
+ if ( 'user' in components && ! ( 'publicKey' in components ) ) {
90
+ logger . warn ( 'Use of `user` in DsnComponents is deprecated. Please use `publicKey` instead.' ) ;
91
+ components . publicKey = components . user ;
92
+ }
93
+ // TODO this is for backwards compatibility, and can be removed in a future version
94
+ this . user = components . publicKey || '' ;
95
+
86
96
this . protocol = components . protocol ;
87
- this . user = components . user ;
97
+ this . publicKey = components . publicKey || '' ;
88
98
this . pass = components . pass || '' ;
89
99
this . host = components . host ;
90
100
this . port = components . port || '' ;
@@ -94,7 +104,7 @@ export class Dsn implements DsnComponents {
94
104
95
105
/** Validates this Dsn and throws on error. */
96
106
private _validate ( ) : void {
97
- [ 'protocol' , 'user ' , 'host' , 'projectId' ] . forEach ( component => {
107
+ [ 'protocol' , 'publicKey ' , 'host' , 'projectId' ] . forEach ( component => {
98
108
if ( ! this [ component as keyof DsnComponents ] ) {
99
109
throw new SentryError ( `${ ERROR_MESSAGE } : ${ component } missing` ) ;
100
110
}
0 commit comments