npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

openweathermap-ts

v1.2.10

Published

An abstract layer over openWeatherMap APIs

Downloads

7,015

Readme

openweathermap-ts

Build Status npm version Donate

An abstract layer over openWeatherMap APIs to simplify making calls. Built with TypeScript and Promises. Even the returned JSON responses are typed for your benefits!

Note: The library currently only supports free tier API services:

  • Current Weather
  • 3-hour Forecast. (5 day / 3 hour)

Installation

// install using npm
npm install openweathermap-ts

You will also need to register for an API key at the official site.

Basic Setup

const OpenWeatherMap = require('openweathermap-ts');
// or
import OpenWeatherMap from 'openweathermap-ts';

const openWeather = new OpenWeatherMap({
  apiKey: 'Your API Key'
});

Usage

There are 2 ways of using public getter methods.

  1. Pass in argument/s
  2. Store preferences in the location object

When both are used, the argument/s precede over the defined location object.

Notice some methods are grouped together with & because their arguments are the same. But they produce different results.


getCurrentWeatherByCityName & getThreeHourForecastByCityName

/**
 * @param {
 *  cityName: string,
 *  state?: string, // Spell it out. E.g, Texas
 *  countryCode?: CountryCodeType
 * }
 */
openWeather
  .getCurrentWeatherByCityName({
    cityName: 'Cedar Park'
  })
  .then((weather) => console.log('Weather object is', weather));

// or async await example
try {
  const weather = await openWeather.getThreeHourForecastByCityName({
    cityName: 'Cedar Park',
    state: 'Texas',
    countryCode: 'US'
  });
  console.log('Weather object is', weather);
} catch (error) {
  console.error('Error is ', error);
}

List of ISO 3166 CountryCodeType

getCurrentWeatherByCityId & getThreeHourForecastByCityId

/**
 * @param cityId: number
 */
openWeather
  .getCurrentWeatherByCityId(1835848)
  .then((weather) => {
    console.log('Weather object is', weather);
  })
  .catch((error) => console.error('Error is ', error));

Or

// set the cityId in location object
openWeather.setCityId(1835848);

// invoke the method without an argument
openWeather
  .getThreeHourForecastByCityId()
  .then((weather) => {
    console.log('Weather object is', weather);
  })
  .catch((error) => console.error('Error is ', error));

List of CityIds

getCurrentWeatherByGeoCoordinates & getThreeHourForecastByGeoCoordinates

/**
 * @params latitude: number, longitude: number
 */
openWeather
  .getCurrentWeatherByGeoCoordinates(33.426971, -117.611992)
  .then((weather) => console.log('Weather object is', weather));

getCurrentWeatherByZipcode & getThreeHourForecastByZipcode

/**
 * @params zipcode: number, countryCode?: CountryCodeType
 */
openWeather
  .getCurrentWeatherByZipcode(84604)
  .then((data) => console.log('Weather object is', data));

Or

openWeather
  .getCurrentWeatherByZipcode(84604, 'US')
  .then((data) => console.log('Weather object is', data));

List of CountryCodeType


 

Config Methods

Helpers for settings object

/**
 * @param apiKey: string
 */
openWeather.setApiKey('yourApiKey');

/**
 * @param units: 'imperial'| 'metric' | 'standard' (Kelvin)
 */
openWeather.setUnits('metric');

/**
 * @param language: LanguageTypes
 */
openWeather.setLanguage('kr');
// LanguageTypes List -> https://github.com/shimphillip/openweathermap-ts/blob/master/languages.md

/**
 * @param none
 */
openWeather.getAllSettings();

/**
 * @param none
 */
openWeather.clearSettings(); // Remember to reset your API Key

 

Helpers for location object

/**
 * @param cityId: number
 */
openWeather.setCityId(1835848);

/**
 * @param {
 *  cityName: string,
 *  state?: string, // (optional) Spell it out. E.g, Texas
 *  countryCode?: CountryCodeTypes // (optional)
 * }
 */
openWeather.setCityName({
  cityName: 'Austin'
});
// List of CountryCodeTypes https://github.com/shimphillip/openweathermap-ts/blob/master/src/helpers/country-codes.ts

/**
 * @params latitude: number, longitude: number
 */
openWeather.setGeoCoordinates(33.426971, -117.611992);

/**
 * @params zipcode: number, countryCodeTypes?: string (optional)
 */
openWeather.setZipCode(84604, 'US');
// List of CountryCodeType https://github.com/shimphillip/openweathermap-ts/blob/master/src/helpers/country-codes.ts

/**
 * @param none
 */
openWeather.getAllLocations();

/**
 * @param none
 */
openWeather.clearLocation();

 

Official API Docs

Current Weather API: https://openweathermap.org/current 5 day / 3 hour Forecast API: https://openweathermap.org/forecast5

Bug Reports

Please create issues or pull requests at https://github.com/shimphillip/openweathermap-ts

Future Works 🚀

  • CityID validations
  • Filter out unsolicited information options
  • Handle coordinate types
  • Supports other formats like XML
  • Enforce strict rules on countryCodes and states
  • support for paid services like Daily Forecast and Hourly Forecast

Love what you use? Buy me a ~~coffee~~ boba!🍹