Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit b141a94

Browse files
refactor: Convert lib/plugins from CJS to ES module (optimizely#427)
Summary: This PR converts contents inside `lib/plugins` to ESModule. Test plan: All unit test and Full stack compatibility suite tests pass. Co-authored-by: zashraf1985 <35262377+zashraf1985@users.noreply.github.com> Co-authored-by: Zeeshan Ashraf <zashraf@folio3.com>
1 parent eaa9d3f commit b141a94

File tree

14 files changed

+168
-163
lines changed

14 files changed

+168
-163
lines changed

packages/optimizely-sdk/lib/index.browser.tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ describe('javascript-sdk', function() {
5555
sinon.stub(configValidator, 'validate');
5656

5757
xhr = sinon.useFakeXMLHttpRequest();
58+
global.XMLHttpRequest = xhr;
5859
requests = [];
5960
xhr.onCreate = function(req) {
6061
requests.push(req);

packages/optimizely-sdk/lib/index.browser.umdtests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ describe('javascript-sdk', function() {
4242
sinon.stub(Optimizely.prototype, 'close');
4343

4444
xhr = sinon.useFakeXMLHttpRequest();
45+
global.XMLHttpRequest = xhr;
4546
requests = [];
4647
xhr.onCreate = function(req) {
4748
requests.push(req);
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016, 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.
@@ -17,12 +17,10 @@
1717
/**
1818
* Default error handler implementation
1919
*/
20-
module.exports = {
21-
/**
22-
* Handle given exception
23-
* @param {Object} exception An exception object
24-
*/
25-
handleError: function() {
26-
// no-op
27-
},
28-
};
20+
export var handleError = function() {
21+
// no-op
22+
}
23+
24+
export default {
25+
handleError,
26+
}

packages/optimizely-sdk/lib/plugins/error_handler/index.tests.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016, 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,16 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var errorHandler = require('./');
16+
import { assert } from 'chai';
1717

18-
var chai = require('chai');
19-
var assert = chai.assert;
18+
import { handleError } from './';
2019

2120
describe('lib/plugins/error_handler', function() {
2221
describe('APIs', function() {
2322
describe('handleError', function() {
2423
it('should just be a no-op function', function() {
25-
assert.isFunction(errorHandler.handleError);
24+
assert.isFunction(handleError);
2625
});
2726
});
2827
});
Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017, Optimizely
2+
* Copyright 2016-2017, 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.
@@ -17,53 +17,51 @@ var POST_METHOD = 'POST';
1717
var GET_METHOD = 'GET';
1818
var READYSTATE_COMPLETE = 4;
1919

20-
module.exports = {
21-
/**
22-
* Sample event dispatcher implementation for tracking impression and conversions
23-
* Users of the SDK can provide their own implementation
24-
* @param {Object} eventObj
25-
* @param {Function} callback
26-
*/
27-
dispatchEvent: function(eventObj, callback) {
28-
var url = eventObj.url;
29-
var params = eventObj.params;
30-
var req;
31-
if (eventObj.httpVerb === POST_METHOD) {
32-
req = new XMLHttpRequest();
33-
req.open(POST_METHOD, url, true);
34-
req.setRequestHeader('Content-Type', 'application/json');
35-
req.onreadystatechange = function() {
36-
if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') {
37-
try {
38-
callback(params);
39-
} catch (e) {
40-
// TODO: Log this somehow (consider adding a logger to the EventDispatcher interface)
41-
}
20+
/**
21+
* Sample event dispatcher implementation for tracking impression and conversions
22+
* Users of the SDK can provide their own implementation
23+
* @param {Object} eventObj
24+
* @param {Function} callback
25+
*/
26+
export var dispatchEvent = function(eventObj, callback) {
27+
var url = eventObj.url;
28+
var params = eventObj.params;
29+
var req;
30+
if (eventObj.httpVerb === POST_METHOD) {
31+
req = new XMLHttpRequest();
32+
req.open(POST_METHOD, url, true);
33+
req.setRequestHeader('Content-Type', 'application/json');
34+
req.onreadystatechange = function() {
35+
if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') {
36+
try {
37+
callback(params);
38+
} catch (e) {
39+
// TODO: Log this somehow (consider adding a logger to the EventDispatcher interface)
4240
}
43-
};
44-
req.send(JSON.stringify(params));
45-
} else {
46-
// add param for cors headers to be sent by the log endpoint
47-
url += '?wxhr=true';
48-
if (params) {
49-
url += '&' + toQueryString(params);
5041
}
42+
};
43+
req.send(JSON.stringify(params));
44+
} else {
45+
// add param for cors headers to be sent by the log endpoint
46+
url += '?wxhr=true';
47+
if (params) {
48+
url += '&' + toQueryString(params);
49+
}
5150

52-
req = new XMLHttpRequest();
53-
req.open(GET_METHOD, url, true);
54-
req.onreadystatechange = function() {
55-
if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') {
56-
try {
57-
callback();
58-
} catch (e) {
59-
// TODO: Log this somehow (consider adding a logger to the EventDispatcher interface)
60-
}
51+
req = new XMLHttpRequest();
52+
req.open(GET_METHOD, url, true);
53+
req.onreadystatechange = function() {
54+
if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') {
55+
try {
56+
callback();
57+
} catch (e) {
58+
// TODO: Log this somehow (consider adding a logger to the EventDispatcher interface)
6159
}
62-
};
63-
req.send();
64-
}
65-
},
66-
};
60+
}
61+
};
62+
req.send();
63+
}
64+
}
6765

6866
var toQueryString = function(obj) {
6967
return Object.keys(obj)
@@ -72,3 +70,7 @@ var toQueryString = function(obj) {
7270
})
7371
.join('&');
7472
};
73+
74+
export default {
75+
dispatchEvent,
76+
};

packages/optimizely-sdk/lib/plugins/event_dispatcher/index.browser.tests.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017, Optimizely
2+
* Copyright 2016-2017, 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,10 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var eventDispatcher = require('./index.browser');
17-
var chai = require('chai');
18-
var assert = chai.assert;
19-
var sinon = require('sinon');
16+
import { assert } from 'chai';
17+
import sinon from 'sinon';
18+
19+
import { dispatchEvent } from './index.browser';
2020

2121
describe('lib/plugins/event_dispatcher/browser', function() {
2222
describe('APIs', function() {
@@ -25,7 +25,6 @@ describe('lib/plugins/event_dispatcher/browser', function() {
2525
var requests;
2626
beforeEach(function() {
2727
xhr = sinon.useFakeXMLHttpRequest();
28-
global.XMLHttpRequest = xhr;
2928
requests = [];
3029
xhr.onCreate = function(req) {
3130
requests.push(req);
@@ -48,7 +47,7 @@ describe('lib/plugins/event_dispatcher/browser', function() {
4847
};
4948

5049
var callback = sinon.spy();
51-
eventDispatcher.dispatchEvent(eventObj, callback);
50+
dispatchEvent(eventObj, callback);
5251
assert.strictEqual(1, requests.length);
5352
assert.strictEqual(requests[0].method, 'POST');
5453
assert.strictEqual(requests[0].requestBody, JSON.stringify(eventParams));
@@ -67,7 +66,7 @@ describe('lib/plugins/event_dispatcher/browser', function() {
6766
};
6867

6968
var callback = sinon.spy();
70-
eventDispatcher.dispatchEvent(eventObj, callback);
69+
dispatchEvent(eventObj, callback);
7170
requests[0].respond([
7271
200,
7372
{},
@@ -84,7 +83,7 @@ describe('lib/plugins/event_dispatcher/browser', function() {
8483
};
8584

8685
var callback = sinon.spy();
87-
eventDispatcher.dispatchEvent(eventObj, callback);
86+
dispatchEvent(eventObj, callback);
8887
requests[0].respond([200, {}, '{"url":"https://cdn.com/event","httpVerb":"GET"']);
8988
sinon.assert.calledOnce(callback);
9089
done();
Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018, Optimizely
2+
* Copyright 2016-2018, 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,51 +13,53 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var http = require('http');
17-
var https = require('https');
18-
var url = require('url');
16+
import http from 'http';
17+
import https from 'https';
18+
import url from 'url';
1919

20-
module.exports = {
21-
/**
22-
* Dispatch an HTTP request to the given url and the specified options
23-
* @param {Object} eventObj Event object containing
24-
* @param {string} eventObj.url the url to make the request to
25-
* @param {Object} eventObj.params parameters to pass to the request (i.e. in the POST body)
26-
* @param {string} eventObj.httpVerb the HTTP request method type. only POST is supported.
27-
* @param {function} callback callback to execute
28-
* @return {ClientRequest|undefined} ClientRequest object which made the request, or undefined if no request was made (error)
29-
*/
30-
dispatchEvent: function(eventObj, callback) {
31-
// Non-POST requests not supported
32-
if (eventObj.httpVerb !== 'POST') {
33-
return;
34-
}
20+
/**
21+
* Dispatch an HTTP request to the given url and the specified options
22+
* @param {Object} eventObj Event object containing
23+
* @param {string} eventObj.url the url to make the request to
24+
* @param {Object} eventObj.params parameters to pass to the request (i.e. in the POST body)
25+
* @param {string} eventObj.httpVerb the HTTP request method type. only POST is supported.
26+
* @param {function} callback callback to execute
27+
* @return {ClientRequest|undefined} ClientRequest object which made the request, or undefined if no request was made (error)
28+
*/
29+
export var dispatchEvent = function(eventObj, callback) {
30+
// Non-POST requests not supported
31+
if (eventObj.httpVerb !== 'POST') {
32+
return;
33+
}
3534

36-
var parsedUrl = url.parse(eventObj.url);
35+
var parsedUrl = url.parse(eventObj.url);
3736

38-
var dataString = JSON.stringify(eventObj.params);
37+
var dataString = JSON.stringify(eventObj.params);
3938

40-
var requestOptions = {
41-
host: parsedUrl.host,
42-
path: parsedUrl.path,
43-
method: 'POST',
44-
headers: {
45-
'content-type': 'application/json',
46-
'content-length': dataString.length.toString(),
47-
},
48-
};
39+
var requestOptions = {
40+
host: parsedUrl.host,
41+
path: parsedUrl.path,
42+
method: 'POST',
43+
headers: {
44+
'content-type': 'application/json',
45+
'content-length': dataString.length.toString(),
46+
},
47+
};
4948

50-
var requestCallback = function(response) {
51-
if (response && response.statusCode && response.statusCode >= 200 && response.statusCode < 400) {
52-
callback(response);
53-
}
54-
};
49+
var requestCallback = function(response) {
50+
if (response && response.statusCode && response.statusCode >= 200 && response.statusCode < 400) {
51+
callback(response);
52+
}
53+
};
54+
55+
var req = (parsedUrl.protocol === 'http:' ? http : https).request(requestOptions, requestCallback);
56+
// Add no-op error listener to prevent this from throwing
57+
req.on('error', function() {});
58+
req.write(dataString);
59+
req.end();
60+
return req;
61+
};
5562

56-
var req = (parsedUrl.protocol === 'http:' ? http : https).request(requestOptions, requestCallback);
57-
// Add no-op error listener to prevent this from throwing
58-
req.on('error', function() {});
59-
req.write(dataString);
60-
req.end();
61-
return req;
62-
},
63+
export default {
64+
dispatchEvent,
6365
};

packages/optimizely-sdk/lib/plugins/event_dispatcher/index.node.tests.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018, Optimizely
2+
* Copyright 2016-2018, 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 eventDispatcher = require('./index.node');
17-
var chai = require('chai');
18-
var assert = chai.assert;
19-
var nock = require('nock');
20-
var sinon = require('sinon');
16+
import nock from 'nock';
17+
import sinon from 'sinon';
18+
import { assert } from 'chai';
19+
20+
import { dispatchEvent } from './index.node';
2121

2222
describe('lib/plugins/event_dispatcher/node', function() {
2323
describe('APIs', function() {
@@ -49,7 +49,7 @@ describe('lib/plugins/event_dispatcher/node', function() {
4949
httpVerb: 'POST',
5050
};
5151

52-
eventDispatcher.dispatchEvent(eventObj, function(resp) {
52+
dispatchEvent(eventObj, function(resp) {
5353
assert.equal(200, resp.statusCode);
5454
done();
5555
});
@@ -64,8 +64,7 @@ describe('lib/plugins/event_dispatcher/node', function() {
6464
httpVerb: 'POST',
6565
};
6666

67-
eventDispatcher
68-
.dispatchEvent(eventObj, stubCallback.callback)
67+
dispatchEvent(eventObj, stubCallback.callback)
6968
.on('response', function(response) {
7069
sinon.assert.calledOnce(stubCallback.callback);
7170
done();
@@ -85,7 +84,7 @@ describe('lib/plugins/event_dispatcher/node', function() {
8584
};
8685

8786
var callback = sinon.spy();
88-
eventDispatcher.dispatchEvent(eventObj, callback);
87+
dispatchEvent(eventObj, callback);
8988
sinon.assert.notCalled(callback);
9089
});
9190
});
@@ -98,7 +97,7 @@ describe('lib/plugins/event_dispatcher/node', function() {
9897
};
9998

10099
var callback = sinon.spy();
101-
eventDispatcher.dispatchEvent(eventObj, callback);
100+
dispatchEvent(eventObj, callback);
102101
sinon.assert.notCalled(callback);
103102
});
104103
});

0 commit comments

Comments
 (0)