Skip to content

Commit 227ab98

Browse files
author
uzair-folio3
committed
Merge branch 'uzair/index_node_to_ts' into uzair/node-conversion
2 parents e08bfab + 18651ec commit 227ab98

File tree

2 files changed

+65
-22
lines changed

2 files changed

+65
-22
lines changed

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

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,54 @@
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';
26-
import Optimizely from './optimizely';
28+
import { Optimizely } from '@optimizely/optimizely-sdk/optimizely';
2729
import * as enums from './utils/enums';
2830
import loggerPlugin from './plugins/logger';
2931
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 { ProjectConfig } from '@optimizely/optimizely-sdk/lib/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+
export interface Config {
44+
datafile?: 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,
@@ -111,7 +117,7 @@ var createInstance = function(config) {
111117
config.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,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/****************************************************************************
2+
* Copyright 2020, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
declare module '@optimizely/optimizely-sdk/optimizely' {
17+
import { ErrorHandler, LogHandler, LogLevel } from '@optimizely/js-sdk-logging';
18+
import { ProjectConfig } from '@optimizely/optimizely-sdk/lib/core/project_config';
19+
interface OptimizelyConfig {
20+
datafile: ProjectConfig;
21+
errorHandler?: ErrorHandler;
22+
eventDispatcher: (...args: unknown[]) => unknown;
23+
logger?: LogHandler;
24+
logLevel?: LogLevel;
25+
userProfileService?: import('../shared_types').UserProfileService;
26+
eventBatchSize: number;
27+
eventFlushInterval: number;
28+
sdkKey: string;
29+
isValidInstance: boolean;
30+
clientEngine: string;
31+
clientVersion: string;
32+
}
33+
34+
export class Optimizely {
35+
constructor(config: OptimizelyConfig);
36+
}
37+
}

0 commit comments

Comments
 (0)