interstellar_ai/app/backend/weather.ts
sageTheDM 91353bd051 main (#137)
Reviewed-on: https://interstellardevelopment.org/code/code/React-Group/interstellar_ai/pulls/137
Reviewed-by: Patrick <patrick_pluto@noreply.localhost>
Co-authored-by: sageTheDM <info@photofuel.tech>
Co-committed-by: sageTheDM <info@photofuel.tech>
2024-10-11 10:18:33 +02:00

29 lines
1.3 KiB
TypeScript

import axios from "axios";
// Initialize the API URL for the weather service
const apiURL = new URL("http://localhost:5000/interstellar_ai/api/weather");
if (typeof window !== 'undefined') {
apiURL.hostname = window.location.hostname; // Use current hostname in the browser
} else {
apiURL.hostname = "localhost"; // Fallback for server-side
}
// Function to get weather data
export const getWeather = async (data: object): Promise<string> => {
try {
// Make a POST request to the weather API with the provided data
const response = await axios.post(apiURL.href, data);
const status = response.data.status; // Extract the status from the response
const success = response.data.response; // Extract the actual weather data from the response
// Send the status and success response back to the worker
postMessage({ status, success });
console.log(JSON.stringify(success)); // Log the successful response for debugging
return JSON.stringify(success); // Return the weather data as a JSON string
} catch (error) {
// Handle any errors that occur during the request
postMessage({ status: 500, success: false });
console.log(error); // Log the error for debugging
return ""; // Return an empty string in case of an error
}
};