1
1
import { Request , Response , NextFunction } from "express" ;
2
2
import path , { dirname } from "path" ;
3
3
import { fileURLToPath } from "url" ;
4
- import axios from "axios" ;
4
+ import axios , { AxiosResponse } from "axios" ;
5
5
6
6
const API_KEY : string = process . env . API_KEY ;
7
+ const LOCATION_URL : string = "https://geocode.maps.co/search" ;
8
+ const WEATHER_URL : string = "https://api.weather.gov/points" ;
7
9
8
10
export function getPublicFolderPath ( ) : string {
9
11
const __filename = fileURLToPath ( import . meta. url ) ; //abs path to filename
@@ -19,10 +21,37 @@ export async function getLocationData(
19
21
const city : string = req . params . city ;
20
22
try {
21
23
const response = await axios . get (
22
- `https://geocode.maps.co/search ?q=${ city } &api_key=${ API_KEY } `
24
+ `${ LOCATION_URL } ?q=${ city } &api_key=${ API_KEY } `
23
25
) ;
24
26
res . status ( response . status ) . send ( response . data ) ;
25
27
} catch ( err ) {
26
28
next ( err ) ;
27
29
}
28
30
}
31
+
32
+ export function getWeatherData (
33
+ req : Request ,
34
+ res : Response ,
35
+ next : NextFunction
36
+ ) {
37
+ const city : string = req . params . city ;
38
+ axios
39
+ . get ( `${ LOCATION_URL } ?q=${ city } &api_key=${ API_KEY } ` )
40
+ . then ( ( resp ) => {
41
+ const top_match = resp . data [ 0 ] ;
42
+ return `${ WEATHER_URL } /${ top_match [ "lat" ] } ,${ top_match [ "lon" ] } ` ;
43
+ } )
44
+ . then ( ( url ) => {
45
+ return axios . get ( url ) ;
46
+ } )
47
+ . then ( ( resp ) => {
48
+ const forecast_url = resp . data [ "properties" ] [ "forecast" ] ;
49
+ return axios . get ( forecast_url ) ;
50
+ } )
51
+ . then ( ( resp ) => {
52
+ res . status ( resp . status ) . send ( resp . data [ "properties" ] [ "periods" ] ) ;
53
+ } )
54
+ . catch ( ( err ) => {
55
+ next ( err ) ;
56
+ } ) ;
57
+ }
0 commit comments