@@ -21,16 +21,29 @@ import { ERROR_MESSAGES, ODP_USER_KEY } from '../../utils/enums';
21
21
import { VuidManager } from '../../plugins/vuid_manager' ;
22
22
23
23
import { OdpConfig } from './odp_config' ;
24
- import { OdpEventManager } from './odp_event_manager' ;
25
- import { OdpSegmentManager } from './odp_segment_manager' ;
24
+ import { IOdpEventManager } from './odp_event_manager' ;
25
+ import { IOdpSegmentManager } from './odp_segment_manager' ;
26
26
import { OptimizelySegmentOption } from './optimizely_segment_option' ;
27
27
import { invalidOdpDataFound } from './odp_utils' ;
28
28
import { OdpEvent } from './odp_event' ;
29
29
30
+ export interface IOdpManager {
31
+ enabled : boolean ;
32
+ segmentManager : IOdpSegmentManager | undefined ;
33
+ eventManager : IOdpEventManager | undefined ;
34
+ updateSettings ( { apiKey, apiHost, segmentsToCheck } : OdpConfig ) : boolean ;
35
+ close ( ) : void ;
36
+ fetchQualifiedSegments ( userId : string , options ?: Array < OptimizelySegmentOption > ) : Promise < string [ ] | null > ;
37
+ identifyUser ( userId ?: string , vuid ?: string ) : void ;
38
+ sendEvent ( { type, action, identifiers, data } : OdpEvent ) : void ;
39
+ isVuidEnabled ( ) : boolean ;
40
+ getVuid ( ) : string | undefined ;
41
+ }
42
+
30
43
/**
31
44
* Orchestrates segments manager, event manager, and ODP configuration
32
45
*/
33
- export abstract class OdpManager {
46
+ export abstract class OdpManager implements IOdpManager {
34
47
initPromise ?: Promise < void > ;
35
48
enabled = true ;
36
49
logger : LogHandler = getLogger ( ) ;
@@ -40,20 +53,20 @@ export abstract class OdpManager {
40
53
* ODP Segment Manager which provides an interface to the remote ODP server (GraphQL API) for audience segments mapping.
41
54
* It fetches all qualified segments for the given user context and manages the segments cache for all user contexts.
42
55
*/
43
- public segmentManager : OdpSegmentManager | undefined ;
56
+ segmentManager : IOdpSegmentManager | undefined ;
44
57
45
58
/**
46
59
* ODP Event Manager which provides an interface to the remote ODP server (REST API) for events.
47
60
* It will queue all pending events (persistent) and send them (in batches of up to 10 events) to the ODP server when possible.
48
61
*/
49
- public eventManager : OdpEventManager | undefined ;
62
+ eventManager : IOdpEventManager | undefined ;
50
63
51
64
constructor ( ) { }
52
65
53
66
/**
54
67
* Provides a method to update ODP Manager's ODP Config API Key, API Host, and Audience Segments
55
68
*/
56
- public updateSettings ( { apiKey, apiHost, segmentsToCheck } : OdpConfig ) : boolean {
69
+ updateSettings ( { apiKey, apiHost, segmentsToCheck } : OdpConfig ) : boolean {
57
70
if ( ! this . enabled ) {
58
71
return false ;
59
72
}
@@ -85,7 +98,7 @@ export abstract class OdpManager {
85
98
/**
86
99
* Attempts to stop the current instance of ODP Manager's event manager, if it exists and is running.
87
100
*/
88
- public close ( ) : void {
101
+ close ( ) : void {
89
102
if ( ! this . enabled ) {
90
103
return ;
91
104
}
@@ -100,10 +113,7 @@ export abstract class OdpManager {
100
113
* @param {Array<OptimizelySegmentOption> } options - An array of OptimizelySegmentOption used to ignore and/or reset the cache.
101
114
* @returns {Promise<string[] | null> } A promise holding either a list of qualified segments or null.
102
115
*/
103
- public async fetchQualifiedSegments (
104
- userId : string ,
105
- options : Array < OptimizelySegmentOption > = [ ]
106
- ) : Promise < string [ ] | null > {
116
+ async fetchQualifiedSegments ( userId : string , options : Array < OptimizelySegmentOption > = [ ] ) : Promise < string [ ] | null > {
107
117
if ( ! this . enabled ) {
108
118
this . logger . log ( LogLevel . ERROR , ERROR_MESSAGES . ODP_NOT_ENABLED ) ;
109
119
return null ;
@@ -127,7 +137,7 @@ export abstract class OdpManager {
127
137
* @param {string } vuid (Optional) Secondary unique identifier of a target user, primarily used by client SDKs.
128
138
* @returns
129
139
*/
130
- public identifyUser ( userId ?: string , vuid ?: string ) : void {
140
+ identifyUser ( userId ?: string , vuid ?: string ) : void {
131
141
if ( ! this . enabled ) {
132
142
this . logger . log ( LogLevel . DEBUG , LOG_MESSAGES . ODP_IDENTIFY_FAILED_ODP_DISABLED ) ;
133
143
return ;
@@ -155,7 +165,13 @@ export abstract class OdpManager {
155
165
* Sends an event to the ODP Server via the ODP Events API
156
166
* @param {OdpEvent } > ODP Event to send to event manager
157
167
*/
158
- public sendEvent ( { type, action, identifiers, data } : OdpEvent ) : void {
168
+ sendEvent ( { type, action, identifiers, data } : OdpEvent ) : void {
169
+ let mType = type ;
170
+
171
+ if ( typeof mType !== 'string' || mType === '' ) {
172
+ mType = 'fullstack' ;
173
+ }
174
+
159
175
if ( ! this . enabled ) {
160
176
throw new Error ( ERROR_MESSAGES . ODP_NOT_ENABLED ) ;
161
177
}
@@ -172,10 +188,14 @@ export abstract class OdpManager {
172
188
throw new Error ( ERROR_MESSAGES . ODP_SEND_EVENT_FAILED_EVENT_MANAGER_MISSING ) ;
173
189
}
174
190
175
- this . eventManager . sendEvent ( new OdpEvent ( type , action , identifiers , data ) ) ;
191
+ if ( typeof action !== 'string' || action === '' ) {
192
+ throw new Error ( 'ODP action is not valid (cannot be empty).' ) ;
193
+ }
194
+
195
+ this . eventManager . sendEvent ( new OdpEvent ( mType , action , identifiers , data ) ) ;
176
196
}
177
197
178
- public abstract isVuidEnabled ( ) : boolean ;
198
+ abstract isVuidEnabled ( ) : boolean ;
179
199
180
- public abstract getVuid ( ) : string | undefined ;
200
+ abstract getVuid ( ) : string | undefined ;
181
201
}
0 commit comments