UNPKG

cloudku-murotal

Version:

JavaScript client for Murotal API - Islamic prayer times, Quran, Hadith, and Dua

131 lines (101 loc) 3.48 kB
class MurotalAPI { constructor(baseURL = 'https://cloudku.us.kg') { this.baseURL = baseURL; this.fetch = this._getFetch(); } _getFetch() { if (typeof fetch !== 'undefined') return fetch; if (typeof globalThis !== 'undefined' && globalThis.fetch) return globalThis.fetch; if (typeof window !== 'undefined' && window.fetch) return window.fetch; return import('node-fetch').then(mod => mod.default).catch(() => import('undici').then(mod => mod.fetch) ); } async request(endpoint, params = {}) { const url = new URL(`${this.baseURL}/api/murotal${endpoint}`); Object.keys(params).forEach(key => params[key] && url.searchParams.append(key, params[key]) ); const fetchFn = await this.fetch; const response = await (typeof fetchFn === 'function' ? fetchFn(url) : this.fetch(url)); if (!response.ok) throw new Error(`HTTP ${response.status}`); return response.json(); } async getCities() { return this.request('/kota'); } async getPrayerTimes(id, time) { return this.request('/jadwal', { id, time }); } async getPrayerTimesByMonth(id, month, year) { return this.request('/jadwal/month', { id, month, year }); } async searchCities(query) { return this.request('/search/kota', { q: query }); } async getTodayHijriDate() { return this.request('/hijiriahnow'); } async getHijriDate(date) { return this.request('/hijiriah', { date }); } async getRandomHusna() { return this.request('/random/asmaulhusna'); } async getAllHusna() { return this.request('/all/asmulhusna'); } async getHusnaByNumber(id) { return this.request('/husna', { id }); } async getRandomSurah() { return this.request('/random/surah'); } async getAyahRange(surat, ayat, range) { return this.request('/range/surah', { surat, ayat, range }); } async getSingleAyah(surat, ayat) { return this.request('/ayat/single', { surat, ayat }); } async getJuz(id) { return this.request('/juz', { id }); } async getRandomAyah() { return this.request('/random/ayat'); } async getSurahList() { return this.request('/list/surah'); } async getSurahByNumber(id) { return this.request('/surah', { id }); } async getAyahByNumber(id) { return this.request('/ayat', { id }); } async getSurahAyahRange(id, ayat) { return this.request('/range/surah', { id, ayat }); } async getAyahByTheme(id) { return this.request('/ayat/theme', { id }); } async getAllThemes() { return this.request('/themes'); } async getDoaSources() { return this.request('/list/source/doa'); } async searchDoa(query) { return this.request('/search/doa', { q: query }); } async getRandomDoa() { return this.request('/random/doa'); } async getRandomHadistArbain() { return this.request('/random/hadist/arbain'); } async getRandomHadistBukhari() { return this.request('/random/hadist/bukhari'); } } export default MurotalAPI; export { MurotalAPI };