Update getWeatherData to modern TypeScript

This commit is contained in:
Matthew Oslan
2019-05-10 12:57:22 -04:00
parent bef43c9453
commit b11a125528

View File

@@ -1,3 +1,5 @@
import * as express from "express";
var http = require( "http" ), var http = require( "http" ),
SunCalc = require( "suncalc" ), SunCalc = require( "suncalc" ),
moment = require( "moment-timezone" ), moment = require( "moment-timezone" ),
@@ -252,40 +254,34 @@ function checkWeatherRestriction( adjustmentValue, weather ) {
return false; return false;
} }
exports.getWeatherData = async function( req, res ) { exports.getWeatherData = async function( req: express.Request, res: express.Response ) {
var location = req.query.loc; const location: string = getParameter(req.query.loc);
let coordinates: GeoCoordinates;
if ( filters.gps.test( location ) ) { if ( filters.gps.test( location ) ) {
// Handle GPS coordinates by storing each coordinate in an array // Handle GPS coordinates by storing each coordinate in an array
location = location.split( "," ); const split: string[] = location.split( "," );
location = [ parseFloat( location[ 0 ] ), parseFloat( location[ 1 ] ) ]; coordinates = [ parseFloat( split[ 0 ] ), parseFloat( split[ 1 ] ) ];
// Continue with the weather request
const weatherData: OWMWeatherData | TimeData = await getOWMWeatherData( location );
res.json( {
...weatherData,
location: location
} );
} else { } else {
// Attempt to resolve provided location to GPS coordinates when it does not match // Attempt to resolve provided location to GPS coordinates when it does not match
// a GPS coordinate or Weather Underground location using Weather Underground autocomplete // a GPS coordinate or Weather Underground location using Weather Underground autocomplete
let coordinates: GeoCoordinates;
try { try {
coordinates = await resolveCoordinates( location ); coordinates = await resolveCoordinates( location );
} catch (err) { } catch (err) {
res.send( "Error: Unable to resolve location" ); res.send( "Error: Unable to resolve location" );
return; return;
} }
location = coordinates;
const weatherData: OWMWeatherData | TimeData = await getOWMWeatherData( location );
res.json( {
...weatherData,
location: location
} );
} }
// Continue with the weather request
const weatherData: OWMWeatherData | TimeData = await getOWMWeatherData( coordinates );
res.json( {
...weatherData,
location: coordinates
} );
}; };
// API Handler when using the weatherX.py where X represents the // API Handler when using the weatherX.py where X represents the
@@ -523,6 +519,23 @@ function ipToInt( ip ) {
return ( ( ( ( ( ( +ip[ 0 ] ) * 256 ) + ( +ip[ 1 ] ) ) * 256 ) + ( +ip[ 2 ] ) ) * 256 ) + ( +ip[ 3 ] ); return ( ( ( ( ( ( +ip[ 0 ] ) * 256 ) + ( +ip[ 1 ] ) ) * 256 ) + ( +ip[ 2 ] ) ) * 256 ) + ( +ip[ 3 ] );
} }
/**
* Returns a single value for a header/query parameter. If passed a single string, the same string will be returned. If
* an array of strings is passed, the first value will be returned. If this value is null/undefined, an empty string
* will be returned instead.
* @param parameter An array of parameters or a single parameter value.
* @return The first element in the array of parameter or the single parameter provided.
*/
function getParameter( parameter: string | string[] ): string {
if ( Array.isArray( parameter ) ) {
parameter = parameter[0];
}
// Return an empty string if the parameter is undefined.
return parameter || "";
}
/** Geographic coordinates. The 1st element is the latitude, and the 2nd element is the longitude. */ /** Geographic coordinates. The 1st element is the latitude, and the 2nd element is the longitude. */
type GeoCoordinates = [number, number]; type GeoCoordinates = [number, number];