-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
57 lines (49 loc) · 1.44 KB
/
mod.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 {
RelativeLocation,
NOAACordinate,
NOAACordinateProperties,
NOAAForecast,
} from "./@types/types.ts";
/**
* Creates an instance to easily work with the NOAA Weather API. Provides a cloure
* around useful methods for interacting with the US National Weather Service API.
* @example
* import NOAA from "./mod.ts";
* const noaa = await NOAA(40, -105.27);
* if (noaa) {
* const forecast = await noaa.Forecast();
* console.log(forecast);
* }
*/
export default async function NOAA(lat: number, lon: number) {
// https://api.weather.gov/points/{lat},{lon}
const req = await fetch(`https://api.weather.gov/points/${lat},${lon}`);
// Validate that the request succeeded and that the JSON could be parsed properly
if (!req) return null;
let location: RelativeLocation;
let props: NOAACordinateProperties;
// Attempt to parse JSON if fails return null
try {
const data = (await req.json()) as NOAACordinate;
location = data.properties.relativeLocation;
props = data.properties;
} catch (_) {
return null;
}
// return the API
return {
Forecast: () => Forecast(props.forecast),
Location: () => location,
};
}
// Definitions for API Methods
async function Forecast(forecastURL: string) {
const req = await fetch(forecastURL);
// Returns null on failed requests
// Could be network related, server issue, too many requests.
if (!req.ok) {
return null;
}
const forecast = (await req.json()) as NOAAForecast;
return forecast;
}