8000 feat: Convert event dispatcher node plugin to TS (#677) · piotrski/javascript-sdk@14e35b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 14e35b0

Browse files
authored
feat: Convert event dispatcher node plugin to TS (optimizely#677)
* WIP * Finish conversion * Specify baseUrl and paths instead of types for custom type definitions * NIT
1 parent 8450d46 commit 14e35b0

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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';
1717
import https from 'https';
1818
import 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);

packages/optimizely-sdk/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"extends": "../../tsconfig.json",
33
"compilerOptions": {
4-
"types": ["./typings"],
4+
"baseUrl": "./",
5+
"paths": {
6+
"*" : ["*", "./typings/*"]
7+
},
58
"allowJs": true,
69
"declaration": false,
710
"module": "esnext",

0 commit comments

Comments
 (0)
0