diff --git a/dist/cache.d.ts b/dist/cache.d.ts new file mode 100644 index 0000000..14fc118 --- /dev/null +++ b/dist/cache.d.ts @@ -0,0 +1,7 @@ +declare class Cache { + private items; + constructor(); + get(key: string): T | undefined; + set(key: string, value: T): void; +} +export default Cache; diff --git a/dist/cache.js b/dist/cache.js new file mode 100644 index 0000000..e71b391 --- /dev/null +++ b/dist/cache.js @@ -0,0 +1,14 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +class Cache { + constructor() { + this.items = {}; + } + get(key) { + return this.items[key]; + } + set(key, value) { + this.items[key] = value; + } +} +exports.default = Cache; diff --git a/dist/client.d.ts b/dist/client.d.ts index ff36d25..c1ad3e9 100644 --- a/dist/client.d.ts +++ b/dist/client.d.ts @@ -1,4 +1,4 @@ -import { ClientOptions, ForecastResponse, AlertsResponse, ForecastType, AlertOptions } from './types'; +import { ClientOptions, ForecastResponse, AlertsResponse, ForecastType, StationsResponse, Station, AlertOptions } from './types'; /** * The main client * @@ -8,6 +8,8 @@ import { ClientOptions, ForecastResponse, AlertsResponse, ForecastType, AlertOpt */ declare class Client { private options; + private pointCache; + private stationsCache; constructor(options?: ClientOptions); private getPath; private getUrl; @@ -15,7 +17,7 @@ declare class Client { getOptions(): ClientOptions; setOptions(newOptions: ClientOptions): void; /** - * Get weather alerts for a given area + * Get weather alerts for a given area. * * ```typescript * const active = true; @@ -26,7 +28,7 @@ declare class Client { */ getAlerts(active: boolean, options: AlertOptions): Promise; /** - * Get a weather forecast for a given latitude and longitude + * Get a weather forecast for a given latitude and longitude. * * ```typescript * const latitude = 35.6175667; @@ -36,5 +38,27 @@ declare class Client { * */ getForecast(latitude: number, longitude: number, forecastType: ForecastType): Promise; + /** + * Get the closest weather stations for a given latitude and longitude. + * + * ```typescript + * const latitude = 35.6175667; + * const longitude = -80.7709911; + * const stations = await client.getStations(latitude, longitude); + * ``` + * + */ + getStations(latitude: number, longitude: number): Promise; + /** + * Get the closest weather station for a given latitude and longitude. + * + * ```typescript + * const latitude = 35.6175667; + * const longitude = -80.7709911; + * const stationOrNull = await client.getNearestStation(latitude, longitude); + * ``` + * + */ + getNearestStation(latitude: number, longitude: number): Promise; } export { Client }; diff --git a/dist/client.js b/dist/client.js index c0391cb..6ef5857 100644 --- a/dist/client.js +++ b/dist/client.js @@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); exports.Client = void 0; const cross_fetch_1 = __importDefault(require("cross-fetch")); +const cache_1 = __importDefault(require("./cache")); const defaultOptions = { userAgent: 'weathered package' }; @@ -33,6 +34,8 @@ const processOptions = (options) => { class Client { constructor(options) { this.options = { ...defaultOptions, ...options }; + this.pointCache = new cache_1.default(); + this.stationsCache = new cache_1.default(); } getPath(path) { return this.getUrl(API_ROOT + path); @@ -41,9 +44,16 @@ class Client { const resp = await cross_fetch_1.default(url); return await resp.json(); } - getPoint(latitude, longitude) { + async getPoint(latitude, longitude) { + const cacheKey = `${latitude},${longitude}`; + const potentialPointResponse = this.pointCache.get(cacheKey); + if (potentialPointResponse) { + return potentialPointResponse; + } const path = `points/${latitude},${longitude}`; - return this.getPath(path); + const pointResponse = await this.getPath(path); + this.pointCache.set(cacheKey, pointResponse); + return pointResponse; } getOptions() { return { ...this.options }; @@ -52,7 +62,7 @@ class Client { this.options = { ...this.options, ...newOptions }; } /** - * Get weather alerts for a given area + * Get weather alerts for a given area. * * ```typescript * const active = true; @@ -67,7 +77,7 @@ class Client { return this.getPath(path); } /** - * Get a weather forecast for a given latitude and longitude + * Get a weather forecast for a given latitude and longitude. * * ```typescript * const latitude = 35.6175667; @@ -77,10 +87,48 @@ class Client { * */ async getForecast(latitude, longitude, forecastType) { - const pointResp = await this.getPoint(latitude, longitude); + const pointResponse = await this.getPoint(latitude, longitude); const forecastKey = forecastType === 'hourly' ? 'forecastHourly' : 'forecast'; - const url = pointResp.properties[forecastKey]; + const url = pointResponse.properties[forecastKey]; return this.getUrl(url); } + /** + * Get the closest weather stations for a given latitude and longitude. + * + * ```typescript + * const latitude = 35.6175667; + * const longitude = -80.7709911; + * const stations = await client.getStations(latitude, longitude); + * ``` + * + */ + async getStations(latitude, longitude) { + const pointResponse = await this.getPoint(latitude, longitude); + const stationsUrl = pointResponse.properties.observationStations; + const potentionalStationsResponse = this.stationsCache.get(stationsUrl); + if (potentionalStationsResponse) { + return potentionalStationsResponse; + } + const stationsResponse = await this.getUrl(stationsUrl); + this.stationsCache.set(stationsUrl, stationsResponse); + return stationsResponse; + } + /** + * Get the closest weather station for a given latitude and longitude. + * + * ```typescript + * const latitude = 35.6175667; + * const longitude = -80.7709911; + * const stationOrNull = await client.getNearestStation(latitude, longitude); + * ``` + * + */ + async getNearestStation(latitude, longitude) { + const stationsResponse = await this.getStations(latitude, longitude); + if (stationsResponse.features.length > 0) { + return stationsResponse.features[0]; + } + return null; + } } exports.Client = Client; diff --git a/dist/index.d.ts b/dist/index.d.ts index e70e441..96668e2 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,2 +1,2 @@ export { Client } from './client'; -export { ForecastType, Area, Region, RegionType, Urgency, AlertOptions, ClientOptions, PointResponse, ForecastResponse, AlertsResponse, AlertsFeature } from './types'; +export { ForecastType, Area, Region, RegionType, Urgency, AlertOptions, ClientOptions, PointResponse, ForecastResponse, ForecastProperties, AlertsResponse, Station, StationsResponse, AlertsFeature } from './types'; diff --git a/dist/point_cache.d.ts b/dist/point_cache.d.ts new file mode 100644 index 0000000..a15a753 --- /dev/null +++ b/dist/point_cache.d.ts @@ -0,0 +1,8 @@ +import { PointResponse } from './types'; +declare class PointCache { + private points; + constructor(); + get(latitude: number, longitude: number): PointResponse | undefined; + set(latitude: number, longitude: number, pointResponse: PointResponse): void; +} +export default PointCache; diff --git a/dist/point_cache.js b/dist/point_cache.js new file mode 100644 index 0000000..110cbd9 --- /dev/null +++ b/dist/point_cache.js @@ -0,0 +1,15 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const cacheKey = (lat, lng) => `${lat},${lng}`; +class PointCache { + constructor() { + this.points = {}; + } + get(latitude, longitude) { + return this.points[cacheKey(latitude, longitude)]; + } + set(latitude, longitude, pointResponse) { + this.points[cacheKey(latitude, longitude)] = pointResponse; + } +} +exports.default = PointCache; diff --git a/dist/types.d.ts b/dist/types.d.ts index 3272a91..6fa268c 100644 --- a/dist/types.d.ts +++ b/dist/types.d.ts @@ -31,8 +31,15 @@ declare type PointResponse = { properties: { forecast: string; forecastHourly: string; + observationStations: string; }; }; +declare type Station = { + id: string; +}; +declare type StationsResponse = { + features: Station[]; +}; declare type ForecastPeriod = { number: number; name: string; @@ -81,4 +88,4 @@ declare type AlertsFeature = { declare type AlertsResponse = { features: AlertsFeature[]; }; -export { ForecastType, Area, Region, RegionType, Urgency, AlertOptions, ClientOptions, PointResponse, ForecastResponse, AlertsResponse, AlertsFeature }; +export { ForecastType, Area, Region, RegionType, Urgency, AlertOptions, ClientOptions, PointResponse, ForecastResponse, ForecastProperties, Station, StationsResponse, AlertsResponse, AlertsFeature }; diff --git a/docs/assets/js/search.js b/docs/assets/js/search.js index 4671100..e7727cc 100644 --- a/docs/assets/js/search.js +++ b/docs/assets/js/search.js @@ -1 +1 @@ -window.searchData = {"kinds":{"128":"Class","512":"Constructor","1024":"Property","2048":"Method","65536":"Type literal","4194304":"Type alias"},"rows":[{"id":0,"kind":128,"name":"Client","url":"classes/client.html","classes":"tsd-kind-class"},{"id":1,"kind":512,"name":"constructor","url":"classes/client.html#constructor","classes":"tsd-kind-constructor tsd-parent-kind-class","parent":"Client"},{"id":2,"kind":2048,"name":"getOptions","url":"classes/client.html#getoptions","classes":"tsd-kind-method tsd-parent-kind-class","parent":"Client"},{"id":3,"kind":2048,"name":"setOptions","url":"classes/client.html#setoptions","classes":"tsd-kind-method tsd-parent-kind-class","parent":"Client"},{"id":4,"kind":2048,"name":"getAlerts","url":"classes/client.html#getalerts","classes":"tsd-kind-method tsd-parent-kind-class","parent":"Client"},{"id":5,"kind":2048,"name":"getForecast","url":"classes/client.html#getforecast","classes":"tsd-kind-method tsd-parent-kind-class","parent":"Client"},{"id":6,"kind":4194304,"name":"ForecastType","url":"modules.html#forecasttype","classes":"tsd-kind-type-alias"},{"id":7,"kind":4194304,"name":"Area","url":"modules.html#area","classes":"tsd-kind-type-alias"},{"id":8,"kind":4194304,"name":"Region","url":"modules.html#region","classes":"tsd-kind-type-alias"},{"id":9,"kind":4194304,"name":"RegionType","url":"modules.html#regiontype","classes":"tsd-kind-type-alias"},{"id":10,"kind":4194304,"name":"Urgency","url":"modules.html#urgency","classes":"tsd-kind-type-alias"},{"id":11,"kind":4194304,"name":"AlertOptions","url":"modules.html#alertoptions","classes":"tsd-kind-type-alias"},{"id":12,"kind":4194304,"name":"ClientOptions","url":"modules.html#clientoptions","classes":"tsd-kind-type-alias"},{"id":13,"kind":65536,"name":"__type","url":"modules.html#clientoptions.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias","parent":"ClientOptions"},{"id":14,"kind":1024,"name":"userAgent","url":"modules.html#clientoptions.__type.useragent","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ClientOptions.__type"},{"id":15,"kind":4194304,"name":"PointResponse","url":"modules.html#pointresponse","classes":"tsd-kind-type-alias"},{"id":16,"kind":65536,"name":"__type","url":"modules.html#pointresponse.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias","parent":"PointResponse"},{"id":17,"kind":1024,"name":"properties","url":"modules.html#pointresponse.__type.properties","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"PointResponse.__type"},{"id":18,"kind":65536,"name":"__type","url":"modules.html#pointresponse.__type.__type-1","classes":"tsd-kind-type-literal tsd-parent-kind-type-literal","parent":"PointResponse.__type"},{"id":19,"kind":1024,"name":"forecast","url":"modules.html#pointresponse.__type.__type-1.forecast","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"PointResponse.__type.__type"},{"id":20,"kind":1024,"name":"forecastHourly","url":"modules.html#pointresponse.__type.__type-1.forecasthourly","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"PointResponse.__type.__type"},{"id":21,"kind":4194304,"name":"ForecastResponse","url":"modules.html#forecastresponse","classes":"tsd-kind-type-alias"},{"id":22,"kind":65536,"name":"__type","url":"modules.html#forecastresponse.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias","parent":"ForecastResponse"},{"id":23,"kind":1024,"name":"properties","url":"modules.html#forecastresponse.__type.properties","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ForecastResponse.__type"},{"id":24,"kind":4194304,"name":"AlertsResponse","url":"modules.html#alertsresponse","classes":"tsd-kind-type-alias"},{"id":25,"kind":65536,"name":"__type","url":"modules.html#alertsresponse.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias","parent":"AlertsResponse"},{"id":26,"kind":1024,"name":"features","url":"modules.html#alertsresponse.__type.features","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsResponse.__type"},{"id":27,"kind":4194304,"name":"AlertsFeature","url":"modules.html#alertsfeature","classes":"tsd-kind-type-alias"},{"id":28,"kind":65536,"name":"__type","url":"modules.html#alertsfeature.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias","parent":"AlertsFeature"},{"id":29,"kind":1024,"name":"id","url":"modules.html#alertsfeature.__type.id","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type"},{"id":30,"kind":1024,"name":"geometry","url":"modules.html#alertsfeature.__type.geometry","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type"},{"id":31,"kind":65536,"name":"__type","url":"modules.html#alertsfeature.__type.__type-1","classes":"tsd-kind-type-literal tsd-parent-kind-type-literal","parent":"AlertsFeature.__type"},{"id":32,"kind":1024,"name":"type","url":"modules.html#alertsfeature.__type.__type-1.type","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"},{"id":33,"kind":1024,"name":"coordinates","url":"modules.html#alertsfeature.__type.__type-1.coordinates","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"},{"id":34,"kind":1024,"name":"properties","url":"modules.html#alertsfeature.__type.properties","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type"},{"id":35,"kind":65536,"name":"__type","url":"modules.html#alertsfeature.__type.__type-2","classes":"tsd-kind-type-literal tsd-parent-kind-type-literal","parent":"AlertsFeature.__type"},{"id":36,"kind":1024,"name":"areaDesc","url":"modules.html#alertsfeature.__type.__type-2.areadesc","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"},{"id":37,"kind":1024,"name":"sent","url":"modules.html#alertsfeature.__type.__type-2.sent","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"},{"id":38,"kind":1024,"name":"effective","url":"modules.html#alertsfeature.__type.__type-2.effective","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"},{"id":39,"kind":1024,"name":"expries","url":"modules.html#alertsfeature.__type.__type-2.expries","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"},{"id":40,"kind":1024,"name":"description","url":"modules.html#alertsfeature.__type.__type-2.description","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"}],"index":{"version":"2.3.9","fields":["name","parent"],"fieldVectors":[["name/0",[0,18.659]],["parent/0",[]],["name/1",[1,33.322]],["parent/1",[0,1.596]],["name/2",[2,33.322]],["parent/2",[0,1.596]],["name/3",[3,33.322]],["parent/3",[0,1.596]],["name/4",[4,33.322]],["parent/4",[0,1.596]],["name/5",[5,33.322]],["parent/5",[0,1.596]],["name/6",[6,33.322]],["parent/6",[]],["name/7",[7,33.322]],["parent/7",[]],["name/8",[8,33.322]],["parent/8",[]],["name/9",[9,33.322]],["parent/9",[]],["name/10",[10,33.322]],["parent/10",[]],["name/11",[11,33.322]],["parent/11",[]],["name/12",[12,28.214]],["parent/12",[]],["name/13",[13,15.976]],["parent/13",[12,2.413]],["name/14",[14,33.322]],["parent/14",[15,2.85]],["name/15",[16,28.214]],["parent/15",[]],["name/16",[13,15.976]],["parent/16",[16,2.413]],["name/17",[17,24.849]],["parent/17",[18,2.413]],["name/18",[13,15.976]],["parent/18",[18,2.413]],["name/19",[19,33.322]],["parent/19",[20,2.413]],["name/20",[21,33.322]],["parent/20",[20,2.413]],["name/21",[22,28.214]],["parent/21",[]],["name/22",[13,15.976]],["parent/22",[22,2.413]],["name/23",[17,24.849]],["parent/23",[23,2.85]],["name/24",[24,28.214]],["parent/24",[]],["name/25",[13,15.976]],["parent/25",[24,2.413]],["name/26",[25,33.322]],["parent/26",[26,2.85]],["name/27",[27,28.214]],["parent/27",[]],["name/28",[13,15.976]],["parent/28",[27,2.413]],["name/29",[28,33.322]],["parent/29",[29,1.739]],["name/30",[30,33.322]],["parent/30",[29,1.739]],["name/31",[13,15.976]],["parent/31",[29,1.739]],["name/32",[31,33.322]],["parent/32",[32,1.473]],["name/33",[33,33.322]],["parent/33",[32,1.473]],["name/34",[17,24.849]],["parent/34",[29,1.739]],["name/35",[13,15.976]],["parent/35",[29,1.739]],["name/36",[34,33.322]],["parent/36",[32,1.473]],["name/37",[35,33.322]],["parent/37",[32,1.473]],["name/38",[36,33.322]],["parent/38",[32,1.473]],["name/39",[37,33.322]],["parent/39",[32,1.473]],["name/40",[38,33.322]],["parent/40",[32,1.473]]],"invertedIndex":[["__type",{"_index":13,"name":{"13":{},"16":{},"18":{},"22":{},"25":{},"28":{},"31":{},"35":{}},"parent":{}}],["alertoptions",{"_index":11,"name":{"11":{}},"parent":{}}],["alertsfeature",{"_index":27,"name":{"27":{}},"parent":{"28":{}}}],["alertsfeature.__type",{"_index":29,"name":{},"parent":{"29":{},"30":{},"31":{},"34":{},"35":{}}}],["alertsfeature.__type.__type",{"_index":32,"name":{},"parent":{"32":{},"33":{},"36":{},"37":{},"38":{},"39":{},"40":{}}}],["alertsresponse",{"_index":24,"name":{"24":{}},"parent":{"25":{}}}],["alertsresponse.__type",{"_index":26,"name":{},"parent":{"26":{}}}],["area",{"_index":7,"name":{"7":{}},"parent":{}}],["areadesc",{"_index":34,"name":{"36":{}},"parent":{}}],["client",{"_index":0,"name":{"0":{}},"parent":{"1":{},"2":{},"3":{},"4":{},"5":{}}}],["clientoptions",{"_index":12,"name":{"12":{}},"parent":{"13":{}}}],["clientoptions.__type",{"_index":15,"name":{},"parent":{"14":{}}}],["constructor",{"_index":1,"name":{"1":{}},"parent":{}}],["coordinates",{"_index":33,"name":{"33":{}},"parent":{}}],["description",{"_index":38,"name":{"40":{}},"parent":{}}],["effective",{"_index":36,"name":{"38":{}},"parent":{}}],["expries",{"_index":37,"name":{"39":{}},"parent":{}}],["features",{"_index":25,"name":{"26":{}},"parent":{}}],["forecast",{"_index":19,"name":{"19":{}},"parent":{}}],["forecasthourly",{"_index":21,"name":{"20":{}},"parent":{}}],["forecastresponse",{"_index":22,"name":{"21":{}},"parent":{"22":{}}}],["forecastresponse.__type",{"_index":23,"name":{},"parent":{"23":{}}}],["forecasttype",{"_index":6,"name":{"6":{}},"parent":{}}],["geometry",{"_index":30,"name":{"30":{}},"parent":{}}],["getalerts",{"_index":4,"name":{"4":{}},"parent":{}}],["getforecast",{"_index":5,"name":{"5":{}},"parent":{}}],["getoptions",{"_index":2,"name":{"2":{}},"parent":{}}],["id",{"_index":28,"name":{"29":{}},"parent":{}}],["pointresponse",{"_index":16,"name":{"15":{}},"parent":{"16":{}}}],["pointresponse.__type",{"_index":18,"name":{},"parent":{"17":{},"18":{}}}],["pointresponse.__type.__type",{"_index":20,"name":{},"parent":{"19":{},"20":{}}}],["properties",{"_index":17,"name":{"17":{},"23":{},"34":{}},"parent":{}}],["region",{"_index":8,"name":{"8":{}},"parent":{}}],["regiontype",{"_index":9,"name":{"9":{}},"parent":{}}],["sent",{"_index":35,"name":{"37":{}},"parent":{}}],["setoptions",{"_index":3,"name":{"3":{}},"parent":{}}],["type",{"_index":31,"name":{"32":{}},"parent":{}}],["urgency",{"_index":10,"name":{"10":{}},"parent":{}}],["useragent",{"_index":14,"name":{"14":{}},"parent":{}}]],"pipeline":[]}} \ No newline at end of file +window.searchData = {"kinds":{"128":"Class","512":"Constructor","1024":"Property","2048":"Method","65536":"Type literal","4194304":"Type alias"},"rows":[{"id":0,"kind":128,"name":"Client","url":"classes/client.html","classes":"tsd-kind-class"},{"id":1,"kind":512,"name":"constructor","url":"classes/client.html#constructor","classes":"tsd-kind-constructor tsd-parent-kind-class","parent":"Client"},{"id":2,"kind":2048,"name":"getOptions","url":"classes/client.html#getoptions","classes":"tsd-kind-method tsd-parent-kind-class","parent":"Client"},{"id":3,"kind":2048,"name":"setOptions","url":"classes/client.html#setoptions","classes":"tsd-kind-method tsd-parent-kind-class","parent":"Client"},{"id":4,"kind":2048,"name":"getAlerts","url":"classes/client.html#getalerts","classes":"tsd-kind-method tsd-parent-kind-class","parent":"Client"},{"id":5,"kind":2048,"name":"getForecast","url":"classes/client.html#getforecast","classes":"tsd-kind-method tsd-parent-kind-class","parent":"Client"},{"id":6,"kind":2048,"name":"getStations","url":"classes/client.html#getstations","classes":"tsd-kind-method tsd-parent-kind-class","parent":"Client"},{"id":7,"kind":2048,"name":"getNearestStation","url":"classes/client.html#getneareststation","classes":"tsd-kind-method tsd-parent-kind-class","parent":"Client"},{"id":8,"kind":4194304,"name":"ForecastType","url":"modules.html#forecasttype","classes":"tsd-kind-type-alias"},{"id":9,"kind":4194304,"name":"Area","url":"modules.html#area","classes":"tsd-kind-type-alias"},{"id":10,"kind":4194304,"name":"Region","url":"modules.html#region","classes":"tsd-kind-type-alias"},{"id":11,"kind":4194304,"name":"RegionType","url":"modules.html#regiontype","classes":"tsd-kind-type-alias"},{"id":12,"kind":4194304,"name":"Urgency","url":"modules.html#urgency","classes":"tsd-kind-type-alias"},{"id":13,"kind":4194304,"name":"AlertOptions","url":"modules.html#alertoptions","classes":"tsd-kind-type-alias"},{"id":14,"kind":4194304,"name":"ClientOptions","url":"modules.html#clientoptions","classes":"tsd-kind-type-alias"},{"id":15,"kind":65536,"name":"__type","url":"modules.html#clientoptions.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias","parent":"ClientOptions"},{"id":16,"kind":1024,"name":"userAgent","url":"modules.html#clientoptions.__type.useragent","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ClientOptions.__type"},{"id":17,"kind":4194304,"name":"PointResponse","url":"modules.html#pointresponse","classes":"tsd-kind-type-alias"},{"id":18,"kind":65536,"name":"__type","url":"modules.html#pointresponse.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias","parent":"PointResponse"},{"id":19,"kind":1024,"name":"properties","url":"modules.html#pointresponse.__type.properties","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"PointResponse.__type"},{"id":20,"kind":65536,"name":"__type","url":"modules.html#pointresponse.__type.__type-1","classes":"tsd-kind-type-literal tsd-parent-kind-type-literal","parent":"PointResponse.__type"},{"id":21,"kind":1024,"name":"forecast","url":"modules.html#pointresponse.__type.__type-1.forecast","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"PointResponse.__type.__type"},{"id":22,"kind":1024,"name":"forecastHourly","url":"modules.html#pointresponse.__type.__type-1.forecasthourly","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"PointResponse.__type.__type"},{"id":23,"kind":1024,"name":"observationStations","url":"modules.html#pointresponse.__type.__type-1.observationstations","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"PointResponse.__type.__type"},{"id":24,"kind":4194304,"name":"ForecastResponse","url":"modules.html#forecastresponse","classes":"tsd-kind-type-alias"},{"id":25,"kind":65536,"name":"__type","url":"modules.html#forecastresponse.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias","parent":"ForecastResponse"},{"id":26,"kind":1024,"name":"properties","url":"modules.html#forecastresponse.__type.properties","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ForecastResponse.__type"},{"id":27,"kind":4194304,"name":"ForecastProperties","url":"modules.html#forecastproperties","classes":"tsd-kind-type-alias"},{"id":28,"kind":65536,"name":"__type","url":"modules.html#forecastproperties.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias","parent":"ForecastProperties"},{"id":29,"kind":1024,"name":"updated","url":"modules.html#forecastproperties.__type.updated","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ForecastProperties.__type"},{"id":30,"kind":1024,"name":"units","url":"modules.html#forecastproperties.__type.units","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ForecastProperties.__type"},{"id":31,"kind":1024,"name":"forecastGenerator","url":"modules.html#forecastproperties.__type.forecastgenerator","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ForecastProperties.__type"},{"id":32,"kind":1024,"name":"generatedAt","url":"modules.html#forecastproperties.__type.generatedat","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ForecastProperties.__type"},{"id":33,"kind":1024,"name":"updateTime","url":"modules.html#forecastproperties.__type.updatetime","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ForecastProperties.__type"},{"id":34,"kind":1024,"name":"validTimes","url":"modules.html#forecastproperties.__type.validtimes","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ForecastProperties.__type"},{"id":35,"kind":1024,"name":"elevation","url":"modules.html#forecastproperties.__type.elevation","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ForecastProperties.__type"},{"id":36,"kind":65536,"name":"__type","url":"modules.html#forecastproperties.__type.__type-1","classes":"tsd-kind-type-literal tsd-parent-kind-type-literal","parent":"ForecastProperties.__type"},{"id":37,"kind":1024,"name":"value","url":"modules.html#forecastproperties.__type.__type-1.value","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ForecastProperties.__type.__type"},{"id":38,"kind":1024,"name":"unitCode","url":"modules.html#forecastproperties.__type.__type-1.unitcode","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ForecastProperties.__type.__type"},{"id":39,"kind":1024,"name":"periods","url":"modules.html#forecastproperties.__type.periods","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"ForecastProperties.__type"},{"id":40,"kind":4194304,"name":"AlertsResponse","url":"modules.html#alertsresponse","classes":"tsd-kind-type-alias"},{"id":41,"kind":65536,"name":"__type","url":"modules.html#alertsresponse.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias","parent":"AlertsResponse"},{"id":42,"kind":1024,"name":"features","url":"modules.html#alertsresponse.__type.features","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsResponse.__type"},{"id":43,"kind":4194304,"name":"Station","url":"modules.html#station","classes":"tsd-kind-type-alias"},{"id":44,"kind":65536,"name":"__type","url":"modules.html#station.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias","parent":"Station"},{"id":45,"kind":1024,"name":"id","url":"modules.html#station.__type.id","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"Station.__type"},{"id":46,"kind":4194304,"name":"StationsResponse","url":"modules.html#stationsresponse","classes":"tsd-kind-type-alias"},{"id":47,"kind":65536,"name":"__type","url":"modules.html#stationsresponse.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias","parent":"StationsResponse"},{"id":48,"kind":1024,"name":"features","url":"modules.html#stationsresponse.__type.features","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"StationsResponse.__type"},{"id":49,"kind":4194304,"name":"AlertsFeature","url":"modules.html#alertsfeature","classes":"tsd-kind-type-alias"},{"id":50,"kind":65536,"name":"__type","url":"modules.html#alertsfeature.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias","parent":"AlertsFeature"},{"id":51,"kind":1024,"name":"id","url":"modules.html#alertsfeature.__type.id","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type"},{"id":52,"kind":1024,"name":"geometry","url":"modules.html#alertsfeature.__type.geometry","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type"},{"id":53,"kind":65536,"name":"__type","url":"modules.html#alertsfeature.__type.__type-1","classes":"tsd-kind-type-literal tsd-parent-kind-type-literal","parent":"AlertsFeature.__type"},{"id":54,"kind":1024,"name":"type","url":"modules.html#alertsfeature.__type.__type-1.type","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"},{"id":55,"kind":1024,"name":"coordinates","url":"modules.html#alertsfeature.__type.__type-1.coordinates","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"},{"id":56,"kind":1024,"name":"properties","url":"modules.html#alertsfeature.__type.properties","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type"},{"id":57,"kind":65536,"name":"__type","url":"modules.html#alertsfeature.__type.__type-2","classes":"tsd-kind-type-literal tsd-parent-kind-type-literal","parent":"AlertsFeature.__type"},{"id":58,"kind":1024,"name":"areaDesc","url":"modules.html#alertsfeature.__type.__type-2.areadesc","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"},{"id":59,"kind":1024,"name":"sent","url":"modules.html#alertsfeature.__type.__type-2.sent","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"},{"id":60,"kind":1024,"name":"effective","url":"modules.html#alertsfeature.__type.__type-2.effective","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"},{"id":61,"kind":1024,"name":"expries","url":"modules.html#alertsfeature.__type.__type-2.expries","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"},{"id":62,"kind":1024,"name":"description","url":"modules.html#alertsfeature.__type.__type-2.description","classes":"tsd-kind-property tsd-parent-kind-type-literal","parent":"AlertsFeature.__type.__type"}],"index":{"version":"2.3.9","fields":["name","parent"],"fieldVectors":[["name/0",[0,20.188]],["parent/0",[]],["name/1",[1,37.534]],["parent/1",[0,1.79]],["name/2",[2,37.534]],["parent/2",[0,1.79]],["name/3",[3,37.534]],["parent/3",[0,1.79]],["name/4",[4,37.534]],["parent/4",[0,1.79]],["name/5",[5,37.534]],["parent/5",[0,1.79]],["name/6",[6,37.534]],["parent/6",[0,1.79]],["name/7",[7,37.534]],["parent/7",[0,1.79]],["name/8",[8,37.534]],["parent/8",[]],["name/9",[9,37.534]],["parent/9",[]],["name/10",[10,37.534]],["parent/10",[]],["name/11",[11,37.534]],["parent/11",[]],["name/12",[12,37.534]],["parent/12",[]],["name/13",[13,37.534]],["parent/13",[]],["name/14",[14,32.426]],["parent/14",[]],["name/15",[15,16.332]],["parent/15",[14,2.875]],["name/16",[16,37.534]],["parent/16",[17,3.328]],["name/17",[18,32.426]],["parent/17",[]],["name/18",[15,16.332]],["parent/18",[18,2.875]],["name/19",[19,29.061]],["parent/19",[20,2.875]],["name/20",[15,16.332]],["parent/20",[20,2.875]],["name/21",[21,37.534]],["parent/21",[22,2.577]],["name/22",[23,37.534]],["parent/22",[22,2.577]],["name/23",[24,37.534]],["parent/23",[22,2.577]],["name/24",[25,32.426]],["parent/24",[]],["name/25",[15,16.332]],["parent/25",[25,2.875]],["name/26",[19,29.061]],["parent/26",[26,3.328]],["name/27",[27,32.426]],["parent/27",[]],["name/28",[15,16.332]],["parent/28",[27,2.875]],["name/29",[28,37.534]],["parent/29",[29,1.691]],["name/30",[30,37.534]],["parent/30",[29,1.691]],["name/31",[31,37.534]],["parent/31",[29,1.691]],["name/32",[32,37.534]],["parent/32",[29,1.691]],["name/33",[33,37.534]],["parent/33",[29,1.691]],["name/34",[34,37.534]],["parent/34",[29,1.691]],["name/35",[35,37.534]],["parent/35",[29,1.691]],["name/36",[15,16.332]],["parent/36",[29,1.691]],["name/37",[36,37.534]],["parent/37",[37,2.875]],["name/38",[38,37.534]],["parent/38",[37,2.875]],["name/39",[39,37.534]],["parent/39",[29,1.691]],["name/40",[40,32.426]],["parent/40",[]],["name/41",[15,16.332]],["parent/41",[40,2.875]],["name/42",[41,32.426]],["parent/42",[42,3.328]],["name/43",[43,32.426]],["parent/43",[]],["name/44",[15,16.332]],["parent/44",[43,2.875]],["name/45",[44,32.426]],["parent/45",[45,3.328]],["name/46",[46,32.426]],["parent/46",[]],["name/47",[15,16.332]],["parent/47",[46,2.875]],["name/48",[41,32.426]],["parent/48",[47,3.328]],["name/49",[48,32.426]],["parent/49",[]],["name/50",[15,16.332]],["parent/50",[48,2.875]],["name/51",[44,32.426]],["parent/51",[49,2.176]],["name/52",[50,37.534]],["parent/52",[49,2.176]],["name/53",[15,16.332]],["parent/53",[49,2.176]],["name/54",[51,37.534]],["parent/54",[52,1.901]],["name/55",[53,37.534]],["parent/55",[52,1.901]],["name/56",[19,29.061]],["parent/56",[49,2.176]],["name/57",[15,16.332]],["parent/57",[49,2.176]],["name/58",[54,37.534]],["parent/58",[52,1.901]],["name/59",[55,37.534]],["parent/59",[52,1.901]],["name/60",[56,37.534]],["parent/60",[52,1.901]],["name/61",[57,37.534]],["parent/61",[52,1.901]],["name/62",[58,37.534]],["parent/62",[52,1.901]]],"invertedIndex":[["__type",{"_index":15,"name":{"15":{},"18":{},"20":{},"25":{},"28":{},"36":{},"41":{},"44":{},"47":{},"50":{},"53":{},"57":{}},"parent":{}}],["alertoptions",{"_index":13,"name":{"13":{}},"parent":{}}],["alertsfeature",{"_index":48,"name":{"49":{}},"parent":{"50":{}}}],["alertsfeature.__type",{"_index":49,"name":{},"parent":{"51":{},"52":{},"53":{},"56":{},"57":{}}}],["alertsfeature.__type.__type",{"_index":52,"name":{},"parent":{"54":{},"55":{},"58":{},"59":{},"60":{},"61":{},"62":{}}}],["alertsresponse",{"_index":40,"name":{"40":{}},"parent":{"41":{}}}],["alertsresponse.__type",{"_index":42,"name":{},"parent":{"42":{}}}],["area",{"_index":9,"name":{"9":{}},"parent":{}}],["areadesc",{"_index":54,"name":{"58":{}},"parent":{}}],["client",{"_index":0,"name":{"0":{}},"parent":{"1":{},"2":{},"3":{},"4":{},"5":{},"6":{},"7":{}}}],["clientoptions",{"_index":14,"name":{"14":{}},"parent":{"15":{}}}],["clientoptions.__type",{"_index":17,"name":{},"parent":{"16":{}}}],["constructor",{"_index":1,"name":{"1":{}},"parent":{}}],["coordinates",{"_index":53,"name":{"55":{}},"parent":{}}],["description",{"_index":58,"name":{"62":{}},"parent":{}}],["effective",{"_index":56,"name":{"60":{}},"parent":{}}],["elevation",{"_index":35,"name":{"35":{}},"parent":{}}],["expries",{"_index":57,"name":{"61":{}},"parent":{}}],["features",{"_index":41,"name":{"42":{},"48":{}},"parent":{}}],["forecast",{"_index":21,"name":{"21":{}},"parent":{}}],["forecastgenerator",{"_index":31,"name":{"31":{}},"parent":{}}],["forecasthourly",{"_index":23,"name":{"22":{}},"parent":{}}],["forecastproperties",{"_index":27,"name":{"27":{}},"parent":{"28":{}}}],["forecastproperties.__type",{"_index":29,"name":{},"parent":{"29":{},"30":{},"31":{},"32":{},"33":{},"34":{},"35":{},"36":{},"39":{}}}],["forecastproperties.__type.__type",{"_index":37,"name":{},"parent":{"37":{},"38":{}}}],["forecastresponse",{"_index":25,"name":{"24":{}},"parent":{"25":{}}}],["forecastresponse.__type",{"_index":26,"name":{},"parent":{"26":{}}}],["forecasttype",{"_index":8,"name":{"8":{}},"parent":{}}],["generatedat",{"_index":32,"name":{"32":{}},"parent":{}}],["geometry",{"_index":50,"name":{"52":{}},"parent":{}}],["getalerts",{"_index":4,"name":{"4":{}},"parent":{}}],["getforecast",{"_index":5,"name":{"5":{}},"parent":{}}],["getneareststation",{"_index":7,"name":{"7":{}},"parent":{}}],["getoptions",{"_index":2,"name":{"2":{}},"parent":{}}],["getstations",{"_index":6,"name":{"6":{}},"parent":{}}],["id",{"_index":44,"name":{"45":{},"51":{}},"parent":{}}],["observationstations",{"_index":24,"name":{"23":{}},"parent":{}}],["periods",{"_index":39,"name":{"39":{}},"parent":{}}],["pointresponse",{"_index":18,"name":{"17":{}},"parent":{"18":{}}}],["pointresponse.__type",{"_index":20,"name":{},"parent":{"19":{},"20":{}}}],["pointresponse.__type.__type",{"_index":22,"name":{},"parent":{"21":{},"22":{},"23":{}}}],["properties",{"_index":19,"name":{"19":{},"26":{},"56":{}},"parent":{}}],["region",{"_index":10,"name":{"10":{}},"parent":{}}],["regiontype",{"_index":11,"name":{"11":{}},"parent":{}}],["sent",{"_index":55,"name":{"59":{}},"parent":{}}],["setoptions",{"_index":3,"name":{"3":{}},"parent":{}}],["station",{"_index":43,"name":{"43":{}},"parent":{"44":{}}}],["station.__type",{"_index":45,"name":{},"parent":{"45":{}}}],["stationsresponse",{"_index":46,"name":{"46":{}},"parent":{"47":{}}}],["stationsresponse.__type",{"_index":47,"name":{},"parent":{"48":{}}}],["type",{"_index":51,"name":{"54":{}},"parent":{}}],["unitcode",{"_index":38,"name":{"38":{}},"parent":{}}],["units",{"_index":30,"name":{"30":{}},"parent":{}}],["updated",{"_index":28,"name":{"29":{}},"parent":{}}],["updatetime",{"_index":33,"name":{"33":{}},"parent":{}}],["urgency",{"_index":12,"name":{"12":{}},"parent":{}}],["useragent",{"_index":16,"name":{"16":{}},"parent":{}}],["validtimes",{"_index":34,"name":{"34":{}},"parent":{}}],["value",{"_index":36,"name":{"37":{}},"parent":{}}]],"pipeline":[]}} \ No newline at end of file diff --git a/docs/classes/client.html b/docs/classes/client.html index cf5d468..0ca4e97 100644 --- a/docs/classes/client.html +++ b/docs/classes/client.html @@ -2716,9 +2716,15 @@
  • getForecast
  • +
  • + getNearestStation +
  • getOptions
  • +
  • + getStations +
  • setOptions
  • @@ -2739,6 +2745,9 @@
  • ClientOptions
  • +
  • + ForecastProperties +
  • ForecastResponse
  • @@ -2754,6 +2763,12 @@
  • RegionType
  • +
  • + Station +
  • +
  • + StationsResponse +
  • Urgency
  • @@ -2793,7 +2808,9 @@

    Methods

    @@ -2812,7 +2829,7 @@

    constructor

  • Parameters

    @@ -2838,12 +2855,12 @@

    getAlerts

  • -

    Get weather alerts for a given area

    +

    Get weather alerts for a given area.

    const active = true;
     const latitude = 35.6175667;
    @@ -2874,12 +2891,12 @@ 

    getForecast

  • -

    Get a weather forecast for a given latitude and longitude

    +

    Get a weather forecast for a given latitude and longitude.

    const latitude = 35.6175667;
     const longitude = -80.7709911;
    @@ -2902,6 +2919,41 @@ 

    Returns Promise +
    + +

    getNearestStation

    +
      +
    • getNearestStation(latitude: number, longitude: number): Promise<null | Station>
    • +
    +
      +
    • + +
      +
      +

      Get the closest weather station for a given latitude and longitude.

      +
      +
      const latitude = 35.6175667;
      +const longitude = -80.7709911;
      +const stationOrNull = await client.getNearestStation(latitude, longitude);
      +
      +
      +

      Parameters

      +
        +
      • +
        latitude: number
        +
      • +
      • +
        longitude: number
        +
      • +
      +

      Returns Promise<null | Station>

      +
    • +
    +

    getOptions

    @@ -2912,13 +2964,48 @@

    getOptions

  • Returns ClientOptions

  • +
    + +

    getStations

    +
      +
    • getStations(latitude: number, longitude: number): Promise<StationsResponse>
    • +
    +
      +
    • + +
      +
      +

      Get the closest weather stations for a given latitude and longitude.

      +
      +
      const latitude = 35.6175667;
      +const longitude = -80.7709911;
      +const stations = await client.getStations(latitude, longitude);
      +
      +
      +

      Parameters

      +
        +
      • +
        latitude: number
        +
      • +
      • +
        longitude: number
        +
      • +
      +

      Returns Promise<StationsResponse>

      +
    • +
    +

    setOptions

    @@ -2929,7 +3016,7 @@

    setOptions

  • Parameters

    diff --git a/docs/index.html b/docs/index.html index c43a805..e9af8e5 100644 --- a/docs/index.html +++ b/docs/index.html @@ -2722,6 +2722,9 @@
  • ClientOptions
  • +
  • + ForecastProperties +
  • ForecastResponse
  • @@ -2737,6 +2740,12 @@
  • RegionType
  • +
  • + Station +
  • +
  • + StationsResponse +
  • Urgency
  • @@ -2838,11 +2847,14 @@

    Type aliases

  • AlertsResponse
  • Area
  • ClientOptions
  • +
  • ForecastProperties
  • ForecastResponse
  • ForecastType
  • PointResponse
  • Region
  • RegionType
  • +
  • Station
  • +
  • StationsResponse
  • Urgency
  • @@ -2857,7 +2869,7 @@

    AlertOptions

    AlertOptions: UrgencyOption & XOR<AreaOption, XOR<PointOption, XOR<RegionOption, RegionTypeOption>>>
    @@ -2867,7 +2879,7 @@

    AlertsFeature

    AlertsFeature: { geometry: { coordinates: number[][]; type: "Polygon" }; id: string; properties: { areaDesc: string; description: string; effective: string; expries: string; sent: string } }
    @@ -2916,7 +2928,7 @@

    AlertsResponse

    AlertsResponse: { features: AlertsFeature[] }
    @@ -2934,7 +2946,7 @@

    Area

    Area: "AL" | "AK" | "AS" | "AR" | "AZ" | "CA" | "CO" | "CT" | "DE" | "DC" | "FL" | "GA" | "GU" | "HI" | "ID" | "IL" | "IN" | "IA" | "KS" | "KY" | "LA" | "ME" | "MD" | "MA" | "MI" | "MN" | "MS" | "MO" | "MT" | "NE" | "NV" | "NH" | "NJ" | "NM" | "NY" | "NC" | "ND" | "OH" | "OK" | "OR" | "PA" | "PR" | "RI" | "SC" | "SD" | "TN" | "TX" | "UT" | "VT" | "VI" | "VA" | "WA" | "WV" | "WI" | "WY" | "PZ" | "PK" | "PH" | "PS" | "PM" | "AN" | "AM" | "GM" | "LS" | "LM" | "LH" | "LC" | "LE" | "LO"
    @@ -2944,7 +2956,7 @@

    ClientOptions

    ClientOptions: { userAgent?: string }
    @@ -2956,20 +2968,67 @@
    Optional userAgent
    +
    + +

    ForecastProperties

    +
    ForecastProperties: { elevation: { unitCode: string; value: number }; forecastGenerator: string; generatedAt: string; periods: ForecastPeriod[]; units: string; updateTime: string; updated: string; validTimes: string }
    + +
    +

    Type declaration

    +
      +
    • +
      elevation: { unitCode: string; value: number }
      +
        +
      • +
        unitCode: string
        +
      • +
      • +
        value: number
        +
      • +
      +
    • +
    • +
      forecastGenerator: string
      +
    • +
    • +
      generatedAt: string
      +
    • +
    • +
      periods: ForecastPeriod[]
      +
    • +
    • +
      units: string
      +
    • +
    • +
      updateTime: string
      +
    • +
    • +
      updated: string
      +
    • +
    • +
      validTimes: string
      +
    • +
    +
    +

    ForecastResponse

    -
    ForecastResponse: { properties: ForecastProperties }
    +
    ForecastResponse: { properties: ForecastProperties }

    Type declaration

    @@ -2980,24 +3039,24 @@

    ForecastType

    ForecastType: "hourly" | "baseline"

    PointResponse

    -
    PointResponse: { properties: { forecast: string; forecastHourly: string } }
    +
    PointResponse: { properties: { forecast: string; forecastHourly: string; observationStations: string } }

    Type declaration

    • -
      properties: { forecast: string; forecastHourly: string }
      +
      properties: { forecast: string; forecastHourly: string; observationStations: string }
      • forecast: string
        @@ -3005,6 +3064,9 @@
        forecast:
        forecastHourly: string
      • +
      • +
        observationStations: string
        +
    @@ -3016,7 +3078,7 @@

    Region

    Region: "AL" | "AT" | "GL" | "GM" | "PA" | "PI"
    @@ -3026,9 +3088,45 @@

    RegionType

    RegionType: "land" | "marine"
    + +
    + +

    Station

    +
    Station: { id: string }
    + +
    +

    Type declaration

    +
      +
    • +
      id: string
      +
    • +
    +
    +
    +
    + +

    StationsResponse

    +
    StationsResponse: { features: Station[] }
    + +
    +

    Type declaration

    + +
    @@ -3036,7 +3134,7 @@

    Urgency

    Urgency: "immediate" | "expected" | "future" | "past" | "unknown"
    diff --git a/docs/modules.html b/docs/modules.html index 1ea7e41..478e925 100644 --- a/docs/modules.html +++ b/docs/modules.html @@ -2722,6 +2722,9 @@
  • ClientOptions
  • +
  • + ForecastProperties +
  • ForecastResponse
  • @@ -2737,6 +2740,12 @@
  • RegionType
  • +
  • + Station +
  • +
  • + StationsResponse +
  • Urgency
  • @@ -2837,11 +2846,14 @@

    Type aliases

  • AlertsResponse
  • Area
  • ClientOptions
  • +
  • ForecastProperties
  • ForecastResponse
  • ForecastType
  • PointResponse
  • Region
  • RegionType
  • +
  • Station
  • +
  • StationsResponse
  • Urgency
  • @@ -2856,7 +2868,7 @@

    AlertOptions

    AlertOptions: UrgencyOption & XOR<AreaOption, XOR<PointOption, XOR<RegionOption, RegionTypeOption>>>
    @@ -2866,7 +2878,7 @@

    AlertsFeature

    AlertsFeature: { geometry: { coordinates: number[][]; type: "Polygon" }; id: string; properties: { areaDesc: string; description: string; effective: string; expries: string; sent: string } }
    @@ -2915,7 +2927,7 @@

    AlertsResponse

    AlertsResponse: { features: AlertsFeature[] }
    @@ -2933,7 +2945,7 @@

    Area

    Area: "AL" | "AK" | "AS" | "AR" | "AZ" | "CA" | "CO" | "CT" | "DE" | "DC" | "FL" | "GA" | "GU" | "HI" | "ID" | "IL" | "IN" | "IA" | "KS" | "KY" | "LA" | "ME" | "MD" | "MA" | "MI" | "MN" | "MS" | "MO" | "MT" | "NE" | "NV" | "NH" | "NJ" | "NM" | "NY" | "NC" | "ND" | "OH" | "OK" | "OR" | "PA" | "PR" | "RI" | "SC" | "SD" | "TN" | "TX" | "UT" | "VT" | "VI" | "VA" | "WA" | "WV" | "WI" | "WY" | "PZ" | "PK" | "PH" | "PS" | "PM" | "AN" | "AM" | "GM" | "LS" | "LM" | "LH" | "LC" | "LE" | "LO"
    @@ -2943,7 +2955,7 @@

    ClientOptions

    ClientOptions: { userAgent?: string }
    @@ -2955,20 +2967,67 @@
    Optional userAgent
    +
    + +

    ForecastProperties

    +
    ForecastProperties: { elevation: { unitCode: string; value: number }; forecastGenerator: string; generatedAt: string; periods: ForecastPeriod[]; units: string; updateTime: string; updated: string; validTimes: string }
    + +
    +

    Type declaration

    +
      +
    • +
      elevation: { unitCode: string; value: number }
      +
        +
      • +
        unitCode: string
        +
      • +
      • +
        value: number
        +
      • +
      +
    • +
    • +
      forecastGenerator: string
      +
    • +
    • +
      generatedAt: string
      +
    • +
    • +
      periods: ForecastPeriod[]
      +
    • +
    • +
      units: string
      +
    • +
    • +
      updateTime: string
      +
    • +
    • +
      updated: string
      +
    • +
    • +
      validTimes: string
      +
    • +
    +
    +

    ForecastResponse

    -
    ForecastResponse: { properties: ForecastProperties }
    +
    ForecastResponse: { properties: ForecastProperties }

    Type declaration

    @@ -2979,24 +3038,24 @@

    ForecastType

    ForecastType: "hourly" | "baseline"

    PointResponse

    -
    PointResponse: { properties: { forecast: string; forecastHourly: string } }
    +
    PointResponse: { properties: { forecast: string; forecastHourly: string; observationStations: string } }

    Type declaration

    • -
      properties: { forecast: string; forecastHourly: string }
      +
      properties: { forecast: string; forecastHourly: string; observationStations: string }
      • forecast: string
        @@ -3004,6 +3063,9 @@
        forecast:
        forecastHourly: string
      • +
      • +
        observationStations: string
        +
    @@ -3015,7 +3077,7 @@

    Region

    Region: "AL" | "AT" | "GL" | "GM" | "PA" | "PI"
    @@ -3025,9 +3087,45 @@

    RegionType

    RegionType: "land" | "marine"
    + +
    + +

    Station

    +
    Station: { id: string }
    + +
    +

    Type declaration

    +
      +
    • +
      id: string
      +
    • +
    +
    +
    +
    + +

    StationsResponse

    +
    StationsResponse: { features: Station[] }
    + +
    +

    Type declaration

    + +
    @@ -3035,7 +3133,7 @@

    Urgency

    Urgency: "immediate" | "expected" | "future" | "past" | "unknown"
    diff --git a/src/cache.ts b/src/cache.ts new file mode 100644 index 0000000..f0d701c --- /dev/null +++ b/src/cache.ts @@ -0,0 +1,17 @@ +class Cache { + private items: { [key: string]: T; }; + + constructor() { + this.items = {}; + } + + get(key: string): T | undefined { + return this.items[key]; + } + + set(key: string, value: T): void { + this.items[key] = value; + } +} + +export default Cache; diff --git a/src/client.ts b/src/client.ts index 3a5dd27..8400ee4 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,6 +1,7 @@ import fetch from 'cross-fetch'; -import { ClientOptions, PointResponse, ForecastResponse, AlertsResponse, ForecastType, AlertOptions } from './types'; +import Cache from './cache'; +import { ClientOptions, PointResponse, ForecastResponse, AlertsResponse, ForecastType, StationsResponse, Station, AlertOptions } from './types'; const defaultOptions: ClientOptions = { userAgent: 'weathered package' @@ -36,9 +37,13 @@ const processOptions = (options: AlertOptions) => { */ class Client { private options: ClientOptions; + private pointCache: Cache; + private stationsCache: Cache; constructor(options?: ClientOptions) { this.options = {...defaultOptions, ...options}; + this.pointCache = new Cache(); + this.stationsCache = new Cache(); } private getPath(path: string) { @@ -50,21 +55,31 @@ class Client { return await resp.json(); } - private getPoint(latitude: number, longitude: number) : Promise { + private async getPoint(latitude: number, longitude: number): Promise { + const cacheKey = `${latitude},${longitude}`; + const potentialPointResponse = this.pointCache.get(cacheKey); + + if (potentialPointResponse) { + return potentialPointResponse; + } + const path = `points/${latitude},${longitude}`; - return this.getPath(path); + const pointResponse = await this.getPath(path); + this.pointCache.set(cacheKey, pointResponse); + + return pointResponse; } - getOptions() : ClientOptions { + getOptions(): ClientOptions { return {...this.options}; } - setOptions(newOptions: ClientOptions) : void { + setOptions(newOptions: ClientOptions): void { this.options = {...this.options, ...newOptions}; } /** - * Get weather alerts for a given area + * Get weather alerts for a given area. * * ```typescript * const active = true; @@ -73,14 +88,14 @@ class Client { * const alerts = await client.getAlerts(active, { latitude, longitude }); * ``` */ - getAlerts(active: boolean, options: AlertOptions) : Promise { + getAlerts(active: boolean, options: AlertOptions): Promise { const params = processOptions(options); const path = `alerts${ active ? '/active' : ''}?${params}`; return this.getPath(path); } /** - * Get a weather forecast for a given latitude and longitude + * Get a weather forecast for a given latitude and longitude. * * ```typescript * const latitude = 35.6175667; @@ -89,12 +104,57 @@ class Client { * ``` * */ - async getForecast(latitude: number, longitude: number, forecastType: ForecastType) : Promise { - const pointResp = await this.getPoint(latitude, longitude); + async getForecast(latitude: number, longitude: number, forecastType: ForecastType): Promise { + const pointResponse = await this.getPoint(latitude, longitude); const forecastKey = forecastType === 'hourly' ? 'forecastHourly' : 'forecast'; - const url = pointResp.properties[forecastKey]; + const url = pointResponse.properties[forecastKey]; return this.getUrl(url); } + + /** + * Get the closest weather stations for a given latitude and longitude. + * + * ```typescript + * const latitude = 35.6175667; + * const longitude = -80.7709911; + * const stations = await client.getStations(latitude, longitude); + * ``` + * + */ + async getStations(latitude: number, longitude: number): Promise { + const pointResponse = await this.getPoint(latitude, longitude); + const stationsUrl = pointResponse.properties.observationStations; + + const potentionalStationsResponse = this.stationsCache.get(stationsUrl); + + if (potentionalStationsResponse) { + return potentionalStationsResponse; + } + + const stationsResponse = await this.getUrl(stationsUrl); + this.stationsCache.set(stationsUrl, stationsResponse); + + return stationsResponse; + } + + /** + * Get the closest weather station for a given latitude and longitude. + * + * ```typescript + * const latitude = 35.6175667; + * const longitude = -80.7709911; + * const stationOrNull = await client.getNearestStation(latitude, longitude); + * ``` + * + */ + async getNearestStation(latitude: number, longitude: number): Promise { + const stationsResponse = await this.getStations(latitude, longitude); + if (stationsResponse.features.length > 0) { + return stationsResponse.features[0]; + } + + return null; + } } export { Client }; diff --git a/src/index.ts b/src/index.ts index b71aeac..998a7cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ export { Client } from './client'; -export { ForecastType, Area, Region, RegionType, Urgency, AlertOptions, ClientOptions, PointResponse, ForecastResponse, AlertsResponse, AlertsFeature } from './types'; +export { ForecastType, Area, Region, RegionType, Urgency, AlertOptions, ClientOptions, PointResponse, ForecastResponse, ForecastProperties, AlertsResponse, Station, StationsResponse, AlertsFeature } from './types'; diff --git a/src/types.ts b/src/types.ts index c0244f6..721326f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -52,6 +52,29 @@ type PointResponse = { properties: { forecast: string; forecastHourly: string; + observationStations: string; + } +} + +type Station = { + id: string; +} + +type StationsResponse = { + features: Station[]; +} + +type PresentWeather = { + [key: string]: string | null; +} + +type ObservationResponse = { + properties: { + presentWeather: PresentWeather[], + temperature: { + value: number; + unitCode: string; + } } } @@ -70,6 +93,7 @@ type ForecastPeriod = { shortForecast: string; detailedForecast: string; } + type ForecastProperties = { updated: string; units: string; @@ -104,4 +128,4 @@ type AlertsResponse = { features: AlertsFeature[]; } -export { ForecastType, Area, Region, RegionType, Urgency, AlertOptions, ClientOptions, PointResponse, ForecastResponse, AlertsResponse, AlertsFeature }; +export { ForecastType, Area, Region, RegionType, Urgency, AlertOptions, ClientOptions, PointResponse, ForecastResponse, ForecastProperties, Station, StationsResponse, AlertsResponse, AlertsFeature };