Skip to content

refactor: Convert lib/optimizely/* and lib/tests/* to ES module #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2017-2019, Optimizely, Inc. and contributors *
* Copyright 2017-2020, Optimizely, Inc. and contributors *
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these changes in decision_service/index.tests.js included in this PR by mistake?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file references Optimizely which is converted to es module in this PR. We needed to add .default to the import in this file hence this change

* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand All @@ -14,7 +14,7 @@
* limitations under the License. *
***************************************************************************/

var Optimizely = require('../../optimizely');
var Optimizely = require('../../optimizely').default;
var eventBuilder = require('../../core/event_builder/index.js');
var eventDispatcher = require('../../plugins/event_dispatcher/index.node');
var errorHandler = require('../../plugins/error_handler');
Expand Down
90 changes: 45 additions & 45 deletions packages/optimizely-sdk/lib/optimizely/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016-2019, Optimizely, Inc. and contributors *
* Copyright 2016-2020, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand All @@ -13,21 +13,21 @@
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

var fns = require('../utils/fns');
var attributesValidator = require('../utils/attributes_validator');
var decisionService = require('../core/decision_service');
var enums = require('../utils/enums');
var eventBuilder = require('../core/event_builder/index.js');
var eventHelpers = require('../core/event_builder/event_helpers');
var eventProcessor = require('@optimizely/js-sdk-event-processor');
var eventTagsValidator = require('../utils/event_tags_validator');
var notificationCenter = require('../core/notification_center');
var projectConfig = require('../core/project_config');
var jsSdkUtils = require('@optimizely/js-sdk-utils');
var userProfileServiceValidator = require('../utils/user_profile_service_validator');
var stringValidator = require('../utils/string_value_validator');
var projectConfigManager = require('../core/project_config/project_config_manager');
import { sprintf, objectValues } from '@optimizely/js-sdk-utils';
import * as eventProcessor from '@optimizely/js-sdk-event-processor';

import fns from '../utils/fns'
import attributesValidator from '../utils/attributes_validator';
import decisionService from '../core/decision_service';
import enums from '../utils/enums';
import eventBuilder from '../core/event_builder/index.js';
import eventHelpers from '../core/event_builder/event_helpers';
import eventTagsValidator from '../utils/event_tags_validator';
import notificationCenter from '../core/notification_center';
import projectConfig from '../core/project_config';
import userProfileServiceValidator from '../utils/user_profile_service_validator';
import stringValidator from '../utils/string_value_validator';
import projectConfigManager from '../core/project_config/project_config_manager';

var ERROR_MESSAGES = enums.ERROR_MESSAGES;
var LOG_LEVEL = enums.LOG_LEVEL;
Expand Down Expand Up @@ -58,7 +58,7 @@ function Optimizely(config) {
if (enums.VALID_CLIENT_ENGINES.indexOf(clientEngine) === -1) {
config.logger.log(
LOG_LEVEL.INFO,
jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_CLIENT_ENGINE, MODULE_NAME, clientEngine)
sprintf(LOG_MESSAGES.INVALID_CLIENT_ENGINE, MODULE_NAME, clientEngine)
);
clientEngine = enums.NODE_CLIENT_ENGINE;
}
Expand All @@ -81,7 +81,7 @@ function Optimizely(config) {
function(configObj) {
this.logger.log(
LOG_LEVEL.INFO,
jsSdkUtils.sprintf(LOG_MESSAGES.UPDATED_OPTIMIZELY_CONFIG, MODULE_NAME, configObj.revision, configObj.projectId)
sprintf(LOG_MESSAGES.UPDATED_OPTIMIZELY_CONFIG, MODULE_NAME, configObj.revision, configObj.projectId)
);
this.notificationCenter.sendNotifications(NOTIFICATION_TYPES.OPTIMIZELY_CONFIG_UPDATE);
}.bind(this)
Expand All @@ -94,7 +94,7 @@ function Optimizely(config) {
try {
if (userProfileServiceValidator.validate(config.userProfileService)) {
userProfileService = config.userProfileService;
this.logger.log(LOG_LEVEL.INFO, jsSdkUtils.sprintf(LOG_MESSAGES.VALID_USER_PROFILE_SERVICE, MODULE_NAME));
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VALID_USER_PROFILE_SERVICE, MODULE_NAME));
}
} catch (ex) {
this.logger.log(LOG_LEVEL.WARNING, ex.message);
Expand Down Expand Up @@ -144,7 +144,7 @@ Optimizely.prototype.__isValidInstance = function() {
Optimizely.prototype.activate = function(experimentKey, userId, attributes) {
try {
if (!this.__isValidInstance()) {
this.logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'activate'));
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'activate'));
return null;
}

Expand All @@ -165,7 +165,7 @@ Optimizely.prototype.activate = function(experimentKey, userId, attributes) {

// If experiment is not set to 'Running' status, log accordingly and return variation key
if (!projectConfig.isRunning(configObj, experimentKey)) {
var shouldNotDispatchActivateLogMessage = jsSdkUtils.sprintf(
var shouldNotDispatchActivateLogMessage = sprintf(
LOG_MESSAGES.SHOULD_NOT_DISPATCH_ACTIVATE,
MODULE_NAME,
experimentKey
Expand All @@ -179,7 +179,7 @@ Optimizely.prototype.activate = function(experimentKey, userId, attributes) {
return variationKey;
} catch (ex) {
this.logger.log(LOG_LEVEL.ERROR, ex.message);
var failedActivationLogMessage = jsSdkUtils.sprintf(
var failedActivationLogMessage = sprintf(
LOG_MESSAGES.NOT_ACTIVATING_USER,
MODULE_NAME,
userId,
Expand Down Expand Up @@ -275,7 +275,7 @@ Optimizely.prototype.__emitNotificationCenterActivate = function(experimentKey,
Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
try {
if (!this.__isValidInstance()) {
this.logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'track'));
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'track'));
return;
}

Expand All @@ -291,9 +291,9 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
if (!projectConfig.eventWithKeyExists(configObj, eventKey)) {
this.logger.log(
LOG_LEVEL.WARNING,
jsSdkUtils.sprintf(enums.LOG_MESSAGES.EVENT_KEY_NOT_FOUND, MODULE_NAME, eventKey)
sprintf(enums.LOG_MESSAGES.EVENT_KEY_NOT_FOUND, MODULE_NAME, eventKey)
);
this.logger.log(LOG_LEVEL.WARNING, jsSdkUtils.sprintf(LOG_MESSAGES.NOT_TRACKING_USER, MODULE_NAME, userId));
this.logger.log(LOG_LEVEL.WARNING, sprintf(LOG_MESSAGES.NOT_TRACKING_USER, MODULE_NAME, userId));
return;
}

Expand All @@ -308,14 +308,14 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
clientVersion: this.clientVersion,
configObj: configObj,
});
this.logger.log(LOG_LEVEL.INFO, jsSdkUtils.sprintf(enums.LOG_MESSAGES.TRACK_EVENT, MODULE_NAME, eventKey, userId));
this.logger.log(LOG_LEVEL.INFO, sprintf(enums.LOG_MESSAGES.TRACK_EVENT, MODULE_NAME, eventKey, userId));
// TODO is it okay to not pass a projectConfig as second argument
this.eventProcessor.process(conversionEvent);
this.__emitNotificationCenterTrack(eventKey, userId, attributes, eventTags);
} catch (e) {
this.logger.log(LOG_LEVEL.ERROR, e.message);
this.errorHandler.handleError(e);
var failedTrackLogMessage = jsSdkUtils.sprintf(LOG_MESSAGES.NOT_TRACKING_USER, MODULE_NAME, userId);
var failedTrackLogMessage = sprintf(LOG_MESSAGES.NOT_TRACKING_USER, MODULE_NAME, userId);
this.logger.log(LOG_LEVEL.ERROR, failedTrackLogMessage);
}
};
Expand Down Expand Up @@ -369,7 +369,7 @@ Optimizely.prototype.__emitNotificationCenterTrack = function(eventKey, userId,
Optimizely.prototype.getVariation = function(experimentKey, userId, attributes) {
try {
if (!this.__isValidInstance()) {
this.logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getVariation'));
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getVariation'));
return null;
}

Expand All @@ -387,7 +387,7 @@ Optimizely.prototype.getVariation = function(experimentKey, userId, attributes)
if (!experiment) {
this.logger.log(
LOG_LEVEL.DEBUG,
jsSdkUtils.sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey)
sprintf(ERROR_MESSAGES.INVALID_EXPERIMENT_KEY, MODULE_NAME, experimentKey)
);
return null;
}
Expand Down Expand Up @@ -485,7 +485,7 @@ Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, e
if (stringInputs.hasOwnProperty('user_id')) {
var userId = stringInputs.user_id;
if (typeof userId !== 'string' || userId === null || userId === 'undefined') {
throw new Error(jsSdkUtils.sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, 'user_id'));
throw new Error(sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, 'user_id'));
}

delete stringInputs.user_id;
Expand All @@ -495,7 +495,7 @@ Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, e
for (var index = 0; index < inputKeys.length; index++) {
var key = inputKeys[index];
if (!stringValidator.validate(stringInputs[key])) {
throw new Error(jsSdkUtils.sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, key));
throw new Error(sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, key));
}
}
if (userAttributes) {
Expand All @@ -519,7 +519,7 @@ Optimizely.prototype.__validateInputs = function(stringInputs, userAttributes, e
* @return {null}
*/
Optimizely.prototype.__notActivatingExperiment = function(experimentKey, userId) {
var failedActivationLogMessage = jsSdkUtils.sprintf(
var failedActivationLogMessage = sprintf(
LOG_MESSAGES.NOT_ACTIVATING_USER,
MODULE_NAME,
userId,
Expand Down Expand Up @@ -555,7 +555,7 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes)
if (!this.__isValidInstance()) {
this.logger.log(
LOG_LEVEL.ERROR,
jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'isFeatureEnabled')
sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'isFeatureEnabled')
);
return false;
}
Expand Down Expand Up @@ -594,12 +594,12 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes)
if (featureEnabled === true) {
this.logger.log(
LOG_LEVEL.INFO,
jsSdkUtils.sprintf(LOG_MESSAGES.FEATURE_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)
sprintf(LOG_MESSAGES.FEATURE_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)
);
} else {
this.logger.log(
LOG_LEVEL.INFO,
jsSdkUtils.sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)
sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)
);
featureEnabled = false;
}
Expand Down Expand Up @@ -639,7 +639,7 @@ Optimizely.prototype.getEnabledFeatures = function(userId, attributes) {
if (!this.__isValidInstance()) {
this.logger.log(
LOG_LEVEL.ERROR,
jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getEnabledFeatures')
sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, 'getEnabledFeatures')
);
return enabledFeatures;
}
Expand All @@ -653,7 +653,7 @@ Optimizely.prototype.getEnabledFeatures = function(userId, attributes) {
return enabledFeatures;
}

jsSdkUtils.objectValues(configObj.featureKeyMap).forEach(
objectValues(configObj.featureKeyMap).forEach(
function(feature) {
if (this.isFeatureEnabled(feature.key, userId, attributes)) {
enabledFeatures.push(feature.key);
Expand Down Expand Up @@ -721,7 +721,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
var apiName = variableType
? 'getFeatureVariable' + variableType.charAt(0).toUpperCase() + variableType.slice(1)
: 'getFeatureVariable';
this.logger.log(LOG_LEVEL.ERROR, jsSdkUtils.sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, apiName));
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, apiName));
return null;
}

Expand Down Expand Up @@ -749,7 +749,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
} else if (variable.type !== variableType) {
this.logger.log(
LOG_LEVEL.WARNING,
jsSdkUtils.sprintf(LOG_MESSAGES.VARIABLE_REQUESTED_WITH_WRONG_TYPE, MODULE_NAME, variableType, variable.type)
sprintf(LOG_MESSAGES.VARIABLE_REQUESTED_WITH_WRONG_TYPE, MODULE_NAME, variableType, variable.type)
);
return null;
}
Expand All @@ -766,7 +766,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
variableValue = value;
this.logger.log(
LOG_LEVEL.INFO,
jsSdkUtils.sprintf(
sprintf(
LOG_MESSAGES.USER_RECEIVED_VARIABLE_VALUE,
MODULE_NAME,
variableKey,
Expand All @@ -778,7 +778,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
} else {
this.logger.log(
LOG_LEVEL.INFO,
jsSdkUtils.sprintf(
sprintf(
LOG_MESSAGES.FEATURE_NOT_ENABLED_RETURN_DEFAULT_VARIABLE_VALUE,
MODULE_NAME,
featureFlag.key,
Expand All @@ -790,7 +790,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
} else {
this.logger.log(
LOG_LEVEL.INFO,
jsSdkUtils.sprintf(
sprintf(
LOG_MESSAGES.VARIABLE_NOT_USED_RETURN_DEFAULT_VARIABLE_VALUE,
MODULE_NAME,
variableKey,
Expand All @@ -801,7 +801,7 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
} else {
this.logger.log(
LOG_LEVEL.INFO,
jsSdkUtils.sprintf(
sprintf(
LOG_MESSAGES.USER_RECEIVED_DEFAULT_VARIABLE_VALUE,
MODULE_NAME,
userId,
Expand Down Expand Up @@ -1103,7 +1103,7 @@ Optimizely.prototype.onReady = function(options) {
delete this.__readyTimeouts[timeoutId];
resolveTimeoutPromise({
success: false,
reason: jsSdkUtils.sprintf('onReady timeout expired after %s ms', timeout),
reason: sprintf('onReady timeout expired after %s ms', timeout),
});
}.bind(this);
var readyTimeout = setTimeout(onReadyTimeout, timeout);
Expand Down Expand Up @@ -1132,4 +1132,4 @@ Optimizely.prototype.onReady = function(options) {
return Promise.race([this.__readyPromise, timeoutPromise]);
};

module.exports = Optimizely;
export default Optimizely;
51 changes: 24 additions & 27 deletions packages/optimizely-sdk/lib/optimizely/index.tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016-2019, Optimizely, Inc. and contributors *
* Copyright 2016-2020, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand All @@ -13,31 +13,28 @@
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/

var Optimizely = require('./');
var AudienceEvaluator = require('../core/audience_evaluator');
var bluebird = require('bluebird');
var bucketer = require('../core/bucketer');
var projectConfigManager = require('../core/project_config/project_config_manager');
var enums = require('../utils/enums');
var eventBuilder = require('../core/event_builder/index.js');
var eventDispatcher = require('../plugins/event_dispatcher/index.node');
var eventProcessor = require('@optimizely/js-sdk-event-processor');
var errorHandler = require('../plugins/error_handler');
var fns = require('../utils/fns');
var jsonSchemaValidator = require('../utils/json_schema_validator');
var logger = require('../plugins/logger');
var decisionService = require('../core/decision_service');
var testData = require('../tests/test_data');
var jsonSchemaValidator = require('../utils/json_schema_validator');
var projectConfig = require('../core/project_config');
var logging = require('@optimizely/js-sdk-logging');

var chai = require('chai');
var assert = chai.assert;
var sinon = require('sinon');
var sprintf = require('@optimizely/js-sdk-utils').sprintf;
var uuid = require('uuid');
import { assert } from 'chai';
import sinon from 'sinon';
import uuid from 'uuid';
import { sprintf } from '@optimizely/js-sdk-utils';
import * as eventProcessor from '@optimizely/js-sdk-event-processor';
import * as logging from '@optimizely/js-sdk-logging';

import Optimizely from './';
import AudienceEvaluator from '../core/audience_evaluator';
import bluebird from 'bluebird';
import bucketer from '../core/bucketer';
import projectConfigManager from '../core/project_config/project_config_manager';
import enums from '../utils/enums';
import eventBuilder from '../core/event_builder/index.js';
import eventDispatcher from '../plugins/event_dispatcher/index.node';
import errorHandler from '../plugins/error_handler';
import fns from '../utils/fns';
import logger from '../plugins/logger';
import decisionService from '../core/decision_service';
import jsonSchemaValidator from '../utils/json_schema_validator';
import projectConfig from '../core/project_config';
import testData from '../tests/test_data';

var ERROR_MESSAGES = enums.ERROR_MESSAGES;
var LOG_LEVEL = enums.LOG_LEVEL;
Expand Down Expand Up @@ -6650,7 +6647,7 @@ describe('lib/optimizely', function() {
});

it('should instantiate the eventProcessor with the provided event flush interval and event batch size', function() {
optlyInstance = new Optimizely({
var optlyInstance = new Optimizely({
clientEngine: 'node-sdk',
errorHandler: errorHandler,
eventDispatcher: eventDispatcher,
Expand Down
Loading