node-ts-open-weather-map
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,8 @@ | ||
# [1.2.0](https://github.com/ovaar/node-ts-open-weather-map/compare/v1.1.0...v1.2.0) (2020-08-03) | ||
### Features | ||
* **OpenWeatherMap:** Add `*ByCityId` APIs ([#3](https://github.com/ovaar/node-ts-open-weather-map/issues/3)) ([e152da9](https://github.com/ovaar/node-ts-open-weather-map/commit/e152da9addb9cc2f115ae821289dbed8158fbeca)) | ||
# [1.1.0](https://github.com/ovaar/node-ts-open-weather-map/compare/v1.0.1...v1.1.0) (2020-07-07) | ||
@@ -2,0 +9,0 @@ |
@@ -80,2 +80,50 @@ 'use strict'; | ||
} | ||
async byCityId(cityId) { | ||
try { | ||
const params = new url_1.URLSearchParams({ | ||
id: cityId.toString(), | ||
units: this.options.temperatureUnit | ||
}); | ||
const url = this.getBaseUrl(OpenWeatherMapApiDataType.Weather) + | ||
'&' + | ||
params.toString(); | ||
const { data } = await axios_1.default.get(url); | ||
return data; | ||
} | ||
catch (error) { | ||
throw error; | ||
} | ||
} | ||
async forecastByCityId(cityId) { | ||
try { | ||
const params = new url_1.URLSearchParams({ | ||
id: cityId.toString(), | ||
units: this.options.temperatureUnit | ||
}); | ||
const url = this.getBaseUrl(OpenWeatherMapApiDataType.Forecast) + | ||
'&' + | ||
params.toString(); | ||
const { data } = await axios_1.default.get(url); | ||
return data; | ||
} | ||
catch (error) { | ||
throw error; | ||
} | ||
} | ||
async dailyForecastByCityId(cityId) { | ||
try { | ||
const params = new url_1.URLSearchParams({ | ||
id: cityId.toString(), | ||
units: this.options.temperatureUnit | ||
}); | ||
const url = this.getBaseUrl(OpenWeatherMapApiDataType.DailyForecast) + | ||
'&' + | ||
params.toString(); | ||
const { data } = await axios_1.default.get(url); | ||
return data; | ||
} | ||
catch (error) { | ||
throw error; | ||
} | ||
} | ||
getBaseUrl(type) { | ||
@@ -82,0 +130,0 @@ const params = new url_1.URLSearchParams({ |
@@ -20,3 +20,6 @@ export declare enum OpenWeatherMapApiUnits { | ||
dailyForecastByCityName(queryOpts: IByCityNameOptions): Promise<any>; | ||
byCityId(cityId: number | string): Promise<any>; | ||
forecastByCityId(cityId: number | string): Promise<any>; | ||
dailyForecastByCityId(cityId: number | string): Promise<any>; | ||
private getBaseUrl; | ||
} |
{ | ||
"name": "node-ts-open-weather-map", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Node.js typescript OpenWeatherMap api wrapper", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -88,3 +88,3 @@ 'use strict' | ||
} | ||
public async dailyForecastByCityName(queryOpts: IByCityNameOptions): Promise<any> { | ||
@@ -109,2 +109,59 @@ try { | ||
public async byCityId(cityId: number | string): Promise<any> { | ||
try { | ||
const params = new URLSearchParams({ | ||
id: cityId.toString(), | ||
units: this.options.temperatureUnit | ||
}) | ||
const url = | ||
this.getBaseUrl(OpenWeatherMapApiDataType.Weather) + | ||
'&' + | ||
params.toString() | ||
const { data } = await axios.get(url) | ||
return data | ||
} catch (error) { | ||
throw error | ||
} | ||
} | ||
public async forecastByCityId(cityId: number | string): Promise<any> { | ||
try { | ||
const params = new URLSearchParams({ | ||
id: cityId.toString(), | ||
units: this.options.temperatureUnit | ||
}) | ||
const url = | ||
this.getBaseUrl(OpenWeatherMapApiDataType.Forecast) + | ||
'&' + | ||
params.toString() | ||
const { data } = await axios.get(url) | ||
return data | ||
} catch (error) { | ||
throw error | ||
} | ||
} | ||
public async dailyForecastByCityId(cityId: number | string): Promise<any> { | ||
try { | ||
const params = new URLSearchParams({ | ||
id: cityId.toString(), | ||
units: this.options.temperatureUnit | ||
}) | ||
const url = | ||
this.getBaseUrl(OpenWeatherMapApiDataType.DailyForecast) + | ||
'&' + | ||
params.toString() | ||
const { data } = await axios.get(url) | ||
return data | ||
} catch (error) { | ||
throw error | ||
} | ||
} | ||
private getBaseUrl(type: OpenWeatherMapApiDataType): URL { | ||
@@ -111,0 +168,0 @@ const params = new URLSearchParams({ |
@@ -163,3 +163,3 @@ declare let describe: jest.Describe | ||
it('Should get the dailyForecastByCityName successfully', async () => { | ||
it('Should get the dailyForecastByCityName successfully', async () => { | ||
const expectedUrl = | ||
@@ -211,2 +211,129 @@ 'https://api.openweathermap.org/data/2.5/forecast/daily?APPID=xxx-xxx-xxx&q=Eindhoven%2Cnl&units=metric' | ||
}) | ||
it('Should get the weather byCityId successfully', async () => { | ||
const expectedUrl = | ||
'https://api.openweathermap.org/data/1.1/weather?APPID=xxx-xxx-xxx&id=12345&units=imperial' | ||
const response: AxiosResponse = { | ||
config: {}, | ||
data: sampleWeatherData, | ||
headers: {}, | ||
status: 200, | ||
statusText: 'success' | ||
} | ||
const stub = sandbox.stub(axios, 'get').resolves(response) | ||
const options: IOpenWeatherMapApiOptions = { | ||
apiVersion: '1.1', | ||
key: 'xxx-xxx-xxx', | ||
temperatureUnit: OpenWeatherMapApiUnits.Fahrenheit | ||
} | ||
const api = new OpenWeatherMapApi(options) | ||
const data = await api.byCityId(12345) | ||
sinon.assert.called(stub) | ||
const call = stub.getCall(0) | ||
expect(data).toBe(sampleWeatherData) | ||
expect(call.args[0]).toBe(expectedUrl) | ||
}) | ||
it('Should throw an error byCityId fails', async () => { | ||
sandbox.stub(axios, 'get').rejects(new Error('http error')) | ||
const options: IOpenWeatherMapApiOptions = { | ||
key: 'xxx-xxx-xxx' | ||
} | ||
const api = new OpenWeatherMapApi(options) | ||
await expect( | ||
api.byCityId(23456) | ||
).rejects.toThrow('http error') | ||
}) | ||
it('Should get the forecastByCityId successfully', async () => { | ||
const expectedUrl = | ||
'https://api.openweathermap.org/data/2.5/forecast?APPID=xxx-xxx-xxx&id=34567&units=metric' | ||
const response: AxiosResponse = { | ||
config: {}, | ||
data: sampleForecastData, | ||
headers: {}, | ||
status: 200, | ||
statusText: 'success' | ||
} | ||
const stub = sandbox.stub(axios, 'get').resolves(response) | ||
const options: IOpenWeatherMapApiOptions = { | ||
key: 'xxx-xxx-xxx' | ||
} | ||
const api = new OpenWeatherMapApi(options) | ||
const data = await api.forecastByCityId(34567) | ||
sinon.assert.called(stub) | ||
const call = stub.getCall(0) | ||
expect(data).toBe(sampleForecastData) | ||
expect(call.args[0]).toBe(expectedUrl) | ||
}) | ||
it('Should throw an error forecastByCityId fails', async () => { | ||
sandbox.stub(axios, 'get').rejects(new Error('http error')) | ||
const options: IOpenWeatherMapApiOptions = { | ||
key: 'xxx-xxx-xxx' | ||
} | ||
const api = new OpenWeatherMapApi(options) | ||
await expect( | ||
api.forecastByCityId(45678) | ||
).rejects.toThrow('http error') | ||
}) | ||
it('Should get the dailyForecastByCityId successfully', async () => { | ||
const expectedUrl = | ||
'https://api.openweathermap.org/data/2.5/forecast/daily?APPID=xxx-xxx-xxx&id=56789&units=metric' | ||
const response: AxiosResponse = { | ||
config: {}, | ||
data: sampleDailyForecastData, | ||
headers: {}, | ||
status: 200, | ||
statusText: 'success' | ||
} | ||
const stub = sandbox.stub(axios, 'get').resolves(response) | ||
const options: IOpenWeatherMapApiOptions = { | ||
key: 'xxx-xxx-xxx' | ||
} | ||
const api = new OpenWeatherMapApi(options) | ||
const data = await api.dailyForecastByCityId(56789) | ||
sinon.assert.called(stub) | ||
const call = stub.getCall(0) | ||
expect(data).toBe(sampleDailyForecastData) | ||
expect(call.args[0]).toBe(expectedUrl) | ||
}) | ||
it('Should throw an error dailyForecastByCityId fails', async () => { | ||
sandbox.stub(axios, 'get').rejects(new Error('http error')) | ||
const options: IOpenWeatherMapApiOptions = { | ||
key: 'xxx-xxx-xxx' | ||
} | ||
const api = new OpenWeatherMapApi(options) | ||
await expect( | ||
api.dailyForecastByCityId(67890) | ||
).rejects.toThrow('http error') | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
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
41284
996