1- import { ISentryBrowserOptions , SentryBrowser } from '@sentry/browser' ;
2- import { Client , Event , IAdapter , IBreadcrumb , IUser } from '@sentry/core' ;
1+ import { SentryBrowser , SentryBrowserOptions } from '@sentry/browser' ;
2+ import {
3+ Adapter ,
4+ Breadcrumb ,
5+ Client ,
6+ Context ,
7+ SentryEvent ,
8+ User ,
9+ } from '@sentry/core' ;
310
411declare var window : any ;
512declare var document : any ;
613
714const CORDOVA_DEVICE_RDY_TIMEOUT = 10000 ;
815
916export interface ISentryBrowserConstructable < T > {
10- new ( client : Client , options ?: ISentryBrowserOptions ) : T ;
17+ new ( client : Client , options ?: SentryBrowserOptions ) : T ;
1118}
1219
13- export interface ISentryCordovaOptions {
20+ export interface SentryCordovaOptions extends SentryBrowserOptions {
1421 deviceReadyTimeout ?: number ;
1522 sentryBrowser ?: ISentryBrowserConstructable < SentryBrowser > ;
1623}
1724
18- export class SentryCordova implements IAdapter {
25+ export class SentryCordova implements Adapter {
1926 private browser : SentryBrowser ;
2027 private client : Client ;
2128 private deviceReadyCallback : any ;
@@ -24,29 +31,27 @@ export class SentryCordova implements IAdapter {
2431 private PLUGIN_NAME = 'Sentry' ;
2532 private PATH_STRIP_RE = / ^ .* \/ [ ^ \. ] + ( \. a p p | C o d e P u s h | .* (? = \/ ) ) / ;
2633
27- constructor ( client : Client , public options : ISentryCordovaOptions = { } ) {
34+ constructor ( client : Client , public options : SentryCordovaOptions = { } ) {
2835 this . client = client ;
2936 if ( ! options . sentryBrowser ) {
3037 throw new Error (
31- 'must pass SentryBrowser as an option { sentryBrowser: SentryBrowser }'
38+ 'must pass SentryBrowser as an option { sentryBrowser: SentryBrowser }' ,
3239 ) ;
3340 }
3441 this . browser = new options . sentryBrowser ( client ) ;
3542 return this ;
3643 }
3744
38- public async install ( ) {
39- this . browser . setOptions ( {
40- allowDuplicates : true ,
41- } ) ;
45+ public async install ( ) : Promise < boolean > {
4246 await this . browser . install ( ) ;
4347 // This will prefix frames in raven with app://
4448 // this is just a fallback if native is not available
4549 this . setupNormalizeFrames ( ) ;
4650
4751 return new Promise < boolean > ( ( resolve , reject ) => {
4852 if ( this . isCordova ( ) ) {
49- const timeout = this . options . deviceReadyTimeout || CORDOVA_DEVICE_RDY_TIMEOUT ;
53+ const timeout =
54+ this . options . deviceReadyTimeout || CORDOVA_DEVICE_RDY_TIMEOUT ;
5055 const deviceReadyTimeout = setTimeout ( ( ) => {
5156 reject ( `deviceready wasn't called for ${ timeout } ms` ) ;
5257 } , timeout ) ;
@@ -59,11 +64,6 @@ export class SentryCordova implements IAdapter {
5964 }
6065 } )
6166 . then ( success => {
62- if ( success && this . isCordova ( ) ) {
63- // We only want to register the breadcrumbcallback on success and running on
64- // Cordova otherwise we will get an endless loop
65- this . browser . setBreadcrumbCallback ( crumb => this . captureBreadcrumb ( crumb ) ) ;
66- }
6767 this . tryToSetSentryRelease ( ) ;
6868 return Promise . resolve ( success ) ;
6969 } )
@@ -74,68 +74,74 @@ export class SentryCordova implements IAdapter {
7474 return this . browser as any ;
7575 }
7676
77- public async setOptions ( options : ISentryCordovaOptions ) {
77+ public async setOptions ( options : SentryCordovaOptions ) : Promise < void > {
7878 Object . assign ( this . options , options ) ;
79- return this ;
79+ return ;
8080 }
8181
82- public captureException ( exception : Error ) {
82+ public captureException ( exception : Error ) : Promise < SentryEvent > {
8383 return this . browser . captureException ( exception ) ;
8484 }
8585
86- public captureMessage ( message : string ) {
86+ public captureMessage ( message : string ) : Promise < SentryEvent > {
8787 return this . browser . captureMessage ( message ) ;
8888 }
8989
90- public captureBreadcrumb ( crumb : IBreadcrumb ) {
90+ public captureBreadcrumb ( crumb : Breadcrumb ) : Promise < Breadcrumb > {
9191 return this . nativeCall ( 'captureBreadcrumb' , crumb ) ;
9292 }
9393
94- public send ( event : Event ) {
94+ public send ( event : SentryEvent ) : Promise < void > {
9595 return this . nativeCall ( 'send' , event ) ;
9696 }
9797
98- public async setUserContext ( user ?: IUser ) {
99- await this . nativeCall ( 'setUserContext' , user ) ;
100- return this ;
101- }
102-
103- public async setTagsContext ( tags ?: { [ key : string ] : any } ) {
104- await this . nativeCall ( 'setTagsContext' , tags ) ;
105- return this ;
98+ public async getContext ( ) : Promise < Context > {
99+ return this . nativeCall ( 'getContext' ) ;
106100 }
107101
108- public async setExtraContext ( extra ?: { [ key : string ] : any } ) {
109- await this . nativeCall ( 'setExtraContext' , extra ) ;
110- return this ;
102+ public async setContext ( context : Context ) : Promise < void > {
103+ if ( context . extra ) {
104+ await this . nativeCall ( 'setExtraContext' , context . extra ) ;
105+ }
106+ if ( context . user ) {
107+ await this . nativeCall ( 'setUserContext' , context . user ) ;
108+ }
109+ if ( context . tags ) {
110+ await this . nativeCall ( 'setTagsContext' , context . tags ) ;
111+ }
111112 }
112113
113- public async clearContext ( ) {
114+ public async clearContext ( ) : Promise < any > {
114115 return this . nativeCall ( 'clearContext' ) ;
115116 }
116117
117- public async setRelease ( release : string ) {
118+ public async setRelease ( release : string ) : Promise < void > {
118119 return this . setInternalOption ( 'release' , release ) ;
119120 }
120121
121- public async setDist ( dist : string ) {
122+ public async setDist ( dist : string ) : Promise < void > {
122123 return this . setInternalOption ( 'dist' , dist ) ;
123124 }
124125
125- public async setVersion ( version : string ) {
126+ public async setVersion ( version : string ) : Promise < void > {
126127 return this . setInternalOption ( 'version' , version ) ;
127128 }
128129
129130 // Private helpers
130131
131- private setInternalOption ( key : string , value : string ) {
132- return this . setExtraContext ( {
133- [ `__sentry_${ key } ` ] : value ,
132+ private setInternalOption ( key : string , value : string ) : Promise < void > {
133+ return this . setContext ( {
134+ extra : {
135+ [ `__sentry_${ key } ` ] : value ,
136+ } ,
134137 } ) ;
135138 }
136139
137- private tryToSetSentryRelease ( ) {
138- if ( window . SENTRY_RELEASE !== undefined && window . SENTRY_RELEASE . id !== undefined ) {
140+ private tryToSetSentryRelease ( ) : void {
141+ if (
142+ window . SENTRY_RELEASE !== undefined &&
143+ window . SENTRY_RELEASE . id !== undefined
144+ ) {
139145 this . setRelease ( window . SENTRY_RELEASE . id ) ;
140146 this . browser . getRaven ( ) . setRelease ( window . SENTRY_RELEASE . id ) ;
141147 this . client . log ( 'received release from window.SENTRY_RELEASE' ) ;
@@ -145,17 +151,24 @@ export class SentryCordova implements IAdapter {
145151 // ---------------------------------------
146152
147153 // CORDOVA --------------------
148- private isCordova ( ) {
154+ private isCordova ( ) : boolean {
149155 return window . cordova !== undefined || window . Cordova !== undefined ;
150156 }
151157
152158 private nativeCall ( action : string , ...args : any [ ] ) : Promise < any > {
153159 return new Promise ( ( resolve , reject ) => {
154- const exec = window && ( window as any ) . Cordova && ( window as any ) . Cordova . exec ;
160+ const exec =
161+ window && ( window as any ) . Cordova && ( window as any ) . Cordova . exec ;
155162 if ( ! exec ) {
156163 reject ( 'Cordova.exec not available' ) ;
157164 } else {
158- ( window as any ) . Cordova . exec ( resolve , reject , this . PLUGIN_NAME , action , args ) ;
165+ ( window as any ) . Cordova . exec (
166+ resolve ,
167+ reject ,
168+ this . PLUGIN_NAME ,
169+ action ,
170+ args ,
171+ ) ;
159172 }
160173 } ) . catch ( e => {
161174 if ( e === 'not implemented' || e === 'Cordova.exec not available' ) {
@@ -170,22 +183,22 @@ export class SentryCordova implements IAdapter {
170183 private runInstall (
171184 resolve : ( value ?: boolean | PromiseLike < boolean > ) => void ,
172185 reject : ( reason ?: any ) => void ,
173- deviceReadyTimeout ?: NodeJS . Timer
174- ) {
186+ deviceReadyTimeout ?: NodeJS . Timer ,
187+ ) : void {
175188 if ( deviceReadyTimeout ) {
176189 document . removeEventListener ( 'deviceready' , this . deviceReadyCallback ) ;
177190 clearTimeout ( deviceReadyTimeout ) ;
178191 }
179- this . nativeCall ( 'install' , this . client . dsn . getDsn ( true ) , this . options )
192+ this . nativeCall ( 'install' , this . client . dsn . toString ( true ) , this . options )
180193 . then ( resolve )
181194 . catch ( reject ) ;
182195 }
183196
184197 // ----------------------------------------------------------
185198 // Raven
186199
187- private wrappedCallback ( callback : any ) {
188- function dataCallback ( data : any , original : any ) {
200+ private wrappedCallback ( callback : any ) : ( data : any , original : any ) => void {
201+ function dataCallback ( data : any , original : any ) : void {
189202 const normalizedData = callback ( data ) || data ;
190203 if ( original ) {
191204 return original ( normalizedData ) || normalizedData ;
@@ -195,35 +208,35 @@ export class SentryCordova implements IAdapter {
195208 return dataCallback ;
196209 }
197210
198- private setupNormalizeFrames ( ) {
211+ private setupNormalizeFrames ( ) : void {
199212 const raven = this . browser . getRaven ( ) ;
200213 raven . setDataCallback (
201214 this . wrappedCallback ( ( data : any ) => {
202215 data = this . normalizeData ( data ) ;
203- // TODO
204- // if (internalDataCallback) {
205- // internalDataCallback(data);
206- // }
207- } )
216+ } ) ,
208217 ) ;
209218 }
210219
211- private normalizeUrl ( url : string , pathStripRe : RegExp ) {
220+ private normalizeUrl ( url : string , pathStripRe : RegExp ) : string {
212221 return 'app://' + url . replace ( / ^ f i l e \: \/ \/ / , '' ) . replace ( pathStripRe , '' ) ;
213222 }
214223
215- private normalizeData ( data : any , pathStripRe ?: RegExp ) {
224+ private normalizeData ( data : any , pathStripRe ?: RegExp ) : any {
216225 if ( data . culprit ) {
217226 data . culprit = this . normalizeUrl ( data . culprit , this . PATH_STRIP_RE ) ;
218227 }
219228 // NOTE: if data.exception exists, exception.values and exception.values[0] are
220229 // guaranteed to exist
221230 const stacktrace =
222- data . stacktrace || ( data . exception && data . exception . values [ 0 ] . stacktrace ) ;
231+ data . stacktrace ||
232+ ( data . exception && data . exception . values [ 0 ] . stacktrace ) ;
223233 if ( stacktrace ) {
224234 stacktrace . frames . forEach ( ( frame : any ) => {
225235 if ( frame . filename !== '[native code]' ) {
226- frame . filename = this . normalizeUrl ( frame . filename , this . PATH_STRIP_RE ) ;
236+ frame . filename = this . normalizeUrl (
237+ frame . filename ,
238+ this . PATH_STRIP_RE ,
239+ ) ;
227240 }
228241 } ) ;
229242 }
0 commit comments