Skip to content

Commit 8450d46

Browse files
authored
feat: Convert event_dispatcher browser plugin to TS (#675)
* Convert index.browser.ts * Incorporate comment
1 parent 10d0bfa commit 8450d46

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

packages/optimizely-sdk/lib/plugins/event_dispatcher/index.browser.js renamed to packages/optimizely-sdk/lib/plugins/event_dispatcher/index.browser.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017, 2020, Optimizely
2+
* Copyright 2016-2017, 2020-2021, 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,20 +13,31 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
var POST_METHOD = 'POST';
17-
var GET_METHOD = 'GET';
18-
var READYSTATE_COMPLETE = 4;
16+
const POST_METHOD = 'POST';
17+
const GET_METHOD = 'GET';
18+
const READYSTATE_COMPLETE = 4;
19+
20+
interface Event {
21+
url: string;
22+
httpVerb: 'POST' | 'GET';
23+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
24+
params: any;
25+
}
26+
1927

2028
/**
2129
* Sample event dispatcher implementation for tracking impression and conversions
2230
* Users of the SDK can provide their own implementation
23-
* @param {Object} eventObj
31+
* @param {Event} eventObj
2432
* @param {Function} callback
2533
*/
26-
export var dispatchEvent = function(eventObj, callback) {
27-
var url = eventObj.url;
28-
var params = eventObj.params;
29-
var req;
34+
export const dispatchEvent = function(
35+
eventObj: Event,
36+
callback: (response: { statusCode: number; }) => void
37+
): void {
38+
const params = eventObj.params;
39+
let url: string = eventObj.url;
40+
let req: XMLHttpRequest;
3041
if (eventObj.httpVerb === POST_METHOD) {
3142
req = new XMLHttpRequest();
3243
req.open(POST_METHOD, url, true);
@@ -53,7 +64,7 @@ export var dispatchEvent = function(eventObj, callback) {
5364
req.onreadystatechange = function() {
5465
if (req.readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function') {
5566
try {
56-
callback();
67+
callback({ statusCode: req.status });
5768
} catch (e) {
5869
// TODO: Log this somehow (consider adding a logger to the EventDispatcher interface)
5970
}
@@ -63,7 +74,8 @@ export var dispatchEvent = function(eventObj, callback) {
6374
}
6475
}
6576

66-
var toQueryString = function(obj) {
77+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
78+
const toQueryString = function(obj: any): string {
6779
return Object.keys(obj)
6880
.map(function(k) {
6981
return encodeURIComponent(k) + '=' + encodeURIComponent(obj[k]);

0 commit comments

Comments
 (0)