1
1
/**
2
- * Copyright 2016-2018, 2020, Optimizely
2
+ * Copyright 2016-2018, 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.
@@ -17,26 +17,31 @@ import http from 'http';
17
17
import https from 'https' ;
18
18
import url from 'url' ;
19
19
20
+ import { Event } from '../../shared_types' ;
21
+
20
22
/**
21
23
* 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)
24
+ * @param { Event } eventObj Event object containing
25
+ * @param {string } eventObj.url the url to make the request to
26
+ * @param {Object } eventObj.params parameters to pass to the request (i.e. in the POST body)
27
+ * @param {string } eventObj.httpVerb the HTTP request method type. only POST is supported.
28
+ * @param {function } callback callback to execute
29
+ * @return {ClientRequest|undefined } ClientRequest object which made the request, or undefined if no request was made (error)
28
30
*/
29
- export var dispatchEvent = function ( eventObj , callback ) {
31
+ export const dispatchEvent = function (
32
+ eventObj : Event ,
33
+ callback : ( response : { statusCode : number } ) => void
34
+ ) : http . ClientRequest | void {
30
35
// Non-POST requests not supported
31
36
if ( eventObj . httpVerb !== 'POST' ) {
32
37
return ;
33
38
}
34
39
35
- var parsedUrl = url . parse ( eventObj . url ) ;
40
+ const parsedUrl = url . parse ( eventObj . url ) ;
36
41
37
- var dataString = JSON . stringify ( eventObj . params ) ;
42
+ const dataString = JSON . stringify ( eventObj . params ) ;
38
43
39
- var requestOptions = {
44
+ const requestOptions = {
40
45
host : parsedUrl . host ,
41
46
path : parsedUrl . path ,
42
47
method : 'POST' ,
@@ -46,13 +51,14 @@ export var dispatchEvent = function(eventObj, callback) {
46
51
} ,
47
52
} ;
48
53
49
- var requestCallback = function ( response ) {
54
+ const requestCallback = function ( response ?: { statusCode : number } ) : void {
50
55
if ( response && response . statusCode && response . statusCode >= 200 && response . statusCode < 400 ) {
51
56
callback ( response ) ;
52
57
}
53
58
} ;
54
59
55
- var req = ( parsedUrl . protocol === 'http:' ? http : https ) . request ( requestOptions , requestCallback ) ;
60
+ const req = ( parsedUrl . protocol === 'http:' ? http : https )
61
+ . request ( requestOptions , requestCallback as ( res : http . IncomingMessage ) => void ) ;
56
62
// Add no-op error listener to prevent this from throwing
57
63
req . on ( 'error' , function ( ) { } ) ;
58
64
req . write ( dataString ) ;
0 commit comments