New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@mharj/openweathermap

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mharj/openweathermap - npm Package Compare versions

Comparing version 0.0.7 to 0.0.8

dist/index.d.mts

652

dist/index.d.ts

@@ -1,4 +0,648 @@

export * from './OpenWeatherV2';
export * from './types';
export * from './lib';
export * from './interfaces';
import { IOption, IResult } from '@luolapeikko/result-option';
import { IAsyncCache } from '@luolapeikko/cache-types';
import { z } from 'zod';
/**
* @internal
*/
declare const dayIconListSchema: z.ZodEnum<["01d", "02d", "03d", "04d", "09d", "10d", "11d", "13d", "50d"]>;
type DayIcon = z.infer<typeof dayIconListSchema>;
/**
* @internal
*/
declare const nightIconListSchema: z.ZodEnum<["01n", "02n", "03n", "04n", "09n", "10n", "11n", "13n", "50n"]>;
type NightIcon = z.infer<typeof nightIconListSchema>;
/**
* @internal
*/
declare const iconSchema: z.ZodUnion<[z.ZodEnum<["01d", "02d", "03d", "04d", "09d", "10d", "11d", "13d", "50d"]>, z.ZodEnum<["01n", "02n", "03n", "04n", "09n", "10n", "11n", "13n", "50n"]>]>;
type Icon = z.infer<typeof iconSchema>;
declare const langCodes: readonly ["af", "al", "ar", "az", "bg", "ca", "cz", "da", "de", "el", "en", "eu", "fa", "fi", "fr", "gl", "he", "hi", "hr", "hu", "id", "it", "ja", "kr", "la", "lt", "mk", "no", "nl", "pl", "pt", "pt", "ro", "ru", "sv", "sk", "sl", "sp", "sr", "th", "tr", "ua", "vi", "zh_cn", "zh_tw", "zu"];
declare const langCodeSchema: z.ZodEnum<["af", "al", "ar", "az", "bg", "ca", "cz", "da", "de", "el", "en", "eu", "fa", "fi", "fr", "gl", "he", "hi", "hr", "hu", "id", "it", "ja", "kr", "la", "lt", "mk", "no", "nl", "pl", "pt", "pt", "ro", "ru", "sv", "sk", "sl", "sp", "sr", "th", "tr", "ua", "vi", "zh_cn", "zh_tw", "zu"]>;
type LangCode = z.infer<typeof langCodeSchema>;
/**
* This is a list of weather ids, groups and descriptions from OpenWeatherMap API
*/
declare const weatherIdGroup: readonly [{
readonly id: 200;
readonly group: "thunderstorm";
readonly description: "thunderstorm_with_light_rain";
}, {
readonly id: 201;
readonly group: "thunderstorm";
readonly description: "thunderstorm_with_rain";
}, {
readonly id: 202;
readonly group: "thunderstorm";
readonly description: "thunderstorm_with_heavy_rain";
}, {
readonly id: 210;
readonly group: "thunderstorm";
readonly description: "light_thunderstorm";
}, {
readonly id: 211;
readonly group: "thunderstorm";
readonly description: "thunderstorm";
}, {
readonly id: 212;
readonly group: "thunderstorm";
readonly description: "heavy_thunderstorm";
}, {
readonly id: 221;
readonly group: "thunderstorm";
readonly description: "ragged_thunderstorm";
}, {
readonly id: 230;
readonly group: "thunderstorm";
readonly description: "thunderstorm_with_light_drizzle";
}, {
readonly id: 231;
readonly group: "thunderstorm";
readonly description: "thunderstorm_with_drizzle";
}, {
readonly id: 232;
readonly group: "thunderstorm";
readonly description: "thunderstorm_with_heavy_drizzle";
}, {
readonly id: 300;
readonly group: "drizzle";
readonly description: "light_intensity_drizzle";
}, {
readonly id: 301;
readonly group: "drizzle";
readonly description: "drizzle";
}, {
readonly id: 302;
readonly group: "drizzle";
readonly description: "heavy_intensity_drizzle";
}, {
readonly id: 310;
readonly group: "drizzle";
readonly description: "light_intensity_drizzle_rain";
}, {
readonly id: 311;
readonly group: "drizzle";
readonly description: "drizzle_rain";
}, {
readonly id: 312;
readonly group: "drizzle";
readonly description: "heavy_intensity_drizzle_rain";
}, {
readonly id: 313;
readonly group: "drizzle";
readonly description: "shower_rain_and_drizzle";
}, {
readonly id: 314;
readonly group: "drizzle";
readonly description: "heavy_shower_rain_and_drizzle";
}, {
readonly id: 321;
readonly group: "drizzle";
readonly description: "shower_drizzle";
}, {
readonly id: 500;
readonly group: "rain";
readonly description: "light_rain";
}, {
readonly id: 501;
readonly group: "rain";
readonly description: "moderate_rain";
}, {
readonly id: 502;
readonly group: "rain";
readonly description: "heavy_intensity_rain";
}, {
readonly id: 503;
readonly group: "rain";
readonly description: "very_heavy_rain";
}, {
readonly id: 504;
readonly group: "rain";
readonly description: "extreme_rain";
}, {
readonly id: 511;
readonly group: "rain";
readonly description: "freezing_rain";
}, {
readonly id: 520;
readonly group: "rain";
readonly description: "light_intensity_shower_rain";
}, {
readonly id: 521;
readonly group: "rain";
readonly description: "shower_rain";
}, {
readonly id: 522;
readonly group: "rain";
readonly description: "heavy_intensity_shower_rain";
}, {
readonly id: 531;
readonly group: "rain";
readonly description: "ragged_shower_rain";
}, {
readonly id: 600;
readonly group: "snow";
readonly description: "light_snow";
}, {
readonly id: 601;
readonly group: "snow";
readonly description: "snow";
}, {
readonly id: 602;
readonly group: "snow";
readonly description: "heavy_snow";
}, {
readonly id: 611;
readonly group: "snow";
readonly description: "sleet";
}, {
readonly id: 612;
readonly group: "snow";
readonly description: "light_shower_sleet";
}, {
readonly id: 613;
readonly group: "snow";
readonly description: "shower_sleet";
}, {
readonly id: 615;
readonly group: "snow";
readonly description: "light_rain_and_snow";
}, {
readonly id: 616;
readonly group: "snow";
readonly description: "rain_and_snow";
}, {
readonly id: 620;
readonly group: "snow";
readonly description: "light_shower_snow";
}, {
readonly id: 621;
readonly group: "snow";
readonly description: "shower_snow";
}, {
readonly id: 622;
readonly group: "snow";
readonly description: "heavy_shower_snow";
}, {
readonly id: 701;
readonly group: "atmosphere";
readonly description: "mist";
}, {
readonly id: 711;
readonly group: "atmosphere";
readonly description: "smoke";
}, {
readonly id: 721;
readonly group: "atmosphere";
readonly description: "haze";
}, {
readonly id: 731;
readonly group: "atmosphere";
readonly description: "sand_dust_whirls";
}, {
readonly id: 741;
readonly group: "atmosphere";
readonly description: "fog";
}, {
readonly id: 751;
readonly group: "atmosphere";
readonly description: "sand";
}, {
readonly id: 761;
readonly group: "atmosphere";
readonly description: "dust";
}, {
readonly id: 762;
readonly group: "atmosphere";
readonly description: "volcanic_ash";
}, {
readonly id: 771;
readonly group: "atmosphere";
readonly description: "squalls";
}, {
readonly id: 781;
readonly group: "atmosphere";
readonly description: "tornado";
}, {
readonly id: 800;
readonly group: "clear";
readonly description: "clear_sky";
}, {
readonly id: 801;
readonly group: "clouds";
readonly description: "few_clouds_11-25_percent";
}, {
readonly id: 802;
readonly group: "clouds";
readonly description: "scattered_clouds_25-50_percent";
}, {
readonly id: 803;
readonly group: "clouds";
readonly description: "broken_clouds_51-84_percent";
}, {
readonly id: 804;
readonly group: "clouds";
readonly description: "overcast_clouds_85-100_percent";
}];
type WeatherID = (typeof weatherIdGroup)[number]['id'];
/**
* WeatherGroup: "clouds" | "rain" | "snow" | "thunderstorm" | "drizzle" | "atmosphere" | "clear"
*/
type WeatherGroup = (typeof weatherIdGroup)[number]['group'];
/**
* List for weather description key types from OpenWeatherMap API based on weather id.
*
* This list of keys can be used to translate weather id to human readable description with different languages.
* @example
* const i18Weather: Record<WeatherDescription, string> = {
* clear_sky: 'Clear sky',
* ...
* }
*/
type WeatherDescription = (typeof weatherIdGroup)[number]['description'];
/**
* Weather id schema
* @internal
*/
declare const weatherIdSchema: z.ZodType<200 | 201 | 202 | 210 | 211 | 212 | 221 | 230 | 231 | 232 | 300 | 301 | 302 | 310 | 311 | 312 | 313 | 314 | 321 | 500 | 501 | 502 | 503 | 504 | 511 | 520 | 521 | 522 | 531 | 600 | 601 | 602 | 611 | 612 | 613 | 615 | 616 | 620 | 621 | 622 | 701 | 711 | 721 | 731 | 741 | 751 | 761 | 762 | 771 | 781 | 800 | 801 | 802 | 803 | 804, z.ZodTypeDef, 200 | 201 | 202 | 210 | 211 | 212 | 221 | 230 | 231 | 232 | 300 | 301 | 302 | 310 | 311 | 312 | 313 | 314 | 321 | 500 | 501 | 502 | 503 | 504 | 511 | 520 | 521 | 522 | 531 | 600 | 601 | 602 | 611 | 612 | 613 | 615 | 616 | 620 | 621 | 622 | 701 | 711 | 721 | 731 | 741 | 751 | 761 | 762 | 771 | 781 | 800 | 801 | 802 | 803 | 804>;
/**
* get weather description key from weather id
* @param id - weather id
* @returns {Option<WeatherDescription>} option for weather description key
* @example
* const weatherComponent = ({data}) => {
* const key = getWeatherV2Description(data.weather[0]?.id).unwrapOr('unknown');
* return (
* <div>
* {t(`weather:${key}`}
* </div>
* );
* }
*/
declare function getWeatherV2Description(id: WeatherID | undefined): IOption<WeatherDescription>;
/**
* @internal
*/
declare const weatherDataV2Schema: z.ZodObject<{
base: z.ZodString;
clouds: z.ZodObject<{
all: z.ZodNumber;
}, "strip", z.ZodTypeAny, {
all: number;
}, {
all: number;
}>;
cod: z.ZodNumber;
coord: z.ZodObject<{
lon: z.ZodNumber;
lat: z.ZodNumber;
}, "strip", z.ZodTypeAny, {
lon: number;
lat: number;
}, {
lon: number;
lat: number;
}>;
dt: z.ZodNumber;
id: z.ZodNumber;
main: z.ZodObject<{
grnd_level: z.ZodOptional<z.ZodNumber>;
humidity: z.ZodNumber;
pressure: z.ZodNumber;
sea_level: z.ZodOptional<z.ZodNumber>;
temp: z.ZodNumber;
temp_max: z.ZodNumber;
temp_min: z.ZodNumber;
}, "strip", z.ZodTypeAny, {
humidity: number;
pressure: number;
temp: number;
temp_max: number;
temp_min: number;
grnd_level?: number | undefined;
sea_level?: number | undefined;
}, {
humidity: number;
pressure: number;
temp: number;
temp_max: number;
temp_min: number;
grnd_level?: number | undefined;
sea_level?: number | undefined;
}>;
name: z.ZodString;
rain: z.ZodOptional<z.ZodObject<{
'1h': z.ZodOptional<z.ZodNumber>;
'3h': z.ZodOptional<z.ZodNumber>;
}, "strip", z.ZodTypeAny, {
'1h'?: number | undefined;
'3h'?: number | undefined;
}, {
'1h'?: number | undefined;
'3h'?: number | undefined;
}>>;
snow: z.ZodOptional<z.ZodObject<{
'1h': z.ZodOptional<z.ZodNumber>;
'3h': z.ZodOptional<z.ZodNumber>;
}, "strip", z.ZodTypeAny, {
'1h'?: number | undefined;
'3h'?: number | undefined;
}, {
'1h'?: number | undefined;
'3h'?: number | undefined;
}>>;
sys: z.ZodObject<{
country: z.ZodString;
id: z.ZodOptional<z.ZodNumber>;
message: z.ZodOptional<z.ZodNumber>;
sunrise: z.ZodNumber;
sunset: z.ZodNumber;
type: z.ZodOptional<z.ZodNumber>;
}, "strip", z.ZodTypeAny, {
country: string;
sunrise: number;
sunset: number;
message?: number | undefined;
type?: number | undefined;
id?: number | undefined;
}, {
country: string;
sunrise: number;
sunset: number;
message?: number | undefined;
type?: number | undefined;
id?: number | undefined;
}>;
timezone: z.ZodNumber;
visibility: z.ZodNumber;
weather: z.ZodArray<z.ZodObject<{
description: z.ZodString;
icon: z.ZodUnion<[z.ZodEnum<["01d", "02d", "03d", "04d", "09d", "10d", "11d", "13d", "50d"]>, z.ZodEnum<["01n", "02n", "03n", "04n", "09n", "10n", "11n", "13n", "50n"]>]>;
id: z.ZodType<200 | 201 | 202 | 210 | 211 | 212 | 221 | 230 | 231 | 232 | 300 | 301 | 302 | 310 | 311 | 312 | 313 | 314 | 321 | 500 | 501 | 502 | 503 | 504 | 511 | 520 | 521 | 522 | 531 | 600 | 601 | 602 | 611 | 612 | 613 | 615 | 616 | 620 | 621 | 622 | 701 | 711 | 721 | 731 | 741 | 751 | 761 | 762 | 771 | 781 | 800 | 801 | 802 | 803 | 804, z.ZodTypeDef, 200 | 201 | 202 | 210 | 211 | 212 | 221 | 230 | 231 | 232 | 300 | 301 | 302 | 310 | 311 | 312 | 313 | 314 | 321 | 500 | 501 | 502 | 503 | 504 | 511 | 520 | 521 | 522 | 531 | 600 | 601 | 602 | 611 | 612 | 613 | 615 | 616 | 620 | 621 | 622 | 701 | 711 | 721 | 731 | 741 | 751 | 761 | 762 | 771 | 781 | 800 | 801 | 802 | 803 | 804>;
main: z.ZodString;
}, "strip", z.ZodTypeAny, {
id: 200 | 201 | 202 | 210 | 211 | 212 | 221 | 230 | 231 | 232 | 300 | 301 | 302 | 310 | 311 | 312 | 313 | 314 | 321 | 500 | 501 | 502 | 503 | 504 | 511 | 520 | 521 | 522 | 531 | 600 | 601 | 602 | 611 | 612 | 613 | 615 | 616 | 620 | 621 | 622 | 701 | 711 | 721 | 731 | 741 | 751 | 761 | 762 | 771 | 781 | 800 | 801 | 802 | 803 | 804;
description: string;
icon: "01d" | "02d" | "03d" | "04d" | "09d" | "10d" | "11d" | "13d" | "50d" | "01n" | "02n" | "03n" | "04n" | "09n" | "10n" | "11n" | "13n" | "50n";
main: string;
}, {
id: 200 | 201 | 202 | 210 | 211 | 212 | 221 | 230 | 231 | 232 | 300 | 301 | 302 | 310 | 311 | 312 | 313 | 314 | 321 | 500 | 501 | 502 | 503 | 504 | 511 | 520 | 521 | 522 | 531 | 600 | 601 | 602 | 611 | 612 | 613 | 615 | 616 | 620 | 621 | 622 | 701 | 711 | 721 | 731 | 741 | 751 | 761 | 762 | 771 | 781 | 800 | 801 | 802 | 803 | 804;
description: string;
icon: "01d" | "02d" | "03d" | "04d" | "09d" | "10d" | "11d" | "13d" | "50d" | "01n" | "02n" | "03n" | "04n" | "09n" | "10n" | "11n" | "13n" | "50n";
main: string;
}>, "many">;
wind: z.ZodObject<{
speed: z.ZodNumber;
deg: z.ZodNumber;
}, "strip", z.ZodTypeAny, {
speed: number;
deg: number;
}, {
speed: number;
deg: number;
}>;
}, "strip", z.ZodTypeAny, {
clouds: {
all: number;
};
id: number;
main: {
humidity: number;
pressure: number;
temp: number;
temp_max: number;
temp_min: number;
grnd_level?: number | undefined;
sea_level?: number | undefined;
};
base: string;
cod: number;
coord: {
lon: number;
lat: number;
};
dt: number;
name: string;
sys: {
country: string;
sunrise: number;
sunset: number;
message?: number | undefined;
type?: number | undefined;
id?: number | undefined;
};
timezone: number;
visibility: number;
weather: {
id: 200 | 201 | 202 | 210 | 211 | 212 | 221 | 230 | 231 | 232 | 300 | 301 | 302 | 310 | 311 | 312 | 313 | 314 | 321 | 500 | 501 | 502 | 503 | 504 | 511 | 520 | 521 | 522 | 531 | 600 | 601 | 602 | 611 | 612 | 613 | 615 | 616 | 620 | 621 | 622 | 701 | 711 | 721 | 731 | 741 | 751 | 761 | 762 | 771 | 781 | 800 | 801 | 802 | 803 | 804;
description: string;
icon: "01d" | "02d" | "03d" | "04d" | "09d" | "10d" | "11d" | "13d" | "50d" | "01n" | "02n" | "03n" | "04n" | "09n" | "10n" | "11n" | "13n" | "50n";
main: string;
}[];
wind: {
speed: number;
deg: number;
};
rain?: {
'1h'?: number | undefined;
'3h'?: number | undefined;
} | undefined;
snow?: {
'1h'?: number | undefined;
'3h'?: number | undefined;
} | undefined;
}, {
clouds: {
all: number;
};
id: number;
main: {
humidity: number;
pressure: number;
temp: number;
temp_max: number;
temp_min: number;
grnd_level?: number | undefined;
sea_level?: number | undefined;
};
base: string;
cod: number;
coord: {
lon: number;
lat: number;
};
dt: number;
name: string;
sys: {
country: string;
sunrise: number;
sunset: number;
message?: number | undefined;
type?: number | undefined;
id?: number | undefined;
};
timezone: number;
visibility: number;
weather: {
id: 200 | 201 | 202 | 210 | 211 | 212 | 221 | 230 | 231 | 232 | 300 | 301 | 302 | 310 | 311 | 312 | 313 | 314 | 321 | 500 | 501 | 502 | 503 | 504 | 511 | 520 | 521 | 522 | 531 | 600 | 601 | 602 | 611 | 612 | 613 | 615 | 616 | 620 | 621 | 622 | 701 | 711 | 721 | 731 | 741 | 751 | 761 | 762 | 771 | 781 | 800 | 801 | 802 | 803 | 804;
description: string;
icon: "01d" | "02d" | "03d" | "04d" | "09d" | "10d" | "11d" | "13d" | "50d" | "01n" | "02n" | "03n" | "04n" | "09n" | "10n" | "11n" | "13n" | "50n";
main: string;
}[];
wind: {
speed: number;
deg: number;
};
rain?: {
'1h'?: number | undefined;
'3h'?: number | undefined;
} | undefined;
snow?: {
'1h'?: number | undefined;
'3h'?: number | undefined;
} | undefined;
}>;
type WeatherDataV2 = z.infer<typeof weatherDataV2Schema>;
declare function isWeatherDataV2(data: unknown): data is WeatherDataV2;
declare function assertWeatherDataV2(data: unknown): asserts data is WeatherDataV2;
declare const CountryCodeList: readonly ["af", "ax", "al", "dz", "as", "ad", "ao", "ai", "aq", "ag", "ar", "am", "aw", "au", "at", "az", "bs", "bh", "bd", "bb", "by", "be", "bz", "bj", "bm", "bt", "bo", "bq", "ba", "bw", "bv", "br", "io", "bn", "bg", "bf", "bi", "kh", "cm", "ca", "cv", "ky", "cf", "td", "cl", "cn", "cx", "cc", "co", "km", "cg", "cd", "ck", "cr", "ci", "hr", "cu", "cw", "cy", "cz", "dk", "dj", "dm", "do", "ec", "eg", "sv", "gq", "er", "ee", "et", "fk", "fo", "fj", "fi", "fr", "gf", "pf", "tf", "ga", "gm", "ge", "de", "gh", "gi", "gr", "gl", "gd", "gp", "gu", "gt", "gg", "gn", "gw", "gy", "ht", "hm", "va", "hn", "hk", "hu", "is", "in", "id", "ir", "iq", "ie", "im", "il", "it", "jm", "jp", "je", "jo", "kz", "ke", "ki", "kp", "kr", "kw", "kg", "la", "lv", "lb", "ls", "lr", "ly", "li", "lt", "lu", "mo", "mk", "mg", "mw", "my", "mv", "ml", "mt", "mh", "mq", "mr", "mu", "yt", "mx", "fm", "md", "mc", "mn", "me", "ms", "ma", "mz", "mm", "na", "nr", "np", "nl", "nc", "nz", "ni", "ne", "ng", "nu", "nf", "mp", "no", "om", "pk", "pw", "ps", "pa", "pg", "py", "pe", "ph", "pn", "pl", "pt", "pr", "qa", "re", "ro", "ru", "rw", "bl", "sh", "kn", "lc", "mf", "pm", "vc", "ws", "sm", "st", "sa", "sn", "rs", "sc", "sl", "sg", "sx", "sk", "si", "sb", "so", "za", "gs", "ss", "es", "lk", "sd", "sr", "sj", "sz", "se", "ch", "sy", "tw", "tj", "tz", "th", "tl", "tg", "tk", "to", "tt", "tn", "tr", "tm", "tc", "tv", "ug", "ua", "ae", "gb", "us", "um", "uy", "uz", "vu", "ve", "vn", "vg", "vi", "wf", "eh", "ye", "zm", "zw"];
/**
* @internal
*/
declare const CountryCodeSchema: z.ZodEnum<["af", "ax", "al", "dz", "as", "ad", "ao", "ai", "aq", "ag", "ar", "am", "aw", "au", "at", "az", "bs", "bh", "bd", "bb", "by", "be", "bz", "bj", "bm", "bt", "bo", "bq", "ba", "bw", "bv", "br", "io", "bn", "bg", "bf", "bi", "kh", "cm", "ca", "cv", "ky", "cf", "td", "cl", "cn", "cx", "cc", "co", "km", "cg", "cd", "ck", "cr", "ci", "hr", "cu", "cw", "cy", "cz", "dk", "dj", "dm", "do", "ec", "eg", "sv", "gq", "er", "ee", "et", "fk", "fo", "fj", "fi", "fr", "gf", "pf", "tf", "ga", "gm", "ge", "de", "gh", "gi", "gr", "gl", "gd", "gp", "gu", "gt", "gg", "gn", "gw", "gy", "ht", "hm", "va", "hn", "hk", "hu", "is", "in", "id", "ir", "iq", "ie", "im", "il", "it", "jm", "jp", "je", "jo", "kz", "ke", "ki", "kp", "kr", "kw", "kg", "la", "lv", "lb", "ls", "lr", "ly", "li", "lt", "lu", "mo", "mk", "mg", "mw", "my", "mv", "ml", "mt", "mh", "mq", "mr", "mu", "yt", "mx", "fm", "md", "mc", "mn", "me", "ms", "ma", "mz", "mm", "na", "nr", "np", "nl", "nc", "nz", "ni", "ne", "ng", "nu", "nf", "mp", "no", "om", "pk", "pw", "ps", "pa", "pg", "py", "pe", "ph", "pn", "pl", "pt", "pr", "qa", "re", "ro", "ru", "rw", "bl", "sh", "kn", "lc", "mf", "pm", "vc", "ws", "sm", "st", "sa", "sn", "rs", "sc", "sl", "sg", "sx", "sk", "si", "sb", "so", "za", "gs", "ss", "es", "lk", "sd", "sr", "sj", "sz", "se", "ch", "sy", "tw", "tj", "tz", "th", "tl", "tg", "tk", "to", "tt", "tn", "tr", "tm", "tc", "tv", "ug", "ua", "ae", "gb", "us", "um", "uy", "uz", "vu", "ve", "vn", "vg", "vi", "wf", "eh", "ye", "zm", "zw"]>;
type CountryCode = z.infer<typeof CountryCodeSchema>;
/**
* Generic argument which can be value, Promise of value, function which returns value or Promise of value.
* @example
* function solveLoadable(value: Loadable<string>): string | Promise<string> {
* return typeof value === 'function' ? value() : value;
* }
*/
type Loadable<T> = T | Promise<T> | (() => T) | (() => Promise<T>);
declare function solveLoadable(value: Loadable<string>): string | Promise<string>;
/**
* Interface for OpenWeatherMap API v2 implementation.
*/
interface IOpenWeatherV2 {
dataWeatherApi: (params: URLSearchParams) => Promise<IResult<WeatherDataV2, SyntaxError | TypeError>>;
}
/**
* Open Weather V2 API Common Options
* @default {lang: 'en', units: 'standard'} in API
* @example
* {lang: 'fi', units: 'metric'}
*/
type OpenWeatherV2CommonOptions = {
/**
* Language code
*/
lang?: LangCode;
/**
* Weather units
*/
units?: 'standard' | 'metric' | 'imperial';
};
/**
* Open Weather V2 API
* @example
* const weather = new OpenWeatherV2('your-api-key');
*
* const cache = new ExpireCache<WeatherDataV2>(undefined, undefined, 900000); // data 15 minutes in cache
* const weather = new OpenWeatherV2(() => Promise.resolve('your-api-key'), cache);
*
* const data: WeatherDataV2 = (await weather.getWeatherById(2643743)).unwrap(); // throws if error
* const data: WeatherDataV2 | undefined = (await weather.getWeatherByCity('Helsinki', 'fi')).ok();
*
* const result: Result<WeatherDataV2> = await weather.getWeatherByLatLon(60.1699, 24.9384);
* result.match({
* Ok: (data: WeatherDataV2) => console.log(data),
* Err: (err: DOMException | TypeError) => console.error(err),
* });
*
* if(result.isOk) {
* const data: WeatherDataV2 = data.ok();
* } else {
* const err: DOMException | TypeError = data.err();
* }
*/
declare class OpenWeatherV2 {
private cache;
private loadableApiKey;
private apiHandler;
private fetchPromiseMap;
/**
* OpenWeatherV2 constructor
* @param {Loadable<string>} loadableApiKey - Loadable API key
* @param {ICacheOrAsync<WeatherDataV2>=} cache - optional async cache implementation
* @param {IOpenWeatherV2=} apiHandler - optional API handler implementation for mocking
*/
constructor(loadableApiKey: Loadable<string>, cache?: IAsyncCache<WeatherDataV2>, apiHandler?: IOpenWeatherV2);
/**
* get weather by Id
* @param {number} id - Weather station ID
* @param {OpenWeatherV2CommonOptions=} opts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```
* @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise
* @example
* const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherResultById(id: 564, {lang: 'fi'});
* if (result.isOk) {
* const weatherData: WeatherDataV2 = result.ok();
* } else {
* const error: DOMException | TypeError = result.err();
* }
*/
getWeatherById(id: number, opts?: OpenWeatherV2CommonOptions): Promise<IResult<WeatherDataV2, DOMException | TypeError>>;
/**
* get weather with city name and optional country code
* @param {string} city - City name
* @param {countryCode=} countryCode - Optional Country code
* @param {OpenWeatherV2CommonOptions=} opts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```
* @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise
* @example
* const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherByCity('Helsinki', 'fi', {lang: 'fi'});
* if (result.isOk) {
* const weatherData: WeatherDataV2 = result.ok();
* } else {
* const error: DOMException | TypeError = result.err();
* }
*/
getWeatherByCity(city: string, countryCode?: CountryCode, opts?: OpenWeatherV2CommonOptions): Promise<IResult<WeatherDataV2, DOMException | TypeError>>;
/**
* get weather with latitude and longitude with Result
* @param {number} lat - Latitude
* @param {number} lon - Longitude
* @param {OpenWeatherV2CommonOptions=} opts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```
* @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise
* @example
* const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherByLatLon(60.1699, 24.9384, {lang: 'fi'});
* if (result.isOk) {
* const weatherData: WeatherDataV2 = result.ok();
* } else {
* const error: DOMException | TypeError = result.err();
* }
*/
getWeatherByLatLon(lat: number, lon: number, opts?: OpenWeatherV2CommonOptions): Promise<IResult<WeatherDataV2, DOMException | TypeError>>;
private buildBaseParams;
/**
* build base cache key
* @param main - main cache key prefix
* @param opts - OpenWeatherV2CommonOptions
* @returns {CacheKey}
*/
private buildBaseCacheKey;
private handleFetch;
}
/**
* Ensures that the error is a DOMException or TypeError.
* @internal
*/
declare function fetchErrorWrapper(err: unknown): DOMException | TypeError;
export { type CountryCode, CountryCodeList, CountryCodeSchema, type DayIcon, type IOpenWeatherV2, type Icon, type LangCode, type Loadable, type NightIcon, OpenWeatherV2, type OpenWeatherV2CommonOptions, type WeatherDataV2, type WeatherDescription, type WeatherGroup, type WeatherID, assertWeatherDataV2, fetchErrorWrapper, getWeatherV2Description, iconSchema, isWeatherDataV2, langCodeSchema, langCodes, solveLoadable, weatherDataV2Schema, weatherIdGroup, weatherIdSchema };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
tslib_1.__exportStar(require("./OpenWeatherV2"), exports);
tslib_1.__exportStar(require("./types"), exports);
tslib_1.__exportStar(require("./lib"), exports);
tslib_1.__exportStar(require("./interfaces"), exports);
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
CountryCodeList: () => CountryCodeList,
CountryCodeSchema: () => CountryCodeSchema,
OpenWeatherV2: () => OpenWeatherV2,
assertWeatherDataV2: () => assertWeatherDataV2,
fetchErrorWrapper: () => fetchErrorWrapper,
getWeatherV2Description: () => getWeatherV2Description,
iconSchema: () => iconSchema,
isWeatherDataV2: () => isWeatherDataV2,
langCodeSchema: () => langCodeSchema,
langCodes: () => langCodes,
solveLoadable: () => solveLoadable,
weatherDataV2Schema: () => weatherDataV2Schema,
weatherIdGroup: () => weatherIdGroup,
weatherIdSchema: () => weatherIdSchema
});
module.exports = __toCommonJS(src_exports);
// src/types/v2/Icon.ts
var import_zod = require("zod");
var dayIconList = ["01d", "02d", "03d", "04d", "09d", "10d", "11d", "13d", "50d"];
var dayIconListSchema = import_zod.z.enum(dayIconList);
var nightIconList = ["01n", "02n", "03n", "04n", "09n", "10n", "11n", "13n", "50n"];
var nightIconListSchema = import_zod.z.enum(nightIconList);
var iconSchema = import_zod.z.union([dayIconListSchema, nightIconListSchema]);
// src/types/v2/weatherIdGroup.ts
var import_result_option = require("@luolapeikko/result-option");
var import_zod2 = require("zod");
var weatherIdGroup = [
{
id: 200,
group: "thunderstorm",
description: "thunderstorm_with_light_rain"
},
{
id: 201,
group: "thunderstorm",
description: "thunderstorm_with_rain"
},
{
id: 202,
group: "thunderstorm",
description: "thunderstorm_with_heavy_rain"
},
{
id: 210,
group: "thunderstorm",
description: "light_thunderstorm"
},
{
id: 211,
group: "thunderstorm",
description: "thunderstorm"
},
{
id: 212,
group: "thunderstorm",
description: "heavy_thunderstorm"
},
{
id: 221,
group: "thunderstorm",
description: "ragged_thunderstorm"
},
{
id: 230,
group: "thunderstorm",
description: "thunderstorm_with_light_drizzle"
},
{
id: 231,
group: "thunderstorm",
description: "thunderstorm_with_drizzle"
},
{
id: 232,
group: "thunderstorm",
description: "thunderstorm_with_heavy_drizzle"
},
{
id: 300,
group: "drizzle",
description: "light_intensity_drizzle"
},
{
id: 301,
group: "drizzle",
description: "drizzle"
},
{
id: 302,
group: "drizzle",
description: "heavy_intensity_drizzle"
},
{
id: 310,
group: "drizzle",
description: "light_intensity_drizzle_rain"
},
{
id: 311,
group: "drizzle",
description: "drizzle_rain"
},
{
id: 312,
group: "drizzle",
description: "heavy_intensity_drizzle_rain"
},
{
id: 313,
group: "drizzle",
description: "shower_rain_and_drizzle"
},
{
id: 314,
group: "drizzle",
description: "heavy_shower_rain_and_drizzle"
},
{
id: 321,
group: "drizzle",
description: "shower_drizzle"
},
{
id: 500,
group: "rain",
description: "light_rain"
},
{
id: 501,
group: "rain",
description: "moderate_rain"
},
{
id: 502,
group: "rain",
description: "heavy_intensity_rain"
},
{
id: 503,
group: "rain",
description: "very_heavy_rain"
},
{
id: 504,
group: "rain",
description: "extreme_rain"
},
{
id: 511,
group: "rain",
description: "freezing_rain"
},
{
id: 520,
group: "rain",
description: "light_intensity_shower_rain"
},
{
id: 521,
group: "rain",
description: "shower_rain"
},
{
id: 522,
group: "rain",
description: "heavy_intensity_shower_rain"
},
{
id: 531,
group: "rain",
description: "ragged_shower_rain"
},
{
id: 600,
group: "snow",
description: "light_snow"
},
{
id: 601,
group: "snow",
description: "snow"
},
{
id: 602,
group: "snow",
description: "heavy_snow"
},
{
id: 611,
group: "snow",
description: "sleet"
},
{
id: 612,
group: "snow",
description: "light_shower_sleet"
},
{
id: 613,
group: "snow",
description: "shower_sleet"
},
{
id: 615,
group: "snow",
description: "light_rain_and_snow"
},
{
id: 616,
group: "snow",
description: "rain_and_snow"
},
{
id: 620,
group: "snow",
description: "light_shower_snow"
},
{
id: 621,
group: "snow",
description: "shower_snow"
},
{
id: 622,
group: "snow",
description: "heavy_shower_snow"
},
{
id: 701,
group: "atmosphere",
description: "mist"
},
{
id: 711,
group: "atmosphere",
description: "smoke"
},
{
id: 721,
group: "atmosphere",
description: "haze"
},
{
id: 731,
group: "atmosphere",
description: "sand_dust_whirls"
},
{
id: 741,
group: "atmosphere",
description: "fog"
},
{
id: 751,
group: "atmosphere",
description: "sand"
},
{
id: 761,
group: "atmosphere",
description: "dust"
},
{
id: 762,
group: "atmosphere",
description: "volcanic_ash"
},
{
id: 771,
group: "atmosphere",
description: "squalls"
},
{
id: 781,
group: "atmosphere",
description: "tornado"
},
{
id: 800,
group: "clear",
description: "clear_sky"
},
{
id: 801,
group: "clouds",
description: "few_clouds_11-25_percent"
},
{
id: 802,
group: "clouds",
description: "scattered_clouds_25-50_percent"
},
{
id: 803,
group: "clouds",
description: "broken_clouds_51-84_percent"
},
{
id: 804,
group: "clouds",
description: "overcast_clouds_85-100_percent"
}
];
var weatherIdSchema = import_zod2.z.custom((val) => {
if (typeof val !== "number") {
return { message: "Expected a number", success: false };
}
if (!weatherIdGroup.find((x) => x.id === val)) {
return { message: "Expected a valid weather id", success: false };
}
return val;
});
function getWeatherV2Description(id) {
return (0, import_result_option.undefinedOptionWrap)(weatherIdGroup.find((x) => x.id === id)?.description);
}
// src/types/v2/index.ts
var import_zod4 = require("zod");
// src/types/v2/Language.ts
var import_zod3 = require("zod");
var langCodes = [
"af",
"al",
"ar",
"az",
"bg",
"ca",
"cz",
"da",
"de",
"el",
"en",
"eu",
"fa",
"fi",
"fr",
"gl",
"he",
"hi",
"hr",
"hu",
"id",
"it",
"ja",
"kr",
"la",
"lt",
"mk",
"no",
"nl",
"pl",
"pt",
"pt",
"ro",
"ru",
"sv",
"sk",
"sl",
"sp",
"sr",
"th",
"tr",
"ua",
"vi",
"zh_cn",
"zh_tw",
"zu"
];
var langCodeSchema = import_zod3.z.enum(langCodes);
// src/types/v2/index.ts
var coordSchema = import_zod4.z.object({
lon: import_zod4.z.number(),
lat: import_zod4.z.number()
});
var weatherSchema = import_zod4.z.object({
description: import_zod4.z.string(),
icon: iconSchema,
id: weatherIdSchema,
main: import_zod4.z.string()
});
var mainSchema = import_zod4.z.object({
grnd_level: import_zod4.z.number().optional(),
humidity: import_zod4.z.number(),
pressure: import_zod4.z.number(),
sea_level: import_zod4.z.number().optional(),
temp: import_zod4.z.number(),
temp_max: import_zod4.z.number(),
temp_min: import_zod4.z.number()
});
var windSchema = import_zod4.z.object({
speed: import_zod4.z.number(),
deg: import_zod4.z.number()
});
var rainSchema = import_zod4.z.object({
"1h": import_zod4.z.number().optional(),
"3h": import_zod4.z.number().optional()
});
var snowSchema = import_zod4.z.object({
"1h": import_zod4.z.number().optional(),
"3h": import_zod4.z.number().optional()
});
var sysSchema = import_zod4.z.object({
country: import_zod4.z.string(),
id: import_zod4.z.number().optional(),
message: import_zod4.z.number().optional(),
sunrise: import_zod4.z.number(),
sunset: import_zod4.z.number(),
type: import_zod4.z.number().optional()
});
var weatherDataV2Schema = import_zod4.z.object({
base: import_zod4.z.string(),
clouds: import_zod4.z.object({
all: import_zod4.z.number()
}),
cod: import_zod4.z.number(),
coord: coordSchema,
dt: import_zod4.z.number(),
id: import_zod4.z.number(),
main: mainSchema,
name: import_zod4.z.string(),
rain: rainSchema.optional(),
snow: snowSchema.optional(),
sys: sysSchema,
timezone: import_zod4.z.number(),
visibility: import_zod4.z.number(),
weather: import_zod4.z.array(weatherSchema),
wind: windSchema
});
function isWeatherDataV2(data) {
return weatherDataV2Schema.safeParse(data).success;
}
function assertWeatherDataV2(data) {
weatherDataV2Schema.parse(data);
}
// src/types/ISO3166-Countries.ts
var import_zod5 = require("zod");
var CountryCodeList = [
"af",
"ax",
"al",
"dz",
"as",
"ad",
"ao",
"ai",
"aq",
"ag",
"ar",
"am",
"aw",
"au",
"at",
"az",
"bs",
"bh",
"bd",
"bb",
"by",
"be",
"bz",
"bj",
"bm",
"bt",
"bo",
"bq",
"ba",
"bw",
"bv",
"br",
"io",
"bn",
"bg",
"bf",
"bi",
"kh",
"cm",
"ca",
"cv",
"ky",
"cf",
"td",
"cl",
"cn",
"cx",
"cc",
"co",
"km",
"cg",
"cd",
"ck",
"cr",
"ci",
"hr",
"cu",
"cw",
"cy",
"cz",
"dk",
"dj",
"dm",
"do",
"ec",
"eg",
"sv",
"gq",
"er",
"ee",
"et",
"fk",
"fo",
"fj",
"fi",
"fr",
"gf",
"pf",
"tf",
"ga",
"gm",
"ge",
"de",
"gh",
"gi",
"gr",
"gl",
"gd",
"gp",
"gu",
"gt",
"gg",
"gn",
"gw",
"gy",
"ht",
"hm",
"va",
"hn",
"hk",
"hu",
"is",
"in",
"id",
"ir",
"iq",
"ie",
"im",
"il",
"it",
"jm",
"jp",
"je",
"jo",
"kz",
"ke",
"ki",
"kp",
"kr",
"kw",
"kg",
"la",
"lv",
"lb",
"ls",
"lr",
"ly",
"li",
"lt",
"lu",
"mo",
"mk",
"mg",
"mw",
"my",
"mv",
"ml",
"mt",
"mh",
"mq",
"mr",
"mu",
"yt",
"mx",
"fm",
"md",
"mc",
"mn",
"me",
"ms",
"ma",
"mz",
"mm",
"na",
"nr",
"np",
"nl",
"nc",
"nz",
"ni",
"ne",
"ng",
"nu",
"nf",
"mp",
"no",
"om",
"pk",
"pw",
"ps",
"pa",
"pg",
"py",
"pe",
"ph",
"pn",
"pl",
"pt",
"pr",
"qa",
"re",
"ro",
"ru",
"rw",
"bl",
"sh",
"kn",
"lc",
"mf",
"pm",
"vc",
"ws",
"sm",
"st",
"sa",
"sn",
"rs",
"sc",
"sl",
"sg",
"sx",
"sk",
"si",
"sb",
"so",
"za",
"gs",
"ss",
"es",
"lk",
"sd",
"sr",
"sj",
"sz",
"se",
"ch",
"sy",
"tw",
"tj",
"tz",
"th",
"tl",
"tg",
"tk",
"to",
"tt",
"tn",
"tr",
"tm",
"tc",
"tv",
"ug",
"ua",
"ae",
"gb",
"us",
"um",
"uy",
"uz",
"vu",
"ve",
"vn",
"vg",
"vi",
"wf",
"eh",
"ye",
"zm",
"zw"
];
var CountryCodeSchema = import_zod5.z.enum(CountryCodeList);
// src/types/index.ts
function solveLoadable(value) {
return typeof value === "function" ? value() : value;
}
// src/OpenWeatherV2.ts
var import_result_option2 = require("@luolapeikko/result-option");
// src/lib/fetchUtils.ts
function fetchErrorWrapper(err) {
if (err instanceof DOMException || err instanceof TypeError) {
return err;
} else {
return new TypeError(`Unknown error: ${String(err)}`);
}
}
// src/OpenWeatherV2.ts
var fetchResult = (0, import_result_option2.safeAsyncResultBuilder)(fetch);
function toParams(data) {
return Object.entries(data).reduce((acc, [key, value]) => {
acc[key] = String(value);
return acc;
}, {});
}
function isJson(response) {
const contentType = response.headers.get("content-type");
return contentType && contentType.startsWith("application/json") || false;
}
function isOpenWeatherError(data) {
return typeof data === "object" && data !== null && "cod" in data && "message" in data;
}
var basePath = "https://api.openweathermap.org/data/2.5/weather";
function buildUrl(params) {
return `${basePath}?${params.toString()}`;
}
function buildLogUrl(params) {
const logParams = new URLSearchParams(params);
logParams.set("appid", "***");
return buildUrl(logParams);
}
var defaultImplementation = {
dataWeatherApi: async (params) => {
const logUrl = buildLogUrl(params);
const result = await fetchResult(buildUrl(params));
if (!result.isOk) {
return (0, import_result_option2.Err)(result.err());
}
const res = result.ok();
if (!res.ok) {
if (isJson(res)) {
const data2 = await res.json();
if (isOpenWeatherError(data2)) {
return (0, import_result_option2.Err)(new TypeError(`OpenWeatherV2 error: ${data2.message} from ${logUrl}`));
}
}
return (0, import_result_option2.Err)(new TypeError(`OpenWeatherV2 http error: ${res.status} ${res.statusText} from ${logUrl}`));
}
if (!isJson(res)) {
return (0, import_result_option2.Err)(new TypeError(`OpenWeatherV2 response is not json payload from ${logUrl}`));
}
const jsonResult = await (0, import_result_option2.safeAsyncResult)(res.json());
if (!jsonResult.isOk) {
return (0, import_result_option2.Err)(jsonResult.err());
}
const data = jsonResult.ok();
assertWeatherDataV2(data);
return (0, import_result_option2.Ok)(data);
}
};
var OpenWeatherV2 = class {
cache;
loadableApiKey;
apiHandler;
fetchPromiseMap = /* @__PURE__ */ new Map();
/**
* OpenWeatherV2 constructor
* @param {Loadable<string>} loadableApiKey - Loadable API key
* @param {ICacheOrAsync<WeatherDataV2>=} cache - optional async cache implementation
* @param {IOpenWeatherV2=} apiHandler - optional API handler implementation for mocking
*/
constructor(loadableApiKey, cache, apiHandler = defaultImplementation) {
this.loadableApiKey = loadableApiKey;
this.cache = cache;
this.apiHandler = apiHandler;
}
/**
* get weather by Id
* @param {number} id - Weather station ID
* @param {OpenWeatherV2CommonOptions=} opts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```
* @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise
* @example
* const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherResultById(id: 564, {lang: 'fi'});
* if (result.isOk) {
* const weatherData: WeatherDataV2 = result.ok();
* } else {
* const error: DOMException | TypeError = result.err();
* }
*/
async getWeatherById(id, opts = {}) {
try {
const cacheKey = this.buildBaseCacheKey(`id:${id}`, opts);
let cacheEntry = this.cache && await this.cache.get(cacheKey);
if (!cacheEntry) {
const params = await this.buildBaseParams(opts);
params.append("id", String(id));
cacheEntry = await this.handleFetch(cacheKey, params, opts);
}
return (0, import_result_option2.Ok)(cacheEntry);
} catch (err) {
return (0, import_result_option2.Err)(fetchErrorWrapper(err));
}
}
/**
* get weather with city name and optional country code
* @param {string} city - City name
* @param {countryCode=} countryCode - Optional Country code
* @param {OpenWeatherV2CommonOptions=} opts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```
* @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise
* @example
* const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherByCity('Helsinki', 'fi', {lang: 'fi'});
* if (result.isOk) {
* const weatherData: WeatherDataV2 = result.ok();
* } else {
* const error: DOMException | TypeError = result.err();
* }
*/
async getWeatherByCity(city, countryCode, opts = {}) {
try {
const cacheKey = this.buildBaseCacheKey(`q:${city}:${countryCode}`, opts);
let cacheEntry = this.cache && await this.cache.get(cacheKey);
if (!cacheEntry) {
const params = await this.buildBaseParams(opts);
params.append("q", countryCode ? `${city},${countryCode}` : city);
cacheEntry = await this.handleFetch(cacheKey, params, opts);
}
return (0, import_result_option2.Ok)(cacheEntry);
} catch (err) {
return (0, import_result_option2.Err)(fetchErrorWrapper(err));
}
}
/**
* get weather with latitude and longitude with Result
* @param {number} lat - Latitude
* @param {number} lon - Longitude
* @param {OpenWeatherV2CommonOptions=} opts - Common options, example ```{lang: 'fi', units: 'metric'}```, defaults ```{lang: 'en', units: 'standard'}```
* @return {Promise<Result<WeatherDataV2, DOMException | TypeError>>} Weather data Result Promise
* @example
* const result: Result<WeatherDataV2, DOMException | TypeError> = await weather.getWeatherByLatLon(60.1699, 24.9384, {lang: 'fi'});
* if (result.isOk) {
* const weatherData: WeatherDataV2 = result.ok();
* } else {
* const error: DOMException | TypeError = result.err();
* }
*/
async getWeatherByLatLon(lat, lon, opts = {}) {
try {
const cacheKey = this.buildBaseCacheKey(`latlon:${lat}:${lon}`, opts);
let cacheEntry = this.cache && await this.cache.get(cacheKey);
if (!cacheEntry) {
const params = await this.buildBaseParams(opts);
params.append("lat", String(lat));
params.append("lon", String(lon));
cacheEntry = await this.handleFetch(cacheKey, params, opts);
}
return (0, import_result_option2.Ok)(cacheEntry);
} catch (err) {
return (0, import_result_option2.Err)(fetchErrorWrapper(err));
}
}
async buildBaseParams(options) {
const apiKey = await (typeof this.loadableApiKey === "function" ? this.loadableApiKey() : this.loadableApiKey);
const params = new URLSearchParams(toParams(options));
params.append("appid", apiKey);
return params;
}
/**
* build base cache key
* @param main - main cache key prefix
* @param opts - OpenWeatherV2CommonOptions
* @returns {CacheKey}
*/
buildBaseCacheKey(main, { lang, units }) {
return `${main}:${lang}:${units}`;
}
async handleFetch(cacheKey, params, opts) {
let promiseResult = this.fetchPromiseMap.get(cacheKey);
if (!promiseResult) {
promiseResult = this.apiHandler.dataWeatherApi(params);
this.fetchPromiseMap.set(cacheKey, promiseResult);
await promiseResult;
this.fetchPromiseMap.delete(cacheKey);
}
const dataApiResult = await promiseResult;
const data = dataApiResult.unwrap();
assertWeatherDataV2(data);
if (this.cache) {
await this.cache.set(cacheKey, data);
if (!cacheKey.startsWith("id:")) {
await this.cache.set(this.buildBaseCacheKey(`id:${data.id}`, opts), data);
}
}
return data;
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CountryCodeList,
CountryCodeSchema,
OpenWeatherV2,
assertWeatherDataV2,
fetchErrorWrapper,
getWeatherV2Description,
iconSchema,
isWeatherDataV2,
langCodeSchema,
langCodes,
solveLoadable,
weatherDataV2Schema,
weatherIdGroup,
weatherIdSchema
});
//# sourceMappingURL=index.js.map

59

package.json
{
"name": "@mharj/openweathermap",
"version": "0.0.7",
"version": "0.0.8",
"description": "Open Weather API Client",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.js",
"import": "./dist/index.mjs"
}
},
"scripts": {
"doc": "typedoc",
"build": "tsc",
"build": "tsup src/index.ts --sourcemap --format cjs,esm --dts --clean",
"prepublishOnly": "npm run build",
"test": "nyc mocha",
"coverage": "nyc report --reporter=lcovonly",
"lint": "eslint src"
"test": "vitest test --run --no-isolate --coverage",
"coverage": "vitest test --run --no-isolate --reporter=dot --coverage --coverage.reporter=lcov",
"lint": "eslint . --ext .ts"
},

@@ -62,28 +71,30 @@ "files": [

"dependencies": {
"@avanio/expire-cache": "^0.3.3",
"@luolapeikko/result-option": "^0.5.3",
"tslib": "^2.6.2",
"zod": "^3.23.0"
"@luolapeikko/cache-types": "^0.0.7",
"@luolapeikko/result-option": "^1.0.3",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/chai": "^4.3.14",
"@types/mocha": "^10.0.6",
"@types/node": "^18.19.31",
"@typescript-eslint/eslint-plugin": "^7.7.0",
"@typescript-eslint/parser": "^7.7.0",
"chai": "^4.4.1",
"eslint": "^8.57.0",
"@avanio/expire-cache": "^0.6.3",
"@stylistic/eslint-plugin": "^2.10.1",
"@stylistic/eslint-plugin-ts": "^2.10.1",
"@types/node": "^18.19.64",
"@typescript-eslint/eslint-plugin": "^8.13.0",
"@typescript-eslint/parser": "^8.13.0",
"@vitest/coverage-v8": "^2.1.4",
"c8": "^10.1.2",
"eslint": "^8.57.1",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-deprecation": "^2.0.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-deprecation": "^3.0.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-sonarjs": "^0.19.0",
"mocha": "^10.4.0",
"nyc": "^15.1.0",
"prettier": "^3.2.5",
"prettier": "^3.3.3",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.2",
"typedoc": "^0.25.13",
"typedoc-plugin-zod": "^1.1.2"
"tslib": "^2.8.1",
"tsup": "^8.3.5",
"typedoc": "^0.26.11",
"typedoc-plugin-zod": "^1.2.1",
"vite": "^5.4.10",
"vitest": "^2.1.4"
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc