13
13
* See the License for the specific language governing permissions and *
14
14
* limitations under the License. *
15
15
***************************************************************************/
16
- import {
16
+ import {
17
17
getLogger ,
18
18
setLogHandler ,
19
19
setLogLevel ,
20
20
setErrorHandler ,
21
21
getErrorHandler ,
22
22
LogLevel ,
23
+ ErrorHandler ,
24
+ LogHandler ,
23
25
} from '@optimizely/js-sdk-logging' ;
24
26
25
27
import { assign } from './utils/fns' ;
@@ -30,30 +32,35 @@ import configValidator from './utils/config_validator';
30
32
import defaultErrorHandler from './plugins/error_handler' ;
31
33
import defaultEventDispatcher from './plugins/event_dispatcher/index.node' ;
32
34
import eventProcessorConfigValidator from './utils/event_processor_config_validator' ;
35
+ import * as projectConfig from './core/project_config' ;
33
36
34
- var logger = getLogger ( ) ;
37
+ const logger = getLogger ( ) ;
35
38
setLogLevel ( LogLevel . ERROR ) ;
36
39
37
- var DEFAULT_EVENT_BATCH_SIZE = 10 ;
38
- var DEFAULT_EVENT_FLUSH_INTERVAL = 30000 ; // Unit is ms, default is 30s
40
+ const DEFAULT_EVENT_BATCH_SIZE = 10 ;
41
+ const DEFAULT_EVENT_FLUSH_INTERVAL = 30000 ; // Unit is ms, default is 30s
42
+
43
+ interface Config {
44
+ datafile ?: projectConfig . ProjectConfig ;
45
+ errorHandler ?: ErrorHandler ;
46
+ eventDispatcher ?: ( ...args : unknown [ ] ) => unknown ;
47
+ logger ?: LogHandler ;
48
+ logLevel ?: LogLevel ;
49
+ userProfileService ?: import ( './shared_types' ) . UserProfileService ;
50
+ eventBatchSize ?: number ;
51
+ eventFlushInterval ?: number ;
52
+ sdkKey ?: string ;
53
+ isValidInstance ?: boolean ;
54
+ }
39
55
40
56
/**
41
57
* Creates an instance of the Optimizely class
42
- * @param {Object } config
43
- * @param {Object|string } config.datafile
44
- * @param {Object } config.errorHandler
45
- * @param {Object } config.eventDispatcher
46
- * @param {Object } config.logger
47
- * @param {Object } config.logLevel
48
- * @param {Object } config.userProfileService
49
- * @param {Object } config.eventBatchSize
50
- * @param {Object } config.eventFlushInterval
51
- * @param {string } config.sdkKey
52
- * @return {Object } the Optimizely object
58
+ * @param {Config } config
59
+ * @return {Optimizely } the Optimizely object
53
60
*/
54
- var createInstance = function ( config ) {
61
+ const createInstance = function ( config : Config ) : Optimizely | null {
55
62
try {
56
- var hasLogger = false ;
63
+ let hasLogger = false ;
57
64
config = config || { } ;
58
65
59
66
// TODO warn about setting per instance errorHandler / logger / logLevel
@@ -70,7 +77,6 @@ var createInstance = function(config) {
70
77
if ( config . logLevel !== undefined ) {
71
78
setLogLevel ( config . logLevel ) ;
72
79
}
73
-
74
80
try {
75
81
configValidator . validate ( config ) ;
76
82
config . isValidInstance = true ;
@@ -83,7 +89,7 @@ var createInstance = function(config) {
83
89
config . isValidInstance = false ;
84
90
}
85
91
86
- config = assign (
92
+ const optimizelyConfig = assign (
87
93
{
88
94
clientEngine : enums . NODE_CLIENT_ENGINE ,
89
95
eventBatchSize : DEFAULT_EVENT_BATCH_SIZE ,
@@ -96,22 +102,22 @@ var createInstance = function(config) {
96
102
logger : logger ,
97
103
errorHandler : getErrorHandler ( ) ,
98
104
}
99
- ) ;
105
+ ) as projectConfig . ProjectConfig ;
100
106
101
107
if ( ! eventProcessorConfigValidator . validateEventBatchSize ( config . eventBatchSize ) ) {
102
108
logger . warn ( 'Invalid eventBatchSize %s, defaulting to %s' , config . eventBatchSize , DEFAULT_EVENT_BATCH_SIZE ) ;
103
- config . eventBatchSize = DEFAULT_EVENT_BATCH_SIZE ;
109
+ optimizelyConfig . eventBatchSize = DEFAULT_EVENT_BATCH_SIZE ;
104
110
}
105
111
if ( ! eventProcessorConfigValidator . validateEventFlushInterval ( config . eventFlushInterval ) ) {
106
112
logger . warn (
107
113
'Invalid eventFlushInterval %s, defaulting to %s' ,
108
114
config . eventFlushInterval ,
109
115
DEFAULT_EVENT_FLUSH_INTERVAL
110
116
) ;
111
- config . eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL ;
117
+ optimizelyConfig . eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL ;
112
118
}
113
119
114
- return new Optimizely ( config ) ;
120
+ return new Optimizely ( optimizelyConfig ) ;
115
121
} catch ( e ) {
116
122
logger . error ( e ) ;
117
123
return null ;
@@ -129,7 +135,7 @@ export {
129
135
setLogHandler as setLogger ,
130
136
setLogLevel ,
131
137
createInstance ,
132
- }
138
+ } ;
133
139
134
140
export default {
135
141
logging : loggerPlugin ,
0 commit comments