Skip to content

Commit 090b29d

Browse files
committed
refactors openWeatherMaps service to follow export convention of other sevices
1 parent 6793616 commit 090b29d

File tree

3 files changed

+54
-53
lines changed

3 files changed

+54
-53
lines changed

src/controllers/weather.controller.mjs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import {
2-
currentPollution,
3-
currentWeather,
4-
forecastPollution,
5-
forecastWeather,
6-
} from "../services/openWeatherMaps.service.mjs";
1+
import openWeatherMapsService from "../services/openWeatherMaps.service.mjs";
72
import { getCoordinateBound } from "../utils/geoHelpers.mjs";
83
import { translateEpochDay } from "../utils/dateTimeHelpers.mjs";
94

105
export const aggregate = async (req, res) => {
11-
const weatherReq = await currentWeather(req.query);
12-
const pollutionReq = await currentPollution(req.query);
6+
const weatherReq = await openWeatherMapsService.currentWeather(req.query);
7+
const pollutionReq = await openWeatherMapsService.currentPollution(req.query);
138

149
let warnings = null;
1510
const bound = getCoordinateBound(req.query.lat, req.query.lon);
@@ -22,7 +17,7 @@ export const aggregate = async (req, res) => {
2217
}
2318
}
2419

25-
const forecastReq = await forecastWeather(req.query).then((res) => {
20+
const forecastReq = await openWeatherMapsService.forecastWeather(req.query).then((res) => {
2621
const forecastData = res.data;
2722
const upcoming = {};
2823

@@ -47,8 +42,8 @@ export const aggregate = async (req, res) => {
4742
};
4843

4944
export const weather = async (req, res) => {
50-
const weatherReq = await currentWeather(req.query);
51-
const forecastReq = await forecastWeather(req.query);
45+
const weatherReq = await openWeatherMapsService.currentWeather(req.query);
46+
const forecastReq = await openWeatherMapsService.forecastWeather(req.query);
5247

5348
return res.status(200).send({
5449
data: {
@@ -59,8 +54,8 @@ export const weather = async (req, res) => {
5954
};
6055

6156
export const pollution = async (req, res) => {
62-
const pollutionReq = await currentPollution(req.query);
63-
const forecastReq = await forecastPollution(req.query);
57+
const pollutionReq = await openWeatherMapsService.currentPollution(req.query);
58+
const forecastReq = await openWeatherMapsService.forecastPollution(req.query);
6459

6560
return res.status(200).send({
6661
data: {
Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
11
import axios from "axios";
22
import { OWM_API_URL } from "../utils/constants.mjs";
33

4-
export const currentWeather = async (query) =>
5-
axios({
6-
method: 'get',
7-
url: OWM_API_URL + '/weather',
8-
params: {
9-
...query,
10-
appid: process.env.OWM_API_KEY
11-
}
12-
});
4+
const openWeatherMapsService = {
5+
currentWeather: async (query) =>
6+
axios({
7+
method: 'get',
8+
url: OWM_API_URL + '/weather',
9+
params: {
10+
...query,
11+
appid: process.env.OWM_API_KEY,
12+
},
13+
}),
1314

14-
export const forecastWeather = async (query) =>
15-
axios({
16-
method: 'get',
17-
url: OWM_API_URL + '/forecast',
18-
params: {
19-
...query,
20-
appid: process.env.OWM_API_KEY
21-
}
22-
});
15+
forecastWeather: async (query) =>
16+
axios({
17+
method: 'get',
18+
url: OWM_API_URL + '/forecast',
19+
params: {
20+
...query,
21+
appid: process.env.OWM_API_KEY,
22+
},
23+
}),
2324

24-
export const currentPollution = async (query) =>
25-
axios({
26-
method: 'get',
27-
url: OWM_API_URL + '/air_pollution',
28-
params: {
29-
...query,
30-
appid: process.env.OWM_API_KEY
31-
}
32-
});
25+
currentPollution: async (query) =>
26+
axios({
27+
method: 'get',
28+
url: OWM_API_URL + '/air_pollution',
29+
params: {
30+
...query,
31+
appid: process.env.OWM_API_KEY,
32+
},
33+
}),
3334

34-
export const forecastPollution = async (query) =>
35-
axios({
36-
method: 'get',
37-
url: OWM_API_URL + '/air_pollution/forecast',
38-
params: {
39-
...query,
40-
appid: process.env.OWM_API_KEY
41-
}
42-
});
35+
forecastPollution: async (query) =>
36+
axios({
37+
method: 'get',
38+
url: OWM_API_URL + '/air_pollution/forecast',
39+
params: {
40+
...query,
41+
appid: process.env.OWM_API_KEY,
42+
},
43+
}),
44+
};
45+
46+
export default openWeatherMapsService;

src/tests/api.test.mjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import {
77
} from "../fixtures/openWeatherMaps.fixture.mjs";
88

99
jest.unstable_mockModule("../services/openWeatherMaps.service.mjs", () => ({
10-
currentWeather: jest.fn().mockResolvedValue(weather),
11-
forecastWeather: jest.fn().mockResolvedValue(weatherForecast),
12-
currentPollution: jest.fn().mockResolvedValue(airPollution),
13-
forecastPollution: jest.fn().mockResolvedValue(airPollutionForecast),
10+
default: {
11+
currentWeather: jest.fn().mockResolvedValue(weather),
12+
forecastWeather: jest.fn().mockResolvedValue(weatherForecast),
13+
currentPollution: jest.fn().mockResolvedValue(airPollution),
14+
forecastPollution: jest.fn().mockResolvedValue(airPollutionForecast),
15+
},
1416
}));
1517

1618
import request from "supertest";

0 commit comments

Comments
 (0)