Update getData and httpRequest to modern TypeScript

This commit is contained in:
Matthew Oslan
2019-05-09 13:20:49 -04:00
parent 6acb40eed1
commit f2f40d2253

View File

@@ -14,16 +14,19 @@ var http = require( "http" ),
// If location does not match GPS or PWS/ICAO, then attempt to resolve
// location using Weather Underground autocomplete API
function resolveCoordinates( location, callback ) {
async function resolveCoordinates( location, callback ) {
// Generate URL for autocomplete request
var url = "http://autocomplete.wunderground.com/aq?h=0&query=" +
encodeURIComponent( location );
httpRequest( url, function( data ) {
// Parse the reply for JSON data
data = JSON.parse( data );
let data;
try {
data = await getData( url );
} catch (err) {
// If the request fails, indicate no data was found.
callback( false );
}
// Check if the data is valid
if ( typeof data.RESULTS === "object" && data.RESULTS.length && data.RESULTS[ 0 ].tz !== "MISSING" ) {
@@ -35,21 +38,22 @@ function resolveCoordinates( location, callback ) {
// Otherwise, indicate no data was found
callback( false );
}
} );
}
function getData( url, callback ) {
httpRequest( url, function( data ) {
/**
* Makes an HTTP GET request to the specified URL and parses the JSON response body.
* @param url The URL to fetch.
* @return A Promise that will be resolved the with parsed response body if the request succeeds, or will be rejected
* with an Error if the request or JSON parsing fails.
*/
async function getData( url: string ): Promise< any > {
try {
data = JSON.parse( data );
const data: string = await httpRequest(url);
return JSON.parse(data);
} catch (err) {
callback( {} );
return;
// Reject the promise if there was an error making the request or parsing the JSON.
throw err;
}
callback( data );
return;
} );
}
// Retrieve data from Open Weather Map for water level calculations
@@ -57,11 +61,19 @@ function getOWMWateringData( location, callback ) {
var OWM_API_KEY = process.env.OWM_API_KEY,
forecastUrl = "http://api.openweathermap.org/data/2.5/forecast?appid=" + OWM_API_KEY + "&units=imperial&lat=" + location[ 0 ] + "&lon=" + location[ 1 ];
getTimeData( location, function( weather ) {
getTimeData( location, async function( weather ) {
// Perform the HTTP request to retrieve the weather data
getData( forecastUrl, function( forecast ) {
let forecast;
try {
forecast = await getData( forecastUrl );
} catch (err) {
// Return just the time data if retrieving the forecast fails.
callback( weather );
return;
}
// Return just the time data if the forecast data is incomplete.
if ( !forecast || !forecast.list ) {
callback( weather );
return;
@@ -85,7 +97,6 @@ function getOWMWateringData( location, callback ) {
callback( weather );
} );
} );
}
// Retrieve weather data from Open Weather Map for App
@@ -94,12 +105,19 @@ function getOWMWeatherData( location, callback ) {
currentUrl = "http://api.openweathermap.org/data/2.5/weather?appid=" + OWM_API_KEY + "&units=imperial&lat=" + location[ 0 ] + "&lon=" + location[ 1 ],
forecastDailyUrl = "http://api.openweathermap.org/data/2.5/forecast/daily?appid=" + OWM_API_KEY + "&units=imperial&lat=" + location[ 0 ] + "&lon=" + location[ 1 ];
getTimeData( location, function( weather ) {
getTimeData( location, async function( weather ) {
getData( currentUrl, function( current ) {
getData( forecastDailyUrl, function( forecast ) {
let current, forecast;
try {
current = await getData( currentUrl );
forecast = await getData( forecastDailyUrl );
} catch (err) {
// Return just the time data if retrieving weather data fails.
callback( weather );
return;
}
// Return just the time data if the weather data is incomplete.
if ( !current || !current.main || !current.wind || !current.weather || !forecast || !forecast.list ) {
callback( weather );
return;
@@ -130,8 +148,6 @@ function getOWMWeatherData( location, callback ) {
callback( weather );
} );
} );
} );
}
// Calculate timezone and sun rise/set information
@@ -386,33 +402,40 @@ exports.getWateringData = function( req, res ) {
}
};
// Generic HTTP request handler that parses the URL and uses the
// native Node.js http module to perform the request
function httpRequest( url, callback ) {
url = url.match( filters.url );
/**
* Makes an HTTP GET request to the specified URL and returns the response body.
* @param url The URL to fetch.
* @return A Promise that will be resolved the with response body if the request succeeds, or will be rejected with an
* Error if the request fails.
*/
async function httpRequest( url: string ): Promise< string > {
return new Promise< any >( ( resolve, reject ) => {
var options = {
host: url[ 1 ],
port: url[ 2 ] || 80,
path: url[ 3 ]
const splitUrl: string[] = url.match( filters.url );
const options = {
host: splitUrl[ 1 ],
port: splitUrl[ 2 ] || 80,
path: splitUrl[ 3 ]
};
http.get( options, function( response ) {
var data = "";
http.get( options, ( response ) => {
let data = "";
// Reassemble the data as it comes in
response.on( "data", function( chunk ) {
response.on( "data", ( chunk ) => {
data += chunk;
} );
// Once the data is completely received, return it to the callback
response.on( "end", function() {
callback( data );
// Once the data is completely received, resolve the promise
response.on( "end", () => {
resolve( data );
} );
} ).on( "error", function() {
} ).on( "error", ( err ) => {
// If the HTTP request fails, return false
callback( false );
// If the HTTP request fails, reject the promise
reject( err );
} );
} );
}