🌞 🌨️ ❄️ @cicciosgamino/openweather-apis
Javascript module wrapped around the OpenWeatherMap.org APIs for free servicies (getForecast4Days() works only with pro services). First step to use the module is get OpenWeatherMap.org API key, request a APPID (API Key) on http://openweathermap.org/appid and start to get the weather data!
Follow the official site for more info about OpenWeather, and here if you want more info about the APIs.
installation •
nodejs •
web •
Errors •
Test •
TODO •
⚠️ Notice
- Only JSON format is handled (NOT XML)
- Only HTTPS requests (NOT HTTP)
Installation
Like all node packages, install and mange operation are done by npm (use the -s if you want save
the package in the package.json file):
npm install @cicciosgamino/openweather-apis
npm install @cicciosgamino/openweather-apis --save
npm install -g @cicciosgamino/openweather-apis
Node.js
Install the package with npm, import the class you need:
import { AsyncWeather } from '@cicciosgamino/openweather-apis'
const weatherInstance = await new AsyncWeather()
const apiKey = process.env['OPENWEATHER_API_KEY'] || ''
const apiKey = 'sdgfd5g5f46...your_api_key_here'
weather.setLang('it')
weather.setCoordinates(50.0467656, 20.0048731)
weather.setZipCodeAndCountryCode(24024, 'IT')
weather.setCityId(4367872)
weather.setCity('Bergamo')
weather.setUnits('metric')
weather.setApiKey(apiKey)
Now with the minimal set in place you can request the weather data! The module version after v5.0.0 included use Promise or Async/Await asynchronous coding style.
weather.getTemperature()
.then(result => console.log(`${typeof result} ${result}`))
.catch(error => console.log(error))
weather.getPressure()
.then(result => console.log(`${typeof result} ${result}`))
.catch(error => console.log(error))
weather.getHumidity()
.then(result => console.log(`${typeof result} ${result}`))
.catch(error => console.log(error))
weather.getTitle()
.then(result => console.log(`${typeof result} ${result}`))
.catch(error => console.log(error))
weather.getDescription()
.then(result => console.log(`${typeof result} ${result}`))
.catch(err => console.log(err))
weather.getSmartJSON()
.then(result => console.log(result))
.catch(err => console.log(err))
weather.getAllWeather()
.then(result => console.log(result))
.catch(error => console.log(error))
getForecast4Days (cnt)
.then(result => console.log(result))
.catch(error => console.log(error))
'Error: Response Code 401 >> Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.'
try {
const r = await weather.getPressure()
console.log(`${typeof r} ${r}`)
} catch(err) {
console.log(err)
}
Web
The module can be used in the browser too. In the example you can see an easy example of Web Component build with Lit that use the module to retrieve the weather data:
import { LitElement, html } from 'lit'
import { resolvePromise } from '../directive/resolve-promise'
import { AsyncWeather } from '../index.js'
class OpenweatherApis extends LitElement {
static get properties () {
return {
temp: Number,
apiKey: String,
weatherAPI: Object
}
}
constructor () {
super()
new AsyncWeather().then(w => {
this.weatherAPI = w
this.weatherAPI.setApiKey(this.apiKey)
})
}
render () {
return html`
Bergamo >>
${this.weatherAPI
? resolvePromise(this.weatherAPI.getTemperature())
: '...'}°C -
${this.weatherAPI
? resolvePromise(this.weatherAPI.getDescription())
: '...'}
`
}
}
customElements.define('openweather-apis', OpenweatherApis)
Comming Soon the WebComponent openweather-apis-element build wih Lit.
Errors
Commons Errors
Error: Response Code 401 >> Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.
Test
The package is tested with mocha and chai. You can find the tests in the /test folder. If you need more tests on module open an issue on the github repo (THANKS).
TODO 🔧
🧑💻 Author
|
---|
@cicciosgamino |
Support
Reach out to me at one of the following places:
Donate
Donate help and contibutions no money!
License
GNU General Public License v3.0
Made 🧑💻 by @cicciosgamino