-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
57 lines (53 loc) · 1.58 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { Request, Response, NextFunction } from "express";
import path, { dirname } from "path";
import { fileURLToPath } from "url";
import axios from "axios";
const API_KEY: string = process.env.API_KEY;
const LOCATION_URL: string = "https://geocode.maps.co/search";
const WEATHER_URL: string = "https://api.weather.gov/points";
export function getPublicFolderPath(): string {
const __filename = fileURLToPath(import.meta.url); //abs path to filename
const __dirname = dirname(__filename); //abs path to directory
return path.join(__dirname, "..", "public");
}
export async function getLocationData(
req: Request,
res: Response,
next: NextFunction
) {
const city: string = req.params.city;
try {
const response = await axios.get(
`${LOCATION_URL}?q=${city}&api_key=${API_KEY}`
);
res.status(response.status).send(response.data);
} catch (err) {
next(err);
}
}
export function getWeatherData(
req: Request,
res: Response,
next: NextFunction
) {
const city: string = req.params.city;
axios
.get(`${LOCATION_URL}?q=${city}&api_key=${API_KEY}`)
.then((resp) => {
const top_match = resp.data[0]; //Get the top choice in the list.
return `${WEATHER_URL}/${top_match["lat"]},${top_match["lon"]}`;
})
.then((url) => {
return axios.get(url);
})
.then((resp) => {
const forecast_url = resp.data["properties"]["forecast"];
return axios.get(forecast_url);
})
.then((resp) => {
res.status(resp.status).send(resp.data["properties"]["periods"]);
})
.catch((err) => {
next(err);
});
}