8000 refactor: Convert lib/core to ES module (Part 2/2) by fayyazarshad · Pull Request #450 · optimizely/javascript-sdk · GitHub
[go: up one dir, main page]

Skip to content

refactor: Convert lib/core to ES module (Part 2/2) #450

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 10 commits into from
Apr 14, 2020
18 changes: 9 additions & 9 deletions packages/optimizely-sdk/lib/core/event_builder/event_helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2019, Optimizely
* Copyright 2019-2020, Optimizely
*
* 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,14 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var logging = require('@optimizely/js-sdk-logging');
import { getLogger } from '@optimizely/js-sdk-logging';

var attributesValidator = require('../../utils/attributes_validator');
var fns = require('../../utils/fns');
var eventTagUtils = require('../../utils/event_tag_utils');
var projectConfig = require('../project_config');
import fns from '../../utils/fns';
import projectConfig from '../project_config';
import eventTagUtils from '../../utils/event_tag_utils';
import attributesValidator from'../../utils/attributes_validator';

var logger = logging.getLogger('EVENT_BUILDER');
var logger = getLogger('EVENT_BUILDER');

/**
* Creates an ImpressionEvent object from decision data
Expand All @@ -34,7 +34,7 @@ var logger = logging.getLogger('EVENT_BUILDER');
* @param {String} config.clientVersion
* @return {Object} an ImpressionEvent object
*/
exports.buildImpressionEvent = function buildImpressionEvent(config) {
export var buildImpressionEvent = function(config) {
var configObj = config.configObj;
var experimentKey = config.experimentKey;
var variationKey = config.variationKey;
Expand Down Expand Up @@ -95,7 +95,7 @@ exports.buildImpressionEvent = function buildImpressionEvent(config) {
* @param {String} config.clientVersion
* @return {Object} a ConversionEvent object
*/
exports.buildConversionEvent = function buildConversionEvent(config) {
export var buildConversionEvent = function(config) {
var configObj = config.configObj;
var userId = config.userId;
var userAttributes = config.userAttributes;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2019, Optimizely
* Copyright 2019-2020, Optimizely
*
* 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,13 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var projectConfig = require('../project_config');
var eventHelpers = require('./event_helpers');
var fns = require('../../utils/fns');
import sinon from 'sinon';
import { assert } from 'chai';

var chai = require('chai');
var assert = chai.assert;
var sinon = require('sinon');
import fns from '../../utils/fns';
import projectConfig from '../project_config';
import { buildImpressionEvent, buildConversionEvent } from './event_helpers';

describe('lib/event_builder/event_helpers', function() {
var configObj;
Expand Down Expand Up @@ -65,7 +64,7 @@ describe('lib/event_builder/event_helpers', function() {

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

var result = eventHelpers.buildImpressionEvent({
var result = buildImpressionEvent({
configObj: configObj,
experimentKey: 'exp1',
variationKey: 'var1',
Expand Down Expand Up @@ -131,7 +130,7 @@ describe('lib/event_builder/event_helpers', function() {
delete configObj['anonymizeIP'];
delete configObj['botFiltering'];

var result = eventHelpers.buildImpressionEvent({
var result = buildImpressionEvent({
configObj: configObj,
experimentKey: 'exp1',
variationKey: 'var1',
Expand Down Expand Up @@ -191,7 +190,7 @@ describe('lib/event_builder/event_helpers', function() {
projectConfig.getEventId.withArgs(configObj, 'event').returns('event-id');
projectConfig.getAttributeId.withArgs(configObj, 'plan_type').returns('plan_type_id');

var result = eventHelpers.buildConversionEvent({
var result = buildConversionEvent({
configObj: configObj,
eventKey: 'event',
eventTags: {
Expand Down Expand Up @@ -257,7 +256,7 @@ describe('lib/event_builder/event_helpers', function() {
delete configObj['anonymizeIP'];
delete configObj['botFiltering'];

var result = eventHelpers.buildConversionEvent({
var result = buildConversionEvent({
configObj: configObj,
eventKey: 'event',
eventTags: {
Expand Down
131 changes: 67 additions & 64 deletions packages/optimizely-sdk/lib/core/event_builder/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2019, Optimizely
* Copyright 2016-2020, Optimizely
*
* 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,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var enums = require('../../utils/enums');
var fns = require('../../utils/fns');
var eventTagUtils = require('../../utils/event_tag_utils');
var projectConfig = require('../project_config');
var attributeValidator = require('../../utils/attributes_validator');
import fns from '../../utils/fns';
import enums from '../../utils/enums';
import projectConfig from '../project_config';
import eventTagUtils from '../../utils/event_tag_utils';
import attributeValidator from '../../utils/attributes_validator';

var ACTIVATE_EVENT_KEY = 'campaign_activated';
var CUSTOM_ATTRIBUTE_FEATURE_TYPE = 'custom';
Expand Down Expand Up @@ -153,62 +153,65 @@ function getVisitorSnapshot(configObj, eventKey, eventTags, logger) {
return snapshot;
}

module.exports = {
/**
* Create impression event params to be sent to the logging endpoint
* @param {Object} options Object containing values needed to build impression event
* @param {Object} options.attributes Object representing user attributes and values which need to be recorded
* @param {string} options.clientEngine The client we are using: node or javascript
* @param {string} options.clientVersion The version of the client
* @param {Object} options.configObj Object representing project configuration, including datafile information and mappings for quick lookup
* @param {string} options.experimentId Experiment for which impression needs to be recorded
* @param {string} options.userId ID for user
* @param {string} options.variationId ID for variation which would be presented to user
* @return {Object} Params to be used in impression event logging endpoint call
*/
getImpressionEvent: function(options) {
var impressionEvent = {
httpVerb: HTTP_VERB,
};

var commonParams = getCommonEventParams(options);
impressionEvent.url = ENDPOINT;

var impressionEventParams = getImpressionEventParams(options.configObj, options.experimentId, options.variationId);
// combine Event params into visitor obj
commonParams.visitors[0].snapshots.push(impressionEventParams);

impressionEvent.params = commonParams;

return impressionEvent;
},

/**
* Create conversion event params to be sent to the logging endpoint
* @param {Object} options Object containing values needed to build conversion event
* @param {Object} options.attributes Object representing user attributes and values which need to be recorded
* @param {string} options.clientEngine The client we are using: node or javascript
* @param {string} options.clientVersion The version of the client
* @param {Object} options.configObj Object representing project configuration, including datafile information and mappings for quick lookup
* @param {string} options.eventKey Event key representing the event which needs to be recorded
* @param {Object} options.eventTags Object with event-specific tags
* @param {Object} options.logger Logger object
* @param {string} options.userId ID for user
* @return {Object} Params to be used in conversion event logging endpoint call
*/
getConversionEvent: function(options) {
var conversionEvent = {
httpVerb: HTTP_VERB,
};

var commonParams = getCommonEventParams(options);
conversionEvent.url = ENDPOINT;

var snapshot = getVisitorSnapshot(options.configObj, options.eventKey, options.eventTags, options.logger);

commonParams.visitors[0].snapshots = [snapshot];
conversionEvent.params = commonParams;

return conversionEvent;
},
/**
* Create impression event params to be sent to the logging endpoint
* @param {Object} options Object containing values needed to build impression event
* @param {Object} options.attributes Object representing user attributes and values which need to be recorded
* @param {string} options.clientEngine The client we are using: node or javascript
* @param {string} options.clientVersion The version of the client
* @param {Object} options.configObj Object representing project configuration, including datafile information and mappings for quick lookup
* @param {string} options.experimentId Experiment for which impression needs to be recorded
* @param {string} options.userId ID for user
* @param {string} options.variationId ID for variation which would be presented to user
* @return {Object} Params to be used in impression event logging endpoint call
*/
export var getImpressionEvent = function(options) {
var impressionEvent = {
httpVerb: HTTP_VERB,
};

var commonParams = getCommonEventParams(options);
impressionEvent.url = ENDPOINT;

var impressionEventParams = getImpressionEventParams(options.configObj, options.experimentId, options.variationId);
// combine Event params into visitor obj
commonParams.visitors[0].snapshots.push(impressionEventParams);

impressionEvent.params = commonParams;

return impressionEvent;
};

/**
* Create conversion event params to be sent to the logging endpoint
* @param {Object} options Object containing values needed to build conversion event
* @param {Object} options.attributes Object representing user attributes and values which need to be recorded
* @param {string} options.clientEngine The client we are using: node or javascript
* @param {string} options.clientVersion The version of the client
* @param {Object} options.configObj Object representing project configuration, including datafile information and mappings for quick lookup
* @param {string} options.eventKey Event key representing the event which needs to be recorded
* @param {Object} options.eventTags Object with event-specific tags
* @param {Object} options.logger Logger object
* @param {string} options.userId ID for user
* @return {Object} Params to be used in conversion event logging endpoint call
*/
export var getConversionEvent = function(options) {
var conversionEvent = {
httpVerb: HTTP_VERB,
};

var commonParams = getCommonEventParams(options);
conversionEvent.url = ENDPOINT;

var snapshot = getVisitorSnapshot(options.configObj, options.eventKey, options.eventTags, options.logger);

commonParams.visitors[0].snapshots = [snapshot];
conversionEvent.params = commonParams;

return conversionEvent;
};

export default {
getConversionEvent: getConversionEvent,
getImpressionEvent: getImpressionEvent,
};
Loading
0