Skip to content

Commit

Permalink
feat: all the apis for audio have been tested 🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
PrantaDas committed Jul 27, 2024
1 parent 793fc3a commit 03e3bbf
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 36 deletions.
73 changes: 37 additions & 36 deletions src/quran/audio.ts
Original file line number Diff line number Diff line change
@@ -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<AxiosResponse | Error | AxiosError> {
getChaptersAudioOfAReciter(id: number, chapter_number: number): Promise<IAudio | Error | AxiosError> {
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<AxiosResponse | Error | AxiosError> {
getAllChaptersAudioOfAReciter(id: number): Promise<IListOfAllAudioOfAReciter | Error | AxiosError> {
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<AxiosResponse | Error | AxiosError> {
getRecitations(language: string = 'en'): Promise<IRecitation | Error | AxiosError> {
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<AxiosResponse | Error | AxiosError> {
getAllAudioFilesofARecitation(recitation_id: number, query?: AudioQueryParams): Promise<ISingleRecitation | Error | AxiosError> {
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<AxiosResponse | Error | AxiosError> {
getListOfChapterReciters(language: string = 'en'): Promise<IReciters | Error | AxiosError> {
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<AxiosResponse | Error | AxiosError> {
getAyahRecitationsForSpecificSurah(recitation_id: number, chapter_number: number): Promise<IAyahRecitationSpecificSurah | Error | AxiosError> {
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<AxiosResponse | Error | AxiosError> {
getAyahRecitationsForSpecificJuz(recitation_id: number, juz_number: number): Promise<IAyahRecitationSpecificJuz | Error | AxiosError> {
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<AxiosResponse | Error | AxiosError> {
getAyahRecitationForSpecificMadaniMushafPage(recitation_id: number, page_number: number): Promise<IAyahRecitationSpecificMadaniMushafPage | Error | AxiosError> {
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<AxiosResponse | Error | AxiosError> {
getAyahRecitationForSpecificRubelHizb(recitation_id: number, rub_el_hizb_number: number): Promise<IAyahRecitationSpecificRubelHizb | Error | AxiosError> {
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<AxiosResponse | Error | AxiosError> {
getAyahRecitationForSpecificHizb(recitation_id: number, hizb_number: number): Promise<IAyahRecitationSpecificHizb | Error | AxiosError> {
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<AxiosResponse | Error | AxiosError> {
getAyahRecitationForSpecificAyah(recitation_id: number, ayah_key: string): Promise<IAyahRecitationSpecificAyah | Error | AxiosError> {
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));
});
}

Expand Down
109 changes: 109 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AxiosError } from "axios";

export interface CustomHeaders {
[key: string]: string;
};
Expand All @@ -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<IAudio | Error | AxiosError>;
getAllChaptersAudioOfAReciter(id: number): Promise<IListOfAllAudioOfAReciter | Error | AxiosError>;
getRecitations(language: string): Promise<IRecitation | Error | AxiosError>;
getAllAudioFilesofARecitation(recitation_id: number, query?: AudioQueryParams): Promise<ISingleRecitation | Error | AxiosError>;
getListOfChapterReciters(language: string): Promise<IReciters | Error | AxiosError>;
getAyahRecitationsForSpecificSurah(recitation_id: number, chapter_number: number): Promise<IAyahRecitationSpecificSurah | Error | AxiosError>;
getAyahRecitationsForSpecificJuz(recitation_id: number, juz_number: number): Promise<IAyahRecitationSpecificJuz | Error | AxiosError>;
getAyahRecitationForSpecificMadaniMushafPage(recitation_id: number, page_number: number): Promise<IAyahRecitationSpecificMadaniMushafPage | Error | AxiosError>;
getAyahRecitationForSpecificRubelHizb(recitation_id: number, rub_el_hizb_number: number): Promise<IAyahRecitationSpecificRubelHizb | Error | AxiosError>;
getAyahRecitationForSpecificHizb(recitation_id: number, hizb_number: number): Promise<IAyahRecitationSpecificHizb | Error | AxiosError>;
getAyahRecitationForSpecificAyah(recitation_id: number, ayah_key: string): Promise<IAyahRecitationSpecificAyah | Error | AxiosError>;
};
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AxiosError, AxiosResponse } from 'axios';

export const handleResponse = <T>(
resolve: (value: T) => void) => (response: AxiosResponse<T>) =>
resolve(response.data);

export const handleError = (reject: (reason: Error | AxiosError) => void) =>
(error: AxiosError) =>
reject(error);

0 comments on commit 03e3bbf

Please sign in to comment.