import axios, { AxiosError, } from 'axios'; import { PrayerTimeOptions } from '../../../interface/api/prayerTimeOptions'; // Define a custom error type type RequestError = { message: string; // You can add more fields as needed, such as status, code, etc. }; export async function getPrayerTimesByAddress( options: PrayerTimeOptions ): Promise { const { date, address, method, shafaq, tune, school, midnightMode, latitudeAdjustmentMethod, adjustment, iso8601, } = options; const endpoint = `http://api.aladhan.com/v1/timingsByAddress/${date}`; const params: Record = { address, }; 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 (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: RequestError = ( 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', // address: 'Regents Park Mosque, London, UK', // }; // getPrayerTimesByAddress(options) // .then((data) => { // console.log(data); // }) // .catch((error) => { // console.error(error); // });