@quranjs/api 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +21 -0
- package/dist/api.cjs.development.js +2627 -0
- package/dist/api.cjs.development.js.map +1 -0
- package/dist/api.cjs.production.min.js +2 -0
- package/dist/api.cjs.production.min.js.map +1 -0
- package/dist/api.esm.js +2618 -0
- package/dist/api.esm.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +8 -0
- package/dist/sdk/index.d.ts +183 -0
- package/dist/sdk/utils.d.ts +125 -0
- package/dist/sdk/v4/_fetcher.d.ts +3 -0
- package/dist/sdk/v4/audio.d.ts +52 -0
- package/dist/sdk/v4/chapters.d.ts +13 -0
- package/dist/sdk/v4/index.d.ts +173 -0
- package/dist/sdk/v4/juzs.d.ts +5 -0
- package/dist/sdk/v4/resources.d.ts +26 -0
- package/dist/sdk/v4/search.d.ts +22 -0
- package/dist/sdk/v4/verses.d.ts +64 -0
- package/dist/types/ChapterId.d.ts +1 -0
- package/dist/types/HizbNumber.d.ts +1 -0
- package/dist/types/JuzNumber.d.ts +1 -0
- package/dist/types/PageNumber.d.ts +1 -0
- package/dist/types/RubNumber.d.ts +1 -0
- package/dist/types/VerseKey.d.ts +1 -0
- package/dist/types/api/ApiResponses.d.ts +18 -0
- package/dist/types/api/AudioData.d.ts +17 -0
- package/dist/types/api/AudioResponse.d.ts +8 -0
- package/dist/types/api/Chapter.d.ts +14 -0
- package/dist/types/api/ChapterInfo.d.ts +8 -0
- package/dist/types/api/Footnote.d.ts +6 -0
- package/dist/types/api/Juz.d.ts +8 -0
- package/dist/types/api/Pagination.d.ts +7 -0
- package/dist/types/api/Reciter.d.ts +21 -0
- package/dist/types/api/Resources.d.ts +70 -0
- package/dist/types/api/Segment.d.ts +4 -0
- package/dist/types/api/Tafsir.d.ts +7 -0
- package/dist/types/api/TafsirInfo.d.ts +9 -0
- package/dist/types/api/TranslatedName.d.ts +4 -0
- package/dist/types/api/Translation.d.ts +17 -0
- package/dist/types/api/Transliteration.d.ts +4 -0
- package/dist/types/api/Verse.d.ts +32 -0
- package/dist/types/api/Word.d.ts +28 -0
- package/dist/types/api/index.d.ts +18 -0
- package/dist/types/index.d.ts +88 -0
- package/dist/utils/misc.d.ts +1 -0
- package/dist/utils/qs-stringify.d.ts +21 -0
- package/package.json +64 -0
- package/src/index.ts +2 -0
- package/src/sdk/index.ts +9 -0
- package/src/sdk/utils.ts +230 -0
- package/src/sdk/v4/_fetcher.ts +28 -0
- package/src/sdk/v4/audio.ts +266 -0
- package/src/sdk/v4/chapters.ts +83 -0
- package/src/sdk/v4/index.ts +17 -0
- package/src/sdk/v4/juzs.ts +17 -0
- package/src/sdk/v4/resources.ts +224 -0
- package/src/sdk/v4/search.ts +40 -0
- package/src/sdk/v4/verses.ts +213 -0
- package/src/types/ChapterId.ts +115 -0
- package/src/types/HizbNumber.ts +61 -0
- package/src/types/JuzNumber.ts +31 -0
- package/src/types/PageNumber.ts +519 -0
- package/src/types/RubNumber.ts +241 -0
- package/src/types/VerseKey.ts +6237 -0
- package/src/types/api/ApiResponses.ts +19 -0
- package/src/types/api/AudioData.ts +20 -0
- package/src/types/api/AudioResponse.ts +9 -0
- package/src/types/api/Chapter.ts +15 -0
- package/src/types/api/ChapterInfo.ts +8 -0
- package/src/types/api/Footnote.ts +6 -0
- package/src/types/api/Juz.ts +8 -0
- package/src/types/api/Pagination.ts +7 -0
- package/src/types/api/Reciter.ts +21 -0
- package/src/types/api/Resources.ts +81 -0
- package/src/types/api/Segment.ts +4 -0
- package/src/types/api/Tafsir.ts +7 -0
- package/src/types/api/TafsirInfo.ts +10 -0
- package/src/types/api/TranslatedName.ts +4 -0
- package/src/types/api/Translation.ts +21 -0
- package/src/types/api/Transliteration.ts +4 -0
- package/src/types/api/Verse.ts +35 -0
- package/src/types/api/Word.ts +30 -0
- package/src/types/api/index.ts +18 -0
- package/src/types/index.ts +134 -0
- package/src/utils/misc.ts +3 -0
- package/src/utils/qs-stringify.ts +81 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ChapterId,
|
|
3
|
+
// HizbNumber,
|
|
4
|
+
JuzNumber,
|
|
5
|
+
Language,
|
|
6
|
+
PageNumber,
|
|
7
|
+
TranslationField,
|
|
8
|
+
// RubNumber,
|
|
9
|
+
Verse,
|
|
10
|
+
VerseField,
|
|
11
|
+
VerseKey,
|
|
12
|
+
WordField,
|
|
13
|
+
} from '@/types';
|
|
14
|
+
import { decamelize } from 'humps';
|
|
15
|
+
import Utils from '../utils';
|
|
16
|
+
import { fetcher } from './_fetcher';
|
|
17
|
+
|
|
18
|
+
type GetVerseOptions = Partial<{
|
|
19
|
+
language: Language;
|
|
20
|
+
reciter: string | number;
|
|
21
|
+
words: boolean;
|
|
22
|
+
translations: string[] | number[];
|
|
23
|
+
tafsirs: string[] | number[];
|
|
24
|
+
wordFields: Partial<Record<WordField, boolean>>;
|
|
25
|
+
translationFields: Partial<Record<TranslationField, boolean>>;
|
|
26
|
+
fields: Partial<Record<VerseField, boolean>>;
|
|
27
|
+
page: number;
|
|
28
|
+
perPage: number;
|
|
29
|
+
}>;
|
|
30
|
+
|
|
31
|
+
const defaultOptions: GetVerseOptions = {
|
|
32
|
+
language: Language.ARABIC,
|
|
33
|
+
perPage: 50,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const getVerseOptions = (options: GetVerseOptions = {}) => {
|
|
37
|
+
const initial = { ...defaultOptions, ...options };
|
|
38
|
+
const result: any = { language: initial.language, perPage: initial.perPage };
|
|
39
|
+
|
|
40
|
+
if (initial.page) result.page = initial.page;
|
|
41
|
+
if (initial.words !== undefined) result.words = initial.words;
|
|
42
|
+
if (initial.translations)
|
|
43
|
+
result.translations = initial.translations.join(',');
|
|
44
|
+
if (initial.tafsirs) result.tafsirs = initial.tafsirs.join(',');
|
|
45
|
+
|
|
46
|
+
if (initial.fields) {
|
|
47
|
+
const fields: string[] = [];
|
|
48
|
+
for (const [key, value] of Object.entries(initial.fields)) {
|
|
49
|
+
if (value) fields.push(decamelize(key));
|
|
50
|
+
}
|
|
51
|
+
result.fields = fields.join(',');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (initial.wordFields) {
|
|
55
|
+
const wordFields: string[] = [];
|
|
56
|
+
for (const [key, value] of Object.entries(initial.wordFields)) {
|
|
57
|
+
if (value) wordFields.push(decamelize(key));
|
|
58
|
+
}
|
|
59
|
+
result.wordFields = wordFields.join(',');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (initial.translationFields) {
|
|
63
|
+
const translationFields: string[] = [];
|
|
64
|
+
for (const [key, value] of Object.entries(initial.translationFields)) {
|
|
65
|
+
if (value) translationFields.push(decamelize(key));
|
|
66
|
+
}
|
|
67
|
+
result.translationFields = translationFields.join(',');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (initial.reciter) result.audio = initial.reciter;
|
|
71
|
+
|
|
72
|
+
return result;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Get a specific ayah with key. Key is combination of surah number and ayah number.
|
|
77
|
+
* @description https://quran.api-docs.io/v4/verses/by-specific-verse-by-key
|
|
78
|
+
* @param {VerseKey} key - surah number and ayah number separated by a colon.
|
|
79
|
+
* @param {GetVerseOptions} options
|
|
80
|
+
* @example
|
|
81
|
+
* quran.v4.verses.findByKey('1:1')
|
|
82
|
+
* quran.v4.verses.findByKey('101:5')
|
|
83
|
+
*/
|
|
84
|
+
const findByKey = async (key: VerseKey, options?: GetVerseOptions) => {
|
|
85
|
+
if (!Utils.isValidVerseKey(key)) throw new Error('Invalid verse key');
|
|
86
|
+
const params = getVerseOptions(options);
|
|
87
|
+
const url = `/verses/by_key/${key}`;
|
|
88
|
+
const { verse } = await fetcher<{ verse: Verse }>(url, params);
|
|
89
|
+
|
|
90
|
+
return verse;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Get all ayahs for a specific chapter (surah).
|
|
95
|
+
* @description https://quran.api-docs.io/v4/verses/by_chapter
|
|
96
|
+
* @param {ChapterId} id - chapter id (surah number)
|
|
97
|
+
* @param {GetVerseOptions} options
|
|
98
|
+
* @example
|
|
99
|
+
* quran.v4.verses.findByChapter('1')
|
|
100
|
+
* quran.v4.verses.findByChapter('101')
|
|
101
|
+
*/
|
|
102
|
+
const findByChapter = async (id: ChapterId, options?: GetVerseOptions) => {
|
|
103
|
+
if (!Utils.isValidChapterId(id)) throw new Error('Invalid chapter id');
|
|
104
|
+
const params = getVerseOptions(options);
|
|
105
|
+
const url = `/verses/by_chapter/${id}`;
|
|
106
|
+
const { verses } = await fetcher<{ verses: Verse[] }>(url, params);
|
|
107
|
+
|
|
108
|
+
return verses;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Get all ayahs for a specific page in the Quran.
|
|
113
|
+
* @description https://quran.api-docs.io/v4/verses/by-page
|
|
114
|
+
* @param {PageNumber} page - Quran page number
|
|
115
|
+
* @param {GetVerseOptions} options
|
|
116
|
+
* @example
|
|
117
|
+
* quran.v4.verses.findByPage('1')
|
|
118
|
+
* quran.v4.verses.findByPage('101')
|
|
119
|
+
*/
|
|
120
|
+
const findByPage = async (page: PageNumber, options?: GetVerseOptions) => {
|
|
121
|
+
if (!Utils.isValidQuranPage(page)) throw new Error('Invalid page');
|
|
122
|
+
|
|
123
|
+
const params = getVerseOptions(options);
|
|
124
|
+
const url = `/verses/by_page/${page}`;
|
|
125
|
+
const { verses } = await fetcher<{ verses: Verse[] }>(url, params);
|
|
126
|
+
|
|
127
|
+
return verses;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Get all ayahs for a Juz.
|
|
132
|
+
* @description https://quran.api-docs.io/v4/verses/by-juz
|
|
133
|
+
* @param {JuzNumber} id - juz number
|
|
134
|
+
* @param {GetVerseOptions} options
|
|
135
|
+
* @example
|
|
136
|
+
* quran.v4.verses.findByJuz('1')
|
|
137
|
+
* quran.v4.verses.findByJuz('29')
|
|
138
|
+
*/
|
|
139
|
+
const findByJuz = async (juz: JuzNumber, options?: GetVerseOptions) => {
|
|
140
|
+
if (!Utils.isValidJuz(juz)) throw new Error('Invalid juz');
|
|
141
|
+
|
|
142
|
+
const params = getVerseOptions(options);
|
|
143
|
+
const url = `/verses/by_juz/${juz}`;
|
|
144
|
+
const { verses } = await fetcher<{ verses: Verse[] }>(url, params);
|
|
145
|
+
|
|
146
|
+
return verses;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// TODO: uncomment when API is fixed
|
|
150
|
+
/**
|
|
151
|
+
* Get all ayahs for a Hizb.
|
|
152
|
+
* @description https://quran.api-docs.io/v4/verses/by-hizb-number
|
|
153
|
+
* @param {HizbNumber} hizb - hizb number
|
|
154
|
+
* @param {GetVerseOptions} options
|
|
155
|
+
* @example
|
|
156
|
+
* quran.v4.verses.findByHizb('1')
|
|
157
|
+
* quran.v4.verses.findByHizb('29')
|
|
158
|
+
*/
|
|
159
|
+
// const findByHizb = async (hizb: HizbNumber, options?: GetVerseOptions) => {
|
|
160
|
+
// if (!Utils.isValidHizb(hizb)) throw new Error('Invalid hizb');
|
|
161
|
+
|
|
162
|
+
// const params = getVerseOptions(options);
|
|
163
|
+
// const url = `/verses/by_hizb/${hizb}`;
|
|
164
|
+
// const { verses } = await fetcher<{ verses: Verse[] }>(url, params);
|
|
165
|
+
|
|
166
|
+
// return verses;
|
|
167
|
+
// };
|
|
168
|
+
|
|
169
|
+
// TODO: uncomment when API is fixed
|
|
170
|
+
/**
|
|
171
|
+
* Get all ayahs for a Rub.
|
|
172
|
+
* @description https://quran.api-docs.io/v4/verses/by-rub-number
|
|
173
|
+
* @param {RubNumber} rub - rub number
|
|
174
|
+
* @param {GetVerseOptions} options
|
|
175
|
+
* @example
|
|
176
|
+
* quran.v4.verses.findByRub('1')
|
|
177
|
+
* quran.v4.verses.findByRub('29')
|
|
178
|
+
*/
|
|
179
|
+
// const findByRub = async (rub: RubNumber, options?: GetVerseOptions) => {
|
|
180
|
+
// if (!Utils.isValidRub(rub)) throw new Error('Invalid rub');
|
|
181
|
+
|
|
182
|
+
// const params = getVerseOptions(options);
|
|
183
|
+
// const url = `/verses/by_rub/${rub}`;
|
|
184
|
+
// const { verses } = await fetcher<{ verses: Verse[] }>(url, params);
|
|
185
|
+
|
|
186
|
+
// return verses;
|
|
187
|
+
// };
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Get a random ayah.
|
|
191
|
+
* @description https://quran.api-docs.io/v4/verses/random
|
|
192
|
+
* @param {GetVerseOptions} options
|
|
193
|
+
* @example
|
|
194
|
+
* quran.v4.verses.findRandom()
|
|
195
|
+
*/
|
|
196
|
+
const findRandom = async (options?: GetVerseOptions) => {
|
|
197
|
+
const params = getVerseOptions(options);
|
|
198
|
+
const { verse } = await fetcher<{ verse: Verse }>('/verses/random', params);
|
|
199
|
+
|
|
200
|
+
return verse;
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
const verses = {
|
|
204
|
+
findByKey,
|
|
205
|
+
findByChapter,
|
|
206
|
+
findByPage,
|
|
207
|
+
findByJuz,
|
|
208
|
+
// findByHizb,
|
|
209
|
+
// findByRub,
|
|
210
|
+
findRandom,
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
export default verses;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
export type ChapterId =
|
|
2
|
+
| '1'
|
|
3
|
+
| '2'
|
|
4
|
+
| '3'
|
|
5
|
+
| '4'
|
|
6
|
+
| '5'
|
|
7
|
+
| '6'
|
|
8
|
+
| '7'
|
|
9
|
+
| '8'
|
|
10
|
+
| '9'
|
|
11
|
+
| '10'
|
|
12
|
+
| '11'
|
|
13
|
+
| '12'
|
|
14
|
+
| '13'
|
|
15
|
+
| '14'
|
|
16
|
+
| '15'
|
|
17
|
+
| '16'
|
|
18
|
+
| '17'
|
|
19
|
+
| '18'
|
|
20
|
+
| '19'
|
|
21
|
+
| '20'
|
|
22
|
+
| '21'
|
|
23
|
+
| '22'
|
|
24
|
+
| '23'
|
|
25
|
+
| '24'
|
|
26
|
+
| '25'
|
|
27
|
+
| '26'
|
|
28
|
+
| '27'
|
|
29
|
+
| '28'
|
|
30
|
+
| '29'
|
|
31
|
+
| '30'
|
|
32
|
+
| '31'
|
|
33
|
+
| '32'
|
|
34
|
+
| '33'
|
|
35
|
+
| '34'
|
|
36
|
+
| '35'
|
|
37
|
+
| '36'
|
|
38
|
+
| '37'
|
|
39
|
+
| '38'
|
|
40
|
+
| '39'
|
|
41
|
+
| '40'
|
|
42
|
+
| '41'
|
|
43
|
+
| '42'
|
|
44
|
+
| '43'
|
|
45
|
+
| '44'
|
|
46
|
+
| '45'
|
|
47
|
+
| '46'
|
|
48
|
+
| '47'
|
|
49
|
+
| '48'
|
|
50
|
+
| '49'
|
|
51
|
+
| '50'
|
|
52
|
+
| '51'
|
|
53
|
+
| '52'
|
|
54
|
+
| '53'
|
|
55
|
+
| '54'
|
|
56
|
+
| '55'
|
|
57
|
+
| '56'
|
|
58
|
+
| '57'
|
|
59
|
+
| '58'
|
|
60
|
+
| '59'
|
|
61
|
+
| '60'
|
|
62
|
+
| '61'
|
|
63
|
+
| '62'
|
|
64
|
+
| '63'
|
|
65
|
+
| '64'
|
|
66
|
+
| '65'
|
|
67
|
+
| '66'
|
|
68
|
+
| '67'
|
|
69
|
+
| '68'
|
|
70
|
+
| '69'
|
|
71
|
+
| '70'
|
|
72
|
+
| '71'
|
|
73
|
+
| '72'
|
|
74
|
+
| '73'
|
|
75
|
+
| '74'
|
|
76
|
+
| '75'
|
|
77
|
+
| '76'
|
|
78
|
+
| '77'
|
|
79
|
+
| '78'
|
|
80
|
+
| '79'
|
|
81
|
+
| '80'
|
|
82
|
+
| '81'
|
|
83
|
+
| '82'
|
|
84
|
+
| '83'
|
|
85
|
+
| '84'
|
|
86
|
+
| '85'
|
|
87
|
+
| '86'
|
|
88
|
+
| '87'
|
|
89
|
+
| '88'
|
|
90
|
+
| '89'
|
|
91
|
+
| '90'
|
|
92
|
+
| '91'
|
|
93
|
+
| '92'
|
|
94
|
+
| '93'
|
|
95
|
+
| '94'
|
|
96
|
+
| '95'
|
|
97
|
+
| '96'
|
|
98
|
+
| '97'
|
|
99
|
+
| '98'
|
|
100
|
+
| '99'
|
|
101
|
+
| '100'
|
|
102
|
+
| '101'
|
|
103
|
+
| '102'
|
|
104
|
+
| '103'
|
|
105
|
+
| '104'
|
|
106
|
+
| '105'
|
|
107
|
+
| '106'
|
|
108
|
+
| '107'
|
|
109
|
+
| '108'
|
|
110
|
+
| '109'
|
|
111
|
+
| '110'
|
|
112
|
+
| '111'
|
|
113
|
+
| '112'
|
|
114
|
+
| '113'
|
|
115
|
+
| '114';
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type HizbNumber =
|
|
2
|
+
| '1'
|
|
3
|
+
| '2'
|
|
4
|
+
| '3'
|
|
5
|
+
| '4'
|
|
6
|
+
| '5'
|
|
7
|
+
| '6'
|
|
8
|
+
| '7'
|
|
9
|
+
| '8'
|
|
10
|
+
| '9'
|
|
11
|
+
| '10'
|
|
12
|
+
| '11'
|
|
13
|
+
| '12'
|
|
14
|
+
| '13'
|
|
15
|
+
| '14'
|
|
16
|
+
| '15'
|
|
17
|
+
| '16'
|
|
18
|
+
| '17'
|
|
19
|
+
| '18'
|
|
20
|
+
| '19'
|
|
21
|
+
| '20'
|
|
22
|
+
| '21'
|
|
23
|
+
| '22'
|
|
24
|
+
| '23'
|
|
25
|
+
| '24'
|
|
26
|
+
| '25'
|
|
27
|
+
| '26'
|
|
28
|
+
| '27'
|
|
29
|
+
| '28'
|
|
30
|
+
| '29'
|
|
31
|
+
| '30'
|
|
32
|
+
| '31'
|
|
33
|
+
| '32'
|
|
34
|
+
| '33'
|
|
35
|
+
| '34'
|
|
36
|
+
| '35'
|
|
37
|
+
| '36'
|
|
38
|
+
| '37'
|
|
39
|
+
| '38'
|
|
40
|
+
| '39'
|
|
41
|
+
| '40'
|
|
42
|
+
| '41'
|
|
43
|
+
| '42'
|
|
44
|
+
| '43'
|
|
45
|
+
| '44'
|
|
46
|
+
| '45'
|
|
47
|
+
| '46'
|
|
48
|
+
| '47'
|
|
49
|
+
| '48'
|
|
50
|
+
| '49'
|
|
51
|
+
| '50'
|
|
52
|
+
| '51'
|
|
53
|
+
| '52'
|
|
54
|
+
| '53'
|
|
55
|
+
| '54'
|
|
56
|
+
| '55'
|
|
57
|
+
| '56'
|
|
58
|
+
| '57'
|
|
59
|
+
| '58'
|
|
60
|
+
| '59'
|
|
61
|
+
| '60';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type JuzNumber =
|
|
2
|
+
| '1'
|
|
3
|
+
| '2'
|
|
4
|
+
| '3'
|
|
5
|
+
| '4'
|
|
6
|
+
| '5'
|
|
7
|
+
| '6'
|
|
8
|
+
| '7'
|
|
9
|
+
| '8'
|
|
10
|
+
| '9'
|
|
11
|
+
| '10'
|
|
12
|
+
| '11'
|
|
13
|
+
| '12'
|
|
14
|
+
| '13'
|
|
15
|
+
| '14'
|
|
16
|
+
| '15'
|
|
17
|
+
| '16'
|
|
18
|
+
| '17'
|
|
19
|
+
| '18'
|
|
20
|
+
| '19'
|
|
21
|
+
| '20'
|
|
22
|
+
| '21'
|
|
23
|
+
| '22'
|
|
24
|
+
| '23'
|
|
25
|
+
| '24'
|
|
26
|
+
| '25'
|
|
27
|
+
| '26'
|
|
28
|
+
| '27'
|
|
29
|
+
| '28'
|
|
30
|
+
| '29'
|
|
31
|
+
| '30';
|