11import { ErrorCodes } from "./errorCodes" ;
22import { LogRequestBody , WSDKErrorSeverity } from "./logRequest" ;
33import { FetchUploader , IFetchPayload } from "../uploaders" ;
4+ import { IStore , SDKConfig } from "../store" ;
45
56// QUESTION: Should we collapse the interface with the class?
67export interface IReportingLogger {
7- error ( msg : string , code : ErrorCodes , stackTrace ?: string ) : void ;
8- warning ( msg : string , code : ErrorCodes ) : void ;
8+ error ( msg : string , code ? : ErrorCodes , stackTrace ?: string ) : void ;
9+ warning ( msg : string , code ? : ErrorCodes ) : void ;
910}
1011
1112export class ReportingLogger implements IReportingLogger {
1213 private readonly isEnabled : boolean ;
1314 private readonly reporter : string = 'mp-wsdk' ;
1415 private readonly integration : string = 'mp-wsdk' ;
1516 private readonly rateLimiter : IRateLimiter ;
16- private loggingUrl : string ;
17- private errorUrl : string ;
18- private accountId : string ;
1917 private integrationName : string ;
18+ private store : IStore ;
2019
2120 constructor (
21+ private readonly config : SDKConfig ,
2222 private readonly sdkVersion : string ,
23- rateLimiter ?: IRateLimiter , // QUESTION: Do we need this in the constructor?
2423 private readonly launcherInstanceGuid ?: string ,
24+ rateLimiter ?: IRateLimiter ,
2525 ) {
2626 this . isEnabled = this . isReportingEnabled ( ) ;
27- console . warn ( 'ReportingLogger: isEnabled' , this . isEnabled ) ;
2827 this . rateLimiter = rateLimiter ?? new RateLimiter ( ) ;
2928 }
3029
31- public setLoggingUrl ( url : string ) {
32- this . loggingUrl = url ;
33- }
34-
35- public setErrorUrl ( url : string ) {
36- this . errorUrl = url ;
37- }
38-
39- public setAccountId ( accountId : string ) {
40- this . accountId = accountId ;
41- }
42-
4330 public setIntegrationName ( integrationName : string ) {
4431 this . integrationName = integrationName ;
4532 }
33+
34+ public setStore ( store : IStore ) {
35+ this . store = store ;
36+ }
4637
47- // TODO: Add an `info` method to the logger for `/v1/log`
38+ public info ( msg : string , code ?: ErrorCodes ) {
39+ this . sendLog ( WSDKErrorSeverity . INFO , msg , code ) ;
40+ }
4841
49- public error ( msg : string , code : ErrorCodes , stackTrace ?: string ) {
50- this . sendLog ( WSDKErrorSeverity . ERROR , msg , code , stackTrace ) ;
42+ public error ( msg : string , code ? : ErrorCodes , stackTrace ?: string ) {
43+ this . sendError ( WSDKErrorSeverity . ERROR , msg , code , stackTrace ) ;
5144 } ;
5245
53- public warning ( msg : string , code : ErrorCodes ) {
54- this . sendLog ( WSDKErrorSeverity . WARNING , msg , code ) ;
46+ public warning ( msg : string , code ? : ErrorCodes ) {
47+ this . sendError ( WSDKErrorSeverity . WARNING , msg , code ) ;
5548 } ;
5649
57- private getVersion ( ) : string {
58- return this . integrationName ?? this . sdkVersion ;
59- }
60-
61- // QUESTION: Should we split this into `sendError` and `sendLog`?
62- private sendLog (
63- severity : WSDKErrorSeverity ,
64- msg : string ,
65- code : ErrorCodes ,
66- stackTrace ?: string
67- ) : void {
50+ private sendToServer ( url : string , severity : WSDKErrorSeverity , msg : string , code ?: ErrorCodes , stackTrace ?: string ) : void {
6851 if ( ! this . canSendLog ( severity ) )
6952 return ;
53+
54+ const logRequest = this . getLogRequest ( severity , msg , code , stackTrace ) ;
55+ const uploader = new FetchUploader ( url ) ;
56+ const payload : IFetchPayload = {
57+ method : 'POST' ,
58+ headers : this . getHeaders ( ) ,
59+ body : JSON . stringify ( logRequest ) ,
60+ } ;
61+ uploader . upload ( payload ) ;
62+ }
7063
71- const logRequest : LogRequestBody = {
64+ private sendLog ( severity : WSDKErrorSeverity , msg : string , code ?: ErrorCodes , stackTrace ?: string ) : void {
65+ const url = this . getLoggingUrl ( ) ;
66+ this . sendToServer ( url , severity , msg , code , stackTrace ) ;
67+ }
68+ private sendError ( severity : WSDKErrorSeverity , msg : string , code ?: ErrorCodes , stackTrace ?: string ) : void {
69+ const url = this . getErrorUrl ( ) ;
70+ this . sendToServer ( url , severity , msg , code , stackTrace ) ;
71+ }
72+
73+ private getLogRequest ( severity : WSDKErrorSeverity , msg : string , code ?: ErrorCodes , stackTrace ?: string ) : LogRequestBody {
74+ return {
7275 additionalInformation : {
7376 message : msg ,
7477 version : this . getVersion ( ) ,
7578 } ,
7679 severity : severity ,
77- code : code ,
80+ code : code ?? ErrorCodes . UNKNOWN_ERROR ,
7881 url : this . getUrl ( ) ,
7982 deviceInfo : this . getUserAgent ( ) ,
80- stackTrace : stackTrace ?? 'this is my stack trace ' ,
83+ stackTrace : stackTrace ?? '' ,
8184 reporter : this . reporter ,
82- integration : this . integration ,
85+ integration : this . integration
8386 } ;
84-
85- this . sendLogToServer ( logRequest ) ;
8687 }
8788
89+ private getVersion ( ) : string {
90+ return this . integrationName ?? this . sdkVersion ;
91+ }
92+
8893 private isReportingEnabled ( ) : boolean {
8994 // QUESTION: Should isDebugModeEnabled take precedence over
9095 // isFeatureFlagEnabled and rokt domain present?
@@ -100,10 +105,7 @@ export class ReportingLogger implements IReportingLogger {
100105 }
101106
102107 private isFeatureFlagEnabled ( ) : boolean {
103- return window .
104- mParticle ?.
105- config ?.
106- isWebSdkLoggingEnabled ?? false ;
108+ return this . config . isWebSdkLoggingEnabled ;
107109 }
108110
109111 private isDebugModeEnabled ( ) : boolean {
@@ -132,8 +134,8 @@ export class ReportingLogger implements IReportingLogger {
132134 return window . navigator . userAgent ;
133135 }
134136
135- private getLoggingUrl = ( ) : string => `https://${ this . loggingUrl } ` ;
136- private getErrorUrl = ( ) : string => `https://${ this . errorUrl } ` ;
137+ private getLoggingUrl = ( ) : string => `https://${ this . config . loggingUrl } ` ;
138+ private getErrorUrl = ( ) : string => `https://${ this . config . errorUrl } ` ;
137139
138140 private getHeaders ( ) : IFetchPayload [ 'headers' ] {
139141 const headers : Record < string , string > = {
@@ -143,20 +145,13 @@ export class ReportingLogger implements IReportingLogger {
143145 'rokt-launcher-version' : this . getVersion ( ) ,
144146 'rokt-wsdk-version' : 'joint' ,
145147 } ;
148+
149+ if ( this . store ?. getRoktAccountId ( ) ) {
150+ headers [ 'rokt-account-id' ] = this . store . getRoktAccountId ( ) ;
151+ }
152+
146153 return headers as IFetchPayload [ 'headers' ] ;
147154 }
148-
149- private sendLogToServer ( logRequest : LogRequestBody ) {
150- const uploadUrl = this . getErrorUrl ( ) ;
151- const uploader = new FetchUploader ( uploadUrl ) ;
152- const payload : IFetchPayload = {
153- method : 'POST' ,
154- headers : this . getHeaders ( ) ,
155- body : JSON . stringify ( logRequest ) ,
156- } ;
157-
158- uploader . upload ( payload ) ;
159- } ;
160155}
161156
162157export interface IRateLimiter {
0 commit comments