diff --git a/packages/optimizely-sdk/lib/index.browser.tests.js b/packages/optimizely-sdk/lib/index.browser.tests.js index 80b6cef6e..20a6aaa1e 100644 --- a/packages/optimizely-sdk/lib/index.browser.tests.js +++ b/packages/optimizely-sdk/lib/index.browser.tests.js @@ -55,6 +55,7 @@ describe('javascript-sdk', function() { sinon.stub(configValidator, 'validate'); xhr = sinon.useFakeXMLHttpRequest(); + global.XMLHttpRequest = xhr; requests = []; xhr.onCreate = function(req) { requests.push(req); diff --git a/packages/optimizely-sdk/lib/index.browser.umdtests.js b/packages/optimizely-sdk/lib/index.browser.umdtests.js index d41b7f463..f18a3dbcb 100644 --- a/packages/optimizely-sdk/lib/index.browser.umdtests.js +++ b/packages/optimizely-sdk/lib/index.browser.umdtests.js @@ -42,6 +42,7 @@ describe('javascript-sdk', function() { sinon.stub(Optimizely.prototype, 'close'); xhr = sinon.useFakeXMLHttpRequest(); + global.XMLHttpRequest = xhr; requests = []; xhr.onCreate = function(req) { requests.push(req); diff --git a/packages/optimizely-sdk/lib/plugins/error_handler/index.js b/packages/optimizely-sdk/lib/plugins/error_handler/index.js index 211f90592..ef4f598b6 100644 --- a/packages/optimizely-sdk/lib/plugins/error_handler/index.js +++ b/packages/optimizely-sdk/lib/plugins/error_handler/index.js @@ -1,5 +1,5 @@ /** - * Copyright 2016, 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. @@ -17,12 +17,10 @@ /** * Default error handler implementation */ -module.exports = { - /** - * Handle given exception - * @param {Object} exception An exception object - */ - handleError: function() { - // no-op - }, -}; +export var handleError = function() { + // no-op +} + +export default { + handleError, +} diff --git a/packages/optimizely-sdk/lib/plugins/error_handler/index.tests.js b/packages/optimizely-sdk/lib/plugins/error_handler/index.tests.js index 753ec01a1..b3a632b92 100644 --- a/packages/optimizely-sdk/lib/plugins/error_handler/index.tests.js +++ b/packages/optimizely-sdk/lib/plugins/error_handler/index.tests.js @@ -1,5 +1,5 @@ /** - * Copyright 2016, 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. @@ -13,16 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var errorHandler = require('./'); +import { assert } from 'chai'; -var chai = require('chai'); -var assert = chai.assert; +import { handleError } from './'; describe('lib/plugins/error_handler', function() { describe('APIs', function() { describe('handleError', function() { it('should just be a no-op function', function() { - assert.isFunction(errorHandler.handleError); + assert.isFunction(handleError); }); }); }); diff --git a/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.browser.js b/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.browser.js index 3772c82ad..8f1438a02 100644 --- a/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.browser.js +++ b/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.browser.js @@ -1,5 +1,5 @@ /** - * Copyright 2016-2017, Optimizely + * Copyright 2016-2017, 2020, Optimizely * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,53 +17,51 @@ var POST_METHOD = 'POST'; var GET_METHOD = 'GET'; var READYSTATE_COMPLETE = 4; -module.exports = { - /** - * Sample event dispatcher implementation for tracking impression and conversions - * Users of the SDK can provide their own implementation - * @param {Object} eventObj - * @param {Function} callback - */ - dispatchEvent: function(eventObj, callback) { - var url = eventObj.url; - var params = eventObj.params; - var req; - if (eventObj.httpVerb === POST_METHOD) { - req = new XMLHttpRequest(); - req.open(POST_METHOD, url, true); - req.setRequestHeader('Content-Type', 'application/json'); - req.onreadystatechange = function() { - if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') { - try { - callback(params); - } catch (e) { - // TODO: Log this somehow (consider adding a logger to the EventDispatcher interface) - } +/** + * Sample event dispatcher implementation for tracking impression and conversions + * Users of the SDK can provide their own implementation + * @param {Object} eventObj + * @param {Function} callback + */ +export var dispatchEvent = function(eventObj, callback) { + var url = eventObj.url; + var params = eventObj.params; + var req; + if (eventObj.httpVerb === POST_METHOD) { + req = new XMLHttpRequest(); + req.open(POST_METHOD, url, true); + req.setRequestHeader('Content-Type', 'application/json'); + req.onreadystatechange = function() { + if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') { + try { + callback(params); + } catch (e) { + // TODO: Log this somehow (consider adding a logger to the EventDispatcher interface) } - }; - req.send(JSON.stringify(params)); - } else { - // add param for cors headers to be sent by the log endpoint - url += '?wxhr=true'; - if (params) { - url += '&' + toQueryString(params); } + }; + req.send(JSON.stringify(params)); + } else { + // add param for cors headers to be sent by the log endpoint + url += '?wxhr=true'; + if (params) { + url += '&' + toQueryString(params); + } - req = new XMLHttpRequest(); - req.open(GET_METHOD, url, true); - req.onreadystatechange = function() { - if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') { - try { - callback(); - } catch (e) { - // TODO: Log this somehow (consider adding a logger to the EventDispatcher interface) - } + req = new XMLHttpRequest(); + req.open(GET_METHOD, url, true); + req.onreadystatechange = function() { + if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') { + try { + callback(); + } catch (e) { + // TODO: Log this somehow (consider adding a logger to the EventDispatcher interface) } - }; - req.send(); - } - }, -}; + } + }; + req.send(); + } +} var toQueryString = function(obj) { return Object.keys(obj) @@ -72,3 +70,7 @@ var toQueryString = function(obj) { }) .join('&'); }; + +export default { + dispatchEvent, +}; diff --git a/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.browser.tests.js b/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.browser.tests.js index 6a774668b..a142ab374 100644 --- a/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.browser.tests.js +++ b/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.browser.tests.js @@ -1,5 +1,5 @@ /** - * Copyright 2016-2017, Optimizely + * Copyright 2016-2017, 2020, Optimizely * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var eventDispatcher = require('./index.browser'); -var chai = require('chai'); -var assert = chai.assert; -var sinon = require('sinon'); +import { assert } from 'chai'; +import sinon from 'sinon'; + +import { dispatchEvent } from './index.browser'; describe('lib/plugins/event_dispatcher/browser', function() { describe('APIs', function() { @@ -25,7 +25,6 @@ describe('lib/plugins/event_dispatcher/browser', function() { var requests; beforeEach(function() { xhr = sinon.useFakeXMLHttpRequest(); - global.XMLHttpRequest = xhr; requests = []; xhr.onCreate = function(req) { requests.push(req); @@ -48,7 +47,7 @@ describe('lib/plugins/event_dispatcher/browser', function() { }; var callback = sinon.spy(); - eventDispatcher.dispatchEvent(eventObj, callback); + dispatchEvent(eventObj, callback); assert.strictEqual(1, requests.length); assert.strictEqual(requests[0].method, 'POST'); assert.strictEqual(requests[0].requestBody, JSON.stringify(eventParams)); @@ -67,7 +66,7 @@ describe('lib/plugins/event_dispatcher/browser', function() { }; var callback = sinon.spy(); - eventDispatcher.dispatchEvent(eventObj, callback); + dispatchEvent(eventObj, callback); requests[0].respond([ 200, {}, @@ -84,7 +83,7 @@ describe('lib/plugins/event_dispatcher/browser', function() { }; var callback = sinon.spy(); - eventDispatcher.dispatchEvent(eventObj, callback); + dispatchEvent(eventObj, callback); requests[0].respond([200, {}, '{"url":"https://cdn.com/event","httpVerb":"GET"']); sinon.assert.calledOnce(callback); done(); diff --git a/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.node.js b/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.node.js index 2e20cca1f..f01d94f4b 100644 --- a/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.node.js +++ b/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.node.js @@ -1,5 +1,5 @@ /** - * Copyright 2016-2018, Optimizely + * Copyright 2016-2018, 2020, Optimizely * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,51 +13,53 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var http = require('http'); -var https = require('https'); -var url = require('url'); +import http from 'http'; +import https from 'https'; +import url from 'url'; -module.exports = { - /** - * Dispatch an HTTP request to the given url and the specified options - * @param {Object} eventObj Event object containing - * @param {string} eventObj.url the url to make the request to - * @param {Object} eventObj.params parameters to pass to the request (i.e. in the POST body) - * @param {string} eventObj.httpVerb the HTTP request method type. only POST is supported. - * @param {function} callback callback to execute - * @return {ClientRequest|undefined} ClientRequest object which made the request, or undefined if no request was made (error) - */ - dispatchEvent: function(eventObj, callback) { - // Non-POST requests not supported - if (eventObj.httpVerb !== 'POST') { - return; - } +/** + * Dispatch an HTTP request to the given url and the specified options + * @param {Object} eventObj Event object containing + * @param {string} eventObj.url the url to make the request to + * @param {Object} eventObj.params parameters to pass to the request (i.e. in the POST body) + * @param {string} eventObj.httpVerb the HTTP request method type. only POST is supported. + * @param {function} callback callback to execute + * @return {ClientRequest|undefined} ClientRequest object which made the request, or undefined if no request was made (error) + */ +export var dispatchEvent = function(eventObj, callback) { + // Non-POST requests not supported + if (eventObj.httpVerb !== 'POST') { + return; + } - var parsedUrl = url.parse(eventObj.url); + var parsedUrl = url.parse(eventObj.url); - var dataString = JSON.stringify(eventObj.params); + var dataString = JSON.stringify(eventObj.params); - var requestOptions = { - host: parsedUrl.host, - path: parsedUrl.path, - method: 'POST', - headers: { - 'content-type': 'application/json', - 'content-length': dataString.length.toString(), - }, - }; + var requestOptions = { + host: parsedUrl.host, + path: parsedUrl.path, + method: 'POST', + headers: { + 'content-type': 'application/json', + 'content-length': dataString.length.toString(), + }, + }; - var requestCallback = function(response) { - if (response && response.statusCode && response.statusCode >= 200 && response.statusCode < 400) { - callback(response); - } - }; + var requestCallback = function(response) { + if (response && response.statusCode && response.statusCode >= 200 && response.statusCode < 400) { + callback(response); + } + }; + + var req = (parsedUrl.protocol === 'http:' ? http : https).request(requestOptions, requestCallback); + // Add no-op error listener to prevent this from throwing + req.on('error', function() {}); + req.write(dataString); + req.end(); + return req; +}; - var req = (parsedUrl.protocol === 'http:' ? http : https).request(requestOptions, requestCallback); - // Add no-op error listener to prevent this from throwing - req.on('error', function() {}); - req.write(dataString); - req.end(); - return req; - }, +export default { + dispatchEvent, }; diff --git a/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.node.tests.js b/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.node.tests.js index 745ac89bd..f17fc9705 100644 --- a/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.node.tests.js +++ b/packages/optimizely-sdk/lib/plugins/event_dispatcher/index.node.tests.js @@ -1,5 +1,5 @@ /** - * Copyright 2016-2018, Optimizely + * Copyright 2016-2018, 2020, Optimizely * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var eventDispatcher = require('./index.node'); -var chai = require('chai'); -var assert = chai.assert; -var nock = require('nock'); -var sinon = require('sinon'); +import nock from 'nock'; +import sinon from 'sinon'; +import { assert } from 'chai'; + +import { dispatchEvent } from './index.node'; describe('lib/plugins/event_dispatcher/node', function() { describe('APIs', function() { @@ -49,7 +49,7 @@ describe('lib/plugins/event_dispatcher/node', function() { httpVerb: 'POST', }; - eventDispatcher.dispatchEvent(eventObj, function(resp) { + dispatchEvent(eventObj, function(resp) { assert.equal(200, resp.statusCode); done(); }); @@ -64,8 +64,7 @@ describe('lib/plugins/event_dispatcher/node', function() { httpVerb: 'POST', }; - eventDispatcher - .dispatchEvent(eventObj, stubCallback.callback) + dispatchEvent(eventObj, stubCallback.callback) .on('response', function(response) { sinon.assert.calledOnce(stubCallback.callback); done(); @@ -85,7 +84,7 @@ describe('lib/plugins/event_dispatcher/node', function() { }; var callback = sinon.spy(); - eventDispatcher.dispatchEvent(eventObj, callback); + dispatchEvent(eventObj, callback); sinon.assert.notCalled(callback); }); }); @@ -98,7 +97,7 @@ describe('lib/plugins/event_dispatcher/node', function() { }; var callback = sinon.spy(); - eventDispatcher.dispatchEvent(eventObj, callback); + dispatchEvent(eventObj, callback); sinon.assert.notCalled(callback); }); }); diff --git a/packages/optimizely-sdk/lib/plugins/logger/enums.js b/packages/optimizely-sdk/lib/plugins/logger/enums.js index d5e657eda..1ec490c5b 100644 --- a/packages/optimizely-sdk/lib/plugins/logger/enums.js +++ b/packages/optimizely-sdk/lib/plugins/logger/enums.js @@ -1,5 +1,5 @@ /** - * Copyright 2016, 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. @@ -13,4 +13,4 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -exports.LOG_LEVEL = require('@optimizely/js-sdk-logging').LogLevel; +export { LOG_LEVEL as LogLevel } from '@optimizely/js-sdk-logging'; diff --git a/packages/optimizely-sdk/lib/plugins/logger/index.js b/packages/optimizely-sdk/lib/plugins/logger/index.js index 31635d153..f9a3ff36b 100644 --- a/packages/optimizely-sdk/lib/plugins/logger/index.js +++ b/packages/optimizely-sdk/lib/plugins/logger/index.js @@ -1,5 +1,5 @@ /** - * Copyright 2016-2017, Optimizely + * Copyright 2016-2017, 2020, Optimizely * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,18 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var logging = require('@optimizely/js-sdk-logging'); +import { ConsoleLogHandler } from '@optimizely/js-sdk-logging'; function NoOpLogger() {} NoOpLogger.prototype.log = function() {}; -module.exports = { - createLogger: function(opts) { - return new logging.ConsoleLogHandler(opts); - }, +export var createLogger = function(opts) { + return new ConsoleLogHandler(opts); +}; + +export var createNoOpLogger = function() { + return new NoOpLogger(); +}; - createNoOpLogger: function() { - return new NoOpLogger(); - }, +export default { + createLogger, + createNoOpLogger, }; diff --git a/packages/optimizely-sdk/lib/plugins/logger/index.react_native.js b/packages/optimizely-sdk/lib/plugins/logger/index.react_native.js index 41168c686..22cc2841e 100644 --- a/packages/optimizely-sdk/lib/plugins/logger/index.react_native.js +++ b/packages/optimizely-sdk/lib/plugins/logger/index.react_native.js @@ -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. @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var LogLevel = require('@optimizely/js-sdk-logging').LogLevel; -var sprintf = require('@optimizely/js-sdk-utils').sprintf; +import { LogLevel } from '@optimizely/js-sdk-logging'; +import { sprintf } from '@optimizely/js-sdk-utils'; function getLogLevelName(level) { switch (level) { @@ -54,12 +54,15 @@ function NoOpLogger() {} NoOpLogger.prototype.log = function() {}; -module.exports = { - createLogger: function() { - return new ReactNativeLogger(); - }, +export var createLogger = function() { + return new ReactNativeLogger(); +} + +export var createNoOpLogger = function() { + return new NoOpLogger(); +} - createNoOpLogger: function() { - return new NoOpLogger(); - }, +export default { + createLogger, + createNoOpLogger, }; diff --git a/packages/optimizely-sdk/lib/plugins/logger/index.react_native.tests.js b/packages/optimizely-sdk/lib/plugins/logger/index.react_native.tests.js index 11dd53619..a62ec3c5a 100644 --- a/packages/optimizely-sdk/lib/plugins/logger/index.react_native.tests.js +++ b/packages/optimizely-sdk/lib/plugins/logger/index.react_native.tests.js @@ -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. @@ -13,26 +13,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var logger = require('./index.react_native'); -var chai = require('chai'); -var enums = require('../../utils/enums'); -var assert = chai.assert; -var sinon = require('sinon'); +import sinon from 'sinon'; +import { assert } from 'chai'; + +import { createLogger } from './index.react_native'; +import { LOG_LEVEL } from '../../utils/enums'; -var LOG_LEVEL = enums.LOG_LEVEL; describe('lib/plugins/logger/react_native', function() { describe('APIs', function() { var defaultLogger; describe('createLogger', function() { it('should return an instance of the default logger', function() { - defaultLogger = logger.createLogger(); + defaultLogger = createLogger(); assert.isObject(defaultLogger); }); }); describe('log', function() { beforeEach(function() { - defaultLogger = logger.createLogger(); + defaultLogger = createLogger(); sinon.stub(console, 'log'); sinon.stub(console, 'info'); diff --git a/packages/optimizely-sdk/lib/plugins/logger/index.tests.js b/packages/optimizely-sdk/lib/plugins/logger/index.tests.js index 5ffae7924..0e3eaac56 100644 --- a/packages/optimizely-sdk/lib/plugins/logger/index.tests.js +++ b/packages/optimizely-sdk/lib/plugins/logger/index.tests.js @@ -1,5 +1,5 @@ /** - * Copyright 2016, 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. @@ -13,20 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -var logger = require('./'); -var chai = require('chai'); -var enums = require('../../utils/enums'); -var assert = chai.assert; -var expect = chai.expect; -var sinon = require('sinon'); - -var LOG_LEVEL = enums.LOG_LEVEL; +import { assert, expect } from 'chai'; +import sinon from 'sinon'; + +import { createLogger } from './'; +import { LOG_LEVEL } from '../../utils/enums';; + describe('lib/plugins/logger', function() { describe('APIs', function() { var defaultLogger; describe('createLogger', function() { it('should return an instance of the default logger', function() { - defaultLogger = logger.createLogger({ logLevel: LOG_LEVEL.NOTSET }); + defaultLogger = createLogger({ logLevel: LOG_LEVEL.NOTSET }); assert.isObject(defaultLogger); expect(defaultLogger.logLevel).to.equal(LOG_LEVEL.NOTSET); }); @@ -34,7 +32,7 @@ describe('lib/plugins/logger', function() { describe('log', function() { beforeEach(function() { - defaultLogger = logger.createLogger({ logLevel: LOG_LEVEL.INFO }); + defaultLogger = createLogger({ logLevel: LOG_LEVEL.INFO }); sinon.stub(console, 'log'); sinon.stub(console, 'info'); @@ -81,7 +79,7 @@ describe('lib/plugins/logger', function() { describe('setLogLevel', function() { beforeEach(function() { - defaultLogger = logger.createLogger({ logLevel: LOG_LEVEL.NOTSET }); + defaultLogger = createLogger({ logLevel: LOG_LEVEL.NOTSET }); }); it('should set the log level to the specified log level', function() { diff --git a/packages/optimizely-sdk/rollup.config.js b/packages/optimizely-sdk/rollup.config.js index 9508b4530..7273b12c0 100644 --- a/packages/optimizely-sdk/rollup.config.js +++ b/packages/optimizely-sdk/rollup.config.js @@ -54,6 +54,7 @@ const umdconfig = { commonjs({ namedExports: { '@optimizely/js-sdk-logging': [ + 'ConsoleLogHandler', 'getLogger', 'setLogLevel', 'LogLevel',