openweather-api-node
Advanced tools
Comparing version 1.1.2 to 1.1.3
59
index.js
@@ -58,4 +58,3 @@ const SUP_LANGS = [ | ||
const axios = require("axios").default, | ||
_ = require("lodash") //todo: remove need of lodash | ||
const axios = require("axios").default | ||
@@ -67,2 +66,25 @@ const currentParser = require("./parsers/current-parser"), | ||
function isObject(item) { | ||
return item && typeof item === "object" && !Array.isArray(item) | ||
} | ||
// https://stackoverflow.com/questions/27936772/how-to-deep-merge-instead-of-shallow-merge | ||
function mergeObj(target, ...sources) { | ||
if (!sources.length) return target | ||
const source = sources.shift() | ||
if (isObject(target) && isObject(source)) { | ||
for (const key in source) { | ||
if (isObject(source[key])) { | ||
if (!target[key]) Object.assign(target, { [key]: {} }) | ||
mergeObj(target[key], source[key]) | ||
} else { | ||
Object.assign(target, { [key]: source[key] }) | ||
} | ||
} | ||
} | ||
return mergeObj(target, ...sources) | ||
} | ||
class OpenWeatherAPI { | ||
@@ -287,3 +309,3 @@ | ||
async getLocation(options = {}) { | ||
await this.#uncacheLocation() | ||
await this.#uncacheLocation(options.key) | ||
options = await this.#parseOptions(options) | ||
@@ -304,3 +326,3 @@ let response = await this.#fetch(`${API_ENDPOINT}${GEO_PATH}reverse?lat=${options.coordinates.lat}&lon=${options.coordinates.lon}&limit=1&appid=${options.key}`) | ||
async getCurrent(options = {}) { | ||
await this.#uncacheLocation() | ||
await this.#uncacheLocation(options.key) | ||
options = await this.#parseOptions(options) | ||
@@ -320,3 +342,3 @@ let response = await this.#fetch(this.#createURL(options, "alerts,minutely,hourly,daily")) | ||
async getMinutelyForecast(limit = Number.POSITIVE_INFINITY, options = {}) { | ||
await this.#uncacheLocation() | ||
await this.#uncacheLocation(options.key) | ||
options = await this.#parseOptions(options) | ||
@@ -336,3 +358,3 @@ let response = await this.#fetch(this.#createURL(options, "alerts,current,hourly,daily")) | ||
async getHourlyForecast(limit = Number.POSITIVE_INFINITY, options = {}) { | ||
await this.#uncacheLocation() | ||
await this.#uncacheLocation(options.key) | ||
options = await this.#parseOptions(options) | ||
@@ -352,3 +374,3 @@ let response = await this.#fetch(this.#createURL(options, "alerts,current,minutely,daily")) | ||
async getDailyForecast(limit = Number.POSITIVE_INFINITY, includeToday = false, options = {}) { | ||
await this.#uncacheLocation() | ||
await this.#uncacheLocation(options.key) | ||
options = await this.#parseOptions(options) | ||
@@ -379,3 +401,3 @@ let response = await this.#fetch(this.#createURL(options, "alerts,current,minutely,hourly")) | ||
async getAlerts(options = {}) { | ||
await this.#uncacheLocation() | ||
await this.#uncacheLocation(options.key) | ||
options = await this.#parseOptions(options) | ||
@@ -394,3 +416,3 @@ let response = await this.#fetch(this.#createURL(options, "current,minutely,hourly,daily")) | ||
async getEverything(options = {}) { | ||
await this.#uncacheLocation() | ||
await this.#uncacheLocation(options.key) | ||
options = await this.#parseOptions(options) | ||
@@ -419,3 +441,3 @@ let response = await this.#fetch(this.#createURL(options)) | ||
async getHistory(dt, options = {}) { | ||
await this.#uncacheLocation() | ||
await this.#uncacheLocation(options.key) | ||
dt = Math.round(new Date(dt).getTime() / 1000) | ||
@@ -446,12 +468,16 @@ options = await this.#parseOptions(options) | ||
weathers.reverse() | ||
return _.merge({}, ...weathers) | ||
return mergeObj({}, ...weathers) | ||
} | ||
// helpers | ||
async #uncacheLocation() { // necessary for some setters to be synchronous | ||
if (this.#globalOptions.coordinates.lat && this.#globalOptions.coordinates.lon) return | ||
async #uncacheLocation(key) { // necessary for some setters to be synchronous | ||
if (this.#globalOptions.coordinates.lat && this.#globalOptions.coordinates.lon) return // ! what if lat or lon = 0 ? | ||
key = this.#globalOptions.key ?? key | ||
if (!key) return | ||
if (this.#globalOptions.locationName) { | ||
this.#globalOptions.coordinates = await this.#evaluateLocationByName(this.#globalOptions.locationName, this.#globalOptions.key) | ||
this.#globalOptions.coordinates = await this.#evaluateLocationByName(this.#globalOptions.locationName, key) | ||
} else if (this.#globalOptions.zipCode) { | ||
this.#globalOptions.coordinates = await this.#evaluateLocationByZipCode(this.#globalOptions.zipCode, this.#globalOptions.key) | ||
this.#globalOptions.coordinates = await this.#evaluateLocationByZipCode(this.#globalOptions.zipCode, key) | ||
} | ||
@@ -477,3 +503,2 @@ } | ||
async #fetch(url) { | ||
// console.log(url) | ||
let response | ||
@@ -532,3 +557,3 @@ try { | ||
} | ||
return _.merge({}, this.#globalOptions, options) | ||
return mergeObj({}, this.#globalOptions, options) | ||
} | ||
@@ -535,0 +560,0 @@ } |
{ | ||
"name": "openweather-api-node", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Simple Node.js package that makes it easy to work with OpenWeather API", | ||
@@ -20,4 +20,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"axios": "^0.21.4", | ||
"lodash": "^4.17.21" | ||
"axios": "^0.21.4" | ||
}, | ||
@@ -24,0 +23,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67438
1
21
1010
- Removedlodash@^4.17.21
- Removedlodash@4.17.21(transitive)