@mharj/openweathermap 0.0.7 → 0.1.0

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