@mharj/openweathermap 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +967 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +650 -0
- package/dist/index.d.mts +476 -467
- package/dist/index.mjs +911 -860
- package/dist/index.mjs.map +1 -1
- package/package.json +52 -46
- package/dist/index.d.ts +0 -641
- package/dist/index.js +0 -942
- package/dist/index.js.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,903 +1,954 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
1
|
+
import { Err, Ok, Option, Result } from "@luolapeikko/result-option";
|
|
2
|
+
import { z } from "zod";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
//#region src/lib/fetchUtils.ts
|
|
5
|
+
/**
|
|
6
|
+
* Ensures that the error is a DOMException or TypeError.
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
5
9
|
function fetchErrorWrapper(err) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} else {
|
|
9
|
-
return new TypeError(`Unknown error: ${String(err)}`);
|
|
10
|
-
}
|
|
10
|
+
if (err instanceof DOMException || err instanceof TypeError) return err;
|
|
11
|
+
else return /* @__PURE__ */ new TypeError(`Unknown error: ${String(err)}`);
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/types/v2/Icon.ts
|
|
16
|
+
/**
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
const dayIconListSchema = z.enum([
|
|
20
|
+
"01d",
|
|
21
|
+
"02d",
|
|
22
|
+
"03d",
|
|
23
|
+
"04d",
|
|
24
|
+
"09d",
|
|
25
|
+
"10d",
|
|
26
|
+
"11d",
|
|
27
|
+
"13d",
|
|
28
|
+
"50d"
|
|
29
|
+
]);
|
|
30
|
+
/**
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
const nightIconListSchema = z.enum([
|
|
34
|
+
"01n",
|
|
35
|
+
"02n",
|
|
36
|
+
"03n",
|
|
37
|
+
"04n",
|
|
38
|
+
"09n",
|
|
39
|
+
"10n",
|
|
40
|
+
"11n",
|
|
41
|
+
"13n",
|
|
42
|
+
"50n"
|
|
43
|
+
]);
|
|
44
|
+
/**
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
const iconSchema = z.union([dayIconListSchema, nightIconListSchema]);
|
|
23
48
|
|
|
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
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/types/v2/weatherIdGroup.ts
|
|
51
|
+
/**
|
|
52
|
+
* This is a list of weather ids, groups and descriptions from OpenWeatherMap API
|
|
53
|
+
*/
|
|
54
|
+
const weatherIdGroup = [
|
|
55
|
+
{
|
|
56
|
+
id: 200,
|
|
57
|
+
group: "thunderstorm",
|
|
58
|
+
description: "thunderstorm_with_light_rain"
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
id: 201,
|
|
62
|
+
group: "thunderstorm",
|
|
63
|
+
description: "thunderstorm_with_rain"
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
id: 202,
|
|
67
|
+
group: "thunderstorm",
|
|
68
|
+
description: "thunderstorm_with_heavy_rain"
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
id: 210,
|
|
72
|
+
group: "thunderstorm",
|
|
73
|
+
description: "light_thunderstorm"
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
id: 211,
|
|
77
|
+
group: "thunderstorm",
|
|
78
|
+
description: "thunderstorm"
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: 212,
|
|
82
|
+
group: "thunderstorm",
|
|
83
|
+
description: "heavy_thunderstorm"
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
id: 221,
|
|
87
|
+
group: "thunderstorm",
|
|
88
|
+
description: "ragged_thunderstorm"
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
id: 230,
|
|
92
|
+
group: "thunderstorm",
|
|
93
|
+
description: "thunderstorm_with_light_drizzle"
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
id: 231,
|
|
97
|
+
group: "thunderstorm",
|
|
98
|
+
description: "thunderstorm_with_drizzle"
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
id: 232,
|
|
102
|
+
group: "thunderstorm",
|
|
103
|
+
description: "thunderstorm_with_heavy_drizzle"
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
id: 300,
|
|
107
|
+
group: "drizzle",
|
|
108
|
+
description: "light_intensity_drizzle"
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
id: 301,
|
|
112
|
+
group: "drizzle",
|
|
113
|
+
description: "drizzle"
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
id: 302,
|
|
117
|
+
group: "drizzle",
|
|
118
|
+
description: "heavy_intensity_drizzle"
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
id: 310,
|
|
122
|
+
group: "drizzle",
|
|
123
|
+
description: "light_intensity_drizzle_rain"
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
id: 311,
|
|
127
|
+
group: "drizzle",
|
|
128
|
+
description: "drizzle_rain"
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
id: 312,
|
|
132
|
+
group: "drizzle",
|
|
133
|
+
description: "heavy_intensity_drizzle_rain"
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
id: 313,
|
|
137
|
+
group: "drizzle",
|
|
138
|
+
description: "shower_rain_and_drizzle"
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
id: 314,
|
|
142
|
+
group: "drizzle",
|
|
143
|
+
description: "heavy_shower_rain_and_drizzle"
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
id: 321,
|
|
147
|
+
group: "drizzle",
|
|
148
|
+
description: "shower_drizzle"
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
id: 500,
|
|
152
|
+
group: "rain",
|
|
153
|
+
description: "light_rain"
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
id: 501,
|
|
157
|
+
group: "rain",
|
|
158
|
+
description: "moderate_rain"
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
id: 502,
|
|
162
|
+
group: "rain",
|
|
163
|
+
description: "heavy_intensity_rain"
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
id: 503,
|
|
167
|
+
group: "rain",
|
|
168
|
+
description: "very_heavy_rain"
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
id: 504,
|
|
172
|
+
group: "rain",
|
|
173
|
+
description: "extreme_rain"
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
id: 511,
|
|
177
|
+
group: "rain",
|
|
178
|
+
description: "freezing_rain"
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
id: 520,
|
|
182
|
+
group: "rain",
|
|
183
|
+
description: "light_intensity_shower_rain"
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
id: 521,
|
|
187
|
+
group: "rain",
|
|
188
|
+
description: "shower_rain"
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
id: 522,
|
|
192
|
+
group: "rain",
|
|
193
|
+
description: "heavy_intensity_shower_rain"
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
id: 531,
|
|
197
|
+
group: "rain",
|
|
198
|
+
description: "ragged_shower_rain"
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
id: 600,
|
|
202
|
+
group: "snow",
|
|
203
|
+
description: "light_snow"
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
id: 601,
|
|
207
|
+
group: "snow",
|
|
208
|
+
description: "snow"
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
id: 602,
|
|
212
|
+
group: "snow",
|
|
213
|
+
description: "heavy_snow"
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
id: 611,
|
|
217
|
+
group: "snow",
|
|
218
|
+
description: "sleet"
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
id: 612,
|
|
222
|
+
group: "snow",
|
|
223
|
+
description: "light_shower_sleet"
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
id: 613,
|
|
227
|
+
group: "snow",
|
|
228
|
+
description: "shower_sleet"
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
id: 615,
|
|
232
|
+
group: "snow",
|
|
233
|
+
description: "light_rain_and_snow"
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
id: 616,
|
|
237
|
+
group: "snow",
|
|
238
|
+
description: "rain_and_snow"
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
id: 620,
|
|
242
|
+
group: "snow",
|
|
243
|
+
description: "light_shower_snow"
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
id: 621,
|
|
247
|
+
group: "snow",
|
|
248
|
+
description: "shower_snow"
|
|
249
|
+
},
|
|
250
|
+
{
|
|
251
|
+
id: 622,
|
|
252
|
+
group: "snow",
|
|
253
|
+
description: "heavy_shower_snow"
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
id: 701,
|
|
257
|
+
group: "atmosphere",
|
|
258
|
+
description: "mist"
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
id: 711,
|
|
262
|
+
group: "atmosphere",
|
|
263
|
+
description: "smoke"
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
id: 721,
|
|
267
|
+
group: "atmosphere",
|
|
268
|
+
description: "haze"
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
id: 731,
|
|
272
|
+
group: "atmosphere",
|
|
273
|
+
description: "sand_dust_whirls"
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
id: 741,
|
|
277
|
+
group: "atmosphere",
|
|
278
|
+
description: "fog"
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
id: 751,
|
|
282
|
+
group: "atmosphere",
|
|
283
|
+
description: "sand"
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
id: 761,
|
|
287
|
+
group: "atmosphere",
|
|
288
|
+
description: "dust"
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
id: 762,
|
|
292
|
+
group: "atmosphere",
|
|
293
|
+
description: "volcanic_ash"
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
id: 771,
|
|
297
|
+
group: "atmosphere",
|
|
298
|
+
description: "squalls"
|
|
299
|
+
},
|
|
300
|
+
{
|
|
301
|
+
id: 781,
|
|
302
|
+
group: "atmosphere",
|
|
303
|
+
description: "tornado"
|
|
304
|
+
},
|
|
305
|
+
{
|
|
306
|
+
id: 800,
|
|
307
|
+
group: "clear",
|
|
308
|
+
description: "clear_sky"
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
id: 801,
|
|
312
|
+
group: "clouds",
|
|
313
|
+
description: "few_clouds_11-25_percent"
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
id: 802,
|
|
317
|
+
group: "clouds",
|
|
318
|
+
description: "scattered_clouds_25-50_percent"
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
id: 803,
|
|
322
|
+
group: "clouds",
|
|
323
|
+
description: "broken_clouds_51-84_percent"
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
id: 804,
|
|
327
|
+
group: "clouds",
|
|
328
|
+
description: "overcast_clouds_85-100_percent"
|
|
329
|
+
}
|
|
303
330
|
];
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
331
|
+
/**
|
|
332
|
+
* Weather id schema
|
|
333
|
+
* @internal
|
|
334
|
+
*/
|
|
335
|
+
const weatherIdSchema = z.custom((val) => {
|
|
336
|
+
if (typeof val !== "number") return {
|
|
337
|
+
message: "Expected a number",
|
|
338
|
+
success: false
|
|
339
|
+
};
|
|
340
|
+
if (!weatherIdGroup.find((x) => x.id === val)) return {
|
|
341
|
+
message: "Expected a valid weather id",
|
|
342
|
+
success: false
|
|
343
|
+
};
|
|
344
|
+
return val;
|
|
312
345
|
});
|
|
346
|
+
/**
|
|
347
|
+
* get weather description key from weather id
|
|
348
|
+
* @param id - weather id
|
|
349
|
+
* @returns {Option<WeatherDescription>} option for weather description key
|
|
350
|
+
* @example
|
|
351
|
+
* const weatherComponent = ({data}) => {
|
|
352
|
+
* const key = getWeatherV2Description(data.weather[0]?.id).unwrapOr('unknown');
|
|
353
|
+
* return (
|
|
354
|
+
* <div>
|
|
355
|
+
* {t(`weather:${key}`}
|
|
356
|
+
* </div>
|
|
357
|
+
* );
|
|
358
|
+
* }
|
|
359
|
+
*/
|
|
313
360
|
function getWeatherV2Description(id) {
|
|
314
|
-
|
|
361
|
+
return Option.fromNullish(weatherIdGroup.find((x) => x.id === id)?.description);
|
|
315
362
|
}
|
|
316
363
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
364
|
+
//#endregion
|
|
365
|
+
//#region src/types/v2/Language.ts
|
|
366
|
+
const langCodes = [
|
|
367
|
+
"af",
|
|
368
|
+
"al",
|
|
369
|
+
"ar",
|
|
370
|
+
"az",
|
|
371
|
+
"bg",
|
|
372
|
+
"ca",
|
|
373
|
+
"cz",
|
|
374
|
+
"da",
|
|
375
|
+
"de",
|
|
376
|
+
"el",
|
|
377
|
+
"en",
|
|
378
|
+
"eu",
|
|
379
|
+
"fa",
|
|
380
|
+
"fi",
|
|
381
|
+
"fr",
|
|
382
|
+
"gl",
|
|
383
|
+
"he",
|
|
384
|
+
"hi",
|
|
385
|
+
"hr",
|
|
386
|
+
"hu",
|
|
387
|
+
"id",
|
|
388
|
+
"it",
|
|
389
|
+
"ja",
|
|
390
|
+
"kr",
|
|
391
|
+
"la",
|
|
392
|
+
"lt",
|
|
393
|
+
"mk",
|
|
394
|
+
"no",
|
|
395
|
+
"nl",
|
|
396
|
+
"pl",
|
|
397
|
+
"pt",
|
|
398
|
+
"pt",
|
|
399
|
+
"ro",
|
|
400
|
+
"ru",
|
|
401
|
+
"sv",
|
|
402
|
+
"sk",
|
|
403
|
+
"sl",
|
|
404
|
+
"sp",
|
|
405
|
+
"sr",
|
|
406
|
+
"th",
|
|
407
|
+
"tr",
|
|
408
|
+
"ua",
|
|
409
|
+
"vi",
|
|
410
|
+
"zh_cn",
|
|
411
|
+
"zh_tw",
|
|
412
|
+
"zu"
|
|
366
413
|
];
|
|
367
|
-
|
|
414
|
+
const langCodeSchema = z.enum(langCodes);
|
|
368
415
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
416
|
+
//#endregion
|
|
417
|
+
//#region src/types/v2/index.ts
|
|
418
|
+
const coordSchema = z.object({
|
|
419
|
+
lon: z.number(),
|
|
420
|
+
lat: z.number()
|
|
373
421
|
});
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
422
|
+
const weatherSchema = z.object({
|
|
423
|
+
description: z.string(),
|
|
424
|
+
icon: iconSchema,
|
|
425
|
+
id: weatherIdSchema,
|
|
426
|
+
main: z.string()
|
|
379
427
|
});
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
428
|
+
const mainSchema = z.object({
|
|
429
|
+
grnd_level: z.number().optional(),
|
|
430
|
+
humidity: z.number(),
|
|
431
|
+
pressure: z.number(),
|
|
432
|
+
sea_level: z.number().optional(),
|
|
433
|
+
temp: z.number(),
|
|
434
|
+
temp_max: z.number(),
|
|
435
|
+
temp_min: z.number()
|
|
388
436
|
});
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
437
|
+
const windSchema = z.object({
|
|
438
|
+
speed: z.number(),
|
|
439
|
+
deg: z.number()
|
|
392
440
|
});
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
441
|
+
const rainSchema = z.object({
|
|
442
|
+
"1h": z.number().optional(),
|
|
443
|
+
"3h": z.number().optional()
|
|
396
444
|
});
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
445
|
+
const snowSchema = z.object({
|
|
446
|
+
"1h": z.number().optional(),
|
|
447
|
+
"3h": z.number().optional()
|
|
400
448
|
});
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
449
|
+
const sysSchema = z.object({
|
|
450
|
+
country: z.string(),
|
|
451
|
+
id: z.number().optional(),
|
|
452
|
+
message: z.number().optional(),
|
|
453
|
+
sunrise: z.number(),
|
|
454
|
+
sunset: z.number(),
|
|
455
|
+
type: z.number().optional()
|
|
408
456
|
});
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
457
|
+
/**
|
|
458
|
+
* @internal
|
|
459
|
+
*/
|
|
460
|
+
const weatherDataV2Schema = z.object({
|
|
461
|
+
base: z.string(),
|
|
462
|
+
clouds: z.object({ all: z.number() }),
|
|
463
|
+
cod: z.number(),
|
|
464
|
+
coord: coordSchema,
|
|
465
|
+
dt: z.number(),
|
|
466
|
+
id: z.number(),
|
|
467
|
+
main: mainSchema,
|
|
468
|
+
name: z.string(),
|
|
469
|
+
rain: rainSchema.optional(),
|
|
470
|
+
snow: snowSchema.optional(),
|
|
471
|
+
sys: sysSchema,
|
|
472
|
+
timezone: z.number(),
|
|
473
|
+
visibility: z.number(),
|
|
474
|
+
weather: z.array(weatherSchema),
|
|
475
|
+
wind: windSchema
|
|
427
476
|
});
|
|
428
477
|
function isWeatherDataV2(data) {
|
|
429
|
-
|
|
478
|
+
return weatherDataV2Schema.safeParse(data).success;
|
|
430
479
|
}
|
|
431
480
|
function assertWeatherDataV2(data) {
|
|
432
|
-
|
|
481
|
+
weatherDataV2Schema.parse(data);
|
|
433
482
|
}
|
|
434
483
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
484
|
+
//#endregion
|
|
485
|
+
//#region src/types/ISO3166-Countries.ts
|
|
486
|
+
const CountryCodeList = [
|
|
487
|
+
"af",
|
|
488
|
+
"ax",
|
|
489
|
+
"al",
|
|
490
|
+
"dz",
|
|
491
|
+
"as",
|
|
492
|
+
"ad",
|
|
493
|
+
"ao",
|
|
494
|
+
"ai",
|
|
495
|
+
"aq",
|
|
496
|
+
"ag",
|
|
497
|
+
"ar",
|
|
498
|
+
"am",
|
|
499
|
+
"aw",
|
|
500
|
+
"au",
|
|
501
|
+
"at",
|
|
502
|
+
"az",
|
|
503
|
+
"bs",
|
|
504
|
+
"bh",
|
|
505
|
+
"bd",
|
|
506
|
+
"bb",
|
|
507
|
+
"by",
|
|
508
|
+
"be",
|
|
509
|
+
"bz",
|
|
510
|
+
"bj",
|
|
511
|
+
"bm",
|
|
512
|
+
"bt",
|
|
513
|
+
"bo",
|
|
514
|
+
"bq",
|
|
515
|
+
"ba",
|
|
516
|
+
"bw",
|
|
517
|
+
"bv",
|
|
518
|
+
"br",
|
|
519
|
+
"io",
|
|
520
|
+
"bn",
|
|
521
|
+
"bg",
|
|
522
|
+
"bf",
|
|
523
|
+
"bi",
|
|
524
|
+
"kh",
|
|
525
|
+
"cm",
|
|
526
|
+
"ca",
|
|
527
|
+
"cv",
|
|
528
|
+
"ky",
|
|
529
|
+
"cf",
|
|
530
|
+
"td",
|
|
531
|
+
"cl",
|
|
532
|
+
"cn",
|
|
533
|
+
"cx",
|
|
534
|
+
"cc",
|
|
535
|
+
"co",
|
|
536
|
+
"km",
|
|
537
|
+
"cg",
|
|
538
|
+
"cd",
|
|
539
|
+
"ck",
|
|
540
|
+
"cr",
|
|
541
|
+
"ci",
|
|
542
|
+
"hr",
|
|
543
|
+
"cu",
|
|
544
|
+
"cw",
|
|
545
|
+
"cy",
|
|
546
|
+
"cz",
|
|
547
|
+
"dk",
|
|
548
|
+
"dj",
|
|
549
|
+
"dm",
|
|
550
|
+
"do",
|
|
551
|
+
"ec",
|
|
552
|
+
"eg",
|
|
553
|
+
"sv",
|
|
554
|
+
"gq",
|
|
555
|
+
"er",
|
|
556
|
+
"ee",
|
|
557
|
+
"et",
|
|
558
|
+
"fk",
|
|
559
|
+
"fo",
|
|
560
|
+
"fj",
|
|
561
|
+
"fi",
|
|
562
|
+
"fr",
|
|
563
|
+
"gf",
|
|
564
|
+
"pf",
|
|
565
|
+
"tf",
|
|
566
|
+
"ga",
|
|
567
|
+
"gm",
|
|
568
|
+
"ge",
|
|
569
|
+
"de",
|
|
570
|
+
"gh",
|
|
571
|
+
"gi",
|
|
572
|
+
"gr",
|
|
573
|
+
"gl",
|
|
574
|
+
"gd",
|
|
575
|
+
"gp",
|
|
576
|
+
"gu",
|
|
577
|
+
"gt",
|
|
578
|
+
"gg",
|
|
579
|
+
"gn",
|
|
580
|
+
"gw",
|
|
581
|
+
"gy",
|
|
582
|
+
"ht",
|
|
583
|
+
"hm",
|
|
584
|
+
"va",
|
|
585
|
+
"hn",
|
|
586
|
+
"hk",
|
|
587
|
+
"hu",
|
|
588
|
+
"is",
|
|
589
|
+
"in",
|
|
590
|
+
"id",
|
|
591
|
+
"ir",
|
|
592
|
+
"iq",
|
|
593
|
+
"ie",
|
|
594
|
+
"im",
|
|
595
|
+
"il",
|
|
596
|
+
"it",
|
|
597
|
+
"jm",
|
|
598
|
+
"jp",
|
|
599
|
+
"je",
|
|
600
|
+
"jo",
|
|
601
|
+
"kz",
|
|
602
|
+
"ke",
|
|
603
|
+
"ki",
|
|
604
|
+
"kp",
|
|
605
|
+
"kr",
|
|
606
|
+
"kw",
|
|
607
|
+
"kg",
|
|
608
|
+
"la",
|
|
609
|
+
"lv",
|
|
610
|
+
"lb",
|
|
611
|
+
"ls",
|
|
612
|
+
"lr",
|
|
613
|
+
"ly",
|
|
614
|
+
"li",
|
|
615
|
+
"lt",
|
|
616
|
+
"lu",
|
|
617
|
+
"mo",
|
|
618
|
+
"mk",
|
|
619
|
+
"mg",
|
|
620
|
+
"mw",
|
|
621
|
+
"my",
|
|
622
|
+
"mv",
|
|
623
|
+
"ml",
|
|
624
|
+
"mt",
|
|
625
|
+
"mh",
|
|
626
|
+
"mq",
|
|
627
|
+
"mr",
|
|
628
|
+
"mu",
|
|
629
|
+
"yt",
|
|
630
|
+
"mx",
|
|
631
|
+
"fm",
|
|
632
|
+
"md",
|
|
633
|
+
"mc",
|
|
634
|
+
"mn",
|
|
635
|
+
"me",
|
|
636
|
+
"ms",
|
|
637
|
+
"ma",
|
|
638
|
+
"mz",
|
|
639
|
+
"mm",
|
|
640
|
+
"na",
|
|
641
|
+
"nr",
|
|
642
|
+
"np",
|
|
643
|
+
"nl",
|
|
644
|
+
"nc",
|
|
645
|
+
"nz",
|
|
646
|
+
"ni",
|
|
647
|
+
"ne",
|
|
648
|
+
"ng",
|
|
649
|
+
"nu",
|
|
650
|
+
"nf",
|
|
651
|
+
"mp",
|
|
652
|
+
"no",
|
|
653
|
+
"om",
|
|
654
|
+
"pk",
|
|
655
|
+
"pw",
|
|
656
|
+
"ps",
|
|
657
|
+
"pa",
|
|
658
|
+
"pg",
|
|
659
|
+
"py",
|
|
660
|
+
"pe",
|
|
661
|
+
"ph",
|
|
662
|
+
"pn",
|
|
663
|
+
"pl",
|
|
664
|
+
"pt",
|
|
665
|
+
"pr",
|
|
666
|
+
"qa",
|
|
667
|
+
"re",
|
|
668
|
+
"ro",
|
|
669
|
+
"ru",
|
|
670
|
+
"rw",
|
|
671
|
+
"bl",
|
|
672
|
+
"sh",
|
|
673
|
+
"kn",
|
|
674
|
+
"lc",
|
|
675
|
+
"mf",
|
|
676
|
+
"pm",
|
|
677
|
+
"vc",
|
|
678
|
+
"ws",
|
|
679
|
+
"sm",
|
|
680
|
+
"st",
|
|
681
|
+
"sa",
|
|
682
|
+
"sn",
|
|
683
|
+
"rs",
|
|
684
|
+
"sc",
|
|
685
|
+
"sl",
|
|
686
|
+
"sg",
|
|
687
|
+
"sx",
|
|
688
|
+
"sk",
|
|
689
|
+
"si",
|
|
690
|
+
"sb",
|
|
691
|
+
"so",
|
|
692
|
+
"za",
|
|
693
|
+
"gs",
|
|
694
|
+
"ss",
|
|
695
|
+
"es",
|
|
696
|
+
"lk",
|
|
697
|
+
"sd",
|
|
698
|
+
"sr",
|
|
699
|
+
"sj",
|
|
700
|
+
"sz",
|
|
701
|
+
"se",
|
|
702
|
+
"ch",
|
|
703
|
+
"sy",
|
|
704
|
+
"tw",
|
|
705
|
+
"tj",
|
|
706
|
+
"tz",
|
|
707
|
+
"th",
|
|
708
|
+
"tl",
|
|
709
|
+
"tg",
|
|
710
|
+
"tk",
|
|
711
|
+
"to",
|
|
712
|
+
"tt",
|
|
713
|
+
"tn",
|
|
714
|
+
"tr",
|
|
715
|
+
"tm",
|
|
716
|
+
"tc",
|
|
717
|
+
"tv",
|
|
718
|
+
"ug",
|
|
719
|
+
"ua",
|
|
720
|
+
"ae",
|
|
721
|
+
"gb",
|
|
722
|
+
"us",
|
|
723
|
+
"um",
|
|
724
|
+
"uy",
|
|
725
|
+
"uz",
|
|
726
|
+
"vu",
|
|
727
|
+
"ve",
|
|
728
|
+
"vn",
|
|
729
|
+
"vg",
|
|
730
|
+
"vi",
|
|
731
|
+
"wf",
|
|
732
|
+
"eh",
|
|
733
|
+
"ye",
|
|
734
|
+
"zm",
|
|
735
|
+
"zw"
|
|
687
736
|
];
|
|
688
|
-
|
|
737
|
+
/**
|
|
738
|
+
* @internal
|
|
739
|
+
*/
|
|
740
|
+
const CountryCodeSchema = z.enum(CountryCodeList);
|
|
689
741
|
|
|
690
|
-
|
|
691
|
-
|
|
742
|
+
//#endregion
|
|
743
|
+
//#region src/OpenWeatherV2.ts
|
|
744
|
+
const fetchResult = Result.wrapAsyncFn(fetch);
|
|
692
745
|
function toParams(data) {
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
746
|
+
return Object.entries(data).reduce((acc, [key, value]) => {
|
|
747
|
+
acc[key] = String(value);
|
|
748
|
+
return acc;
|
|
749
|
+
}, {});
|
|
697
750
|
}
|
|
698
751
|
function isJson(response) {
|
|
699
|
-
|
|
700
|
-
return contentType?.startsWith("application/json") ?? false;
|
|
752
|
+
return response.headers.get("content-type")?.startsWith("application/json") ?? false;
|
|
701
753
|
}
|
|
702
754
|
function isOpenWeatherError(data) {
|
|
703
|
-
|
|
755
|
+
return typeof data === "object" && data !== null && "cod" in data && "message" in data;
|
|
704
756
|
}
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
757
|
+
const defaultCommonOptions = {
|
|
758
|
+
lang: "en",
|
|
759
|
+
units: "standard"
|
|
708
760
|
};
|
|
709
761
|
function buildOpts(opts) {
|
|
710
|
-
|
|
762
|
+
return Object.assign({}, defaultCommonOptions, opts);
|
|
711
763
|
}
|
|
712
|
-
|
|
764
|
+
const basePath = "https://api.openweathermap.org/data/2.5/weather";
|
|
713
765
|
function buildUrl(params) {
|
|
714
|
-
|
|
766
|
+
return `${basePath}?${params.toString()}`;
|
|
715
767
|
}
|
|
716
768
|
function buildLogUrl(params) {
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
769
|
+
const logParams = new URLSearchParams(params);
|
|
770
|
+
logParams.set("appid", "***");
|
|
771
|
+
return buildUrl(logParams);
|
|
720
772
|
}
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
773
|
+
const defaultImplementation = { dataWeatherApi: async (params) => {
|
|
774
|
+
const logUrl = buildLogUrl(params);
|
|
775
|
+
const result = await fetchResult(buildUrl(params));
|
|
776
|
+
if (!result.isOk) return Err(result.err());
|
|
777
|
+
const res = result.ok();
|
|
778
|
+
if (!res.ok) {
|
|
779
|
+
if (isJson(res)) {
|
|
780
|
+
const data = await res.json();
|
|
781
|
+
if (isOpenWeatherError(data)) return Err(/* @__PURE__ */ new TypeError(`OpenWeatherV2 error: ${data.message} from ${logUrl}`));
|
|
782
|
+
}
|
|
783
|
+
return Err(/* @__PURE__ */ new TypeError(`OpenWeatherV2 http error: ${res.status} ${res.statusText} from ${logUrl}`));
|
|
784
|
+
}
|
|
785
|
+
if (!isJson(res)) return Err(/* @__PURE__ */ new TypeError(`OpenWeatherV2 response is not json payload from ${logUrl}`));
|
|
786
|
+
const jsonResult = await Result.safeAsyncCall(() => res.json());
|
|
787
|
+
if (!jsonResult.isOk) return Err(jsonResult.err());
|
|
788
|
+
const data = jsonResult.ok();
|
|
789
|
+
assertWeatherDataV2(data);
|
|
790
|
+
return Ok(data);
|
|
791
|
+
} };
|
|
792
|
+
/**
|
|
793
|
+
* Open Weather V2 API
|
|
794
|
+
* @example
|
|
795
|
+
* const weather = new OpenWeatherV2('your-api-key');
|
|
796
|
+
*
|
|
797
|
+
* const cache = new ExpireCache<WeatherDataV2>(undefined, undefined, 900000); // data 15 minutes in cache
|
|
798
|
+
* const weather = new OpenWeatherV2(() => Promise.resolve('your-api-key'), cache);
|
|
799
|
+
*
|
|
800
|
+
* const data: WeatherDataV2 = (await weather.getWeatherById(2643743)).unwrap(); // throws if error
|
|
801
|
+
* const data: WeatherDataV2 | undefined = (await weather.getWeatherByCity('Helsinki', 'fi')).ok();
|
|
802
|
+
*
|
|
803
|
+
* const result: Result<WeatherDataV2> = await weather.getWeatherByLatLon(60.1699, 24.9384);
|
|
804
|
+
* result.match({
|
|
805
|
+
* Ok: (data: WeatherDataV2) => console.log(data),
|
|
806
|
+
* Err: (err: DOMException | TypeError) => console.error(err),
|
|
807
|
+
* });
|
|
808
|
+
*
|
|
809
|
+
* if(result.isOk) {
|
|
810
|
+
* const data: WeatherDataV2 = data.ok();
|
|
811
|
+
* } else {
|
|
812
|
+
* const err: DOMException | TypeError = data.err();
|
|
813
|
+
* }
|
|
814
|
+
* @since v0.0.1
|
|
815
|
+
*/
|
|
750
816
|
var OpenWeatherV2 = class {
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
}
|
|
885
|
-
return data;
|
|
886
|
-
}
|
|
887
|
-
};
|
|
888
|
-
export {
|
|
889
|
-
CountryCodeList,
|
|
890
|
-
CountryCodeSchema,
|
|
891
|
-
OpenWeatherV2,
|
|
892
|
-
assertWeatherDataV2,
|
|
893
|
-
fetchErrorWrapper,
|
|
894
|
-
getWeatherV2Description,
|
|
895
|
-
iconSchema,
|
|
896
|
-
isWeatherDataV2,
|
|
897
|
-
langCodeSchema,
|
|
898
|
-
langCodes,
|
|
899
|
-
weatherDataV2Schema,
|
|
900
|
-
weatherIdGroup,
|
|
901
|
-
weatherIdSchema
|
|
817
|
+
cache;
|
|
818
|
+
loadableApiKey;
|
|
819
|
+
apiHandler;
|
|
820
|
+
fetchPromiseMap = /* @__PURE__ */ new Map();
|
|
821
|
+
/**
|
|
822
|
+
* OpenWeatherV2 constructor
|
|
823
|
+
* @param {Loadable<string>} loadableApiKey - Loadable API key
|
|
824
|
+
* @param {ICacheOrAsync<WeatherDataV2>=} cache - optional async cache implementation
|
|
825
|
+
* @param {IOpenWeatherV2=} apiHandler - optional API handler implementation for mocking
|
|
826
|
+
*/
|
|
827
|
+
constructor(loadableApiKey, cache, apiHandler = defaultImplementation) {
|
|
828
|
+
this.loadableApiKey = loadableApiKey;
|
|
829
|
+
this.cache = cache;
|
|
830
|
+
this.apiHandler = apiHandler;
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* get weather by Id
|
|
834
|
+
* @param {number} id - Weather station ID
|
|
835
|
+
* @param {OpenWeatherV2CommonOptions=} currentOpts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```
|
|
836
|
+
* @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise
|
|
837
|
+
* @example
|
|
838
|
+
* const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherResultById(id: 564, {lang: 'fi'});
|
|
839
|
+
* if (result.isOk) {
|
|
840
|
+
* const weatherData: WeatherDataV2 = result.ok();
|
|
841
|
+
* } else {
|
|
842
|
+
* const error: DOMException | TypeError = result.err();
|
|
843
|
+
* }
|
|
844
|
+
*/
|
|
845
|
+
async getWeatherById(id, currentOpts = {}) {
|
|
846
|
+
try {
|
|
847
|
+
const opts = buildOpts(currentOpts);
|
|
848
|
+
const cacheKey = this.buildBaseCacheKey(`id:${id}`, opts);
|
|
849
|
+
let cacheEntry = this.cache && await this.cache.get(cacheKey);
|
|
850
|
+
if (!cacheEntry) {
|
|
851
|
+
const params = await this.buildBaseParams(opts);
|
|
852
|
+
params.append("id", String(id));
|
|
853
|
+
cacheEntry = await this.handleFetch(cacheKey, params, opts);
|
|
854
|
+
}
|
|
855
|
+
return Ok(cacheEntry);
|
|
856
|
+
} catch (err) {
|
|
857
|
+
return Err(fetchErrorWrapper(err));
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* get weather with city name and optional country code
|
|
862
|
+
* @param {string} city - City name
|
|
863
|
+
* @param {countryCode=} countryCode - Optional Country code
|
|
864
|
+
* @param {OpenWeatherV2CommonOptions=} currentOpts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```
|
|
865
|
+
* @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise
|
|
866
|
+
* @example
|
|
867
|
+
* const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherByCity('Helsinki', 'fi', {lang: 'fi'});
|
|
868
|
+
* if (result.isOk) {
|
|
869
|
+
* const weatherData: WeatherDataV2 = result.ok();
|
|
870
|
+
* } else {
|
|
871
|
+
* const error: DOMException | TypeError = result.err();
|
|
872
|
+
* }
|
|
873
|
+
*/
|
|
874
|
+
async getWeatherByCity(city, countryCode, currentOpts = {}) {
|
|
875
|
+
try {
|
|
876
|
+
const opts = buildOpts(currentOpts);
|
|
877
|
+
const cacheKey = this.buildBaseCacheKey(`q:${city}:${countryCode}`, opts);
|
|
878
|
+
let cacheEntry = this.cache && await this.cache.get(cacheKey);
|
|
879
|
+
if (!cacheEntry) {
|
|
880
|
+
const params = await this.buildBaseParams(opts);
|
|
881
|
+
params.append("q", countryCode ? `${city},${countryCode}` : city);
|
|
882
|
+
cacheEntry = await this.handleFetch(cacheKey, params, opts);
|
|
883
|
+
}
|
|
884
|
+
return Ok(cacheEntry);
|
|
885
|
+
} catch (err) {
|
|
886
|
+
return Err(fetchErrorWrapper(err));
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* get weather with latitude and longitude with Result
|
|
891
|
+
* @param {number} lat - Latitude
|
|
892
|
+
* @param {number} lon - Longitude
|
|
893
|
+
* @param {OpenWeatherV2CommonOptions=} currentOpts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```
|
|
894
|
+
* @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise
|
|
895
|
+
* @example
|
|
896
|
+
* const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherByLatLon(60.1699, 24.9384, {lang: 'fi'});
|
|
897
|
+
* if (result.isOk) {
|
|
898
|
+
* const weatherData: WeatherDataV2 = result.ok();
|
|
899
|
+
* } else {
|
|
900
|
+
* const error: DOMException | TypeError = result.err();
|
|
901
|
+
* }
|
|
902
|
+
*/
|
|
903
|
+
async getWeatherByLatLon(lat, lon, currentOpts = {}) {
|
|
904
|
+
try {
|
|
905
|
+
const opts = buildOpts(currentOpts);
|
|
906
|
+
const cacheKey = this.buildBaseCacheKey(`latlon:${lat}:${lon}`, opts);
|
|
907
|
+
let cacheEntry = this.cache && await this.cache.get(cacheKey);
|
|
908
|
+
if (!cacheEntry) {
|
|
909
|
+
const params = await this.buildBaseParams(opts);
|
|
910
|
+
params.append("lat", String(lat));
|
|
911
|
+
params.append("lon", String(lon));
|
|
912
|
+
cacheEntry = await this.handleFetch(cacheKey, params, opts);
|
|
913
|
+
}
|
|
914
|
+
return Ok(cacheEntry);
|
|
915
|
+
} catch (err) {
|
|
916
|
+
return Err(fetchErrorWrapper(err));
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
async buildBaseParams(options) {
|
|
920
|
+
const apiKey = await (typeof this.loadableApiKey === "function" ? this.loadableApiKey() : this.loadableApiKey);
|
|
921
|
+
const params = new URLSearchParams(toParams(options));
|
|
922
|
+
params.append("appid", apiKey);
|
|
923
|
+
return params;
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* build base cache key
|
|
927
|
+
* @param main - main cache key prefix
|
|
928
|
+
* @param opts - OpenWeatherV2CommonOptions
|
|
929
|
+
* @returns {string} cache key
|
|
930
|
+
*/
|
|
931
|
+
buildBaseCacheKey(main, { lang, units }) {
|
|
932
|
+
return `${main}:${lang}:${units}`;
|
|
933
|
+
}
|
|
934
|
+
async handleFetch(cacheKey, params, opts) {
|
|
935
|
+
let promiseResult = this.fetchPromiseMap.get(cacheKey);
|
|
936
|
+
if (!promiseResult) {
|
|
937
|
+
promiseResult = this.apiHandler.dataWeatherApi(params);
|
|
938
|
+
this.fetchPromiseMap.set(cacheKey, promiseResult);
|
|
939
|
+
await promiseResult;
|
|
940
|
+
this.fetchPromiseMap.delete(cacheKey);
|
|
941
|
+
}
|
|
942
|
+
const data = (await promiseResult).unwrap();
|
|
943
|
+
assertWeatherDataV2(data);
|
|
944
|
+
if (this.cache) {
|
|
945
|
+
await this.cache.set(cacheKey, data);
|
|
946
|
+
if (!cacheKey.startsWith("id:")) await this.cache.set(this.buildBaseCacheKey(`id:${data.id}`, opts), data);
|
|
947
|
+
}
|
|
948
|
+
return data;
|
|
949
|
+
}
|
|
902
950
|
};
|
|
951
|
+
|
|
952
|
+
//#endregion
|
|
953
|
+
export { CountryCodeList, CountryCodeSchema, OpenWeatherV2, assertWeatherDataV2, fetchErrorWrapper, getWeatherV2Description, iconSchema, isWeatherDataV2, langCodeSchema, langCodes, weatherDataV2Schema, weatherIdGroup, weatherIdSchema };
|
|
903
954
|
//# sourceMappingURL=index.mjs.map
|