@@ -21,16 +21,29 @@ import { ERROR_MESSAGES, ODP_USER_KEY } from '../../utils/enums';
2121import { VuidManager } from '../../plugins/vuid_manager' ;
2222
2323import { 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' ;
2626import { OptimizelySegmentOption } from './optimizely_segment_option' ;
2727import { invalidOdpDataFound } from './odp_utils' ;
2828import { OdpEvent } from './odp_event' ;
2929
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+
3043/**
3144 * Orchestrates segments manager, event manager, and ODP configuration
3245 */
33- export abstract class OdpManager {
46+ export abstract class OdpManager implements IOdpManager {
3447 initPromise ?: Promise < void > ;
3548 enabled = true ;
3649 logger : LogHandler = getLogger ( ) ;
@@ -40,20 +53,20 @@ export abstract class OdpManager {
4053 * ODP Segment Manager which provides an interface to the remote ODP server (GraphQL API) for audience segments mapping.
4154 * It fetches all qualified segments for the given user context and manages the segments cache for all user contexts.
4255 */
43- public segmentManager : OdpSegmentManager | undefined ;
56+ segmentManager : IOdpSegmentManager | undefined ;
4457
4558 /**
4659 * ODP Event Manager which provides an interface to the remote ODP server (REST API) for events.
4760 * It will queue all pending events (persistent) and send them (in batches of up to 10 events) to the ODP server when possible.
4861 */
49- public eventManager : OdpEventManager | undefined ;
62+ eventManager : IOdpEventManager | undefined ;
5063
5164 constructor ( ) { }
5265
5366 /**
5467 * Provides a method to update ODP Manager's ODP Config API Key, API Host, and Audience Segments
5568 */
56- public updateSettings ( { apiKey, apiHost, segmentsToCheck } : OdpConfig ) : boolean {
69+ updateSettings ( { apiKey, apiHost, segmentsToCheck } : OdpConfig ) : boolean {
5770 if ( ! this . enabled ) {
5871 return false ;
5972 }
@@ -85,7 +98,7 @@ export abstract class OdpManager {
8598 /**
8699 * Attempts to stop the current instance of ODP Manager's event manager, if it exists and is running.
87100 */
88- public close ( ) : void {
101+ close ( ) : void {
89102 if ( ! this . enabled ) {
90103 return ;
91104 }
@@ -100,10 +113,7 @@ export abstract class OdpManager {
100113 * @param {Array<OptimizelySegmentOption> } options - An array of OptimizelySegmentOption used to ignore and/or reset the cache.
101114 * @returns {Promise<string[] | null> } A promise holding either a list of qualified segments or null.
102115 */
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 > {
107117 if ( ! this . enabled ) {
108118 this . logger . log ( LogLevel . ERROR , ERROR_MESSAGES . ODP_NOT_ENABLED ) ;
109119 return null ;
@@ -127,7 +137,7 @@ export abstract class OdpManager {
127137 * @param {string } vuid (Optional) Secondary unique identifier of a target user, primarily used by client SDKs.
128138 * @returns
129139 */
130- public identifyUser ( userId ?: string , vuid ?: string ) : void {
140+ identifyUser ( userId ?: string , vuid ?: string ) : void {
131141 if ( ! this . enabled ) {
132142 this . logger . log ( LogLevel . DEBUG , LOG_MESSAGES . ODP_IDENTIFY_FAILED_ODP_DISABLED ) ;
133143 return ;
@@ -155,7 +165,13 @@ export abstract class OdpManager {
155165 * Sends an event to the ODP Server via the ODP Events API
156166 * @param {OdpEvent } > ODP Event to send to event manager
157167 */
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+
159175 if ( ! this . enabled ) {
160176 throw new Error ( ERROR_MESSAGES . ODP_NOT_ENABLED ) ;
161177 }
@@ -172,10 +188,14 @@ export abstract class OdpManager {
172188 throw new Error ( ERROR_MESSAGES . ODP_SEND_EVENT_FAILED_EVENT_MANAGER_MISSING ) ;
173189 }
174190
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 ) ) ;
176196 }
177197
178- public abstract isVuidEnabled ( ) : boolean ;
198+ abstract isVuidEnabled ( ) : boolean ;
179199
180- public abstract getVuid ( ) : string | undefined ;
200+ abstract getVuid ( ) : string | undefined ;
181201}
0 commit comments