Skip to content

Commit 848dfaa

Browse files
committed
converted plugins to es6
1 parent 1b71750 commit 848dfaa

File tree

13 files changed

+156
-160
lines changed

13 files changed

+156
-160
lines changed
Lines changed: 4 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,6 @@
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+
}

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

Lines changed: 4 additions & 4 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,16 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var errorHandler = require('./');
16+
import { handleError } from './';
1717

18-
var chai = require('chai');
18+
import chai from 'chai';
1919
var assert = chai.assert;
2020

2121
describe('lib/plugins/error_handler', function() {
2222
describe('APIs', function() {
2323
describe('handleError', function() {
2424
it('should just be a no-op function', function() {
25-
assert.isFunction(errorHandler.handleError);
25+
assert.isFunction(handleError);
2626
});
2727
});
2828
});
Lines changed: 44 additions & 46 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,58 +17,56 @@ 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 const 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

68-
var toQueryString = function(obj) {
66+
function toQueryString(obj) {
6967
return Object.keys(obj)
7068
.map(function(k) {
7169
return encodeURIComponent(k) + '=' + encodeURIComponent(obj[k]);
7270
})
7371
.join('&');
74-
};
72+
}

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

Lines changed: 7 additions & 8 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');
16+
import { dispatchEvent } from './index.browser';
17+
import chai from 'chai';
18+
import sinon from 'sinon';
1819
var assert = chai.assert;
19-
var sinon = require('sinon');
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: 41 additions & 43 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,49 @@
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 const 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+
};
5554

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-
};
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+
}

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

Lines changed: 10 additions & 10 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,12 @@
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');
16+
import { dispatchEvent } from './index.node';
17+
import nock from 'nock';
18+
import sinon from 'sinon';
19+
import chai from 'chai';
20+
1821
var assert = chai.assert;
19-
var nock = require('nock');
20-
var sinon = require('sinon');
2122

2223
describe('lib/plugins/event_dispatcher/node', function() {
2324
describe('APIs', function() {
@@ -49,7 +50,7 @@ describe('lib/plugins/event_dispatcher/node', function() {
4950
httpVerb: 'POST',
5051
};
5152

52-
eventDispatcher.dispatchEvent(eventObj, function(resp) {
53+
dispatchEvent(eventObj, function(resp) {
5354
assert.equal(200, resp.statusCode);
5455
done();
5556
});
@@ -64,8 +65,7 @@ describe('lib/plugins/event_dispatcher/node', function() {
6465
httpVerb: 'POST',
6566
};
6667

67-
eventDispatcher
68-
.dispatchEvent(eventObj, stubCallback.callback)
68+
dispatchEvent(eventObj, stubCallback.callback)
6969
.on('response', function(response) {
7070
sinon.assert.calledOnce(stubCallback.callback);
7171
done();
@@ -85,7 +85,7 @@ describe('lib/plugins/event_dispatcher/node', function() {
8585
};
8686

8787
var callback = sinon.spy();
88-
eventDispatcher.dispatchEvent(eventObj, callback);
88+
dispatchEvent(eventObj, callback);
8989
sinon.assert.notCalled(callback);
9090
});
9191
});
@@ -98,7 +98,7 @@ describe('lib/plugins/event_dispatcher/node', function() {
9898
};
9999

100100
var callback = sinon.spy();
101-
eventDispatcher.dispatchEvent(eventObj, callback);
101+
dispatchEvent(eventObj, callback);
102102
sinon.assert.notCalled(callback);
103103
});
104104
});

packages/optimizely-sdk/lib/plugins/logger/enums.js

Lines changed: 4 additions & 2 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,4 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
exports.LOG_LEVEL = require('@optimizely/js-sdk-logging').LogLevel;
16+
import { LogLevel } from '@optimizely/js-sdk-logging';
17+
18+
export const LOG_LEVEL = LogLevel;

0 commit comments

Comments
 (0)