-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.ts
182 lines (152 loc) · 5.11 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import fetch from 'cross-fetch';
import Cache from './cache';
import { ClientOptions, PointResponse, ForecastResponse, AlertsResponse, ForecastType, StationsResponse, Station, ObservationResponse, ObservationsResponse, AlertOptions } from './types';
const defaultOptions: ClientOptions = {
userAgent: 'weathered package'
};
const API_ROOT = 'https://api.weather.gov/';
const processOptions = (options: AlertOptions) => {
const { area, latitude, longitude, urgency } = options;
const params = new URLSearchParams();
if (area) {
params.append('area', Array.isArray(area) ? area.join(',') : area);
}
if (latitude !== undefined && longitude !== undefined) {
params.append('point', `${latitude},${longitude}`);
}
if (urgency) {
params.append('urgency', urgency);
}
return params;
};
/**
* The main client
*
* ```typescript
* const client = new Client();
* ```
*/
class Client {
private options: ClientOptions;
private pointCache: Cache<PointResponse>;
private stationsCache: Cache<StationsResponse>;
constructor(options?: ClientOptions) {
this.options = {...defaultOptions, ...options};
this.pointCache = new Cache<PointResponse>();
this.stationsCache = new Cache<StationsResponse>();
}
private getPath(path: string) {
return this.getUrl(API_ROOT + path);
}
private async getUrl(url: string) {
const resp = await fetch(url);
return await resp.json();
}
private async getPoint(latitude: number, longitude: number): Promise<PointResponse> {
const cacheKey = `${latitude},${longitude}`;
const potentialPointResponse = this.pointCache.get(cacheKey);
if (potentialPointResponse) {
return potentialPointResponse;
}
const path = `points/${latitude},${longitude}`;
const pointResponse = await this.getPath(path);
this.pointCache.set(cacheKey, pointResponse);
return pointResponse;
}
getOptions(): ClientOptions {
return {...this.options};
}
setOptions(newOptions: ClientOptions): void {
this.options = {...this.options, ...newOptions};
}
/**
* Get weather alerts for a given area.
*
* ```typescript
* const active = true;
* const latitude = 35.6175667;
* const longitude = -80.7709911;
* const alerts = await client.getAlerts(active, { latitude, longitude });
* ```
*/
getAlerts(active: boolean, options: AlertOptions): Promise<AlertsResponse> {
const params = processOptions(options);
const path = `alerts${ active ? '/active' : ''}?${params}`;
return this.getPath(path);
}
/**
* Get a weather forecast for a given latitude and longitude.
*
* ```typescript
* const latitude = 35.6175667;
* const longitude = -80.7709911;
* const forecast = await client.getForecast(latitude, longitude, 'baseline');
* ```
*
*/
async getForecast(latitude: number, longitude: number, forecastType: ForecastType): Promise<ForecastResponse> {
const pointResponse = await this.getPoint(latitude, longitude);
const forecastKey = forecastType === 'hourly' ? 'forecastHourly' : 'forecast';
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<StationsResponse> {
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<Station | null> {
const stationsResponse = await this.getStations(latitude, longitude);
if (stationsResponse.features.length > 0) {
return stationsResponse.features[0];
}
return null;
}
/**
* Get weather observations for a given station.
*
* ```typescript
* const observations = await client.getStationObservations('KSFO');
* ```
*/
getStationObservations(stationId: string): Promise<ObservationsResponse> {
return this.getPath(`stations/${stationId}/observations`);
}
/**
* Get latest weather observation for a given station.
*
* ```typescript
* const latestObservation = await client.getLatestStationObservations('KSFO');
* ```
*/
getLatestStationObservations(stationId: string): Promise<ObservationResponse> {
return this.getPath(`stations/${stationId}/observations/latest`);
}
}
export { Client };