11/**
2- * Copyright 2016-2018, 2020, Optimizely
2+ * Copyright 2016-2018, 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.
@@ -17,26 +17,31 @@ import http from 'http';
1717import https from 'https' ;
1818import url from 'url' ;
1919
20+ import { Event } from '../../shared_types' ;
21+
2022/**
2123 * 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)
2830 */
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 {
3035 // Non-POST requests not supported
3136 if ( eventObj . httpVerb !== 'POST' ) {
3237 return ;
3338 }
3439
35- var parsedUrl = url . parse ( eventObj . url ) ;
40+ const parsedUrl = url . parse ( eventObj . url ) ;
3641
37- var dataString = JSON . stringify ( eventObj . params ) ;
42+ const dataString = JSON . stringify ( eventObj . params ) ;
3843
39- var requestOptions = {
44+ const requestOptions = {
4045 host : parsedUrl . host ,
4146 path : parsedUrl . path ,
4247 method : 'POST' ,
@@ -46,13 +51,14 @@ export var dispatchEvent = function(eventObj, callback) {
4651 } ,
4752 } ;
4853
49- var requestCallback = function ( response ) {
54+ const requestCallback = function ( response ?: { statusCode : number } ) : void {
5055 if ( response && response . statusCode && response . statusCode >= 200 && response . statusCode < 400 ) {
5156 callback ( response ) ;
5257 }
5358 } ;
5459
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 ) ;
5662 // Add no-op error listener to prevent this from throwing
5763 req . on ( 'error' , function ( ) { } ) ;
5864 req . write ( dataString ) ;
0 commit comments