Automatically URL query format response

This commit is contained in:
Matthew Oslan
2019-06-09 12:07:09 -04:00
parent 0281a835e2
commit 3f748704e0

View File

@@ -266,17 +266,34 @@ export const getWateringData = async function( req: express.Request, res: expres
if ( outputFormat === "json" ) { if ( outputFormat === "json" ) {
res.json( data ); res.json( data );
} else { } else {
res.send( "&scale=" + data.scale + // Return the data formatted as a URL query string.
"&rd=" + data.rd + let formatted = "";
"&tz=" + data.tz + for ( const key in data ) {
"&sunrise=" + data.sunrise + // Skip inherited properties.
"&sunset=" + data.sunset + if ( !data.hasOwnProperty( key ) ) {
"&eip=" + data.eip + continue;
( data.rawData ? "&rawData=" + JSON.stringify( data.rawData ) : "" ) + }
( data.error ? "&error=" + encodeURIComponent( data.error ) : "" )
);
}
let value = data[ key ];
switch ( typeof value ) {
case "undefined":
// Skip undefined properties.
continue;
case "object":
// Convert objects to JSON.
value = JSON.stringify( value );
// Fallthrough.
case "string":
/* URL encode strings. Since the OS firmware uses a primitive version of query string parsing and
decoding, only some characters need to be escaped and only spaces ("+" or "%20") will be decoded. */
value = value.replace( / /g, "+" ).replace( /\n/g, "\\n" ).replace( /&/g, "AMPERSAND" );
break;
}
formatted += `&${ key }=${ value }`;
}
res.send( formatted );
}
}; };
/** /**