Open Weather V2 API

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();
}

v0.0.1

Constructors

  • OpenWeatherV2 constructor

    Parameters

    • loadableApiKey: Loadable<string>

      Loadable API key

    • Optionalcache: IAsyncCache<
          {
              base: string;
              clouds: { all: number };
              cod: number;
              coord: { lat: number; lon: number };
              dt: number;
              id: number;
              main: {
                  grnd_level?: number;
                  humidity: number;
                  pressure: number;
                  sea_level?: number;
                  temp: number;
                  temp_max: number;
                  temp_min: number;
              };
              name: string;
              rain?: { "1h"?: number; "3h"?: number };
              snow?: { "1h"?: number; "3h"?: number };
              sys: {
                  country: string;
                  id?: number;
                  message?: number;
                  sunrise: number;
                  sunset: number;
                  type?: number;
              };
              timezone: number;
              visibility: number;
              weather: {
                  description: string;
                  icon: | "01d"
                  | "02d"
                  | "03d"
                  | "04d"
                  | "09d"
                  | "10d"
                  | "11d"
                  | "13d"
                  | "50d"
                  | "01n"
                  | "02n"
                  | "03n"
                  | "04n"
                  | "09n"
                  | "10n"
                  | "11n"
                  | "13n"
                  | "50n";
                  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;
                  main: string;
              }[];
              wind: { deg: number; speed: number };
          },
          string,
      >

      optional async cache implementation

    • apiHandler: IOpenWeatherV2 = defaultImplementation

      optional API handler implementation for mocking

    Returns OpenWeatherV2

Methods

  • get weather with city name and optional country code

    Parameters

    • city: string

      City name

    • OptionalcountryCode:
          | "at"
          | "id"
          | "cx"
          | "cy"
          | "br"
          | "hr"
          | "li"
          | "td"
          | "th"
          | "tr"
          | "tt"
          | "mn"
          | "mo"
          | "ms"
          | "so"
          | "lr"
          | "in"
          | "af"
          | "al"
          | "ar"
          | "az"
          | "bg"
          | "ca"
          | "cz"
          | "de"
          | "fi"
          | "fr"
          | "gl"
          | "hu"
          | "it"
          | "kr"
          | "la"
          | "lt"
          | "mk"
          | "no"
          | "nl"
          | "pl"
          | "pt"
          | "ro"
          | "ru"
          | "sv"
          | "sk"
          | "sl"
          | "sr"
          | "ua"
          | "vi"
          | "ax"
          | "dz"
          | "as"
          | "ad"
          | "ao"
          | "ai"
          | "aq"
          | "ag"
          | "am"
          | "aw"
          | "au"
          | "bs"
          | "bh"
          | "bd"
          | "bb"
          | "by"
          | "be"
          | "bz"
          | "bj"
          | "bm"
          | "bt"
          | "bo"
          | "bq"
          | "ba"
          | "bw"
          | "bv"
          | "io"
          | "bn"
          | "bf"
          | "bi"
          | "kh"
          | "cm"
          | "cv"
          | "ky"
          | "cf"
          | "cl"
          | "cn"
          | "cc"
          | "co"
          | "km"
          | "cg"
          | "cd"
          | "ck"
          | "cr"
          | "ci"
          | "cu"
          | "cw"
          | "dk"
          | "dj"
          | "dm"
          | "do"
          | "ec"
          | "eg"
          | "gq"
          | "er"
          | "ee"
          | "et"
          | "fk"
          | "fo"
          | "fj"
          | "gf"
          | "pf"
          | "tf"
          | "ga"
          | "gm"
          | "ge"
          | "gh"
          | "gi"
          | "gr"
          | "gd"
          | "gp"
          | "gu"
          | "gt"
          | "gg"
          | "gn"
          | "gw"
          | "gy"
          | "ht"
          | "hm"
          | "va"
          | "hn"
          | "hk"
          | "is"
          | "ir"
          | "iq"
          | "ie"
          | "im"
          | "il"
          | "jm"
          | "jp"
          | "je"
          | "jo"
          | "kz"
          | "ke"
          | "ki"
          | "kp"
          | "kw"
          | "kg"
          | "lv"
          | "lb"
          | "ls"
          | "ly"
          | "lu"
          | "mg"
          | "mw"
          | "my"
          | "mv"
          | "ml"
          | "mt"
          | "mh"
          | "mq"
          | "mr"
          | "mu"
          | "yt"
          | "mx"
          | "fm"
          | "md"
          | "mc"
          | "me"
          | "ma"
          | "mz"
          | "mm"
          | "na"
          | "nr"
          | "np"
          | "nc"
          | "nz"
          | "ni"
          | "ne"
          | "ng"
          | "nu"
          | "nf"
          | "mp"
          | "om"
          | "pk"
          | "pw"
          | "ps"
          | "pa"
          | "pg"
          | "py"
          | "pe"
          | "ph"
          | "pn"
          | "pr"
          | "qa"
          | "re"
          | "rw"
          | "bl"
          | "sh"
          | "kn"
          | "lc"
          | "mf"
          | "pm"
          | "vc"
          | "ws"
          | "sm"
          | "st"
          | "sa"
          | "sn"
          | "rs"
          | "sc"
          | "sg"
          | "sx"
          | "si"
          | "sb"
          | "za"
          | "gs"
          | "ss"
          | "es"
          | "lk"
          | "sd"
          | "sj"
          | "sz"
          | "se"
          | "ch"
          | "sy"
          | "tw"
          | "tj"
          | "tz"
          | "tl"
          | "tg"
          | "tk"
          | "to"
          | "tn"
          | "tm"
          | "tc"
          | "tv"
          | "ug"
          | "ae"
          | "gb"
          | "us"
          | "um"
          | "uy"
          | "uz"
          | "vu"
          | "ve"
          | "vn"
          | "vg"
          | "wf"
          | "eh"
          | "ye"
          | "zm"
          | "zw"

      Optional Country code

    • currentOpts: Partial<OpenWeatherV2CommonOptions> = {}

      Common options, example {lang: 'fi', units: 'metric'}, defaults {lang: 'en', units: 'standard'}

    Returns Promise<
        IResult<
            {
                base: string;
                clouds: { all: number };
                cod: number;
                coord: { lat: number; lon: number };
                dt: number;
                id: number;
                main: {
                    grnd_level?: number;
                    humidity: number;
                    pressure: number;
                    sea_level?: number;
                    temp: number;
                    temp_max: number;
                    temp_min: number;
                };
                name: string;
                rain?: { "1h"?: number; "3h"?: number };
                snow?: { "1h"?: number; "3h"?: number };
                sys: {
                    country: string;
                    id?: number;
                    message?: number;
                    sunrise: number;
                    sunset: number;
                    type?: number;
                };
                timezone: number;
                visibility: number;
                weather: {
                    description: string;
                    icon: | "01d"
                    | "02d"
                    | "03d"
                    | "04d"
                    | "09d"
                    | "10d"
                    | "11d"
                    | "13d"
                    | "50d"
                    | "01n"
                    | "02n"
                    | "03n"
                    | "04n"
                    | "09n"
                    | "10n"
                    | "11n"
                    | "13n"
                    | "50n";
                    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;
                    main: string;
                }[];
                wind: { deg: number; speed: number };
            },
            TypeError
            | DOMException,
        >,
    >

    Weather data Result Promise

    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();
    }
  • get weather by Id

    Parameters

    • id: number

      Weather station ID

    • currentOpts: Partial<OpenWeatherV2CommonOptions> = {}

      Common options, example {lang: 'fi', units: 'metric'}, defaults {lang: 'en', units: 'standard'}

    Returns Promise<
        IResult<
            {
                base: string;
                clouds: { all: number };
                cod: number;
                coord: { lat: number; lon: number };
                dt: number;
                id: number;
                main: {
                    grnd_level?: number;
                    humidity: number;
                    pressure: number;
                    sea_level?: number;
                    temp: number;
                    temp_max: number;
                    temp_min: number;
                };
                name: string;
                rain?: { "1h"?: number; "3h"?: number };
                snow?: { "1h"?: number; "3h"?: number };
                sys: {
                    country: string;
                    id?: number;
                    message?: number;
                    sunrise: number;
                    sunset: number;
                    type?: number;
                };
                timezone: number;
                visibility: number;
                weather: {
                    description: string;
                    icon: | "01d"
                    | "02d"
                    | "03d"
                    | "04d"
                    | "09d"
                    | "10d"
                    | "11d"
                    | "13d"
                    | "50d"
                    | "01n"
                    | "02n"
                    | "03n"
                    | "04n"
                    | "09n"
                    | "10n"
                    | "11n"
                    | "13n"
                    | "50n";
                    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;
                    main: string;
                }[];
                wind: { deg: number; speed: number };
            },
            TypeError
            | DOMException,
        >,
    >

    Weather data Result Promise

    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();
    }
  • get weather with latitude and longitude with Result

    Parameters

    • lat: number

      Latitude

    • lon: number

      Longitude

    • currentOpts: Partial<OpenWeatherV2CommonOptions> = {}

      Common options, example {lang: 'fi', units: 'metric'}, defaults {lang: 'en', units: 'standard'}

    Returns Promise<
        IResult<
            {
                base: string;
                clouds: { all: number };
                cod: number;
                coord: { lat: number; lon: number };
                dt: number;
                id: number;
                main: {
                    grnd_level?: number;
                    humidity: number;
                    pressure: number;
                    sea_level?: number;
                    temp: number;
                    temp_max: number;
                    temp_min: number;
                };
                name: string;
                rain?: { "1h"?: number; "3h"?: number };
                snow?: { "1h"?: number; "3h"?: number };
                sys: {
                    country: string;
                    id?: number;
                    message?: number;
                    sunrise: number;
                    sunset: number;
                    type?: number;
                };
                timezone: number;
                visibility: number;
                weather: {
                    description: string;
                    icon: | "01d"
                    | "02d"
                    | "03d"
                    | "04d"
                    | "09d"
                    | "10d"
                    | "11d"
                    | "13d"
                    | "50d"
                    | "01n"
                    | "02n"
                    | "03n"
                    | "04n"
                    | "09n"
                    | "10n"
                    | "11n"
                    | "13n"
                    | "50n";
                    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;
                    main: string;
                }[];
                wind: { deg: number; speed: number };
            },
            TypeError
            | DOMException,
        >,
    >

    Weather data Result Promise

    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();
    }