@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.
Files changed (88) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +21 -0
  3. package/dist/api.cjs.development.js +2627 -0
  4. package/dist/api.cjs.development.js.map +1 -0
  5. package/dist/api.cjs.production.min.js +2 -0
  6. package/dist/api.cjs.production.min.js.map +1 -0
  7. package/dist/api.esm.js +2618 -0
  8. package/dist/api.esm.js.map +1 -0
  9. package/dist/index.d.ts +2 -0
  10. package/dist/index.js +8 -0
  11. package/dist/sdk/index.d.ts +183 -0
  12. package/dist/sdk/utils.d.ts +125 -0
  13. package/dist/sdk/v4/_fetcher.d.ts +3 -0
  14. package/dist/sdk/v4/audio.d.ts +52 -0
  15. package/dist/sdk/v4/chapters.d.ts +13 -0
  16. package/dist/sdk/v4/index.d.ts +173 -0
  17. package/dist/sdk/v4/juzs.d.ts +5 -0
  18. package/dist/sdk/v4/resources.d.ts +26 -0
  19. package/dist/sdk/v4/search.d.ts +22 -0
  20. package/dist/sdk/v4/verses.d.ts +64 -0
  21. package/dist/types/ChapterId.d.ts +1 -0
  22. package/dist/types/HizbNumber.d.ts +1 -0
  23. package/dist/types/JuzNumber.d.ts +1 -0
  24. package/dist/types/PageNumber.d.ts +1 -0
  25. package/dist/types/RubNumber.d.ts +1 -0
  26. package/dist/types/VerseKey.d.ts +1 -0
  27. package/dist/types/api/ApiResponses.d.ts +18 -0
  28. package/dist/types/api/AudioData.d.ts +17 -0
  29. package/dist/types/api/AudioResponse.d.ts +8 -0
  30. package/dist/types/api/Chapter.d.ts +14 -0
  31. package/dist/types/api/ChapterInfo.d.ts +8 -0
  32. package/dist/types/api/Footnote.d.ts +6 -0
  33. package/dist/types/api/Juz.d.ts +8 -0
  34. package/dist/types/api/Pagination.d.ts +7 -0
  35. package/dist/types/api/Reciter.d.ts +21 -0
  36. package/dist/types/api/Resources.d.ts +70 -0
  37. package/dist/types/api/Segment.d.ts +4 -0
  38. package/dist/types/api/Tafsir.d.ts +7 -0
  39. package/dist/types/api/TafsirInfo.d.ts +9 -0
  40. package/dist/types/api/TranslatedName.d.ts +4 -0
  41. package/dist/types/api/Translation.d.ts +17 -0
  42. package/dist/types/api/Transliteration.d.ts +4 -0
  43. package/dist/types/api/Verse.d.ts +32 -0
  44. package/dist/types/api/Word.d.ts +28 -0
  45. package/dist/types/api/index.d.ts +18 -0
  46. package/dist/types/index.d.ts +88 -0
  47. package/dist/utils/misc.d.ts +1 -0
  48. package/dist/utils/qs-stringify.d.ts +21 -0
  49. package/package.json +64 -0
  50. package/src/index.ts +2 -0
  51. package/src/sdk/index.ts +9 -0
  52. package/src/sdk/utils.ts +230 -0
  53. package/src/sdk/v4/_fetcher.ts +28 -0
  54. package/src/sdk/v4/audio.ts +266 -0
  55. package/src/sdk/v4/chapters.ts +83 -0
  56. package/src/sdk/v4/index.ts +17 -0
  57. package/src/sdk/v4/juzs.ts +17 -0
  58. package/src/sdk/v4/resources.ts +224 -0
  59. package/src/sdk/v4/search.ts +40 -0
  60. package/src/sdk/v4/verses.ts +213 -0
  61. package/src/types/ChapterId.ts +115 -0
  62. package/src/types/HizbNumber.ts +61 -0
  63. package/src/types/JuzNumber.ts +31 -0
  64. package/src/types/PageNumber.ts +519 -0
  65. package/src/types/RubNumber.ts +241 -0
  66. package/src/types/VerseKey.ts +6237 -0
  67. package/src/types/api/ApiResponses.ts +19 -0
  68. package/src/types/api/AudioData.ts +20 -0
  69. package/src/types/api/AudioResponse.ts +9 -0
  70. package/src/types/api/Chapter.ts +15 -0
  71. package/src/types/api/ChapterInfo.ts +8 -0
  72. package/src/types/api/Footnote.ts +6 -0
  73. package/src/types/api/Juz.ts +8 -0
  74. package/src/types/api/Pagination.ts +7 -0
  75. package/src/types/api/Reciter.ts +21 -0
  76. package/src/types/api/Resources.ts +81 -0
  77. package/src/types/api/Segment.ts +4 -0
  78. package/src/types/api/Tafsir.ts +7 -0
  79. package/src/types/api/TafsirInfo.ts +10 -0
  80. package/src/types/api/TranslatedName.ts +4 -0
  81. package/src/types/api/Translation.ts +21 -0
  82. package/src/types/api/Transliteration.ts +4 -0
  83. package/src/types/api/Verse.ts +35 -0
  84. package/src/types/api/Word.ts +30 -0
  85. package/src/types/api/index.ts +18 -0
  86. package/src/types/index.ts +134 -0
  87. package/src/utils/misc.ts +3 -0
  88. package/src/utils/qs-stringify.ts +81 -0
@@ -0,0 +1,88 @@
1
+ export declare enum Language {
2
+ ARABIC = "ar",
3
+ ENGLISH = "en",
4
+ URDU = "ur",
5
+ BENGALI = "bn",
6
+ TURKISH = "tr",
7
+ SPANISH = "es",
8
+ GERMAN = "de",
9
+ BOSNIAN = "bs",
10
+ RUSSIAN = "ru",
11
+ ALBANIAN_AL = "al",
12
+ FRENCH = "fr",
13
+ DUTCH = "nl",
14
+ TAMIL = "ta",
15
+ TAJIK = "tg",
16
+ INDONESIAN = "id",
17
+ UZBEK = "uz",
18
+ VIETNAMESE = "vi",
19
+ CHINESE = "zh",
20
+ ITALIAN = "it",
21
+ JAPANESE = "ja",
22
+ MALAYALAM = "ml",
23
+ AMHARIC = "am",
24
+ KAZAKH = "kk",
25
+ PORTUGUESE = "pt",
26
+ TAGALOG = "tl",
27
+ THAI = "th",
28
+ KOREAN = "ko",
29
+ HINDI = "hi",
30
+ KURDISH = "ku",
31
+ HAUSA = "ha",
32
+ AZERI = "az",
33
+ SWAHILI = "sw",
34
+ PERSIAN = "fa",
35
+ SERBIAN = "sr",
36
+ MARANAO = "mrn",
37
+ AMAZIGH = "zgh",
38
+ ASSAMESE = "as",
39
+ BULGARIAN = "bg",
40
+ CHECHEN = "ce",
41
+ CZECH = "cs",
42
+ DIVEHI = "dv",
43
+ DHIVEHI = "dv",
44
+ MALDIVIAN = "dv",
45
+ FINNISH = "fi",
46
+ GUJAARATI = "gu",
47
+ HEBREW = "he",
48
+ GEORGIAN = "ka",
49
+ CENTRAL_KHMER = "km",
50
+ GANDA = "lg",
51
+ MARATHI = "mr",
52
+ YORUBA = "yo",
53
+ MALAY = "ms",
54
+ NEPALI = "ne",
55
+ SWEDISH = "sv",
56
+ TELUGU = "te",
57
+ TATAR = "tt",
58
+ UIGHUR = "ug",
59
+ UYGHUR = "ug",
60
+ UKRAINIAN = "uk",
61
+ NORWEGIAN = "no",
62
+ OROMO = "om",
63
+ POLISH = "pl",
64
+ PASHTO = "ps",
65
+ ROMANIAN = "ro",
66
+ SINDHI = "sd",
67
+ NORTHERN_SAMI = "se",
68
+ SINHALA = "si",
69
+ SINHALESE = "si",
70
+ SOMALI = "so",
71
+ ALBANIAN_SQ = "sq"
72
+ }
73
+ export declare enum QuranFont {
74
+ MadaniV1 = "code_v1",
75
+ MadaniV2 = "code_v2",
76
+ Uthmani = "text_uthmani"
77
+ }
78
+ export declare type VerseField = 'chapterId' | 'textUthmani' | 'textUthmaniSimple' | 'textImlaei' | 'textImlaeiSimple' | 'textIndopak' | 'textUthmaniTajweed' | 'imageUrl' | 'imageWidth' | 'codeV1' | 'codeV2' | 'v1Page' | 'v2Page';
79
+ export declare type WordField = 'v1Page' | 'v2Page' | 'textUthmani' | 'textImlaei' | 'textIndopak' | 'verseKey' | 'location' | 'codeV1' | 'codeV2';
80
+ export declare type TranslationField = 'resourceName' | 'verseId' | 'languageId' | 'languageName' | 'verseKey' | 'chapterId' | 'verseNumber' | 'juzNumber' | 'hizbNumber' | 'rubNumber' | 'pageNumber';
81
+ export declare type VerseRecitationField = 'id' | 'chapterId' | 'segments' | 'format';
82
+ export * from './VerseKey';
83
+ export * from './ChapterId';
84
+ export * from './HizbNumber';
85
+ export * from './JuzNumber';
86
+ export * from './PageNumber';
87
+ export * from './RubNumber';
88
+ export * from './api';
@@ -0,0 +1 @@
1
+ export declare const removeBeginningSlash: (url: string) => string;
@@ -0,0 +1,21 @@
1
+ declare type QueryObject = Record<string, any>;
2
+ export declare type StringifyFunction = (key: string, value: any) => any;
3
+ declare type Config = {
4
+ eq?: string;
5
+ sep?: string;
6
+ fn?: StringifyFunction;
7
+ prefix?: string;
8
+ };
9
+ /**
10
+ * Given a query object, return the query string
11
+ * See qs-stringify.test.ts for examples
12
+ *
13
+ * @param {QueryObject} obj a query object to be converted to a query string
14
+ * @param {Config} config optional configuration
15
+ * @returns {string} query string
16
+ *
17
+ * @example
18
+ * stringify({a: 1, b: 2}) // returns 'a=1&b=s'
19
+ */
20
+ declare function stringify(obj: QueryObject, config?: Config): string;
21
+ export default stringify;
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@quranjs/api",
3
+ "description": "A library for fetching quran data from the quran.com API.",
4
+ "author": "Ahmed Riad <me@ar1.dev> (https://ar1.dev)",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "version": "1.0.1",
9
+ "license": "MIT",
10
+ "main": "dist/index.js",
11
+ "typings": "dist/index.d.ts",
12
+ "module": "dist/api.esm.js",
13
+ "files": [
14
+ "dist",
15
+ "src"
16
+ ],
17
+ "scripts": {
18
+ "start": "dts watch",
19
+ "build": "dts build",
20
+ "test": "dts test",
21
+ "test:dev": "dts test --watch",
22
+ "lint": "dts lint src test mocks",
23
+ "prepare": "husky install && dts build",
24
+ "size": "size-limit",
25
+ "analyze": "size-limit --why"
26
+ },
27
+ "devDependencies": {
28
+ "@rollup/plugin-alias": "^3.1.9",
29
+ "@rollup/plugin-node-resolve": "^13.1.3",
30
+ "@size-limit/preset-small-lib": "^7.0.8",
31
+ "@types/humps": "^2.0.1",
32
+ "dts-cli": "^1.4.0",
33
+ "husky": "^7.0.4",
34
+ "msw": "^0.39.2",
35
+ "rollup-plugin-buble": "^0.19.8",
36
+ "rollup-plugin-sizes": "^1.0.4",
37
+ "size-limit": "^7.0.8",
38
+ "tslib": "^2.3.1",
39
+ "typescript": "^4.6.2"
40
+ },
41
+ "prettier": {
42
+ "printWidth": 80,
43
+ "semi": true,
44
+ "singleQuote": true,
45
+ "trailingComma": "es5"
46
+ },
47
+ "size-limit": [
48
+ {
49
+ "path": "dist/api.cjs.production.min.js",
50
+ "limit": "10 KB"
51
+ },
52
+ {
53
+ "path": "dist/api.esm.js",
54
+ "limit": "10 KB"
55
+ }
56
+ ],
57
+ "engines": {
58
+ "node": ">=12"
59
+ },
60
+ "dependencies": {
61
+ "cross-fetch": "^3.1.5",
62
+ "humps": "^2.0.1"
63
+ }
64
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { default as quran } from './sdk';
2
+ export * from './types';
@@ -0,0 +1,9 @@
1
+ import v4 from './v4';
2
+ import utils from './utils';
3
+
4
+ const quran = {
5
+ v4,
6
+ utils,
7
+ };
8
+
9
+ export default quran;
@@ -0,0 +1,230 @@
1
+ // this maps chapterNumber to verseCount
2
+ export const versesMaping = {
3
+ '1': 7,
4
+ '2': 286,
5
+ '3': 200,
6
+ '4': 176,
7
+ '5': 120,
8
+ '6': 165,
9
+ '7': 206,
10
+ '8': 75,
11
+ '9': 129,
12
+ '10': 109,
13
+ '11': 123,
14
+ '12': 111,
15
+ '13': 43,
16
+ '14': 52,
17
+ '15': 99,
18
+ '16': 128,
19
+ '17': 111,
20
+ '18': 110,
21
+ '19': 98,
22
+ '20': 135,
23
+ '21': 112,
24
+ '22': 78,
25
+ '23': 118,
26
+ '24': 64,
27
+ '25': 77,
28
+ '26': 227,
29
+ '27': 93,
30
+ '28': 88,
31
+ '29': 69,
32
+ '30': 60,
33
+ '31': 34,
34
+ '32': 30,
35
+ '33': 73,
36
+ '34': 54,
37
+ '35': 45,
38
+ '36': 83,
39
+ '37': 182,
40
+ '38': 88,
41
+ '39': 75,
42
+ '40': 85,
43
+ '41': 54,
44
+ '42': 53,
45
+ '43': 89,
46
+ '44': 59,
47
+ '45': 37,
48
+ '46': 35,
49
+ '47': 38,
50
+ '48': 29,
51
+ '49': 18,
52
+ '50': 45,
53
+ '51': 60,
54
+ '52': 49,
55
+ '53': 62,
56
+ '54': 55,
57
+ '55': 78,
58
+ '56': 96,
59
+ '57': 29,
60
+ '58': 22,
61
+ '59': 24,
62
+ '60': 13,
63
+ '61': 14,
64
+ '62': 11,
65
+ '63': 11,
66
+ '64': 18,
67
+ '65': 12,
68
+ '66': 12,
69
+ '67': 30,
70
+ '68': 52,
71
+ '69': 52,
72
+ '70': 44,
73
+ '71': 28,
74
+ '72': 28,
75
+ '73': 20,
76
+ '74': 56,
77
+ '75': 40,
78
+ '76': 31,
79
+ '77': 50,
80
+ '78': 40,
81
+ '79': 46,
82
+ '80': 42,
83
+ '81': 29,
84
+ '82': 19,
85
+ '83': 36,
86
+ '84': 25,
87
+ '85': 22,
88
+ '86': 17,
89
+ '87': 19,
90
+ '88': 26,
91
+ '89': 30,
92
+ '90': 20,
93
+ '91': 15,
94
+ '92': 21,
95
+ '93': 11,
96
+ '94': 8,
97
+ '95': 8,
98
+ '96': 19,
99
+ '97': 5,
100
+ '98': 8,
101
+ '99': 8,
102
+ '100': 11,
103
+ '101': 11,
104
+ '102': 8,
105
+ '103': 3,
106
+ '104': 9,
107
+ '105': 5,
108
+ '106': 4,
109
+ '107': 7,
110
+ '108': 3,
111
+ '109': 6,
112
+ '110': 3,
113
+ '111': 5,
114
+ '112': 4,
115
+ '113': 5,
116
+ '114': 6,
117
+ };
118
+
119
+ /**
120
+ * Validates chapter id
121
+ * @param id chapter id
122
+ * @example
123
+ isValidChapterId('1') // true
124
+ isValidChapterId('114') // true
125
+ isValidChapterId('0') // false
126
+ isValidChapterId('-1') // false
127
+ isValidChapterId('200') // false
128
+ */
129
+ const isValidChapterId = (id: string | number) => {
130
+ const parsedId = typeof id === 'number' ? id : Number(id);
131
+ if (!parsedId || parsedId <= 0 || parsedId > 114) return false;
132
+ return true;
133
+ };
134
+
135
+ /**
136
+ * Validates juz number
137
+ * @param juz juz number
138
+ * @example
139
+ isValidJuz('1') // true
140
+ isValidJuz('30') // true
141
+ isValidJuz('0') // false
142
+ isValidJuz('-1') // false
143
+ isValidJuz('200') // false
144
+ */
145
+ const isValidJuz = (juz: string | number) => {
146
+ const parsedJuz = typeof juz === 'number' ? juz : Number(juz);
147
+ if (!parsedJuz || parsedJuz <= 0 || parsedJuz > 30) return false;
148
+ return true;
149
+ };
150
+
151
+ /**
152
+ * Validates rub number
153
+ * @param rub rub number
154
+ * @example
155
+ isValidRub('1') // true
156
+ isValidRub('240') // true
157
+ isValidRub('0') // false
158
+ isValidRub('-1') // false
159
+ isValidRub('300') // false
160
+ */
161
+ const isValidRub = (rub: string | number) => {
162
+ const parsedRub = typeof rub === 'number' ? rub : Number(rub);
163
+ if (!parsedRub || parsedRub <= 0 || parsedRub > 240) return false;
164
+ return true;
165
+ };
166
+
167
+ /**
168
+ * Validates hizb number
169
+ * @param hizb hizb number
170
+ * @example
171
+ isValidHizb('1') // true
172
+ isValidHizb('60') // true
173
+ isValidHizb('0') // false
174
+ isValidHizb('-1') // false
175
+ isValidHizb('200') // false
176
+ */
177
+ const isValidHizb = (hizb: string | number) => {
178
+ const parsedHizb = typeof hizb === 'number' ? hizb : Number(hizb);
179
+ if (!parsedHizb || parsedHizb <= 0 || parsedHizb > 60) return false;
180
+ return true;
181
+ };
182
+
183
+ /**
184
+ * Validates mushaf page number
185
+ * @param page mushaf page number
186
+ * @example
187
+ isValidQuranPage('1') // true
188
+ isValidQuranPage('604') // true
189
+ isValidQuranPage('0') // false
190
+ isValidQuranPage('-1') // false
191
+ isValidQuranPage('1000') // false
192
+ */
193
+ const isValidQuranPage = (page: string | number) => {
194
+ const parsedPage = typeof page === 'number' ? page : Number(page);
195
+ if (!parsedPage || parsedPage <= 0 || parsedPage > 604) return false;
196
+ return true;
197
+ };
198
+
199
+ /**
200
+ * Validates verse key
201
+ * @param key colon separated verse key (chapter:verse)
202
+ * @example
203
+ isValidVerseKey('1:1') // true
204
+ isValidVerseKey('30:1') // true
205
+ isValidVerseKey('0') // false
206
+ isValidVerseKey('1:-') // false
207
+ isValidVerseKey('1_1') // false
208
+ */
209
+ const isValidVerseKey = (key: string) => {
210
+ const [chapterId, verseId] = key.trim().split(':');
211
+ if (!chapterId || !verseId || !isValidChapterId(chapterId)) return false;
212
+
213
+ const parsedVerse = Number(verseId);
214
+ const verseCount = (versesMaping as any)[chapterId];
215
+ if (!parsedVerse || parsedVerse <= 0 || parsedVerse > verseCount)
216
+ return false;
217
+
218
+ return true;
219
+ };
220
+
221
+ const Utils = {
222
+ isValidChapterId,
223
+ isValidJuz,
224
+ isValidRub,
225
+ isValidHizb,
226
+ isValidQuranPage,
227
+ isValidVerseKey,
228
+ };
229
+
230
+ export default Utils;
@@ -0,0 +1,28 @@
1
+ import fetch from 'cross-fetch';
2
+ import { camelizeKeys, decamelizeKeys } from 'humps';
3
+ import stringify from '@/utils/qs-stringify';
4
+ import { removeBeginningSlash } from '@/utils/misc';
5
+
6
+ export const API_BASE_URL = 'https://api.quran.com/api/v4/';
7
+
8
+ export const makeUrl = (url: string, params?: Record<string, unknown>) => {
9
+ const baseUrl = `${API_BASE_URL}${removeBeginningSlash(url)}`;
10
+ if (!params) return baseUrl;
11
+
12
+ const decamelizedKeys = decamelizeKeys(params);
13
+ const paramsString = stringify(decamelizedKeys);
14
+ if (!paramsString) return baseUrl;
15
+
16
+ return `${baseUrl}?${paramsString}`;
17
+ };
18
+
19
+ export const fetcher = async <T extends object>(
20
+ url: string,
21
+ params: Record<string, unknown> = {}
22
+ ) => {
23
+ const res = await fetch(makeUrl(url, params));
24
+ if (res.status >= 400) throw new Error(`${res.status} ${res.statusText}`);
25
+ const json = await res.json();
26
+
27
+ return camelizeKeys(json) as T;
28
+ };