Skip to content

Commit 395f635

Browse files
committed
applying original change to the rebased branch
1 parent 145838b commit 395f635

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

packages/optimizely-sdk/lib/index.node.js renamed to packages/optimizely-sdk/lib/index.node.ts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
* See the License for the specific language governing permissions and *
1414
* limitations under the License. *
1515
***************************************************************************/
16-
import {
16+
import {
1717
getLogger,
1818
setLogHandler,
1919
setLogLevel,
2020
setErrorHandler,
2121
getErrorHandler,
2222
LogLevel,
23+
ErrorHandler,
24+
LogHandler,
2325
} from '@optimizely/js-sdk-logging';
2426

2527
import { assign } from './utils/fns';
@@ -30,30 +32,35 @@ import configValidator from './utils/config_validator';
3032
import defaultErrorHandler from './plugins/error_handler';
3133
import defaultEventDispatcher from './plugins/event_dispatcher/index.node';
3234
import eventProcessorConfigValidator from './utils/event_processor_config_validator';
35+
import * as projectConfig from './core/project_config';
3336

34-
var logger = getLogger();
37+
const logger = getLogger();
3538
setLogLevel(LogLevel.ERROR);
3639

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+
}
3955

4056
/**
4157
* 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
5360
*/
54-
var createInstance = function(config) {
61+
const createInstance = function (config: Config): Optimizely | null {
5562
try {
56-
var hasLogger = false;
63+
let hasLogger = false;
5764
config = config || {};
5865

5966
// TODO warn about setting per instance errorHandler / logger / logLevel
@@ -70,7 +77,6 @@ var createInstance = function(config) {
7077
if (config.logLevel !== undefined) {
7178
setLogLevel(config.logLevel);
7279
}
73-
7480
try {
7581
configValidator.validate(config);
7682
config.isValidInstance = true;
@@ -83,7 +89,7 @@ var createInstance = function(config) {
8389
config.isValidInstance = false;
8490
}
8591

86-
config = assign(
92+
const optimizelyConfig = assign(
8793
{
8894
clientEngine: enums.NODE_CLIENT_ENGINE,
8995
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
@@ -96,22 +102,22 @@ var createInstance = function(config) {
96102
logger: logger,
97103
errorHandler: getErrorHandler(),
98104
}
99-
);
105+
) as projectConfig.ProjectConfig;
100106

101107
if (!eventProcessorConfigValidator.validateEventBatchSize(config.eventBatchSize)) {
102108
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;
104110
}
105111
if (!eventProcessorConfigValidator.validateEventFlushInterval(config.eventFlushInterval)) {
106112
logger.warn(
107113
'Invalid eventFlushInterval %s, defaulting to %s',
108114
config.eventFlushInterval,
109115
DEFAULT_EVENT_FLUSH_INTERVAL
110116
);
111-
config.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
117+
optimizelyConfig.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
112118
}
113119

114-
return new Optimizely(config);
120+
return new Optimizely(optimizelyConfig);
115121
} catch (e) {
116122
logger.error(e);
117123
return null;
@@ -129,7 +135,7 @@ export {
129135
setLogHandler as setLogger,
130136
setLogLevel,
131137
createInstance,
132-
}
138+
};
133139

134140
export default {
135141
logging: loggerPlugin,

0 commit comments

Comments
 (0)