Weather API integration
This commit is contained in:
@@ -1 +1 @@
|
||||
export { useQueryAPI } from './useAPI';
|
||||
export { useQueryAPI, useWeatherAPI } from './useAPI';
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user