Weather API integration

This commit is contained in:
Sean Edmunds
2024-11-27 16:00:17 -06:00
parent 18585daaec
commit 4490973a28
5 changed files with 330 additions and 10 deletions

View File

@@ -1 +1 @@
export { useQueryAPI } from './useAPI';
export { useQueryAPI, useWeatherAPI } from './useAPI';

View File

@@ -36,6 +36,41 @@ export function useQueryAPI({ endpoint }: { endpoint: string }) {
});
}
export function useWeatherAPI({ endpoint }: { endpoint: string }) {
let url = '';
if (endpoint === 'baseline') {
url = 'https://api.weather.gov/gridpoints/HGX/55,96';
} else if (endpoint === 'current') {
url = 'https://api.weather.gov/stations/KSGR/observations';
} else if (endpoint === 'forecast') {
url = 'https://api.weather.gov/gridpoints/HGX/55,96/forecast';
}
const callEndpoint = async (endpoint: string) => {
const options: object = {
method: 'GET',
headers: {
'Content-Type': 'application/geo+json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Credentials': true,
'Sec-Fetch-Mode': 'no-cors',
'Sec-Fetch-Site': 'same-site',
},
};
const response = await fetch(url);
if (!response.ok) {
throw new Error(`${response.status} ${response.statusText}`);
}
return await response.json();
};
return useQuery({
queryKey: ['GET', url],
queryFn: () => callEndpoint(endpoint),
});
}
export function useMutationAPI() {
const callEndpoint = async (
method: string,