@mharj/openweathermap 0.0.6 → 0.0.8

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