theweatherapi
Advanced tools
Comparing version 1.1.3 to 2.0.0
{ | ||
"name": "theweatherapi", | ||
"version": "1.1.3", | ||
"version": "2.0.0", | ||
"description": "An NPM Library used to get weather info", | ||
"main": "./src/index.js", | ||
"types": "./typings/index.d.ts", | ||
"files": [ | ||
"src", | ||
"typings" | ||
"src" | ||
], | ||
@@ -39,4 +37,4 @@ "exports": { | ||
"dependencies": { | ||
"axios": "^0.21.1" | ||
"axios": "^0.21.4" | ||
} | ||
} |
@@ -7,3 +7,3 @@ <p> | ||
# TheWeatherAPI | ||
# TheWeatherAPI v2!! | ||
@@ -13,2 +13,4 @@ A package to retrieve weather data from https://www.weatherapi.com/. | ||
Now v2! Introducing the Forecast API; get the forecast of a location for up to 6 days! | ||
# Features | ||
@@ -35,24 +37,20 @@ - Current Weather API | ||
//Get the current weather from the default location and the location of that weather | ||
const weather = client.current.weather; | ||
const weatherInfo = client.current.weather; | ||
const weatherLocation = client.current.location; | ||
//Log the data to the console | ||
console.log(`Current Weather of ${client.defaultLocation}:`, weather); | ||
console.log(`Current Weather of ${client.defaultLocation}:`, weatherInfo); | ||
console.log(`Current Weather Location of ${client.defaultLocation}:`, weatherLocation); | ||
//Get the weather from another location with Air Quality Information | ||
const currentWeatherData = await client.current.get('London', true); | ||
const { weather, location, aqi } = await client.current.get('London', true); | ||
//Destructure the weather data into the weather object and location object | ||
const { weather2, location2 } = currentWeatherData; | ||
//Log the results | ||
console.log(weather2, location2); | ||
console.log(weather, location, aqi); | ||
}); | ||
``` | ||
This is still a beta expect may bugs | ||
## Roadmap | ||
- [x] Current Weather API | ||
- [ ] Forecast Weather API | ||
- [x] Forecast Weather API | ||
- [ ] History Weather API | ||
@@ -64,4 +62,8 @@ - [ ] Astronomy API | ||
## Documentation | ||
Visit [this](./documentation/home.md) page. (Coming Soon™) | ||
<p> | ||
Powered by <a href="https://www.weatherapi.com/" title="Free Weather API">WeatherAPI.com</a> | ||
</p> |
module.exports = { | ||
WeatherClient: require('./WeatherClient'), | ||
Constants: require('./Util/Constants') | ||
//Clients | ||
BaseWeatherClient: require('./client/BaseWeatherClient'), | ||
WeatherClient: require('./client/WeatherClient'), | ||
//Structures | ||
CurrentWeather: require('./structures/Current'), | ||
Location: require('./structures/Location'), | ||
Aqi: require('./structures/Aqi'), | ||
Alert: require('./structures/Alert'), | ||
forecast: require('./structures/forecast'), | ||
//Utils | ||
Constants: require('./Utils/Constants'), | ||
Util: require('./Utils/Util') | ||
}; |
@@ -1,53 +0,88 @@ | ||
module.exports = async (request) => { | ||
const axios = require('axios'); | ||
const Util = require('../Util/Util'); | ||
const { TypeError, WeatherError, RangeError } = require('../errors'); | ||
/** | ||
* A handler to make correct API calls | ||
* @extends null | ||
*/ | ||
class RequestHandler extends null { | ||
let baseUrl = 'http://api.weatherapi.com/v1/'; | ||
/** | ||
* Makes a request to the specified API endpoint. | ||
* @param {object} request The request object. | ||
* @return {object} Request response object. | ||
*/ | ||
static async makeRequest(request) { | ||
const axios = require('axios'); | ||
const Util = require('../Utils/Util'); | ||
const { TypeError, WeatherError, RangeError } = require('../errors'); | ||
let baseUrl = 'http://api.weatherapi.com/v1/'; | ||
//Check if path is a string | ||
Util.verifyString(request.path); | ||
//Check if path is a string | ||
Util.verifyString(request.path); | ||
baseUrl += request.path; | ||
baseUrl += request.path; | ||
//Check if request is an Object | ||
Util.verifyObject(request,'Request Options'); | ||
//Check if request is an Object | ||
Util.verifyObject(request,'Request Options'); | ||
//Add key to URL | ||
baseUrl += `?key=${request.key}`; | ||
//Add key to URL | ||
baseUrl += `?key=${request.key}`; | ||
//Check if the parameters is an array | ||
Util.verifyArray(request.params,'Request Params options'); | ||
//Check if the parameters is an array | ||
Util.verifyArray(request.params,'Request Params options'); | ||
//Add all params to URL | ||
for (const param of request.params) { | ||
if (request.params.length === 0) continue; | ||
Util.verifyString(param, `Request Parameter #${request.params.indexOf(param)}`); | ||
baseUrl += `&${param}`; | ||
} | ||
//Add all params to URL | ||
for (const param of request.params) { | ||
if (request.params.length === 0) continue; | ||
Util.verifyString(param, `Request Parameter #${request.params.indexOf(param)}`); | ||
baseUrl += `&${param}`; | ||
} | ||
//Execute the right request depending on the method | ||
//Execute the right request depending on the method | ||
const methods = ['GET', 'POST', 'PATCH', 'DELETE', 'PUT']; | ||
const methods = ['GET', 'POST', 'PATCH', 'DELETE', 'PUT']; | ||
const method = request.method.toUpperCase(); | ||
const method = request.method.toUpperCase(); | ||
//Check if the method is valid | ||
if (!methods.includes(method)) throw new TypeError('UNKNOWN_METHOD'); | ||
//Check if the method is valid | ||
if (!methods.includes(method)) throw new TypeError('UNKNOWN_METHOD'); | ||
switch(method) { | ||
case('GET'): | ||
const reponse = await axios.get(baseUrl) | ||
.catch(error => handleError(error)); | ||
return reponse; | ||
default: throw new WeatherError('METHOD_NOT_ALLOWED', method); | ||
switch(method) { | ||
case('GET'): | ||
const reponse = await axios.get(baseUrl) | ||
.catch(error => RequestHandler.handleError(error)); | ||
return reponse; | ||
default: throw new WeatherError('METHOD_NOT_ALLOWED', method); | ||
} | ||
} | ||
/** | ||
* Handles the API error and throws a new one accordingly | ||
* @param {Object} error The error object returned by Axios | ||
*/ | ||
static handleError(error) { | ||
const WeatherAPIError = require('./WeatherAPIError'); | ||
const { TypeError, WeatherError, RangeError } = require('../errors'); | ||
if (error.response.status === 404) throw new WeatherError('UKNOWN_API_ENDPOINT'); | ||
throw new WeatherAPIError(error.response.data, error.response.config); | ||
} | ||
/** | ||
* Creates a request object | ||
* @param {String} path The API endpoint (ex: forecast.json/.xml) | ||
* @param {Object} method The HTTP method (ex:GET) | ||
* @param {Array<String>} parameters The HTTP parameters (ex:q=London) | ||
* @param {String} key The API key | ||
*/ | ||
static createRequestObj(path, method, parameters = [], key) { | ||
return { | ||
path, | ||
method, | ||
params: parameters, | ||
key | ||
}; | ||
} | ||
} | ||
const handleError = (error) => { | ||
const WeatherAPIError = require('./WeatherAPIError'); | ||
const { TypeError, WeatherError, RangeError } = require('../errors'); | ||
if (error.response.status === 404) throw new WeatherError('UKNOWN_API_ENDPOINT'); | ||
throw new WeatherAPIError(error.response.data, error.response.config); | ||
} | ||
module.exports = RequestHandler; |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
62093
18
1342
65
1
1
Updatedaxios@^0.21.4