Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
weather-plus
Advanced tools
Weather Plus is a powerful wrapper around various Weather APIs that simplifies adding weather data to your application
An awesome TypeScript weather client for fetching weather data from various weather providers.
Install the package using npm or yarn:
npm install weather-plus
or
yarn add weather-plus
First, import the library into your project:
import WeatherPlus from 'weather-plus';
or with CommonJS:
const WeatherPlus = require('weather-plus').default;
Instantiate the WeatherPlus class with default options:
const weatherPlus = new WeatherPlus();
Fetch weather data:
const weather = await weatherPlus.getWeather(40.748817, -73.985428); // Coordinates for New York City
console.log(weather);
You can specify an ordered list of providers for fallback. If the first provider fails (e.g., due to unsupported location), the next provider in the list will be tried.
const weatherPlus = new WeatherPlus({
providers: ['nws', 'openweather'],
apiKeys: {
openweather: 'your-openweather-api-key', // Replace with your actual API key
},
});
const weatherPlus = new WeatherPlus({
providers: ['nws', 'openweather'],
apiKeys: {
openweather: 'your-openweather-api-key',
},
cacheTTL: 600, // Optional: Cache TTL in seconds (default is 300 seconds)
});
Fetch weather data:
const weather = await weatherPlus.getWeather(51.5074, -0.1278); // Coordinates for London
console.log(weather);
One of the main benefits of this library is the ability to seamlessly switch between weather providers while maintaining a consistent API. This is particularly useful for:
• Fallback Mechanism: Use a free provider by default and fallback to a paid provider if necessary.
• Coverage: Some providers may not support certain locations; having multiple providers ensures broader coverage.
• Cost Optimization: Reduce costs by prioritizing free or cheaper providers.
Available Providers
You can specify the providers in order of preference:
const weatherPlus = new WeatherPlus({
providers: ['nws', 'openweather'],
apiKeys: {
openweather: 'your-openweather-api-key',
},
});
Some providers require API keys. Provide them using the apiKeys object, mapping provider names to their respective API keys.
const weatherPlus = new WeatherPlus({
providers: ['openweather'],
apiKeys: {
openweather: 'your-openweather-api-key',
},
});
To optimize API usage and reduce latency, weather-plus includes built-in caching mechanisms.
By default, if no Redis client is provided, the library uses an in-memory cache.
const weatherPlus = new WeatherPlus();
Note: The in-memory cache is not shared across different instances or servers. It’s suitable for development and testing but not recommended for production environments where you have multiple server instances.
For production use, it’s recommended to use a Redis cache, which allows sharing cached data across different instances of your application.
import { createClient } from 'redis';
const redisClient = createClient();
await redisClient.connect();
const weatherPlus = new WeatherPlus({
redisClient,
});
You can customize the cache TTL (time-to-live) in seconds. The default TTL is 300 seconds (5 minutes).
const weatherPlus = new WeatherPlus({
cacheTTL: 600, // Cache data for 10 minutes
});
You can bypass the cache and force a fresh request to the provider by setting the bypassCache option to true.
const weather = await weatherPlus.getWeather(51.5074, -0.1278, { bypassCache: true });
This will not entirely bypass the cache, it bypasses it for the read request and then the returned data is cached again for future use.
The library uses geohashing to cache weather data for nearby locations efficiently. Geohashing converts latitude and longitude into a short alphanumeric string, representing an area on the Earth’s surface.
By default, the geohash precision is set to 5, which corresponds to an area of approximately 4.9 km x 4.9 km.
const weatherPlus = new WeatherPlus();
You can adjust the geohashPrecision to broaden or narrow the caching area.
// Broader caching area (less precise)
const weatherPlus = new WeatherPlus({
geohashPrecision: 3, // Approximately 156 km x 156 km
});
// Narrower caching area (more precise)
const weatherPlus = new WeatherPlus({
geohashPrecision: 7, // Approximately 610 m x 610 m
});
Note: A lower precision value results in a larger area being considered the same location for caching purposes. Adjust according to your application’s requirements.
The library provides custom error classes to help you handle specific error scenarios gracefully.
This error is thrown when a provider does not support the requested location.
import { InvalidProviderLocationError } from 'weather-plus';
try {
const weather = await weatherPlus.getWeather(51.5074, -0.1278); // London coordinates
} catch (error) {
if (error instanceof InvalidProviderLocationError) {
// Handle the error (e.g., notify the user or log the issue)
} else {
// Handle other types of errors
}
}
This library is built with TypeScript and includes type-safe interfaces for weather data and errors.
interface IWeatherData {
provider: string;
temperature: {
value: number;
unit: string;
};
humidity: {
value: number;
unit: string;
};
dewPoint: {
value: number;
unit: string;
};
conditions: {
value: string;
unit: string;
};
}
Note today the response is fairly basic, but we're working on adding more data all of the time.
import WeatherPlus from 'weather-plus';
import { createClient } from 'redis';
(async () => {
const redisClient = createClient();
await redisClient.connect();
const weatherPlus = new WeatherPlus({
providers: ['nws', 'openweather'],
apiKeys: {
openweather: 'your-openweather-api-key',
},
redisClient,
geohashPrecision: 5,
cacheTTL: 600, // 10 minutes
});
try {
const weather = await weatherPlus.getWeather(51.5074, -0.1278); // London
console.log(weather);
} catch (error) {
console.error('Error fetching weather data:', error);
} finally {
await redisClient.disconnect();
}
})();
MIT
Lovingly crafted in NYC by Victor Quinn at Texture
FAQs
Weather Plus is a powerful wrapper around various Weather APIs that simplifies adding weather data to your application
We found that weather-plus demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.