diff --git a/src/quran/audio.ts b/src/quran/audio.ts index 9b1fa86..38ae9f5 100644 --- a/src/quran/audio.ts +++ b/src/quran/audio.ts @@ -1,127 +1,128 @@ -import { AxiosError, AxiosResponse } from "axios"; +import { AxiosError } from "axios"; import url from "url"; import Api from "../api"; -import { AudioQueryParams } from "../types"; +import { AudioApi, AudioQueryParams, IAudio, IListOfAllAudioOfAReciter, IRecitation, ISingleRecitation, IReciters, IAyahRecitationSpecificSurah, IAyahRecitationSpecificJuz, IAyahRecitationSpecificMadaniMushafPage, IAyahRecitationSpecificRubelHizb, IAyahRecitationSpecificHizb, IAyahRecitationSpecificAyah } from "../types"; +import { handleError, handleResponse } from "../utils"; const api = Api(); const ALLOWED_LANGUAGES = new Set(['en', 'ur', 'bn', 'tr', 'es', 'fr', 'bs', 'ru', 'ml', 'id', 'uz', 'nl', 'de', 'tg', 'ta', 'ja', 'it', 'vi', 'zh', 'sq', 'fa', 'bg', 'bm', 'ha', 'pt', 'ro', 'hi', 'sw', 'kk', 'th', 'tl', 'km', 'as', 'ko', 'so', 'az', 'ku', 'dv', 'ms', 'prs', 'zgh', 'am', 'ce', 'cs', 'fi', 'gu', 'he', 'ka', 'kn', 'ks', 'lg', 'mk', 'mr', 'mrn', 'ne', 'no', 'om', 'pl', 'ps', 'rw', 'sd', 'se', 'si', 'sr', 'sv', 'te', 'tt', 'ug', 'uk', 'sq', 'yo']); -const audio = { +const audio: AudioApi = { - getChaptersAudioOfAReciter(id: number, chapter_number: number): Promise { + getChaptersAudioOfAReciter(id: number, chapter_number: number): Promise { if (!id || !chapter_number) return Promise.reject(new Error('Reciter\'s ID and Chapter number is required')); return new Promise((resolve, reject) => { api.get(`/chapter_recitations/${id}/${chapter_number}`,) - .then((response) => resolve(response.data)) - .catch((error) => reject(error)); + .then(handleResponse(resolve)) + .catch(handleError(reject)); }); }, - getAllChaptersAudioOfAReciter(id: number): Promise { + getAllChaptersAudioOfAReciter(id: number): Promise { if (!id) return Promise.reject(new Error('Reciter\'s ID is required')); return new Promise((resolve, reject) => { api.get(`/chapter_recitations/${id}`) - .then((response) => resolve(response.data)) - .catch((error) => reject(error)); + .then(handleResponse(resolve)) + .catch(handleError(reject)); }); }, - getRecitations(language: string = 'en'): Promise { + getRecitations(language: string = 'en'): Promise { const isLanguageSupported = ALLOWED_LANGUAGES.has(language); if (!isLanguageSupported) return Promise.reject(new Error("Provided language is not supported")); return new Promise((resolve, reject) => { api.get(`/resources/languages?${new url.URLSearchParams({ language })}`) - .then((response) => resolve(response.data)) - .catch((error) => reject(error)); + .then(handleResponse(resolve)) + .catch(handleError(reject)); }); }, - getAllAudioFilesofARecitation(recitation_id: number, query?: AudioQueryParams): Promise { + getAllAudioFilesofARecitation(recitation_id: number, query?: AudioQueryParams): Promise { if (!recitation_id) return Promise.reject(new Error('recitation_id is required')); let uri = `/quran/recitations/${recitation_id}`; if (query && Object.values(query).length > 0) uri = `${uri}?${new URLSearchParams(query as URLSearchParams)}`; return new Promise((resolve, reject) => { api.get(uri) - .then((response) => resolve(response.data)) - .catch((error) => reject(error)); + .then(handleResponse(resolve)) + .catch(handleError(reject)); }); }, - getListOfChapterReciters(language: string = 'en'): Promise { + getListOfChapterReciters(language: string = 'en'): Promise { const isLanguageSupported = ALLOWED_LANGUAGES.has(language); if (!isLanguageSupported) return Promise.reject(new Error("Provided language is not supported")); return new Promise((resolve, reject) => { api.get(`/resources/chapter_reciters?${new URLSearchParams({ language })}`) - .then((response) => resolve(response.data)) - .catch((error) => reject(error)); + .then(handleResponse(resolve)) + .catch(handleError(reject)); }); }, - getAyahRecitationsForSpecificSurah(recitation_id: number, chapter_number: number): Promise { + getAyahRecitationsForSpecificSurah(recitation_id: number, chapter_number: number): Promise { if (!recitation_id || !chapter_number) return Promise.reject(new Error('recitation_id and Chapter number is required')); return new Promise((resolve, reject) => { api.get(`/recitations/${recitation_id}/by_chapter/${chapter_number}`) - .then((response) => resolve(response.data)) - .catch((error) => reject(error)); + .then(handleResponse(resolve)) + .catch(handleError(reject)); }); }, - getAyahRecitationsForSpecificJuz(recitation_id: number, juz_number: number): Promise { + getAyahRecitationsForSpecificJuz(recitation_id: number, juz_number: number): Promise { if (!recitation_id || !juz_number) return Promise.reject(new Error('recitation_id and juz_number is required')); return new Promise((resolve, reject) => { api.get(`/recitations/${recitation_id}/by_juz/${juz_number}`) - .then((response) => resolve(response.data)) - .catch((error) => reject(error)); + .then(handleResponse(resolve)) + .catch(handleError(reject)); }); }, - getAyahRecitationForSpecificMadaniMushafPage(recitation_id: number, page_number: number): Promise { + getAyahRecitationForSpecificMadaniMushafPage(recitation_id: number, page_number: number): Promise { if (!recitation_id || !page_number) return Promise.reject(new Error('recitation_id and Page number is required')); return new Promise((resolve, reject) => { api.get(`/recitations/${recitation_id}/by_page/${page_number}`) - .then((response) => resolve(response.data)) - .catch((error) => reject(error)); + .then(handleResponse(resolve)) + .catch(handleError(reject)); }); }, - getAyahRecitationForSpecificRubelHizb(recitation_id: number, rub_el_hizb_number: number): Promise { + getAyahRecitationForSpecificRubelHizb(recitation_id: number, rub_el_hizb_number: number): Promise { if (!recitation_id || !rub_el_hizb_number) return Promise.reject(new Error('recitation_id and rub_el_hizb_number is required')); return new Promise((resolve, reject) => { api.get(`/recitations/${recitation_id}/by_rub/${rub_el_hizb_number}`) - .then((response) => resolve(response.data)) - .catch((error) => reject(error)); + .then(handleResponse(resolve)) + .catch(handleError(reject)); }); }, - getAyahRecitationForSpecificHizb(recitation_id: number, hizb_number: number): Promise { + getAyahRecitationForSpecificHizb(recitation_id: number, hizb_number: number): Promise { if (!recitation_id || !hizb_number) return Promise.reject(new Error('recitation_id and hizb_number is required')); return new Promise((resolve, reject) => { api.get(`/recitations/${recitation_id}/by_hizb/${hizb_number}`) - .then((response) => resolve(response.data)) - .catch((error) => reject(error)); + .then(handleResponse(resolve)) + .catch(handleError(reject)); }); }, - getAyahRecitationForSpecificAyah(recitation_id: number, ayah_key: string): Promise { + getAyahRecitationForSpecificAyah(recitation_id: number, ayah_key: string): Promise { if (!recitation_id || !ayah_key) return Promise.reject(new Error('recitation_id and ayah_key is required')); return new Promise((resolve, reject) => { api.get(`/recitations/${recitation_id}/by_ayah/${ayah_key}`) - .then((response) => resolve(response.data)) - .catch((error) => reject(error)); + .then(handleResponse(resolve)) + .catch(handleError(reject)); }); } diff --git a/src/types.ts b/src/types.ts index b6c9f04..ce3c0af 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,5 @@ +import { AxiosError } from "axios"; + export interface CustomHeaders { [key: string]: string; }; @@ -16,4 +18,111 @@ export interface AudioQueryParams { hizb_number?: string; rub_el_hizb_number?: string; verse_key?: string; +}; + +export interface IAudio { + id: number; + chapter_id: number; + file_size: number; + format: string; + total_files: number; + audio_url: string; +}; + +export interface IListOfAllAudioOfAReciter { + audio_files: IAudio[]; +}; + +export interface IRecitation { + id: number; + reciter_name: string; + style: string; + translated_name: ITranslatedName; +}; + +export interface ITranslatedName { + name: string; + language_name: string; +}; + +export interface ISingleRecitationAudio { + url: string; + duration: number; + format: string; + segments?: any[]; +}; + +export interface IRecitationMeta { + reciter_name: string; + recitation_style?: string | null; +}; + +export interface ISingleRecitation { + audio_files: ISingleRecitationAudio[]; + meta: IRecitationMeta; +}; + +export interface IReciter { + id: number; + name: string; + arabic_name: string; + relative_path: string; + format: string; + files_size: number; +}; + +export interface IReciters { + reciters: IReciter[]; +}; + +export interface IAudioPagination { + per_page: number; + current_page: number; + next_page: number; + total_pages: number; + total_records: number; +}; + +export interface IAyahRecitationSpecificSurah { + audio_files: ISingleRecitationAudio[]; + pagination: IAudioPagination; +}; + +export interface IAyahRecitationSpecificJuz { + audio_files: ISingleRecitationAudio[]; + pagination: IAudioPagination; +}; + +export interface IAyahRecitationSpecificMadaniMushafPage { + audio_files: ISingleRecitationAudio[]; + pagination: IAudioPagination; +}; + +export interface IAyahRecitationSpecificRubelHizb { + audio_files: ISingleRecitationAudio[]; + pagination: IAudioPagination; +}; + +export interface IAyahRecitationSpecificHizb { + audio_files: ISingleRecitationAudio[]; + pagination: IAudioPagination; +}; + +export interface IAyahRecitationSpecificAyah { + audio_files: ISingleRecitationAudio[]; + pagination: IAudioPagination; +}; + +export interface AudioApi { + getChaptersAudioOfAReciter(id: number, chapter_number: number): Promise; + getAllChaptersAudioOfAReciter(id: number): Promise; + getRecitations(language: string): Promise; + getAllAudioFilesofARecitation(recitation_id: number, query?: AudioQueryParams): Promise; + getListOfChapterReciters(language: string): Promise; + getAyahRecitationsForSpecificSurah(recitation_id: number, chapter_number: number): Promise; + getAyahRecitationsForSpecificJuz(recitation_id: number, juz_number: number): Promise; + getAyahRecitationForSpecificMadaniMushafPage(recitation_id: number, page_number: number): Promise; + getAyahRecitationForSpecificRubelHizb(recitation_id: number, rub_el_hizb_number: number): Promise; + getAyahRecitationForSpecificHizb(recitation_id: number, hizb_number: number): Promise; + getAyahRecitationForSpecificAyah(recitation_id: number, ayah_key: string): Promise; }; \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..fa155c2 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,9 @@ +import { AxiosError, AxiosResponse } from 'axios'; + +export const handleResponse = ( + resolve: (value: T) => void) => (response: AxiosResponse) => + resolve(response.data); + +export const handleError = (reject: (reason: Error | AxiosError) => void) => + (error: AxiosError) => + reject(error); \ No newline at end of file