Socket
Socket
Sign inDemoInstall

openweather-api-node

Package Overview
Dependencies
3
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.2 to 1.1.0

CHANGELOG.md

1

formaters/current-formatter.js

@@ -1,2 +0,1 @@

// TODO: add icon url
const getWeatherModel = require("../weather-model")

@@ -3,0 +2,0 @@

@@ -76,3 +76,4 @@ const SUP_LANGS = [

},
locationName: undefined
locationName: undefined,
zipCode: undefined
}

@@ -117,2 +118,6 @@

case "zipCode":
this.setLocationByZipCode(value)
break
default:

@@ -210,8 +215,9 @@ throw new Error("Unknown parameter: " + key)

*
* @param {String} name - name of the location
* @param {String} name - name of the location (`q` parameter [here](https://openweathermap.org/api/geocoding-api#direct_name))
*/
setLocationByName(name) { // - location setter
setLocationByName(name) {
if (!name) throw new Error("Empty value cannot be a location name: " + name)
this.#globalOptions.coordinates.lat = undefined
this.#globalOptions.coordinates.lon = undefined
this.#globalOptions.zipCode = undefined
this.#globalOptions.locationName = name

@@ -237,7 +243,8 @@ }

*/
setLocationByCoordinates(lat, lon) { // - location setter
setLocationByCoordinates(lat, lon) {
let location = this.#evaluateLocationByCoordinates(lat, lon)
this.#globalOptions.locationName = undefined
this.#globalOptions.zipCode = undefined
this.#globalOptions.coordinates.lat = location.lat
this.#globalOptions.coordinates.lon = location.lon
this.#globalOptions.locationName = undefined
}

@@ -254,2 +261,24 @@

/**
* Sets global location by provided zip/post code
*
* @param {String} zipCode - zip/post code and country code divided by comma (`zip` parameter [here](https://openweathermap.org/api/geocoding-api#direct_zip))
*/
setLocationByZipCode(zipCode) {
if (!zipCode) throw new Error("Empty value cannot be a location zip code: " + zipCode)
this.#globalOptions.coordinates.lat = undefined
this.#globalOptions.coordinates.lon = undefined
this.#globalOptions.locationName = undefined
this.#globalOptions.zipCode = zipCode
}
async #evaluateLocationByZipCode(zipCode) {
let response = await this.#fetch(`${API_ENDPOINT}${GEO_PATH}zip?zip=${zipCode}&appid=${this.#globalOptions.key}`)
let data = response.data
return {
lat: data.lat,
lon: data.lon
}
}
/**
* Getter for location

@@ -379,2 +408,24 @@ *

/**
* Getter for historical data about weather
*
* @param {Date|Number|String} dt - Date from the **previous five days** (Unix time, UTC time zone)
* @param {Object} options - options used only for this call
*/
async getHistory(dt, options = {}) {
await this.#uncacheLocation()
dt = Math.round(new Date(dt).getTime() / 1000)
options = await this.#formatOptions(options)
let response = await this.#fetch(this.#createURL(options, false, dt))
let data = response.data
return {
lat: data.lat,
lon: data.lon,
timezone: data.timezone,
timezone_offset: data.timezone_offset,
current: currentFormatter(data),
hourly: hourlyFormatter(data, Number.POSITIVE_INFINITY)
}
}
// Uncategorized Methods

@@ -399,7 +450,9 @@

this.#globalOptions.coordinates = await this.#evaluateLocationByName(this.#globalOptions.locationName)
} else if (this.#globalOptions.zipCode) {
this.#globalOptions.coordinates = await this.#evaluateLocationByZipCode(this.#globalOptions.zipCode)
}
}
#createURL(options, exclude) {
let url = new URL(DATA_PATH, API_ENDPOINT)
#createURL(options, exclude = false, dt = false) {
let url = new URL(DATA_PATH + (dt === false ? "" : "/timemachine"), API_ENDPOINT)
url.searchParams.append("appid", options.key)

@@ -414,2 +467,4 @@ url.searchParams.append("lat", options.coordinates.lat)

url.searchParams.append("exclude", exclude)
if (dt)
url.searchParams.append("dt", dt)
return url.href

@@ -419,3 +474,2 @@ }

async #fetch(url) {
//console.log("fetching:", url) // ! delete this
let response

@@ -465,2 +519,6 @@ try {

case "zipCode":
options.coordinates = await this.#evaluateLocationByZipCode(value)
break
default:

@@ -467,0 +525,0 @@ throw new Error("Unknown parameter: " + key)

{
"name": "openweather-api-node",
"version": "1.0.2",
"version": "1.1.0",
"description": "Simple Node.js package that makes it easy to work with OpenWeather API",

@@ -13,3 +13,5 @@ "main": "index.js",

"openweathermap",
"meteo"
"meteo",
"weather-api",
"api"
],

@@ -16,0 +18,0 @@ "author": "loloToster",

@@ -8,4 +8,11 @@ # openweather-api-node ☁️

Simple Node.js package that makes it easy to work with OpenWeather API. If you want to learn how to use this package check out examples in *examples* folder. The only thing that you need to get started is API key if you don't have one go to [OpenWeatherMap website](https://openweathermap.org/) and get it. For now this package supports only some weather calls but we are planning on adding more features like: historical data, maps and all the other stuff that is available for free in OpenWeatherMap API.
Simple Node.js package that makes it easy to work with OpenWeather API.
Currently Supported APIs:
* Weather (from OneCall)
* Geocoding
* Historical (from OneCall)
If you want to learn how to use this package check out examples in *examples* folder. The only thing that you need to get started is API key if you don't have one go to [OpenWeatherMap website](https://openweathermap.org/) and get it. For now this package supports only a part of the API but we are planning on adding more features like: air pollution, maps and all the other stuff that is available for free in OpenWeatherMap API.
## Simple Example

@@ -45,2 +52,3 @@ ```js

* [setLocationByCoordinates][slocbycoor]
* [setLocationByZipCode][slocbyzip]
* [getLocation][gloc]

@@ -54,2 +62,3 @@ * [getCurrent][gcur]

* [getEverything][gevery]
* [getHistory][ghis]
* [mergeWeathers][mrgweathers]

@@ -88,3 +97,3 @@ * [Unique features of this package][features]

Getter for global options
Getter for global options.

@@ -105,3 +114,3 @@ **Returns:**

Sets global API key
Sets global API key.

@@ -121,3 +130,3 @@ **Arguments:**

Getter for global key
Getter for global key.

@@ -138,3 +147,3 @@ **Returns:**

Sets global language (Language must be listed [here](https://openweathermap.org/current#multi))
Sets global language (Language must be listed [here](https://openweathermap.org/current#multi)).

@@ -154,3 +163,3 @@ **Arguments:**

Getter for global language
Getter for global language.

@@ -170,3 +179,3 @@ **Returns:**

Sets global units
Sets global units.

@@ -186,3 +195,3 @@ **Arguments:**

Getter for global units
Getter for global units.

@@ -203,3 +212,3 @@ **Returns:**

Sets global location by provided name
Sets global location by provided name. The `name` argument will basically replace the `q` parameter in call described [here](https://openweathermap.org/api/geocoding-api#direct_name).

@@ -219,3 +228,3 @@ **Arguments:**

Sets global location by provided coordinates
Sets global location by provided coordinates.

@@ -232,2 +241,17 @@ **Arguments:**

## setLocationByZipCode(zipCode)
**Description:**
Sets global location by provided zip/post code. The `zipCode` argument will basically replace the `zip` parameter in call described [here](https://openweathermap.org/api/geocoding-api#direct_zip).
**Arguments:**
* **zipCode** - zip/post code and country code divided by comma. Please use ISO 3166 country codes: `{zip code},{country code}`
**Example:**
```js
weather.setLocationByZipCode("E14,GB")
```
*See also:* [options][opt], [getLocation][gloc]
## `async` getLocation(options = {})

@@ -237,3 +261,3 @@

Getter for location
Getter for location.

@@ -259,3 +283,3 @@ **Arguments:**

Getter for current weather
Getter for current weather.

@@ -281,3 +305,3 @@ **Arguments:**

Getter for minutely weather
Getter for minutely weather.

@@ -305,3 +329,3 @@ **Arguments:**

Getter for hourly weather
Getter for hourly weather.

@@ -329,3 +353,3 @@ **Arguments:**

Getter for daily weather
Getter for daily weather.

@@ -382,3 +406,3 @@ **Arguments:**

Getter for alerts
Getter for alerts.

@@ -402,3 +426,3 @@ **Arguments:**

Getter for every type of weather call and alerts
Getter for every type of weather call and alerts.

@@ -434,2 +458,32 @@ **Arguments:**

## `async` getHistory(dt, options = {})
**Description:**
Getter for historical data about weather.
**Arguments:**
* **dt** - Date from the **previous five days** (Unix time, UTC time zone)
* **options** - options used only for this call (defaults to empty object)
**Returns:**
Object that looks like this:
```
{
lat: latitude of the location,
lon: longitude of the location,
timezone: timezone of the location,
timezone_offset: timezone offset of the location,
current: weather object of current data of the time given,
hourly: data block containing hourly historical data starting at 00:00 on the requested day and continues until 23:59 on the same day (UTC time)
}
```
**Example:**
```js
let history = await weather.getHistory(new Date().getTime() - 7200)
```
*See also:* [options][opt], [Weather Object][wobj]
## mergeWeathers(weathers)

@@ -560,2 +614,3 @@

[slocbycoor]: #setlocationbycoordinateslat-lon
[slocbyzip]:#setlocationbyzipcodezipcode
[gloc]: #async-getlocationoptions

@@ -569,2 +624,3 @@ [gcur]: #async-getcurrentoptions

[gevery]: #async-geteverythingoptions--
[ghis]: #async-gethistorydt-options--
[mrgweathers]: #mergeweathersweathers

@@ -9,3 +9,4 @@ const fs = require("fs")

let weather = new OpenWeatherAPI({
key: key
key: key,
locationName: "Hong Kong"
})

@@ -105,2 +106,32 @@

it("handles future in history", async () => {
try {
await weather.getHistory(new Date().getTime() + 900000)
} catch (err) {
assert(err.message.toLowerCase().includes("requested time is in the future"))
return
}
assert(false)
})
it("handles not within 5 days in history", async () => {
try {
await weather.getHistory(new Date().getTime() - 6 * 24 * 60 * 60 * 1000)
} catch (err) {
assert(err.message.toLowerCase().includes("requested time is out of allowed range of 5 days back"))
return
}
assert(false)
})
it("handles no time in history", async () => {
try {
await weather.getHistory()
} catch (err) {
assert(err.message.toLowerCase().includes("no location or time specified"))
return
}
assert(false)
})
})

@@ -73,2 +73,9 @@ const fs = require("fs")

it("gets history", async () => {
weather.setLocationByCoordinates(49.84, 24.03)
let date = new Date().getTime() - 900000
let history = await weather.getHistory(date)
assert(Math.round(date / 1000) == history.current.dt_raw)
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc