import axios, { AxiosError, } from 'axios'; import { PrayerTimeOptions } from '../../interface/api/prayerTimeOptions'; // Define a custom error type type MyCustomError = { message: string; // You can add more fields as needed, such as status, code, etc. }; export async function getPrayerTimes( options: PrayerTimeOptions ): Promise { const { date, latitude, longitude, method, shafaq, tune, school, midnightMode, timezonestring, latitudeAdjustmentMethod, adjustment, iso8601, } = options; const endpoint = `http://api.aladhan.com/v1/timings/${date}`; const params: Record = { latitude, longitude, }; if (method) params.method = method; if (shafaq) params.shafaq = shafaq; if (tune) params.tune = tune; if (school) params.school = school; if (midnightMode) params.midnightMode = midnightMode; if (timezonestring) params.timezonestring = timezonestring; if (latitudeAdjustmentMethod) params.latitudeAdjustmentMethod = latitudeAdjustmentMethod; if (adjustment) params.adjustment = adjustment; if (iso8601) params.iso8601 = iso8601; try { const response = await axios.get( endpoint, { params } ); return response.data; } catch (error: | AxiosError | any) { const customError: MyCustomError = ( error as AxiosError ).response?.data || { message: ( error as AxiosError ).message, }; throw new Error( `Failed to fetch prayer times: ${customError.message}` ); } } // Example usage: // const options: PrayerTimeOptions = { // date: '17-07-2007', // latitude: 51.508515, // longitude: -0.1254872, // method: 2, // }; // getPrayerTimes(options) // .then((data) => { // console.log(data); // }) // .catch((error) => { // console.error(error); // });