1
1
/**
2
- * Copyright 2016-2017, 2020, Optimizely
2
+ * Copyright 2016-2017, 2020-2021 , Optimizely
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
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
+
19
27
20
28
/**
21
29
* Sample event dispatcher implementation for tracking impression and conversions
22
30
* Users of the SDK can provide their own implementation
23
- * @param {Object } eventObj
31
+ * @param {Event } eventObj
24
32
* @param {Function } callback
25
33
*/
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 ;
30
41
if ( eventObj . httpVerb === POST_METHOD ) {
31
42
req = new XMLHttpRequest ( ) ;
32
43
req . open ( POST_METHOD , url , true ) ;
@@ -53,7 +64,7 @@ export var dispatchEvent = function(eventObj, callback) {
53
64
req . onreadystatechange = function ( ) {
54
65
if ( req . readyState === READYSTATE_COMPLETE && callback && typeof callback === 'function' ) {
55
66
try {
56
- callback ( ) ;
67
+ callback ( { statusCode : req . status } ) ;
57
68
} catch ( e ) {
58
69
// TODO: Log this somehow (consider adding a logger to the EventDispatcher interface)
59
70
}
@@ -63,7 +74,8 @@ export var dispatchEvent = function(eventObj, callback) {
63
74
}
64
75
}
65
76
66
- var toQueryString = function ( obj ) {
77
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
78
+ const toQueryString = function ( obj : any ) : string {
67
79
return Object . keys ( obj )
68
80
. map ( function ( k ) {
69
81
return encodeURIComponent ( k ) + '=' + encodeURIComponent ( obj [ k ] ) ;
0 commit comments