From 7743b8018051c45d0a125402cfbf8cf37a68feb4 Mon Sep 17 00:00:00 2001 From: Victor Quinn Date: Fri, 31 May 2024 17:50:18 -0400 Subject: [PATCH] Add types to more of the responses --- src/providers/nws/client.ts | 22 +- src/providers/nws/interfaces.ts | 345 +++++++++++++----- ...stations.json => gridpoints-stations.json} | 0 3 files changed, 265 insertions(+), 102 deletions(-) rename src/providers/nws/responses/{stations.json => gridpoints-stations.json} (100%) diff --git a/src/providers/nws/client.ts b/src/providers/nws/client.ts index 60c1bc7..7b53f68 100644 --- a/src/providers/nws/client.ts +++ b/src/providers/nws/client.ts @@ -1,15 +1,11 @@ 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); @@ -17,22 +13,28 @@ export async function getWeather(lat: number, lng: number) { 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 { const url = `https://api.weather.gov/points/${lat},${lng}`; log(`URL: ${url}`); - const response = await axios.get(url); + const response = await axios.get(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 { - const stationResponse = await axios.get(observationStations); - const stationData = await stationResponse.data; - return stationData.features[0].id; + const stationResponse = await axios.get(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 { const closestStation = `${stationId}/observations/latest`; - const observationResponse = await axios.get(closestStation); + const observationResponse = await axios.get(closestStation); return observationResponse.data; } diff --git a/src/providers/nws/interfaces.ts b/src/providers/nws/interfaces.ts index a954651..0254310 100644 --- a/src/providers/nws/interfaces.ts +++ b/src/providers/nws/interfaces.ts @@ -1,95 +1,256 @@ export interface IGeoJsonContext { - "@version"?: string; - wx?: string; - s?: string; - geo?: string; - unit?: string; - "@vocab"?: string; - geometry?: { - "@id": string; - "@type": string; - }; - city?: string; - state?: string; - distance?: { - "@id": string; - "@type": string; - }; - bearing?: { - "@type": string; - }; - value?: { - "@id": string; - }; - unitCode?: { - "@id": string; - "@type": string; - }; - forecastOffice?: { - "@type": string; - }; - forecastGridData?: { - "@type": string; - }; - publicZone?: { - "@type": string; - }; - county?: { - "@type": string; - }; - } - - export interface IGeometry { - type: string; - coordinates: number[]; - } - - export interface IDistance { - unitCode: string; - value: number; - } - - export interface IBearing { - unitCode: string; - value: number; - } - - export interface IRelativeLocation { - type: string; - geometry: IGeometry; - properties: { - city: string; - state: string; - distance: IDistance; - bearing: IBearing; - }; - } - - export interface IProperties { + "@version"?: string; + wx?: string; + s?: string; + geo?: string; + unit?: string; + "@vocab"?: string; + geometry?: { "@id": string; "@type": string; - cwa: string; - forecastOffice: string; - gridId: string; - gridX: number; - gridY: number; - forecast: string; - forecastHourly: string; - forecastGridData: string; - observationStations: string; - relativeLocation: IRelativeLocation; - forecastZone: string; - county: string; - fireWeatherZone: string; - timeZone: string; - radarStation: string; - } - - export interface IWeatherData { - "@context": (string | IGeoJsonContext)[]; - id: string; - type: string; - geometry: IGeometry; - properties: IProperties; - } - \ No newline at end of file + }; + city?: string; + state?: string; + distance?: IDistance; + bearing?: IBearing; + value?: IValue; + unitCode?: IUnitCode; + forecastOffice?: { + "@type": string; + }; + forecastGridData?: { + "@type": string; + }; + publicZone?: { + "@type": string; + }; + county?: { + "@type": string; + }; +} + +export interface IGeometry { + type: string; + coordinates: number[]; +} + +export interface IDistance { + unitCode: string; + value: number; +} + +export interface IBearing { + unitCode: string; + value: number; +} + +export interface IRelativeLocation { + type: string; + geometry: IGeometry; + properties: { + city: string; + state: string; + distance: IDistance; + bearing: IBearing; + }; +} + +export interface IProperties { + "@id": string; + "@type": string; + cwa: string; + forecastOffice: string; + gridId: string; + gridX: number; + gridY: number; + forecast: string; + forecastHourly: string; + forecastGridData: string; + observationStations: string; + relativeLocation: IRelativeLocation; + forecastZone: string; + county: string; + fireWeatherZone: string; + timeZone: string; + radarStation: string; +} + + +export interface IWeatherData { + "@context": (string | IGeoJsonContext)[]; + id: string; + type: string; + geometry: IGeometry; + properties: IProperties; +} + +export interface IGeometry { + type: string; + coordinates: number[]; +} + +export interface IElevation { + unitCode: string; + value: number; +} + +export interface IProperties { + "@id": string; + "@type": string; + elevation: IElevation; + stationIdentifier: string; + name: string; + timeZone: string; + forecast: string; + county: string; + fireWeatherZone: string; +} + +export interface IFeature { + id: string; + type: string; + geometry: IGeometry; + properties: IProperties; +} + +export interface IPagination { + next: string; +} + +export interface IPointsLatLngResponse { + properties: IProperties; +} + +export interface IGridpointsStations { + "@context": (string | IGeoJsonContext)[]; + type: string; + features: IFeature[]; + observationStations: string[]; + pagination: IPagination; +} + +export interface IValue { + "@id": string; +} + +export interface IUnitCode { + "@id": string; + "@type": string; +} + +export interface IGeometry { + type: string; + coordinates: number[]; +} + +export interface IElevation { + unitCode: string; + value: number; +} + +export interface ITemperature { + unitCode: string; + value: number; + qualityControl: string; +} + +export interface IDewpoint { + unitCode: string; + value: number; + qualityControl: string; +} + +export interface IWindDirection { + unitCode: string; + value: number | null; + qualityControl: string; +} + +export interface IWindSpeed { + unitCode: string; + value: number; + qualityControl: string; +} + +export interface IWindGust { + unitCode: string; + value: number | null; + qualityControl: string; +} + +export interface IBarometricPressure { + unitCode: string; + value: number; + qualityControl: string; +} + +export interface ISeaLevelPressure { + unitCode: string; + value: number; + qualityControl: string; +} + +export interface IVisibility { + unitCode: string; + value: number; + qualityControl: string; +} + +export interface IRelativeHumidity { + unitCode: string; + value: number; + qualityControl: string; +} + +export interface IHeatIndex { + unitCode: string; + value: number; + qualityControl: string; +} + +export interface ICloudLayerBase { + unitCode: string; + value: number | null; +} + +export interface ICloudLayer { + base: ICloudLayerBase; + amount: string; +} + +export interface IProperties { + "@id": string; + "@type": string; + elevation: IElevation; + station: string; + timestamp: string; + rawMessage: string; + textDescription: string; + icon: string; + presentWeather: any[]; + temperature: ITemperature; + dewpoint: IDewpoint; + windDirection: IWindDirection; + windSpeed: IWindSpeed; + windGust: IWindGust; + barometricPressure: IBarometricPressure; + seaLevelPressure: ISeaLevelPressure; + visibility: IVisibility; + maxTemperatureLast24Hours: ITemperature | null; + minTemperatureLast24Hours: ITemperature | null; + precipitationLastHour: ITemperature | null; + precipitationLast3Hours: ITemperature | null; + precipitationLast6Hours: ITemperature | null; + relativeHumidity: IRelativeHumidity; + windChill: ITemperature | null; + heatIndex: IHeatIndex; + cloudLayers: ICloudLayer[]; +} + +export interface IObservationsLatest { + "@context": (string | IGeoJsonContext)[]; + id: string; + type: string; + geometry: IGeometry; + properties: IProperties; +} diff --git a/src/providers/nws/responses/stations.json b/src/providers/nws/responses/gridpoints-stations.json similarity index 100% rename from src/providers/nws/responses/stations.json rename to src/providers/nws/responses/gridpoints-stations.json