项目作者: bayoudhi

项目描述 :
OpenWeatherMap.org Javascript SDK
高级语言: JavaScript
项目地址: git://github.com/bayoudhi/openweatherapi-js-sdk.git
创建时间: 2020-12-06T20:15:48Z
项目社区:https://github.com/bayoudhi/openweatherapi-js-sdk

开源协议:

下载


openweatherapi-js-sdk

Ready-to-use javascript library to consume OpenWeatherMap.org free services.

Request a free api key on http://openweathermap.org/appid.

Typescript supported ✅

Installation

Using npm:

  1. // using npm
  2. npm install openweatherapi-js-sdk --save
  3. // using yarn
  4. yarn add openweatherapi-js-sdk

How to Use

Instantiation

Import createAPI function from the installed library, and create your api using your existing api key, if you don’t have a key, request a free one on http://openweathermap.org/appid.

  1. const { createAPI } = require("openweatherapi-js-sdk");
  2. const api = createAPI("your_api_key");

Current Weather

By city name

  1. // Short example
  2. api
  3. .weather
  4. .getWeatherByCityName({
  5. cityName: "London", // required
  6. units: "metric", // optional
  7. })
  8. .then((weather) => console.log("Weather object is", weather));
  9. // Full example
  10. api
  11. .weather
  12. .getWeatherByCityName({
  13. cityName: "London", // required
  14. stateCode: "uk", // optional
  15. countryCode: "GB", // optional
  16. lang: "fr", // optional
  17. units: "metric", // optional
  18. })
  19. .then((weather) => console.log("Weather object is", weather));

By city id

List of city ID ‘city.list.json.gz’ can be downloaded from this link http://bulk.openweathermap.org/sample.

  1. // Example
  2. api
  3. .weather
  4. .getWeatherByCityId({
  5. cityId: 2172797, // required
  6. lang: "fr", // optional
  7. units: "metric", // optional
  8. })
  9. .then((weather) => console.log("Weather object is", weather));

By geographic coordinates

  1. // Example
  2. api
  3. .weather
  4. .getWeatherByGeo({
  5. latitude: 37, // required
  6. longitude: 10, // required
  7. lang: "fr", // optional
  8. units: "metric", // optional
  9. })
  10. .then((weather) => console.log("Weather object is", weather));

By ZIP code

  1. // Example
  2. api
  3. .weather
  4. .getWeatherByZipCode({
  5. zipCode: 94040, // required
  6. countryCode: "us", // optional
  7. lang: "fr", // optional
  8. units: "metric", // optional - standard, metric and imperial units are available.
  9. })
  10. .then((weather) => console.log("Weather object is", weather));

5 day weather forecast

By city name

  1. // Short example
  2. api
  3. .forecast
  4. .getForecastByCityName({
  5. cityName: "London", // required
  6. units: "metric", // optional
  7. })
  8. .then((weather) => console.log("Weather object is", weather));
  9. // Full example
  10. api
  11. .forecast
  12. .getForecastByCityName({
  13. cityName: "London", // required
  14. stateCode: "uk", // optional
  15. countryCode: "GB", // optional
  16. lang: "fr", // optional
  17. units: "metric", // optional
  18. })
  19. .then((weather) => console.log("Weather object is", weather));

By city id

List of city ID ‘city.list.json.gz’ can be downloaded from this link http://bulk.openweathermap.org/sample.

  1. // Example
  2. api
  3. .forecast
  4. .getForecastByCityId({
  5. cityId: 2172797, // required
  6. lang: "fr", // optional
  7. units: "metric", // optional
  8. })
  9. .then((weather) => console.log("Weather object is", weather));

By geographic coordinates

  1. // Example
  2. api
  3. .forecast
  4. .getForecastByGeo({
  5. latitude: 37, // required
  6. longitude: 10, // required
  7. lang: "fr", // optional
  8. units: "metric", // optional
  9. })
  10. .then((weather) => console.log("Weather object is", weather));

By ZIP code

  1. // Example
  2. api
  3. .forecast
  4. .getForecastByZipCode({
  5. zipCode: 94040, // required
  6. countryCode: "us", // optional
  7. lang: "fr", // optional
  8. units: "metric", // optional - standard, metric and imperial units are available.
  9. })
  10. .then((weather) => console.log("Weather object is", weather));

Common Options

  • units - Units of measurement. standard, metric and imperial units are available. If you do not use the units parameter, standard units will be applied by default. Learn more
  • lang - You can use this parameter to get the output in your language. Learn more

Test

This package is tested using jest. You can find the tests in the /tests folder. This package is developed using the TDD approach.

Feel free 😊 to enhance it and to add more tests 🧪🧪🧪

TODO

Feel free to contribute to this library ❤️

Thanks