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 getMonthPrayerTimesByAddress( options: PrayerTimeOptions ): Promise { const { address, year, month, method, shafaq, tune, school, midnightMode, latitudeAdjustmentMethod, adjustment, iso8601, } = options; const endpoint = `http://api.aladhan.com/v1/calendarByAddress/${ year || '' }/${month || ''}`; const params: Record = { address, }; if (year) params.year = year; if (month) params.month = month; 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 = { // address: 'Sultanahmet Mosque, Istanbul, Turkey', // year: 2017, // month: 4, // method: 2, // }; // getMonthPrayerTimesByAddress(options) // .then((data) => { // console.log(data); // }) // .catch((error) => { // console.error(error); // });