@@ -11,52 +11,26 @@ import {
11
11
VerseKey ,
12
12
VerseRecitationField ,
13
13
} from '../../types' ;
14
- import { decamelize } from 'humps' ;
15
14
import Utils from '../utils' ;
16
- import { fetcher } from './_fetcher' ;
15
+ import { fetcher , mergeApiOptions } from './_fetcher' ;
16
+ import { BaseApiOptions } from '../../types/BaseApiOptions' ;
17
17
18
- type GetChapterRecitationOptions = Partial < {
19
- language : Language ;
20
- } > ;
18
+ type GetChapterRecitationOptions = Partial < BaseApiOptions > ;
21
19
22
20
const defaultChapterRecitationsOptions : GetChapterRecitationOptions = {
23
21
language : Language . ARABIC ,
24
22
} ;
25
23
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
+ > ;
38
29
39
30
const defaultVerseRecitationsOptions : GetVerseRecitationOptions = {
40
31
language : Language . ARABIC ,
41
32
} ;
42
33
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
-
60
34
/**
61
35
* Get all chapter recitations for specific reciter
62
36
* @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 (
69
43
reciterId : string ,
70
44
options ?: GetChapterRecitationOptions
71
45
) => {
72
- const params = getChapterRecitationsOptions ( options ) ;
46
+ const params = mergeApiOptions ( options , defaultChapterRecitationsOptions ) ;
73
47
const { audioFiles } = await fetcher < { audioFiles : ChapterRecitation [ ] } > (
74
48
`/chapter_recitations/${ reciterId } ` ,
75
- params
49
+ params ,
50
+ options ?. fetchFn
76
51
) ;
77
52
return audioFiles ;
78
53
} ;
@@ -93,10 +68,11 @@ const findChapterRecitationById = async (
93
68
) => {
94
69
if ( ! Utils . isValidChapterId ( chapterId ) ) throw new Error ( 'Invalid chapter id' ) ;
95
70
96
- const params = getChapterRecitationsOptions ( options ) ;
71
+ const params = mergeApiOptions ( options , defaultChapterRecitationsOptions ) ;
97
72
const { audioFile } = await fetcher < { audioFile : ChapterRecitation } > (
98
73
`/chapter_recitations/${ reciterId } /${ chapterId } ` ,
99
- params
74
+ params ,
75
+ options ?. fetchFn
100
76
) ;
101
77
102
78
return audioFile ;
@@ -118,11 +94,15 @@ const findVerseRecitationsByChapter = async (
118
94
) => {
119
95
if ( ! Utils . isValidChapterId ( chapterId ) ) throw new Error ( 'Invalid chapter id' ) ;
120
96
121
- const params = getVerseRecitationsOptions ( options ) ;
97
+ const params = mergeApiOptions ( options , defaultVerseRecitationsOptions ) ;
122
98
const data = await fetcher < {
123
99
audioFiles : VerseRecitation [ ] ;
124
100
pagination : Pagination ;
125
- } > ( `/recitations/${ recitationId } /by_chapter/${ chapterId } ` , params ) ;
101
+ } > (
102
+ `/recitations/${ recitationId } /by_chapter/${ chapterId } ` ,
103
+ params ,
104
+ options ?. fetchFn
105
+ ) ;
126
106
127
107
return data ;
128
108
} ;
@@ -143,11 +123,11 @@ const findVerseRecitationsByJuz = async (
143
123
) => {
144
124
if ( ! Utils . isValidJuz ( juz ) ) throw new Error ( 'Invalid juz' ) ;
145
125
146
- const params = getVerseRecitationsOptions ( options ) ;
126
+ const params = mergeApiOptions ( options , defaultVerseRecitationsOptions ) ;
147
127
const data = await fetcher < {
148
128
audioFiles : VerseRecitation [ ] ;
149
129
pagination : Pagination ;
150
- } > ( `/recitations/${ recitationId } /by_juz/${ juz } ` , params ) ;
130
+ } > ( `/recitations/${ recitationId } /by_juz/${ juz } ` , params , options ?. fetchFn ) ;
151
131
152
132
return data ;
153
133
} ;
@@ -168,11 +148,11 @@ const findVerseRecitationsByPage = async (
168
148
) => {
169
149
if ( ! Utils . isValidQuranPage ( page ) ) throw new Error ( 'Invalid page' ) ;
170
150
171
- const params = getVerseRecitationsOptions ( options ) ;
151
+ const params = mergeApiOptions ( options , defaultVerseRecitationsOptions ) ;
172
152
const data = await fetcher < {
173
153
audioFiles : VerseRecitation [ ] ;
174
154
pagination : Pagination ;
175
- } > ( `/recitations/${ recitationId } /by_page/${ page } ` , params ) ;
155
+ } > ( `/recitations/${ recitationId } /by_page/${ page } ` , params , options ?. fetchFn ) ;
176
156
177
157
return data ;
178
158
} ;
@@ -193,11 +173,11 @@ const findVerseRecitationsByRub = async (
193
173
) => {
194
174
if ( ! Utils . isValidRub ( rub ) ) throw new Error ( 'Invalid rub' ) ;
195
175
196
- const params = getVerseRecitationsOptions ( options ) ;
176
+ const params = mergeApiOptions ( options , defaultVerseRecitationsOptions ) ;
197
177
const data = await fetcher < {
198
178
audioFiles : VerseRecitation [ ] ;
199
179
pagination : Pagination ;
200
- } > ( `/recitations/${ recitationId } /by_rub/${ rub } ` , params ) ;
180
+ } > ( `/recitations/${ recitationId } /by_rub/${ rub } ` , params , options ?. fetchFn ) ;
201
181
202
182
return data ;
203
183
} ;
@@ -218,11 +198,11 @@ const findVerseRecitationsByHizb = async (
218
198
) => {
219
199
if ( ! Utils . isValidHizb ( hizb ) ) throw new Error ( 'Invalid hizb' ) ;
220
200
221
- const params = getVerseRecitationsOptions ( options ) ;
201
+ const params = mergeApiOptions ( options , defaultVerseRecitationsOptions ) ;
222
202
const data = await fetcher < {
223
203
audioFiles : VerseRecitation [ ] ;
224
204
pagination : Pagination ;
225
- } > ( `/recitations/${ recitationId } /by_hizb/${ hizb } ` , params ) ;
205
+ } > ( `/recitations/${ recitationId } /by_hizb/${ hizb } ` , params , options ?. fetchFn ) ;
226
206
227
207
return data ;
228
208
} ;
@@ -243,11 +223,11 @@ const findVerseRecitationsByKey = async (
243
223
) => {
244
224
if ( ! Utils . isValidVerseKey ( key ) ) throw new Error ( 'Invalid verse key' ) ;
245
225
246
- const params = getVerseRecitationsOptions ( options ) ;
226
+ const params = mergeApiOptions ( options , defaultVerseRecitationsOptions ) ;
247
227
const data = await fetcher < {
248
228
audioFiles : VerseRecitation [ ] ;
249
229
pagination : Pagination ;
250
- } > ( `/recitations/${ recitationId } /by_ayah/${ key } ` , params ) ;
230
+ } > ( `/recitations/${ recitationId } /by_ayah/${ key } ` , params , options ?. fetchFn ) ;
251
231
252
232
return data ;
253
233
} ;
0 commit comments