Skip to content

Commit 7b0a0ad

Browse files
refactor: Convert lib/core to ES module (Part 2/2) (#450)
Summary: This PR only converts contents inside lib/core to ESModule. lib/core/ event_builder notification_center, optimizely_config project_config Test plan: All unit test and Full stack compatibility suite tests are getting passed. Co-authored-by: zashraf1985 <35262377+zashraf1985@users.noreply.github.com> Co-authored-by: Zeeshan Ashraf <zashraf@folio3.com>
1 parent 422fc91 commit 7b0a0ad

File tree

15 files changed

+717
-693
lines changed

15 files changed

+717
-693
lines changed

packages/optimizely-sdk/lib/core/event_builder/event_helpers.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019, Optimizely
2+
* Copyright 2019-2020, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,14 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var logging = require('@optimizely/js-sdk-logging');
16+
import { getLogger } from '@optimizely/js-sdk-logging';
1717

18-
var attributesValidator = require('../../utils/attributes_validator');
19-
var fns = require('../../utils/fns');
20-
var eventTagUtils = require('../../utils/event_tag_utils');
21-
var projectConfig = require('../project_config');
18+
import fns from '../../utils/fns';
19+
import projectConfig from '../project_config';
20+
import eventTagUtils from '../../utils/event_tag_utils';
21+
import attributesValidator from'../../utils/attributes_validator';
2222

23-
var logger = logging.getLogger('EVENT_BUILDER');
23+
var logger = getLogger('EVENT_BUILDER');
2424

2525
/**
2626
* Creates an ImpressionEvent object from decision data
@@ -34,7 +34,7 @@ var logger = logging.getLogger('EVENT_BUILDER');
3434
* @param {String} config.clientVersion
3535
* @return {Object} an ImpressionEvent object
3636
*/
37-
exports.buildImpressionEvent = function buildImpressionEvent(config) {
37+
export var buildImpressionEvent = function(config) {
3838
var configObj = config.configObj;
3939
var experimentKey = config.experimentKey;
4040
var variationKey = config.variationKey;
@@ -95,7 +95,7 @@ exports.buildImpressionEvent = function buildImpressionEvent(config) {
9595
* @param {String} config.clientVersion
9696
* @return {Object} a ConversionEvent object
9797
*/
98-
exports.buildConversionEvent = function buildConversionEvent(config) {
98+
export var buildConversionEvent = function(config) {
9999
var configObj = config.configObj;
100100
var userId = config.userId;
101101
var userAttributes = config.userAttributes;

packages/optimizely-sdk/lib/core/event_builder/event_helpers.tests.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019, Optimizely
2+
* Copyright 2019-2020, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,13 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var projectConfig = require('../project_config');
17-
var eventHelpers = require('./event_helpers');
18-
var fns = require('../../utils/fns');
16+
import sinon from 'sinon';
17+
import { assert } from 'chai';
1918

20-
var chai = require('chai');
21-
var assert = chai.assert;
22-
var sinon = require('sinon');
19+
import fns from '../../utils/fns';
20+
import projectConfig from '../project_config';
21+
import { buildImpressionEvent, buildConversionEvent } from './event_helpers';
2322

2423
describe('lib/event_builder/event_helpers', function() {
2524
var configObj;
@@ -65,7 +64,7 @@ describe('lib/event_builder/event_helpers', function() {
6564

6665
projectConfig.getAttributeId.withArgs(configObj, 'plan_type').returns('plan_type_id');
6766

68-
var result = eventHelpers.buildImpressionEvent({
67+
var result = buildImpressionEvent({
6968
configObj: configObj,
7069
experimentKey: 'exp1',
7170
variationKey: 'var1',
@@ -131,7 +130,7 @@ describe('lib/event_builder/event_helpers', function() {
131130
delete configObj['anonymizeIP'];
132131
delete configObj['botFiltering'];
133132

134-
var result = eventHelpers.buildImpressionEvent({
133+
var result = buildImpressionEvent({
135134
configObj: configObj,
136135
experimentKey: 'exp1',
137136
variationKey: 'var1',
@@ -191,7 +190,7 @@ describe('lib/event_builder/event_helpers', function() {
191190
projectConfig.getEventId.withArgs(configObj, 'event').returns('event-id');
192191
projectConfig.getAttributeId.withArgs(configObj, 'plan_type').returns('plan_type_id');
193192

194-
var result = eventHelpers.buildConversionEvent({
193+
var result = buildConversionEvent({
195194
configObj: configObj,
196195
eventKey: 'event',
197196
eventTags: {
@@ -257,7 +256,7 @@ describe('lib/event_builder/event_helpers', function() {
257256
delete configObj['anonymizeIP'];
258257
delete configObj['botFiltering'];
259258

260-
var result = eventHelpers.buildConversionEvent({
259+
var result = buildConversionEvent({
261260
configObj: configObj,
262261
eventKey: 'event',
263262
eventTags: {

packages/optimizely-sdk/lib/core/event_builder/index.js

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019, Optimizely
2+
* Copyright 2016-2020, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,11 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var enums = require('../../utils/enums');
17-
var fns = require('../../utils/fns');
18-
var eventTagUtils = require('../../utils/event_tag_utils');
19-
var projectConfig = require('../project_config');
20-
var attributeValidator = require('../../utils/attributes_validator');
16+
import fns from '../../utils/fns';
17+
import enums from '../../utils/enums';
18+
import projectConfig from '../project_config';
19+
import eventTagUtils from '../../utils/event_tag_utils';
20+
import attributeValidator from '../../utils/attributes_validator';
2121

2222
var ACTIVATE_EVENT_KEY = 'campaign_activated';
2323
var CUSTOM_ATTRIBUTE_FEATURE_TYPE = 'custom';
@@ -153,62 +153,65 @@ function getVisitorSnapshot(configObj, eventKey, eventTags, logger) {
153153
return snapshot;
154154
}
155155

156-
module.exports = {
157-
/**
158-
* Create impression event params to be sent to the logging endpoint
159-
* @param {Object} options Object containing values needed to build impression event
160-
* @param {Object} options.attributes Object representing user attributes and values which need to be recorded
161-
* @param {string} options.clientEngine The client we are using: node or javascript
162-
* @param {string} options.clientVersion The version of the client
163-
* @param {Object} options.configObj Object representing project configuration, including datafile information and mappings for quick lookup
164-
* @param {string} options.experimentId Experiment for which impression needs to be recorded
165-
* @param {string} options.userId ID for user
166-
* @param {string} options.variationId ID for variation which would be presented to user
167-
* @return {Object} Params to be used in impression event logging endpoint call
168-
*/
169-
getImpressionEvent: function(options) {
170-
var impressionEvent = {
171-
httpVerb: HTTP_VERB,
172-
};
173-
174-
var commonParams = getCommonEventParams(options);
175-
impressionEvent.url = ENDPOINT;
176-
177-
var impressionEventParams = getImpressionEventParams(options.configObj, options.experimentId, options.variationId);
178-
// combine Event params into visitor obj
179-
commonParams.visitors[0].snapshots.push(impressionEventParams);
180-
181-
impressionEvent.params = commonParams;
182-
183-
return impressionEvent;
184-
},
185-
186-
/**
187-
* Create conversion event params to be sent to the logging endpoint
188-
* @param {Object} options Object containing values needed to build conversion event
189-
* @param {Object} options.attributes Object representing user attributes and values which need to be recorded
190-
* @param {string} options.clientEngine The client we are using: node or javascript
191-
* @param {string} options.clientVersion The version of the client
192-
* @param {Object} options.configObj Object representing project configuration, including datafile information and mappings for quick lookup
193-
* @param {string} options.eventKey Event key representing the event which needs to be recorded
194-
* @param {Object} options.eventTags Object with event-specific tags
195-
* @param {Object} options.logger Logger object
196-
* @param {string} options.userId ID for user
197-
* @return {Object} Params to be used in conversion event logging endpoint call
198-
*/
199-
getConversionEvent: function(options) {
200-
var conversionEvent = {
201-
httpVerb: HTTP_VERB,
202-
};
203-
204-
var commonParams = getCommonEventParams(options);
205-
conversionEvent.url = ENDPOINT;
206-
207-
var snapshot = getVisitorSnapshot(options.configObj, options.eventKey, options.eventTags, options.logger);
208-
209-
commonParams.visitors[0].snapshots = [snapshot];
210-
conversionEvent.params = commonParams;
211-
212-
return conversionEvent;
213-
},
156+
/**
157+
* Create impression event params to be sent to the logging endpoint
158+
* @param {Object} options Object containing values needed to build impression event
159+
* @param {Object} options.attributes Object representing user attributes and values which need to be recorded
160+
* @param {string} options.clientEngine The client we are using: node or javascript
161+
* @param {string} options.clientVersion The version of the client
162+
* @param {Object} options.configObj Object representing project configuration, including datafile information and mappings for quick lookup
163+
* @param {string} options.experimentId Experiment for which impression needs to be recorded
164+
* @param {string} options.userId ID for user
165+
* @param {string} options.variationId ID for variation which would be presented to user
166+
* @return {Object} Params to be used in impression event logging endpoint call
167+
*/
168+
export var getImpressionEvent = function(options) {
169+
var impressionEvent = {
170+
httpVerb: HTTP_VERB,
171+
};
172+
173+
var commonParams = getCommonEventParams(options);
174+
impressionEvent.url = ENDPOINT;
175+
176+
var impressionEventParams = getImpressionEventParams(options.configObj, options.experimentId, options.variationId);
177+
// combine Event params into visitor obj
178+
commonParams.visitors[0].snapshots.push(impressionEventParams);
179+
180+
impressionEvent.params = commonParams;
181+
182+
return impressionEvent;
183+
};
184+
185+
/**
186+
* Create conversion event params to be sent to the logging endpoint
187+
* @param {Object} options Object containing values needed to build conversion event
188+
* @param {Object} options.attributes Object representing user attributes and values which need to be recorded
189+
* @param {string} options.clientEngine The client we are using: node or javascript
190+
* @param {string} options.clientVersion The version of the client
191+
* @param {Object} options.configObj Object representing project configuration, including datafile information and mappings for quick lookup
192+
* @param {string} options.eventKey Event key representing the event which needs to be recorded
193+
* @param {Object} options.eventTags Object with event-specific tags
194+
* @param {Object} options.logger Logger object
195+
* @param {string} options.userId ID for user
196+
* @return {Object} Params to be used in conversion event logging endpoint call
197+
*/
198+
export var getConversionEvent = function(options) {
199+
var conversionEvent = {
200+
httpVerb: HTTP_VERB,
201+
};
202+
203+
var commonParams = getCommonEventParams(options);
204+
conversionEvent.url = ENDPOINT;
205+
206+
var snapshot = getVisitorSnapshot(options.configObj, options.eventKey, options.eventTags, options.logger);
207+
208+
commonParams.visitors[0].snapshots = [snapshot];
209+
conversionEvent.params = commonParams;
210+
211+
return conversionEvent;
212+
};
213+
214+
export default {
215+
getConversionEvent: getConversionEvent,
216+
getImpressionEvent: getImpressionEvent,
214217
};

0 commit comments

Comments
 (0)