diff --git a/README.md b/README.md index 65cb01c..1509321 100644 --- a/README.md +++ b/README.md @@ -35,31 +35,31 @@ pnpm install al-quran-sdk Provides methods to fetch verses based on various criteria. -#### `getVerseByChapter(chapter_number: string, query?: VerseQuery): Promise` +#### `getVerseByChapter(chapter_number: string, query?: VerseQuery): Promise` Fetches verses by chapter number. -#### `getVerseByPage(page_number: string, query?: VerseQuery): Promise` +#### `getVerseByPage(page_number: string, query?: VerseQuery): Promise` Fetches verses by page number. -#### `getVerseByJuz(juz_number: string, query?: VerseQuery): Promise` +#### `getVerseByJuz(juz_number: string, query?: VerseQuery): Promise` Fetches verses by Juz number. -#### `getVerseByHizbNumber(hizb_number: string, query?: VerseQuery): Promise` +#### `getVerseByHizbNumber(hizb_number: string, query?: VerseQuery): Promise` Fetches verses by Hizb number. -#### `getVerseByRubElHizbNumber(rub_el_hizb_number: string, query?: VerseQuery): Promise` +#### `getVerseByRubElHizbNumber(rub_el_hizb_number: string, query?: VerseQuery): Promise` Fetches verses by Rub el Hizb number. -#### `getSpecificVerseByVerseKey(verse_key: string, query?: VerseQuery): Promise` +#### `getSpecificVerseByVerseKey(verse_key: string, query?: VerseQuery): Promise` Fetches a specific verse by its key. -#### `getRandomAyah(query?: VerseQuery): Promise` +#### `getRandomAyah(query?: VerseQuery): Promise` Fetches a random verse. @@ -67,39 +67,39 @@ Fetches a random verse. Provides methods to fetch resource information. -#### `getRecitationInfo(recitation_id: string): Promise` +#### `getRecitationInfo(recitation_id: string): Promise` Fetches information about a specific recitation. -#### `getTranslationInfo(translation_id: string): Promise` +#### `getTranslationInfo(translation_id: string): Promise` Fetches information about a specific translation. -#### `getTranslations(language: string = 'en'): Promise` +#### `getTranslations(language: string = 'en'): Promise` Fetches translations based on language. -#### `getTafsirs(language: string = 'en'): Promise` +#### `getTafsirs(language: string = 'en'): Promise` Fetches Tafsirs based on language. -#### `getTafsirInfo(tafsir_id: string): Promise` +#### `getTafsirInfo(tafsir_id: string): Promise` Fetches information about a specific Tafsir. -#### `getRecitationStyles(): Promise` +#### `getRecitationStyles(): Promise` Fetches available recitation styles. -#### `getLanguages(): Promise` +#### `getLanguages(): Promise` Fetches available languages. -#### `getChapterInfos(): Promise` +#### `getChapterInfos(): Promise` Fetches information about chapters. -#### `getVerseMedias(): Promise` +#### `getVerseMedias(): Promise` Fetches media associated with verses. @@ -107,39 +107,39 @@ Fetches media associated with verses. Provides methods to fetch Quranic text in various scripts. -#### `getIndoPakScriptOfAyah(query?: QuranQuery): Promise` +#### `getIndoPakScriptOfAyah(query?: QuranQuery): Promise` Fetches the Indo-Pak script of an Ayah. -#### `getUthmaniTajweedScriptOfAyah(query?: QuranQuery): Promise` +#### `getUthmaniTajweedScriptOfAyah(query?: QuranQuery): Promise` Fetches the Uthmani Tajweed script of an Ayah. -#### `getUthmaniScriptOfAyah(query?: QuranQuery): Promise` +#### `getUthmaniScriptOfAyah(query?: QuranQuery): Promise` Fetches the Uthmani script of an Ayah. -#### `getUthmaniSimpleScriptOfAyah(query?: QuranQuery): Promise` +#### `getUthmaniSimpleScriptOfAyah(query?: QuranQuery): Promise` Fetches the Uthmani simple script of an Ayah. -#### `getImlaeiSimpleTextOfAyah(query?: QuranQuery): Promise` +#### `getImlaeiSimpleTextOfAyah(query?: QuranQuery): Promise` Fetches the Imlaei simple text of an Ayah. -#### `getASingleTranslation(translation_id: string, query?: TranslationQuery): Promise` +#### `getASingleTranslation(translation_id: string, query?: TranslationQuery): Promise` Fetches a single translation. -#### `getSingleTafsir(tafsir_id: string, query?: TranslationQuery): Promise` +#### `getSingleTafsir(tafsir_id: string, query?: TranslationQuery): Promise` Fetches a single Tafsir. -#### `getGlyphCodesOfAyahV1(query?: QuranQuery): Promise` +#### `getGlyphCodesOfAyahV1(query?: QuranQuery): Promise` Fetches glyph codes of an Ayah (version 1). -#### `getGlyphCodesOfAyahV2(query?: QuranQuery): Promise` +#### `getGlyphCodesOfAyahV2(query?: QuranQuery): Promise` Fetches glyph codes of an Ayah (version 2). @@ -147,7 +147,7 @@ Fetches glyph codes of an Ayah (version 2). Provides methods to fetch information about Juz. -#### `getAllJuzs(): Promise` +#### `getAllJuzs(): Promise` Fetches all Juz. @@ -155,15 +155,15 @@ Fetches all Juz. Provides methods to fetch chapter information. -#### `listChapters(language = 'en'): Promise` +#### `listChapters(language = 'en'): Promise` Fetches a list of chapters based on language. -#### `getChapter(id: number, language = 'en'): Promise` +#### `getChapter(id: number, language = 'en'): Promise` Fetches information about a specific chapter. -#### `getChapterInfo(chapter_id: number, language = 'en'): Promise` +#### `getChapterInfo(chapter_id: number, language = 'en'): Promise` Fetches detailed information about a specific chapter. @@ -171,47 +171,47 @@ Fetches detailed information about a specific chapter. Provides methods to fetch audio recitations and related data. -#### `getChaptersAudioOfAReciter(id: number, chapter_number: number): Promise` +#### `getChaptersAudioOfAReciter(id: number, chapter_number: number): Promise` Fetches audio recordings of a specific chapter by a particular reciter. -#### `getAllChaptersAudioOfAReciter(id: number): Promise` +#### `getAllChaptersAudioOfAReciter(id: number): Promise` Fetches all chapter recordings by a specific reciter. -#### `getRecitations(language: string = 'en'): Promise` +#### `getRecitations(language: string = 'en'): Promise` Fetches recitations based on language. -#### `getAllAudioFilesofARecitation(recitation_id: number, query?: AudioQueryParams): Promise` +#### `getAllAudioFilesofARecitation(recitation_id: number, query?: AudioQueryParams): Promise` Fetches all audio files of a specific recitation. -#### `getListOfChapterReciters(language: string = 'en'): Promise` +#### `getListOfChapterReciters(language: string = 'en'): Promise` Fetches a list of chapter reciters based on language. -#### `getAyahRecitationsForSpecificSurah(recitation_id: number, chapter_number: number): Promise` +#### `getAyahRecitationsForSpecificSurah(recitation_id: number, chapter_number: number): Promise` Fetches Ayah recitations for a specific Surah. -#### `getAyahRecitationsForSpecificJuz(recitation_id: number, juz_number: number): Promise` +#### `getAyahRecitationsForSpecificJuz(recitation_id: number, juz_number: number): Promise` Fetches Ayah recitations for a specific Juz. -#### `getAyahRecitationForSpecificMadaniMushafPage(recitation_id: number, page_number: number): Promise` +#### `getAyahRecitationForSpecificMadaniMushafPage(recitation_id: number, page_number: number): Promise` Fetches Ayah recitations for a specific Madani Mushaf page. -#### `getAyahRecitationForSpecificRubelHizb(recitation_id: number, rub_el_hizb_number: number): Promise` +#### `getAyahRecitationForSpecificRubelHizb(recitation_id: number, rub_el_hizb_number: number): Promise` Fetches Ayah recitations for a specific Rub el Hizb. -#### `getAyahRecitationForSpecificHizb(recitation_id: number, hizb_number: number): Promise` +#### `getAyahRecitationForSpecificHizb(recitation_id: number, hizb_number: number): Promise` Fetches Ayah recitations for a specific Hizb. -#### `getAyahRecitationForSpecificAyah(recitation_id: number, ayah_key: string): Promise` +#### `getAyahRecitationForSpecificAyah(recitation_id: number, ayah_key: string): Promise` Fetches Ayah recitations for a specific Ayah. diff --git a/package.json b/package.json index 998e85b..140315b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "al-quran-sdk", - "version": "1.0.3", + "version": "1.0.4", "description": "An API wrapper for Quran on top of quran.com", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/src/apis/audio.ts b/src/apis/audio.ts index 5e85801..d3ad106 100644 --- a/src/apis/audio.ts +++ b/src/apis/audio.ts @@ -1,16 +1,19 @@ -import { AxiosError } from "axios"; import url from "url"; -import Api from "../req"; +import { AudioError, LanguageValidationError } from "../errors"; import { + ALLOWED_LANGUAGES, AudioApi, AudioQueryParams, - IAudio, IListOfAllAudioOfAReciter, IRecitation, - ISingleRecitation, IReciters, IAyahRecitationSpecificSurah, IAyahRecitationSpecificJuz, + IAudio, + IAyahRecitationSpecificAyah, + IAyahRecitationSpecificHizb, + IAyahRecitationSpecificJuz, IAyahRecitationSpecificMadaniMushafPage, IAyahRecitationSpecificRubelHizb, - IAyahRecitationSpecificHizb, IAyahRecitationSpecificAyah, ALLOWED_LANGUAGES -} from "../types"; -import { handleError, handleResponse } from "../utils"; - -const api = Api(); + IAyahRecitationSpecificSurah, + IListOfAllAudioOfAReciter, IRecitation, + IReciters, + ISingleRecitation +} from "../interfaces"; +import { apiWraper } from "../utils"; export const audio: AudioApi = { @@ -23,15 +26,10 @@ export const audio: AudioApi = { * @returns A promise that resolves to the audio recordings or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/chapter-reciter-audio-file} */ - getChaptersAudioOfAReciter(id: number, chapter_number: number): Promise { + async 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(handleResponse(resolve)) - .catch(handleError(reject)); - }); + throw new AudioError('Reciter\'s ID and Chapter number is required'); + return await apiWraper(`/chapter_recitations/${id}/${chapter_number}`); }, /** @@ -41,15 +39,10 @@ export const audio: AudioApi = { * @returns A promise that resolves to the list of audio recordings or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/chapter-reciter-audio-files} */ - getAllChaptersAudioOfAReciter(id: number): Promise { + async 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(handleResponse(resolve)) - .catch(handleError(reject)); - }); + throw new AudioError('Reciter\'s ID is required'); + return await apiWraper(`/chapter_recitations/${id}`); }, /** @@ -59,14 +52,10 @@ export const audio: AudioApi = { * @returns A promise that resolves to the list of recitations or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/recitations} */ - getRecitations(language: string = 'en'): Promise { + async 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(handleResponse(resolve)) - .catch(handleError(reject)); - }); + if (!isLanguageSupported) throw new LanguageValidationError("Provided language is not supported"); + return await apiWraper(`/resources/languages?${new url.URLSearchParams({ language })}`); }, @@ -78,16 +67,12 @@ export const audio: AudioApi = { * @returns A promise that resolves to the list of audio files or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/recitation-autio-files} */ - getAllAudioFilesofARecitation(recitation_id: number, query?: AudioQueryParams): Promise { - if (!recitation_id) return Promise.reject(new Error('recitation_id is required')); + async getAllAudioFilesofARecitation(recitation_id: number, query?: AudioQueryParams): Promise { + if (!recitation_id) throw new AudioError('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(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, @@ -99,14 +84,10 @@ export const audio: AudioApi = { * @returns A promise that resolves to the list of chapter reciters or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/chapter-reciters} */ - getListOfChapterReciters(language: string = 'en'): Promise { + async 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(handleResponse(resolve)) - .catch(handleError(reject)); - }); + if (!isLanguageSupported) throw new LanguageValidationError("Provided language is not supported"); + return await apiWraper(`/resources/chapter_reciters?${new URLSearchParams({ language })}`); }, @@ -118,14 +99,10 @@ export const audio: AudioApi = { * @returns A promise that resolves to the audio recitations or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/list-surah-recitation} */ - getAyahRecitationsForSpecificSurah(recitation_id: number, chapter_number: number): Promise { + async 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(handleResponse(resolve)) - .catch(handleError(reject)); - }); + throw new AudioError('recitation_id and Chapter number is required'); + return await apiWraper(`/recitations/${recitation_id}/by_chapter/${chapter_number}`); }, @@ -137,14 +114,10 @@ export const audio: AudioApi = { * @returns A promise that resolves to the audio recitations or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/list-juz-recitaiton} */ - getAyahRecitationsForSpecificJuz(recitation_id: number, juz_number: number): Promise { + async 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(handleResponse(resolve)) - .catch(handleError(reject)); - }); + throw new AudioError('recitation_id and juz_number is required'); + return await apiWraper(`/recitations/${recitation_id}/by_juz/${juz_number}`) }, @@ -156,14 +129,10 @@ export const audio: AudioApi = { * @returns A promise that resolves to the audio recitations or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/list-page-recitaiton} */ - getAyahRecitationForSpecificMadaniMushafPage(recitation_id: number, page_number: number): Promise { + async 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(handleResponse(resolve)) - .catch(handleError(reject)); - }); + throw new AudioError('recitation_id and Page number is required'); + return await apiWraper(`/recitations/${recitation_id}/by_page/${page_number}`); }, @@ -175,14 +144,10 @@ export const audio: AudioApi = { * @returns A promise that resolves to the audio recitations or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/list-rub-el-hizb-recitaiton} */ - getAyahRecitationForSpecificRubelHizb(recitation_id: number, rub_el_hizb_number: number): Promise { + async 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(handleResponse(resolve)) - .catch(handleError(reject)); - }); + throw new AudioError('recitation_id and rub_el_hizb_number is required'); + return await apiWraper(`/recitations/${recitation_id}/by_rub/${rub_el_hizb_number}`); }, @@ -194,14 +159,10 @@ export const audio: AudioApi = { * @returns A promise that resolves to the audio recitations or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/list-hizb-recitaiton} */ - getAyahRecitationForSpecificHizb(recitation_id: number, hizb_number: number): Promise { + async 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(handleResponse(resolve)) - .catch(handleError(reject)); - }); + throw new AudioError('recitation_id and hizb_number is required'); + return await apiWraper(`/recitations/${recitation_id}/by_hizb/${hizb_number}`); }, @@ -213,14 +174,10 @@ export const audio: AudioApi = { * @returns A promise that resolves to the audio recitations or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/list-ayah-recitaiton} */ - getAyahRecitationForSpecificAyah(recitation_id: number, ayah_key: string): Promise { + async 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(handleResponse(resolve)) - .catch(handleError(reject)); - }); + throw new AudioError('recitation_id and ayah_key is required'); + return await apiWraper(`/recitations/${recitation_id}/by_ayah/${ayah_key}`); } }; diff --git a/src/apis/chapter.test.ts b/src/apis/chapter.test.ts index bc6e545..b197318 100644 --- a/src/apis/chapter.test.ts +++ b/src/apis/chapter.test.ts @@ -1,4 +1,4 @@ -import { ListChapters } from "../types"; +import { ListChapters } from "../interfaces"; import { chapter } from "./chapter"; describe("Fetch all the chapter according to language", () => { diff --git a/src/apis/chapter.ts b/src/apis/chapter.ts index 4988b79..9539d5d 100644 --- a/src/apis/chapter.ts +++ b/src/apis/chapter.ts @@ -1,9 +1,6 @@ -import { AxiosError } from "axios"; -import Api from "../req"; -import { ChapterApi, ListChapters, Chapter, ChapterInfo, ALLOWED_LANGUAGES } from "../types"; -import { handleError, handleResponse } from "../utils"; - -const api = Api(); +import { LanguageValidationError } from "../errors"; +import { ALLOWED_LANGUAGES, Chapter, ChapterApi, ChapterInfo, ListChapters } from "../interfaces"; +import { apiWraper } from "../utils"; export const chapter: ChapterApi = { @@ -15,14 +12,10 @@ export const chapter: ChapterApi = { * @returns A promise that resolves to the list of chapters or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/list-chapters} */ - listChapters(language = 'en'): Promise { + async listChapters(language = 'en'): Promise { const isLanguageSupported = language && ALLOWED_LANGUAGES.has(language); - if (!isLanguageSupported) return Promise.reject(new Error("Provided language is not supported")); - return new Promise((resolve, reject) => { - api.get(`/chapters?${new URLSearchParams({ language })}`) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + if (!isLanguageSupported) throw new LanguageValidationError("Provided language is not supported"); + return await apiWraper(`/chapters?${new URLSearchParams({ language })}`); }, /** @@ -33,13 +26,9 @@ export const chapter: ChapterApi = { * @returns A promise that resolves to the chapter details or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/get-chapter} */ - getChapter(id: number, language = 'en'): Promise { - if (language && !ALLOWED_LANGUAGES.has(language)) return Promise.reject(new Error("Provided language is not supported")); - return new Promise((resolve, reject) => { - api.get(`/chapters/${id}?${new URLSearchParams({ language })}`) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + async getChapter(id: number, language = 'en'): Promise { + if (language && !ALLOWED_LANGUAGES.has(language)) throw new LanguageValidationError("Provided language is not supported"); + return await apiWraper(`/chapters/${id}?${new URLSearchParams({ language })}`); }, /** @@ -50,13 +39,9 @@ export const chapter: ChapterApi = { * @returns A promise that resolves to the chapter information or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/info} */ - getChapterInfo(chapter_id: number, language = 'en'): Promise { - if (language && !ALLOWED_LANGUAGES.has(language)) return Promise.reject(new Error("Provided language is not supported")); - return new Promise((resolve, reject) => { - api.get(`/chapters/${chapter_id}/info?${new URLSearchParams({ language })}`) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + async getChapterInfo(chapter_id: number, language = 'en'): Promise { + if (language && !ALLOWED_LANGUAGES.has(language)) throw new LanguageValidationError("Provided language is not supported"); + return await apiWraper(`/chapters/${chapter_id}/info?${new URLSearchParams({ language })}`); } }; diff --git a/src/apis/juz.ts b/src/apis/juz.ts index 2c65389..52f7ee8 100644 --- a/src/apis/juz.ts +++ b/src/apis/juz.ts @@ -1,9 +1,6 @@ -import { AxiosError } from "axios"; -import Api from "../req"; -import { JuzApi, JuzResponse } from "../types"; -import { handleError, handleResponse } from "../utils"; +import { JuzApi, JuzResponse } from "../interfaces"; +import { apiWraper, handleError, handleResponse } from "../utils"; -const api = Api(); export const juz: JuzApi = { @@ -14,11 +11,7 @@ export const juz: JuzApi = { * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/juzs} */ - getAllJuzs(): Promise { - return new Promise((resolve, reject) => { - api.get('/juzs') - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + async getAllJuzs(): Promise { + return await apiWraper('/juzs'); }, }; diff --git a/src/apis/quran.ts b/src/apis/quran.ts index 0179f34..959264f 100644 --- a/src/apis/quran.ts +++ b/src/apis/quran.ts @@ -1,18 +1,17 @@ -import { AxiosError } from "axios"; -import Api from "../req"; import { - QuranQuery, QuranResponse, UthmaniTajweedResponse, - UthmaniScriptResponse, - UthmaniSimpleScriptResponse, ImlaeiSimpleTextResponse, - TranslationQuery, SingleTranslationResponse, - SingleTafsirResponse, GlyphCodesOfAyahV1Response, GlyphCodesOfAyahV2Response, - QuranApi -} from "../types"; -import { handleError, handleResponse } from "../utils"; - -const api = Api(); + ImlaeiSimpleTextResponse, + QuranApi, + QuranQuery, QuranResponse, + SingleTafsirResponse, + SingleTranslationResponse, + TranslationQuery, + UthmaniScriptResponse, + UthmaniSimpleScriptResponse, + UthmaniTajweedResponse +} from "../interfaces"; +import { apiWraper } from "../utils"; export const quran: QuranApi = { @@ -23,13 +22,9 @@ export const quran: QuranApi = { * @returns A promise that resolves to the Indo-Pak script response or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/quran-verses-indopak} */ - getIndoPakScriptOfAyah(query?: QuranQuery): Promise { + async getIndoPakScriptOfAyah(query?: QuranQuery): Promise { const uri = query ? `/quran/verses/code_v1?${new URLSearchParams(query as URLSearchParams)}` : '/quran/verses/code_v1'; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return apiWraper(uri); }, /** @@ -39,13 +34,9 @@ export const quran: QuranApi = { * @returns A promise that resolves to the Uthmani Tajweed script response or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/quran-verses-uthmani-tajweed} */ - getUthmaniTajweedScriptOfAyah(query?: QuranQuery): Promise { + async getUthmaniTajweedScriptOfAyah(query?: QuranQuery): Promise { const uri = query ? `/quran/verses/uthmani_tajweed?${new URLSearchParams(query as URLSearchParams)}` : '/quran/verses/uthmani_tajweed'; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, @@ -56,13 +47,9 @@ export const quran: QuranApi = { * @returns A promise that resolves to the Uthmani script response or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/quran-verses-uthmani} */ - getUthmaniScriptOfAyah(query?: QuranQuery): Promise { + async getUthmaniScriptOfAyah(query?: QuranQuery): Promise { const uri = query ? `/quran/verses/uthmani?${new URLSearchParams(query as URLSearchParams)}` : '/quran/verses/uthmani'; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, /** @@ -72,13 +59,9 @@ export const quran: QuranApi = { * @returns A promise that resolves to the Uthmani simple script response or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/quran-verses-uthmani-simple} */ - getUthmaniSimpleScriptOfAyah(query?: QuranQuery): Promise { + async getUthmaniSimpleScriptOfAyah(query?: QuranQuery): Promise { const uri = query ? `/quran/verses/uthmani_simple?${new URLSearchParams(query as URLSearchParams)}` : '/quran/verses/uthmani_simple'; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, /** @@ -88,13 +71,9 @@ export const quran: QuranApi = { * @returns A promise that resolves to the Imlaei simple text response or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/quran-verses-imlaei} */ - getImlaeiSimpleTextOfAyah(query?: QuranQuery): Promise { + async getImlaeiSimpleTextOfAyah(query?: QuranQuery): Promise { const uri = query ? `/quran/verses/imlaei?${new URLSearchParams(query as URLSearchParams)}` : '/quran/verses/imlaei'; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, /** @@ -105,13 +84,9 @@ export const quran: QuranApi = { * @returns A promise that resolves to the single translation response or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/translation} */ - getASingleTranslation(translation_id: string, query?: TranslationQuery): Promise { + async getASingleTranslation(translation_id: string, query?: TranslationQuery): Promise { const uri = query ? `/quran/translations/${translation_id}?${new URLSearchParams(query as URLSearchParams)}` : `/quran/translations/${translation_id}`; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, /** @@ -122,13 +97,9 @@ export const quran: QuranApi = { * @returns A promise that resolves to the single tafsir response or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/tafsir} */ - getSingleTafsir(tafsir_id: string, query?: TranslationQuery): Promise { + async getSingleTafsir(tafsir_id: string, query?: TranslationQuery): Promise { const uri = query ? `/quran/tafsirs/${tafsir_id}?${new URLSearchParams(query as URLSearchParams)}` : `/quran/tafsirs/${tafsir_id}`; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, /** @@ -139,13 +110,9 @@ export const quran: QuranApi = { * @returns A promise that resolves to the single tafsir response or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/quran-verses-code-v-1} */ - getGlyphCodesOfAyahV1(query?: QuranQuery): Promise { + async getGlyphCodesOfAyahV1(query?: QuranQuery): Promise { const uri = query ? `/quran/verses/code_v1?${new URLSearchParams(query as URLSearchParams)}` : '/quran/verses/code_v1'; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, /** @@ -155,12 +122,8 @@ export const quran: QuranApi = { * @returns A promise that resolves to the glyph codes response (version 2) or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/quran-verses-code-v-2} */ - getGlyphCodesOfAyahV2(query?: QuranQuery): Promise { + async getGlyphCodesOfAyahV2(query?: QuranQuery): Promise { const uri = query ? `/quran/verses/code_v2?${new URLSearchParams(query as URLSearchParams)}` : '/quran/verses/code_v2'; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri) }, }; diff --git a/src/apis/resource.ts b/src/apis/resource.ts index 3b1d55e..1ce8761 100644 --- a/src/apis/resource.ts +++ b/src/apis/resource.ts @@ -1,14 +1,17 @@ -import { AxiosError } from "axios"; -import Api from "../req"; +import { LanguageValidationError, ResourceError } from "../errors"; import { - ResourceApi, RecitaionInfo, - TranslationInfo, TranslationResponse, TafsirsResponse, - TafsirInfoResponse, RecitationStyleResponse, LanguageResponse, - ChapterInfos, VerseMediaResponse, ALLOWED_LANGUAGES -} from "../types"; -import { handleError, handleResponse } from "../utils"; - -const api = Api(); + ALLOWED_LANGUAGES, + ChapterInfos, + LanguageResponse, + RecitaionInfo, + RecitationStyleResponse, + ResourceApi, + TafsirInfoResponse, + TafsirsResponse, + TranslationInfo, TranslationResponse, + VerseMediaResponse +} from "../interfaces"; +import { apiWraper } from "../utils"; export const resources: ResourceApi = { @@ -20,13 +23,9 @@ export const resources: ResourceApi = { * @returns A promise that resolves to the recitation information or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/recitation-info} */ - getRecitationInfo(recitation_id: string): Promise { - if (!recitation_id) return Promise.reject(new Error('Recitation ID is required')); - return new Promise((resolve, reject) => { - api.get(`/resources/recitations/${recitation_id}/info`) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + async getRecitationInfo(recitation_id: string): Promise { + if (!recitation_id) throw new ResourceError('Recitation ID is required'); + return await apiWraper(`/resources/recitations/${recitation_id}/info`); }, /** @@ -36,13 +35,9 @@ export const resources: ResourceApi = { * @returns A promise that resolves to the translation information or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/translation-info} */ - getTranslationInfo(translation_id: string): Promise { - if (!translation_id) return Promise.reject(new Error('Translation ID is required')); - return new Promise((resolve, reject) => { - api.get(`/resources/translations/${translation_id}/info`) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + async getTranslationInfo(translation_id: string): Promise { + if (!translation_id) throw new ResourceError('Translation ID is required'); + return await apiWraper(`/resources/translations/${translation_id}/info`); }, /** @@ -52,14 +47,10 @@ export const resources: ResourceApi = { * @returns A promise that resolves to the translation response or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/translations} */ - getTranslations(language: string = 'en'): Promise { + async getTranslations(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/translations?${new URLSearchParams({ language })}`) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + if (!isLanguageSupported) throw new LanguageValidationError("Provided language is not supported"); + return await apiWraper(`/resources/translations?${new URLSearchParams({ language })}`); }, /** @@ -69,14 +60,10 @@ export const resources: ResourceApi = { * @returns A promise that resolves to the tafsirs response or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/tafsirs} */ - getTafsirs(language: string = 'en'): Promise { + async getTafsirs(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/tafsirs?${new URLSearchParams({ language })}`) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + if (!isLanguageSupported) throw new LanguageValidationError("Provided language is not supported"); + return await apiWraper(`/resources/tafsirs?${new URLSearchParams({ language })}`); }, /** @@ -86,13 +73,9 @@ export const resources: ResourceApi = { * @returns A promise that resolves to the tafsir information or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/tafsir-info} */ - getTafsirInfo(tafsir_id: string): Promise { - if (!tafsir_id) return Promise.reject(new Error('Tafsir ID is required')); - return new Promise((resolve, reject) => { - api.get(`/resources/tafsirs/${tafsir_id}/info`) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + async getTafsirInfo(tafsir_id: string): Promise { + if (!tafsir_id) throw new ResourceError('Tafsir ID is required'); + return await apiWraper(`/resources/tafsirs/${tafsir_id}/info`); }, /** @@ -101,12 +84,8 @@ export const resources: ResourceApi = { * @returns A promise that resolves to the recitation style response or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/recitation-styles} */ - getRecitationStyles(): Promise { - return new Promise((resolve, reject) => { - api.get('/resources/recitation_styles') - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + async getRecitationStyles(): Promise { + return await apiWraper('/resources/recitation_styles'); }, @@ -116,12 +95,8 @@ export const resources: ResourceApi = { * @returns A promise that resolves to the language response or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/languages} */ - getLanguages(): Promise { - return new Promise((resolve, reject) => { - api.get('/resources/languages') - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + async getLanguages(): Promise { + return await apiWraper('/resources/languages'); }, /** @@ -130,12 +105,8 @@ export const resources: ResourceApi = { * @returns A promise that resolves to the chapter information or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/chapter-info} */ - getChapterInfos(): Promise { - return new Promise((resolve, reject) => { - api.get(`/resources/chapter_infos`) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + async getChapterInfos(): Promise { + return await apiWraper(`/resources/chapter_infos`); }, /** @@ -144,11 +115,7 @@ export const resources: ResourceApi = { * @returns A promise that resolves to the verse media response or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/verse-media} */ - getVerseMedias(): Promise { - return new Promise((resolve, reject) => { - api.get(`/resources/verse_media`) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + async getVerseMedias(): Promise { + return apiWraper(`/resources/verse_media`); } }; diff --git a/src/apis/verse.ts b/src/apis/verse.ts index 1b60a07..19e5ec8 100644 --- a/src/apis/verse.ts +++ b/src/apis/verse.ts @@ -1,9 +1,7 @@ -import { AxiosError } from "axios"; -import Api from "../req"; -import { ALLOWED_LANGUAGES, VerseApi, VerseQuery, VerseResponse } from "../types"; -import { handleError, handleResponse } from "../utils"; +import { LanguageValidationError } from "../errors"; +import { ALLOWED_LANGUAGES, VerseApi, VerseQuery, VerseResponse } from "../interfaces"; +import { apiWraper, handleError, handleResponse } from "../utils"; -const api = Api(); export const verse: VerseApi = { @@ -15,14 +13,10 @@ export const verse: VerseApi = { * @returns A promise that resolves to the response containing the verses or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/verses-by-chapter-number} */ - getVerseByChapter(chapter_number: string, query?: VerseQuery): Promise { - if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new Error('Provided query language is not allowed'); + async getVerseByChapter(chapter_number: string, query?: VerseQuery): Promise { + if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new LanguageValidationError('Provided query language is not allowed'); const uri = query ? `/verses/by_chapter/${chapter_number}?${new URLSearchParams(query as URLSearchParams)}` : `/verses/by_chapter/${chapter_number}`; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, /** @@ -33,14 +27,10 @@ export const verse: VerseApi = { * @returns A promise that resolves to the response containing the verses or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/verses-by-page-number} */ - getVerseByPage(page_number: string, query?: VerseQuery): Promise { - if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new Error('Provided query language is not allowed'); + async getVerseByPage(page_number: string, query?: VerseQuery): Promise { + if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new LanguageValidationError('Provided query language is not allowed'); const uri = query ? `/verses/by_page/${page_number}?${new URLSearchParams(query as URLSearchParams)}` : `/verses/by_page/${page_number}`; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, /** @@ -51,14 +41,10 @@ export const verse: VerseApi = { * @returns A promise that resolves to the response containing the verses or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/verses-by-juz-number} */ - getVerseByJuz(juz_number: string, query?: VerseQuery): Promise { - if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new Error('Provided query language is not allowed'); + async getVerseByJuz(juz_number: string, query?: VerseQuery): Promise { + if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new LanguageValidationError('Provided query language is not allowed'); const uri = query ? `/verses/by_juz/${juz_number}?${new URLSearchParams(query as URLSearchParams)}` : `/verses/by_juz/${juz_number}`; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, /** @@ -69,14 +55,10 @@ export const verse: VerseApi = { * @returns A promise that resolves to the response containing the verses or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/verses-by-hizb-number} */ - getVerseByHizbNumber(hizb_number: string, query?: VerseQuery): Promise { - if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new Error('Provided query language is not allowed'); + async getVerseByHizbNumber(hizb_number: string, query?: VerseQuery): Promise { + if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new LanguageValidationError('Provided query language is not allowed'); const uri = query ? `/verses/by_hizb/${hizb_number}?${new URLSearchParams(query as URLSearchParams)}` : `/verses/by_hizb/${hizb_number}`; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, /** @@ -87,14 +69,10 @@ export const verse: VerseApi = { * @returns A promise that resolves to the response containing the verses or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/verses-by-rub-el-hizb-number} */ - getVerseByRubElHizbNumber(rub_el_hizb_number: string, query?: VerseQuery): Promise { - if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new Error('Provided query language is not allowed'); + async getVerseByRubElHizbNumber(rub_el_hizb_number: string, query?: VerseQuery): Promise { + if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new LanguageValidationError('Provided query language is not allowed'); const uri = query ? `/verses/by_rub/${rub_el_hizb_number}?${new URLSearchParams(query as URLSearchParams)}` : `/verses/by_rub/${rub_el_hizb_number}`; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, /** @@ -105,14 +83,10 @@ export const verse: VerseApi = { * @returns A promise that resolves to the response containing the verse or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/verses-by-verse-key} */ - getSpecificVerseByVerseKey(verse_key: string, query?: VerseQuery): Promise { - if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new Error('Provided query language is not allowed'); + async getSpecificVerseByVerseKey(verse_key: string, query?: VerseQuery): Promise { + if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new LanguageValidationError('Provided query language is not allowed'); const uri = query ? `/verses/by_key/${verse_key}?${new URLSearchParams(query as URLSearchParams)}` : `/verses/by_key/${verse_key}`; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, /** @@ -122,13 +96,9 @@ export const verse: VerseApi = { * @returns A promise that resolves to the response containing a random verse or rejects with an error. * @see {@link https://api-docs.quran.com/docs/quran.com_versioned/random-verse} */ - getRandomAyah(query?: VerseQuery): Promise { - if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new Error('Provided query language is not allowed'); + async getRandomAyah(query?: VerseQuery): Promise { + if (query?.language && !ALLOWED_LANGUAGES.has(query.language)) throw new LanguageValidationError('Provided query language is not allowed'); const uri = query ? `/verses/random?${new URLSearchParams(query as URLSearchParams)}` : `/verses/random`; - return new Promise((resolve, reject) => { - api.get(uri) - .then(handleResponse(resolve)) - .catch(handleError(reject)); - }); + return await apiWraper(uri); }, }; diff --git a/src/customError.ts b/src/customError.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/errors.ts b/src/errors.ts new file mode 100644 index 0000000..efcfc20 --- /dev/null +++ b/src/errors.ts @@ -0,0 +1,29 @@ +export class LanguageValidationError extends Error { + constructor(message: string) { + super(message); + } +}; + +export class AudioError extends Error { + constructor(message: string) { + super(message); + } +}; + +export class ChapterError extends Error { + constructor(message: string) { + super(message); + } +}; + +export class ResourceError extends Error { + constructor(message: string) { + super(message); + } +}; + +export class VerseError extends Error { + constructor(message: string) { + super(message); + } +}; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 56f9b33..a41b05a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ export * from './apis'; -export * from './types'; \ No newline at end of file +export * from './interfaces'; \ No newline at end of file diff --git a/src/types.ts b/src/interfaces.ts similarity index 82% rename from src/types.ts rename to src/interfaces.ts index 4055bc7..a1dc772 100644 --- a/src/types.ts +++ b/src/interfaces.ts @@ -116,17 +116,17 @@ export interface IAyahRecitationSpecificAyah { }; 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; + 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; }; export interface ChapterTranslate { @@ -166,9 +166,9 @@ export interface ChapterInfoResponse { }; export interface ChapterApi { - listChapters: (language?: string) => Promise; - getChapter: (id: number, language?: string) => Promise; - getChapterInfo: (chapter_id: number, language?: string) => Promise; + listChapters: (language?: string) => Promise; + getChapter: (id: number, language?: string) => Promise; + getChapterInfo: (chapter_id: number, language?: string) => Promise; }; @@ -178,7 +178,7 @@ export interface JuzResponse { export interface JuzApi { - getAllJuzs: () => Promise; + getAllJuzs: () => Promise; }; export interface RecitaionInfo { @@ -268,15 +268,15 @@ export interface VerseMediaResponse { }; export interface ResourceApi { - getRecitationInfo: (recitation_id: string) => Promise; - getTranslationInfo: (translation_id: string) => Promise; - getTranslations: (language?: string) => Promise; - getTafsirs: (language?: string) => Promise; - getTafsirInfo: (tafsir_id: string) => Promise; - getRecitationStyles: () => Promise; - getLanguages: () => Promise; - getChapterInfos: () => Promise; - getVerseMedias: () => Promise; + getRecitationInfo: (recitation_id: string) => Promise; + getTranslationInfo: (translation_id: string) => Promise; + getTranslations: (language?: string) => Promise; + getTafsirs: (language?: string) => Promise; + getTafsirInfo: (tafsir_id: string) => Promise; + getRecitationStyles: () => Promise; + getLanguages: () => Promise; + getChapterInfos: () => Promise; + getVerseMedias: () => Promise; }; export interface VerseResponse { @@ -356,13 +356,13 @@ export interface VerseQuery { export interface VerseApi { - getVerseByChapter: (chapter_number: string, query?: VerseQuery) => Promise; - getVerseByPage: (page_number: string, query?: VerseQuery) => Promise; - getVerseByJuz: (juz_number: string, query?: VerseQuery) => Promise; - getVerseByHizbNumber: (hizb_number: string, query?: VerseQuery) => Promise; - getVerseByRubElHizbNumber: (rub_el_hizb_number: string, query?: VerseQuery) => Promise; - getSpecificVerseByVerseKey: (verse_key: string, query?: VerseQuery) => Promise; - getRandomAyah: (query?: VerseQuery) => Promise; + getVerseByChapter: (chapter_number: string, query?: VerseQuery) => Promise; + getVerseByPage: (page_number: string, query?: VerseQuery) => Promise; + getVerseByJuz: (juz_number: string, query?: VerseQuery) => Promise; + getVerseByHizbNumber: (hizb_number: string, query?: VerseQuery) => Promise; + getVerseByRubElHizbNumber: (rub_el_hizb_number: string, query?: VerseQuery) => Promise; + getSpecificVerseByVerseKey: (verse_key: string, query?: VerseQuery) => Promise; + getRandomAyah: (query?: VerseQuery) => Promise; }; export interface QuranQuery { @@ -489,13 +489,13 @@ export interface GlyphCodesOfAyahV2 { export interface QuranApi { - getIndoPakScriptOfAyah: (query?: QuranQuery) => Promise; - getUthmaniTajweedScriptOfAyah: (query?: QuranQuery) => Promise; - getUthmaniScriptOfAyah: (query?: QuranQuery) => Promise; - getUthmaniSimpleScriptOfAyah: (query?: QuranQuery) => Promise; - getImlaeiSimpleTextOfAyah: (query?: QuranQuery) => Promise; - getASingleTranslation: (translation_id: string, query?: TranslationQuery) => Promise; - getSingleTafsir: (tafsir_id: string, query?: TranslationQuery) => Promise; - getGlyphCodesOfAyahV1: (query?: QuranQuery) => Promise; - getGlyphCodesOfAyahV2: (query?: QuranQuery) => Promise; + getIndoPakScriptOfAyah: (query?: QuranQuery) => Promise; + getUthmaniTajweedScriptOfAyah: (query?: QuranQuery) => Promise; + getUthmaniScriptOfAyah: (query?: QuranQuery) => Promise; + getUthmaniSimpleScriptOfAyah: (query?: QuranQuery) => Promise; + getImlaeiSimpleTextOfAyah: (query?: QuranQuery) => Promise; + getASingleTranslation: (translation_id: string, query?: TranslationQuery) => Promise; + getSingleTafsir: (tafsir_id: string, query?: TranslationQuery) => Promise; + getGlyphCodesOfAyahV1: (query?: QuranQuery) => Promise; + getGlyphCodesOfAyahV2: (query?: QuranQuery) => Promise; }; \ No newline at end of file diff --git a/src/req.ts b/src/req.ts index 9b13c0a..ae17d0c 100644 --- a/src/req.ts +++ b/src/req.ts @@ -1,7 +1,7 @@ import Agent from "agentkeepalive"; import axios, { AxiosInstance } from "axios"; import config from "./config"; -import { CustomHeaders } from "./types"; +import { CustomHeaders } from "./interfaces"; @@ -49,7 +49,7 @@ const Api = ( }); axiosInstance.interceptors.response.use( - (response) => response, + (response) => response.data, (error) => Promise.reject(error.toJSON()), ); diff --git a/src/utils.ts b/src/utils.ts index a1a9705..2fcf865 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,7 @@ import { AxiosError, AxiosResponse } from 'axios'; +import Api from './req'; + +const api = Api(); /** * Handles the response from an Axios request. @@ -20,4 +23,9 @@ export const handleResponse = ( */ export const handleError = (reject: (reason: Error | AxiosError) => void) => (error: AxiosError) => - reject(error); \ No newline at end of file + reject(error); + + +export const apiWraper = async (query: string): Promise => { + return await api.get(query); +}; \ No newline at end of file