openweathermap-ts
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
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');
import OpenWeatherMap from 'openweathermap-ts';
const openWeather = new OpenWeatherMap({
apiKey: 'Your API Key'
});
Usage
There are 2 ways of using public getter methods.
- Pass in argument/s
- 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
openWeather
.getCurrentWeatherByCityName({
cityName: 'Cedar Park'
})
.then((weather) => console.log('Weather object is', weather));
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
openWeather
.getCurrentWeatherByCityId(1835848)
.then((weather) => {
console.log('Weather object is', weather);
})
.catch((error) => console.error('Error is ', error));
Or
openWeather.setCityId(1835848);
openWeather
.getThreeHourForecastByCityId()
.then((weather) => {
console.log('Weather object is', weather);
})
.catch((error) => console.error('Error is ', error));
List of CityIds
getCurrentWeatherByGeoCoordinates & getThreeHourForecastByGeoCoordinates
openWeather
.getCurrentWeatherByGeoCoordinates(33.426971, -117.611992)
.then((weather) => console.log('Weather object is', weather));
getCurrentWeatherByZipcode & getThreeHourForecastByZipcode
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
openWeather.setApiKey('yourApiKey');
openWeather.setUnits('metric');
openWeather.setLanguage('kr');
openWeather.getAllSettings();
openWeather.clearSettings();
Helpers for location object
openWeather.setCityId(1835848);
openWeather.setCityName({
cityName: 'Austin'
});
openWeather.setGeoCoordinates(33.426971, -117.611992);
openWeather.setZipCode(84604, 'US');
openWeather.getAllLocations();
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!🍹