Skip to content

Commit

Permalink
Add types to more of the responses
Browse files Browse the repository at this point in the history
  • Loading branch information
victorquinn committed May 31, 2024
1 parent f2b470d commit 7743b80
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 102 deletions.
22 changes: 12 additions & 10 deletions src/providers/nws/client.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
import axios from 'axios';
import debug from 'debug';
import { WeatherData } from '../../types';
import { IWeatherData, IGeoJsonContext, IProperties } from './interfaces';
import { IGridpointsStations, IPointsLatLngResponse, IObservationsLatest } from './interfaces';


const log = debug('weather-plus:nws:client');

interface IStationResponse {
properties: IProperties;
}

export async function getWeather(lat: number, lng: number) {
const observationStations = await fetchObservationStationUrl(lat, lng);
const stationId = await fetchNearbyStations(observationStations);
const observation = await fetchLatestObservation(stationId);
return convertToWeatherData(observation);
}

// Fetch the observation station URL from the Weather.gov API
// https://api.weather.gov/points/40.7128,-74.0060
export async function fetchObservationStationUrl(lat: number, lng: number): Promise<string> {
const url = `https://api.weather.gov/points/${lat},${lng}`;
log(`URL: ${url}`);
const response = await axios.get<IStationResponse>(url);
const response = await axios.get<IPointsLatLngResponse>(url);
return response.data.properties.observationStations;
}

// Fetch the nearby stations from the Weather.gov API
// https://api.weather.gov/gridpoints/OKX/33,35/stations
export async function fetchNearbyStations(observationStations: string): Promise<string> {
const stationResponse = await axios.get(observationStations);
const stationData = await stationResponse.data;
return stationData.features[0].id;
const stationResponse = await axios.get<IGridpointsStations>(observationStations);
const stationUrl = stationResponse.data.features[0].id;
return stationUrl;
}

// Fetch the latest observation from the Weather.gov API
// https://api.weather.gov/stations/KNYC/observations/latest
export async function fetchLatestObservation(stationId: string): Promise<any> {
const closestStation = `${stationId}/observations/latest`;
const observationResponse = await axios.get(closestStation);
const observationResponse = await axios.get<IObservationsLatest>(closestStation);
return observationResponse.data;
}

Expand Down
Loading

0 comments on commit 7743b80

Please sign in to comment.