Skip to content

Commit 6c62080

Browse files
ozayr-zaviaruzair-folio3
andauthored
refactor: converted entry level modules to ts (#590)
Summary: Converted packages\optimizely-sdk\lib\index.node.js to TS Converted packages\optimizely-sdk\lib\index.browser.js to TS Converted packages\optimizely-sdk\lib\index.react_native.js to TS Test plan: All existing unit tests should pass All existing test cases on FSC should pass Co-authored-by: uzair-folio3 <[email protected]>
1 parent 0f83ea8 commit 6c62080

File tree

7 files changed

+161
-163
lines changed

7 files changed

+161
-163
lines changed

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

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,42 @@
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,
22-
LogLevel,
22+
LogLevel
2323
} from '@optimizely/js-sdk-logging';
2424
import { LocalStoragePendingEventsDispatcher } from '@optimizely/js-sdk-event-processor';
25-
26-
import fns from './utils/fns';
27-
import * as configValidator from './utils/config_validator';
25+
import configValidator from './utils/config_validator';
2826
import defaultErrorHandler from './plugins/error_handler';
2927
import defaultEventDispatcher from './plugins/event_dispatcher/index.browser';
3028
import * as enums from './utils/enums';
3129
import loggerPlugin from './plugins/logger';
3230
import Optimizely from './optimizely';
3331
import eventProcessorConfigValidator from './utils/event_processor_config_validator';
32+
import { SDKOptions } from './shared_types';
3433

35-
var logger = getLogger();
34+
const logger = getLogger();
3635
setLogHandler(loggerPlugin.createLogger());
3736
setLogLevel(LogLevel.INFO);
3837

39-
var MODULE_NAME = 'INDEX_BROWSER';
40-
var DEFAULT_EVENT_BATCH_SIZE = 10;
41-
var DEFAULT_EVENT_FLUSH_INTERVAL = 1000; // Unit is ms, default is 1s
38+
const MODULE_NAME = 'INDEX_BROWSER';
39+
const DEFAULT_EVENT_BATCH_SIZE = 10;
40+
const DEFAULT_EVENT_FLUSH_INTERVAL = 1000; // Unit is ms, default is 1s
4241

43-
var hasRetriedEvents = false;
42+
let hasRetriedEvents = false;
4443

4544
/**
4645
* Creates an instance of the Optimizely class
47-
* @param {Object} config
48-
* @param {Object|string} config.datafile
49-
* @param {Object} config.errorHandler
50-
* @param {Object} config.eventDispatcher
51-
* @param {Object} config.logger
52-
* @param {Object} config.logLevel
53-
* @param {Object} config.userProfileService
54-
* @param {Object} config.eventBatchSize
55-
* @param {Object} config.eventFlushInterval
56-
* @param {string} config.sdkKey
57-
* @return {Object} the Optimizely object
46+
* @param {SDKOptions} config
47+
* @return {Optimizely|null} the Optimizely object
48+
* null on error
5849
*/
59-
var createInstance = function(config) {
50+
const createInstance = function(config: SDKOptions): Optimizely | null {
6051
try {
61-
config = config || {};
6252

6353
// TODO warn about setting per instance errorHandler / logger / logLevel
6454
if (config.errorHandler) {
@@ -81,7 +71,7 @@ var createInstance = function(config) {
8171
config.isValidInstance = false;
8272
}
8373

84-
var eventDispatcher;
74+
let eventDispatcher;
8575
// prettier-ignore
8676
if (config.eventDispatcher == null) { // eslint-disable-line eqeqeq
8777
// only wrap the event dispatcher with pending events retry if the user didnt override
@@ -97,42 +87,40 @@ var createInstance = function(config) {
9787
eventDispatcher = config.eventDispatcher;
9888
}
9989

100-
config = fns.assign(
101-
{
102-
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
103-
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
104-
eventFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
105-
},
106-
config,
107-
{
108-
eventDispatcher: eventDispatcher,
109-
// always get the OptimizelyLogger facade from logging
110-
logger: logger,
111-
errorHandler: getErrorHandler(),
112-
}
113-
);
90+
let eventBatchSize = config.eventBatchSize;
91+
let eventFlushInterval = config.eventFlushInterval;
11492

11593
if (!eventProcessorConfigValidator.validateEventBatchSize(config.eventBatchSize)) {
11694
logger.warn('Invalid eventBatchSize %s, defaulting to %s', config.eventBatchSize, DEFAULT_EVENT_BATCH_SIZE);
117-
config.eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
95+
eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
11896
}
11997
if (!eventProcessorConfigValidator.validateEventFlushInterval(config.eventFlushInterval)) {
12098
logger.warn(
12199
'Invalid eventFlushInterval %s, defaulting to %s',
122100
config.eventFlushInterval,
123101
DEFAULT_EVENT_FLUSH_INTERVAL
124102
);
125-
config.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
103+
eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
126104
}
127105

128-
var optimizely = new Optimizely(config);
106+
const optimizelyOptions = {
107+
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE,
108+
eventDispatcher: eventDispatcher,
109+
...config,
110+
eventBatchSize: eventBatchSize,
111+
eventFlushInterval: eventFlushInterval,
112+
logger: logger,
113+
errorHandler: getErrorHandler()
114+
};
115+
116+
const optimizely = new Optimizely(optimizelyOptions);
129117

130118
try {
131119
if (typeof window.addEventListener === 'function') {
132-
var unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload';
120+
const unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload';
133121
window.addEventListener(
134122
unloadEvent,
135-
function() {
123+
() => {
136124
optimizely.close();
137125
},
138126
false
@@ -149,7 +137,7 @@ var createInstance = function(config) {
149137
}
150138
};
151139

152-
var __internalResetRetryState = function() {
140+
const __internalResetRetryState = function(): void {
153141
hasRetriedEvents = false;
154142
};
155143

@@ -164,16 +152,16 @@ export {
164152
setLogHandler as setLogger,
165153
setLogLevel,
166154
createInstance,
167-
__internalResetRetryState,
168-
}
155+
__internalResetRetryState,
156+
};
169157

170158
export default {
171159
logging: loggerPlugin,
172160
errorHandler: defaultErrorHandler,
173161
eventDispatcher: defaultEventDispatcher,
174-
enums: enums,
162+
enums,
175163
setLogger: setLogHandler,
176-
setLogLevel: setLogLevel,
177-
createInstance: createInstance,
178-
__internalResetRetryState: __internalResetRetryState,
164+
setLogLevel,
165+
createInstance,
166+
__internalResetRetryState,
179167
};

packages/optimizely-sdk/lib/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ declare module '@optimizely/optimizely-sdk' {
5151

5252
export type DatafileOptions = import ('./shared_types').DatafileOptions;
5353

54+
export type SDKOptions = import ('./shared_types').SDKOptions;
55+
56+
export type OptimizelyOptions = import ('./optimizely').OptimizelyOptions;
57+
5458
export type UserProfileService = import('./shared_types').UserProfileService;
5559

5660
export type UserProfile = import('./shared_types').UserProfile;
@@ -195,6 +199,7 @@ declare module '@optimizely/optimizely-sdk/lib/utils/enums' {
195199
DECISION = 'DECISION:type, userId, attributes, decisionInfo',
196200
OPTIMIZELY_CONFIG_UPDATE = 'OPTIMIZELY_CONFIG_UPDATE',
197201
TRACK = 'TRACK:event_key, user_id, attributes, event_tags, event',
202+
LOG_EVENT = "LOG_EVENT:logEvent"
198203
}
199204
}
200205

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

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,38 @@
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,
22-
LogLevel,
22+
LogLevel
2323
} from '@optimizely/js-sdk-logging';
24-
25-
import fns from './utils/fns';
2624
import Optimizely from './optimizely';
2725
import * as enums from './utils/enums';
2826
import loggerPlugin from './plugins/logger';
2927
import configValidator from './utils/config_validator';
3028
import defaultErrorHandler from './plugins/error_handler';
3129
import defaultEventDispatcher from './plugins/event_dispatcher/index.node';
3230
import eventProcessorConfigValidator from './utils/event_processor_config_validator';
31+
import { SDKOptions } from './shared_types';
3332

34-
var logger = getLogger();
33+
const logger = getLogger();
3534
setLogLevel(LogLevel.ERROR);
3635

37-
var DEFAULT_EVENT_BATCH_SIZE = 10;
38-
var DEFAULT_EVENT_FLUSH_INTERVAL = 30000; // Unit is ms, default is 30s
36+
const DEFAULT_EVENT_BATCH_SIZE = 10;
37+
const DEFAULT_EVENT_FLUSH_INTERVAL = 30000; // Unit is ms, default is 30s
3938

4039
/**
4140
* 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
41+
* @param {SDKOptions} config
42+
* @return {Optimizely|null} the Optimizely object
43+
* null on error
5344
*/
54-
var createInstance = function(config) {
45+
const createInstance = function(config: SDKOptions): Optimizely | null {
5546
try {
56-
var hasLogger = false;
57-
config = config || {};
47+
let hasLogger = false;
5848

5949
// TODO warn about setting per instance errorHandler / logger / logLevel
6050
if (config.errorHandler) {
@@ -70,7 +60,6 @@ var createInstance = function(config) {
7060
if (config.logLevel !== undefined) {
7161
setLogLevel(config.logLevel);
7262
}
73-
7463
try {
7564
configValidator.validate(config);
7665
config.isValidInstance = true;
@@ -83,35 +72,33 @@ var createInstance = function(config) {
8372
config.isValidInstance = false;
8473
}
8574

86-
config = fns.assign(
87-
{
88-
clientEngine: enums.NODE_CLIENT_ENGINE,
89-
eventBatchSize: DEFAULT_EVENT_BATCH_SIZE,
90-
eventDispatcher: defaultEventDispatcher,
91-
eventFlushInterval: DEFAULT_EVENT_FLUSH_INTERVAL,
92-
},
93-
config,
94-
{
95-
// always get the OptimizelyLogger facade from logging
96-
logger: logger,
97-
errorHandler: getErrorHandler(),
98-
}
99-
);
75+
let eventBatchSize = config.eventBatchSize;
76+
let eventFlushInterval = config.eventFlushInterval;
10077

10178
if (!eventProcessorConfigValidator.validateEventBatchSize(config.eventBatchSize)) {
10279
logger.warn('Invalid eventBatchSize %s, defaulting to %s', config.eventBatchSize, DEFAULT_EVENT_BATCH_SIZE);
103-
config.eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
80+
eventBatchSize = DEFAULT_EVENT_BATCH_SIZE;
10481
}
10582
if (!eventProcessorConfigValidator.validateEventFlushInterval(config.eventFlushInterval)) {
10683
logger.warn(
10784
'Invalid eventFlushInterval %s, defaulting to %s',
10885
config.eventFlushInterval,
10986
DEFAULT_EVENT_FLUSH_INTERVAL
11087
);
111-
config.eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
88+
eventFlushInterval = DEFAULT_EVENT_FLUSH_INTERVAL;
11289
}
11390

114-
return new Optimizely(config);
91+
const optimizelyOptions = {
92+
clientEngine: enums.NODE_CLIENT_ENGINE,
93+
eventDispatcher: defaultEventDispatcher,
94+
...config,
95+
eventBatchSize: eventBatchSize,
96+
eventFlushInterval: eventFlushInterval,
97+
logger: logger,
98+
errorHandler: getErrorHandler()
99+
};
100+
101+
return new Optimizely(optimizelyOptions);
115102
} catch (e) {
116103
logger.error(e);
117104
return null;
@@ -129,14 +116,14 @@ export {
129116
setLogHandler as setLogger,
130117
setLogLevel,
131118
createInstance,
132-
}
119+
};
133120

134121
export default {
135122
logging: loggerPlugin,
136123
errorHandler: defaultErrorHandler,
137124
eventDispatcher: defaultEventDispatcher,
138-
enums: enums,
125+
enums,
139126
setLogger: setLogHandler,
140-
setLogLevel: setLogLevel,
141-
createInstance: createInstance,
127+
setLogLevel,
128+
createInstance,
142129
};

0 commit comments

Comments
 (0)