@@ -11,52 +11,26 @@ import {
1111 VerseKey ,
1212 VerseRecitationField ,
1313} from '../../types' ;
14- import { decamelize } from 'humps' ;
1514import Utils from '../utils' ;
16- import { fetcher } from './_fetcher' ;
15+ import { fetcher , mergeApiOptions } from './_fetcher' ;
16+ import { BaseApiOptions } from '../../types/BaseApiOptions' ;
1717
18- type GetChapterRecitationOptions = Partial < {
19- language : Language ;
20- } > ;
18+ type GetChapterRecitationOptions = Partial < BaseApiOptions > ;
2119
2220const defaultChapterRecitationsOptions : GetChapterRecitationOptions = {
2321 language : Language . ARABIC ,
2422} ;
2523
26- const getChapterRecitationsOptions = (
27- options : GetChapterRecitationOptions = { }
28- ) => {
29- const final : any = { ...defaultChapterRecitationsOptions , ...options } ;
30-
31- return final ;
32- } ;
33-
34- type GetVerseRecitationOptions = Partial < {
35- language : Language ;
36- fields : Partial < Record < VerseRecitationField , boolean > > ;
37- } > ;
24+ type GetVerseRecitationOptions = Partial <
25+ BaseApiOptions & {
26+ fields : Partial < Record < VerseRecitationField , boolean > > ;
27+ }
28+ > ;
3829
3930const defaultVerseRecitationsOptions : GetVerseRecitationOptions = {
4031 language : Language . ARABIC ,
4132} ;
4233
43- const getVerseRecitationsOptions = (
44- options : GetVerseRecitationOptions = { }
45- ) => {
46- const initial = { ...defaultVerseRecitationsOptions , ...options } ;
47- const final : any = { language : initial . language } ;
48-
49- if ( initial . fields ) {
50- const fields : string [ ] = [ ] ;
51- for ( const [ key , value ] of Object . entries ( initial . fields ) ) {
52- if ( value ) fields . push ( decamelize ( key ) ) ;
53- }
54- final . fields = fields . join ( ',' ) ;
55- }
56-
57- return final ;
58- } ;
59-
6034/**
6135 * Get all chapter recitations for specific reciter
6236 * @description https://quran.api-docs.io/v4/audio-recitations/list-of-all-surah-audio-files-for-specific-reciter
@@ -69,10 +43,11 @@ const findAllChapterRecitations = async (
6943 reciterId : string ,
7044 options ?: GetChapterRecitationOptions
7145) => {
72- const params = getChapterRecitationsOptions ( options ) ;
46+ const params = mergeApiOptions ( options , defaultChapterRecitationsOptions ) ;
7347 const { audioFiles } = await fetcher < { audioFiles : ChapterRecitation [ ] } > (
7448 `/chapter_recitations/${ reciterId } ` ,
75- params
49+ params ,
50+ options ?. fetchFn
7651 ) ;
7752 return audioFiles ;
7853} ;
@@ -93,10 +68,11 @@ const findChapterRecitationById = async (
9368) => {
9469 if ( ! Utils . isValidChapterId ( chapterId ) ) throw new Error ( 'Invalid chapter id' ) ;
9570
96- const params = getChapterRecitationsOptions ( options ) ;
71+ const params = mergeApiOptions ( options , defaultChapterRecitationsOptions ) ;
9772 const { audioFile } = await fetcher < { audioFile : ChapterRecitation } > (
9873 `/chapter_recitations/${ reciterId } /${ chapterId } ` ,
99- params
74+ params ,
75+ options ?. fetchFn
10076 ) ;
10177
10278 return audioFile ;
@@ -118,11 +94,15 @@ const findVerseRecitationsByChapter = async (
11894) => {
11995 if ( ! Utils . isValidChapterId ( chapterId ) ) throw new Error ( 'Invalid chapter id' ) ;
12096
121- const params = getVerseRecitationsOptions ( options ) ;
97+ const params = mergeApiOptions ( options , defaultVerseRecitationsOptions ) ;
12298 const data = await fetcher < {
12399 audioFiles : VerseRecitation [ ] ;
124100 pagination : Pagination ;
125- } > ( `/recitations/${ recitationId } /by_chapter/${ chapterId } ` , params ) ;
101+ } > (
102+ `/recitations/${ recitationId } /by_chapter/${ chapterId } ` ,
103+ params ,
104+ options ?. fetchFn
105+ ) ;
126106
127107 return data ;
128108} ;
@@ -143,11 +123,11 @@ const findVerseRecitationsByJuz = async (
143123) => {
144124 if ( ! Utils . isValidJuz ( juz ) ) throw new Error ( 'Invalid juz' ) ;
145125
146- const params = getVerseRecitationsOptions ( options ) ;
126+ const params = mergeApiOptions ( options , defaultVerseRecitationsOptions ) ;
147127 const data = await fetcher < {
148128 audioFiles : VerseRecitation [ ] ;
149129 pagination : Pagination ;
150- } > ( `/recitations/${ recitationId } /by_juz/${ juz } ` , params ) ;
130+ } > ( `/recitations/${ recitationId } /by_juz/${ juz } ` , params , options ?. fetchFn ) ;
151131
152132 return data ;
153133} ;
@@ -168,11 +148,11 @@ const findVerseRecitationsByPage = async (
168148) => {
169149 if ( ! Utils . isValidQuranPage ( page ) ) throw new Error ( 'Invalid page' ) ;
170150
171- const params = getVerseRecitationsOptions ( options ) ;
151+ const params = mergeApiOptions ( options , defaultVerseRecitationsOptions ) ;
172152 const data = await fetcher < {
173153 audioFiles : VerseRecitation [ ] ;
174154 pagination : Pagination ;
175- } > ( `/recitations/${ recitationId } /by_page/${ page } ` , params ) ;
155+ } > ( `/recitations/${ recitationId } /by_page/${ page } ` , params , options ?. fetchFn ) ;
176156
177157 return data ;
178158} ;
@@ -193,11 +173,11 @@ const findVerseRecitationsByRub = async (
193173) => {
194174 if ( ! Utils . isValidRub ( rub ) ) throw new Error ( 'Invalid rub' ) ;
195175
196- const params = getVerseRecitationsOptions ( options ) ;
176+ const params = mergeApiOptions ( options , defaultVerseRecitationsOptions ) ;
197177 const data = await fetcher < {
198178 audioFiles : VerseRecitation [ ] ;
199179 pagination : Pagination ;
200- } > ( `/recitations/${ recitationId } /by_rub/${ rub } ` , params ) ;
180+ } > ( `/recitations/${ recitationId } /by_rub/${ rub } ` , params , options ?. fetchFn ) ;
201181
202182 return data ;
203183} ;
@@ -218,11 +198,11 @@ const findVerseRecitationsByHizb = async (
218198) => {
219199 if ( ! Utils . isValidHizb ( hizb ) ) throw new Error ( 'Invalid hizb' ) ;
220200
221- const params = getVerseRecitationsOptions ( options ) ;
201+ const params = mergeApiOptions ( options , defaultVerseRecitationsOptions ) ;
222202 const data = await fetcher < {
223203 audioFiles : VerseRecitation [ ] ;
224204 pagination : Pagination ;
225- } > ( `/recitations/${ recitationId } /by_hizb/${ hizb } ` , params ) ;
205+ } > ( `/recitations/${ recitationId } /by_hizb/${ hizb } ` , params , options ?. fetchFn ) ;
226206
227207 return data ;
228208} ;
@@ -243,11 +223,11 @@ const findVerseRecitationsByKey = async (
243223) => {
244224 if ( ! Utils . isValidVerseKey ( key ) ) throw new Error ( 'Invalid verse key' ) ;
245225
246- const params = getVerseRecitationsOptions ( options ) ;
226+ const params = mergeApiOptions ( options , defaultVerseRecitationsOptions ) ;
247227 const data = await fetcher < {
248228 audioFiles : VerseRecitation [ ] ;
249229 pagination : Pagination ;
250- } > ( `/recitations/${ recitationId } /by_ayah/${ key } ` , params ) ;
230+ } > ( `/recitations/${ recitationId } /by_ayah/${ key } ` , params , options ?. fetchFn ) ;
251231
252232 return data ;
253233} ;
0 commit comments