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