@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,19 @@
|
|
|
1
|
+
import { Translation } from './Translation';
|
|
2
|
+
import { Word } from './Word';
|
|
3
|
+
|
|
4
|
+
export interface SearchResponse {
|
|
5
|
+
search: {
|
|
6
|
+
query: string;
|
|
7
|
+
totalResults: number;
|
|
8
|
+
currentPage: number;
|
|
9
|
+
totalPages: number;
|
|
10
|
+
results?: {
|
|
11
|
+
verseKey: string;
|
|
12
|
+
verse_id: number;
|
|
13
|
+
text: string;
|
|
14
|
+
highlighted: string;
|
|
15
|
+
words: Word[];
|
|
16
|
+
translations: Translation[];
|
|
17
|
+
}[];
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { VerseKey } from '../VerseKey';
|
|
2
|
+
import { Segment } from './Segment';
|
|
3
|
+
|
|
4
|
+
export interface ChapterRecitation {
|
|
5
|
+
id: number;
|
|
6
|
+
chapterId: number;
|
|
7
|
+
fileSize: number;
|
|
8
|
+
format: string;
|
|
9
|
+
audioUrl: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface VerseRecitation {
|
|
13
|
+
verseKey: VerseKey;
|
|
14
|
+
url: string;
|
|
15
|
+
|
|
16
|
+
id?: number;
|
|
17
|
+
chapterId?: number;
|
|
18
|
+
segments?: Segment[];
|
|
19
|
+
format?: string;
|
|
20
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { TranslatedName } from './TranslatedName';
|
|
2
|
+
|
|
3
|
+
export interface Chapter {
|
|
4
|
+
id: number;
|
|
5
|
+
versesCount: number;
|
|
6
|
+
bismillahPre: boolean;
|
|
7
|
+
revelationOrder: number;
|
|
8
|
+
revelationPlace: string;
|
|
9
|
+
pages: Array<number>;
|
|
10
|
+
nameComplex: string;
|
|
11
|
+
nameSimple: string;
|
|
12
|
+
transliteratedName: string;
|
|
13
|
+
nameArabic: string;
|
|
14
|
+
translatedName: TranslatedName;
|
|
15
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface Reciter {
|
|
2
|
+
id: number;
|
|
3
|
+
name: string;
|
|
4
|
+
recitationStyle: string;
|
|
5
|
+
relativePath: string;
|
|
6
|
+
profilePicture?: string;
|
|
7
|
+
coverImage?: string;
|
|
8
|
+
bio?: string;
|
|
9
|
+
qirat?: {
|
|
10
|
+
languageName: string;
|
|
11
|
+
name: string;
|
|
12
|
+
};
|
|
13
|
+
style?: {
|
|
14
|
+
languageName: string;
|
|
15
|
+
name: string;
|
|
16
|
+
};
|
|
17
|
+
translatedName?: {
|
|
18
|
+
languageName: string;
|
|
19
|
+
name: string;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { TranslatedName } from './TranslatedName';
|
|
2
|
+
|
|
3
|
+
export interface RecitationResource {
|
|
4
|
+
id?: number;
|
|
5
|
+
reciterName?: string;
|
|
6
|
+
style?: string;
|
|
7
|
+
translatedName?: TranslatedName;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface RecitationInfoResource {
|
|
11
|
+
id?: number;
|
|
12
|
+
info?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface TranslationResource {
|
|
16
|
+
id?: number;
|
|
17
|
+
name?: string;
|
|
18
|
+
authorName?: string;
|
|
19
|
+
slug?: string;
|
|
20
|
+
languageName?: string;
|
|
21
|
+
translatedName?: TranslatedName;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface TranslationInfoResource {
|
|
25
|
+
id?: number;
|
|
26
|
+
info?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface TafsirResource {
|
|
30
|
+
id?: number;
|
|
31
|
+
name?: string;
|
|
32
|
+
authorName?: string;
|
|
33
|
+
slug?: string;
|
|
34
|
+
languageName?: string;
|
|
35
|
+
translatedName?: TranslatedName;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface TafsirInfoResource {
|
|
39
|
+
id?: number;
|
|
40
|
+
info?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface RecitationStylesResource {
|
|
44
|
+
mujawwad: string;
|
|
45
|
+
murattal: string;
|
|
46
|
+
muallim: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface LanguageResource {
|
|
50
|
+
id?: number;
|
|
51
|
+
name?: string;
|
|
52
|
+
nativeName?: string;
|
|
53
|
+
isoCode?: string;
|
|
54
|
+
direction?: string;
|
|
55
|
+
translatedNames?: TranslatedName[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface ChapterInfoResource {
|
|
59
|
+
id?: number;
|
|
60
|
+
name?: string;
|
|
61
|
+
authorName?: string;
|
|
62
|
+
slug?: string;
|
|
63
|
+
languageName?: string;
|
|
64
|
+
translatedName?: TranslatedName;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface VerseMediaResource {
|
|
68
|
+
id?: number;
|
|
69
|
+
name?: string;
|
|
70
|
+
authorName?: string;
|
|
71
|
+
languageName?: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface ChapterReciterResource {
|
|
75
|
+
id: number;
|
|
76
|
+
name: string;
|
|
77
|
+
arabicName?: string;
|
|
78
|
+
relativePath?: string;
|
|
79
|
+
format?: string;
|
|
80
|
+
filesSize?: number; // in kb
|
|
81
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { VerseKey } from '../VerseKey';
|
|
2
|
+
|
|
3
|
+
export interface Translation {
|
|
4
|
+
id?: number;
|
|
5
|
+
text: string;
|
|
6
|
+
resourceId: number;
|
|
7
|
+
resourceName?: string;
|
|
8
|
+
|
|
9
|
+
verseId?: number;
|
|
10
|
+
|
|
11
|
+
languageId?: number;
|
|
12
|
+
languageName?: string;
|
|
13
|
+
|
|
14
|
+
verseKey?: VerseKey;
|
|
15
|
+
chapterId?: number;
|
|
16
|
+
verseNumber?: number;
|
|
17
|
+
juzNumber?: number;
|
|
18
|
+
hizbNumber?: number;
|
|
19
|
+
rubNumber?: number;
|
|
20
|
+
pageNumber?: number;
|
|
21
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { VerseKey } from '../VerseKey';
|
|
2
|
+
import { AudioResponse } from './AudioResponse';
|
|
3
|
+
import { Tafsir } from './Tafsir';
|
|
4
|
+
import { Translation } from './Translation';
|
|
5
|
+
import { Word } from './Word';
|
|
6
|
+
|
|
7
|
+
export interface Verse {
|
|
8
|
+
id: number;
|
|
9
|
+
verseNumber: number;
|
|
10
|
+
verseKey: VerseKey;
|
|
11
|
+
chapterId?: number | string;
|
|
12
|
+
pageNumber: number;
|
|
13
|
+
juzNumber: number;
|
|
14
|
+
hizbNumber: number;
|
|
15
|
+
rubElHizbNumber: number;
|
|
16
|
+
// verseIndex: number;
|
|
17
|
+
words?: Word[];
|
|
18
|
+
textUthmani?: string;
|
|
19
|
+
textUthmaniSimple?: string;
|
|
20
|
+
textUthmaniTajweed?: string;
|
|
21
|
+
textImlaei?: string;
|
|
22
|
+
textImlaeiSimple?: string;
|
|
23
|
+
textIndopak?: string;
|
|
24
|
+
sajdahNumber: null;
|
|
25
|
+
// sajdahType: null;
|
|
26
|
+
imageUrl?: string;
|
|
27
|
+
imageWidth?: number;
|
|
28
|
+
v1Page?: number;
|
|
29
|
+
v2Page?: number;
|
|
30
|
+
codeV1?: string;
|
|
31
|
+
codeV2?: string;
|
|
32
|
+
translations?: Translation[];
|
|
33
|
+
tafsirs?: Tafsir[];
|
|
34
|
+
audio?: AudioResponse;
|
|
35
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { VerseKey } from '../VerseKey';
|
|
2
|
+
import { Translation } from './Translation';
|
|
3
|
+
import { Transliteration } from './Transliteration';
|
|
4
|
+
|
|
5
|
+
export enum CharType {
|
|
6
|
+
Word = 'word',
|
|
7
|
+
End = 'end',
|
|
8
|
+
Pause = 'pause',
|
|
9
|
+
Sajdah = 'sajdah',
|
|
10
|
+
RubElHizb = 'rub-el-hizb',
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Word {
|
|
14
|
+
id?: number;
|
|
15
|
+
position: number;
|
|
16
|
+
audioUrl: string;
|
|
17
|
+
charTypeName: CharType;
|
|
18
|
+
codeV1?: string;
|
|
19
|
+
codeV2?: string;
|
|
20
|
+
pageNumber?: number;
|
|
21
|
+
lineNumber?: number;
|
|
22
|
+
text?: string;
|
|
23
|
+
textUthmani?: string;
|
|
24
|
+
textIndopak?: string;
|
|
25
|
+
textImlaei?: string;
|
|
26
|
+
translation: Translation;
|
|
27
|
+
transliteration: Transliteration;
|
|
28
|
+
location?: string; // chapter:verse:word
|
|
29
|
+
verseKey?: VerseKey;
|
|
30
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export * from './ApiResponses';
|
|
2
|
+
export * from './AudioData';
|
|
3
|
+
export * from './AudioResponse';
|
|
4
|
+
export * from './Chapter';
|
|
5
|
+
export * from './ChapterInfo';
|
|
6
|
+
export * from './Footnote';
|
|
7
|
+
export * from './Reciter';
|
|
8
|
+
export * from './Segment';
|
|
9
|
+
export * from './Tafsir';
|
|
10
|
+
export * from './TafsirInfo';
|
|
11
|
+
export * from './TranslatedName';
|
|
12
|
+
export * from './Translation';
|
|
13
|
+
export * from './Transliteration';
|
|
14
|
+
export * from './Verse';
|
|
15
|
+
export * from './Word';
|
|
16
|
+
export * from './Juz';
|
|
17
|
+
export * from './Pagination';
|
|
18
|
+
export * from './Resources';
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
export 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
|
+
|
|
43
|
+
DIVEHI = 'dv',
|
|
44
|
+
DHIVEHI = 'dv',
|
|
45
|
+
MALDIVIAN = 'dv',
|
|
46
|
+
|
|
47
|
+
FINNISH = 'fi',
|
|
48
|
+
GUJAARATI = 'gu',
|
|
49
|
+
HEBREW = 'he',
|
|
50
|
+
GEORGIAN = 'ka',
|
|
51
|
+
CENTRAL_KHMER = 'km',
|
|
52
|
+
GANDA = 'lg',
|
|
53
|
+
MARATHI = 'mr',
|
|
54
|
+
YORUBA = 'yo',
|
|
55
|
+
MALAY = 'ms',
|
|
56
|
+
NEPALI = 'ne',
|
|
57
|
+
SWEDISH = 'sv',
|
|
58
|
+
TELUGU = 'te',
|
|
59
|
+
TATAR = 'tt',
|
|
60
|
+
|
|
61
|
+
UIGHUR = 'ug',
|
|
62
|
+
UYGHUR = 'ug',
|
|
63
|
+
|
|
64
|
+
UKRAINIAN = 'uk',
|
|
65
|
+
NORWEGIAN = 'no',
|
|
66
|
+
OROMO = 'om',
|
|
67
|
+
POLISH = 'pl',
|
|
68
|
+
PASHTO = 'ps',
|
|
69
|
+
ROMANIAN = 'ro',
|
|
70
|
+
SINDHI = 'sd',
|
|
71
|
+
NORTHERN_SAMI = 'se',
|
|
72
|
+
|
|
73
|
+
SINHALA = 'si',
|
|
74
|
+
SINHALESE = 'si',
|
|
75
|
+
|
|
76
|
+
SOMALI = 'so',
|
|
77
|
+
ALBANIAN_SQ = 'sq',
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export enum QuranFont {
|
|
81
|
+
MadaniV1 = 'code_v1',
|
|
82
|
+
MadaniV2 = 'code_v2',
|
|
83
|
+
Uthmani = 'text_uthmani',
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type VerseField =
|
|
87
|
+
| 'chapterId'
|
|
88
|
+
| 'textUthmani'
|
|
89
|
+
| 'textUthmaniSimple'
|
|
90
|
+
| 'textImlaei'
|
|
91
|
+
| 'textImlaeiSimple'
|
|
92
|
+
| 'textIndopak'
|
|
93
|
+
| 'textUthmaniTajweed'
|
|
94
|
+
| 'imageUrl'
|
|
95
|
+
| 'imageWidth'
|
|
96
|
+
| 'codeV1'
|
|
97
|
+
| 'codeV2'
|
|
98
|
+
| 'v1Page'
|
|
99
|
+
| 'v2Page';
|
|
100
|
+
|
|
101
|
+
export type WordField =
|
|
102
|
+
| 'v1Page'
|
|
103
|
+
| 'v2Page'
|
|
104
|
+
| 'textUthmani'
|
|
105
|
+
| 'textImlaei'
|
|
106
|
+
| 'textIndopak'
|
|
107
|
+
| 'verseKey'
|
|
108
|
+
| 'location'
|
|
109
|
+
| 'codeV1'
|
|
110
|
+
| 'codeV2';
|
|
111
|
+
|
|
112
|
+
export type TranslationField =
|
|
113
|
+
| 'resourceName'
|
|
114
|
+
| 'verseId'
|
|
115
|
+
| 'languageId'
|
|
116
|
+
| 'languageName'
|
|
117
|
+
| 'verseKey'
|
|
118
|
+
| 'chapterId'
|
|
119
|
+
| 'verseNumber'
|
|
120
|
+
| 'juzNumber'
|
|
121
|
+
| 'hizbNumber'
|
|
122
|
+
| 'rubNumber'
|
|
123
|
+
| 'pageNumber';
|
|
124
|
+
|
|
125
|
+
export type VerseRecitationField = 'id' | 'chapterId' | 'segments' | 'format';
|
|
126
|
+
|
|
127
|
+
export * from './VerseKey';
|
|
128
|
+
export * from './ChapterId';
|
|
129
|
+
export * from './HizbNumber';
|
|
130
|
+
export * from './JuzNumber';
|
|
131
|
+
export * from './PageNumber';
|
|
132
|
+
export * from './RubNumber';
|
|
133
|
+
|
|
134
|
+
export * from './api';
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Reference: https://github.com/billjs/query-string/blob/master/src/index.ts
|
|
2
|
+
|
|
3
|
+
type QueryObject = Record<string, any>;
|
|
4
|
+
export type StringifyFunction = (key: string, value: any) => any;
|
|
5
|
+
type Config = {
|
|
6
|
+
eq?: string;
|
|
7
|
+
sep?: string;
|
|
8
|
+
fn?: StringifyFunction;
|
|
9
|
+
prefix?: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const defaultStringifyFunction: StringifyFunction = (
|
|
13
|
+
_key: string,
|
|
14
|
+
value: any
|
|
15
|
+
) => value;
|
|
16
|
+
const defaultEq = '=';
|
|
17
|
+
const defaultSep = '&';
|
|
18
|
+
const defaultConfig: Config = {
|
|
19
|
+
eq: defaultEq,
|
|
20
|
+
sep: defaultSep,
|
|
21
|
+
prefix: '',
|
|
22
|
+
fn: defaultStringifyFunction,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Given a query object, return the query string
|
|
27
|
+
* See qs-stringify.test.ts for examples
|
|
28
|
+
*
|
|
29
|
+
* @param {QueryObject} obj a query object to be converted to a query string
|
|
30
|
+
* @param {Config} config optional configuration
|
|
31
|
+
* @returns {string} query string
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* stringify({a: 1, b: 2}) // returns 'a=1&b=s'
|
|
35
|
+
*/
|
|
36
|
+
function stringify(obj: QueryObject, config = defaultConfig): string {
|
|
37
|
+
const { eq, sep, fn, prefix } = { ...defaultConfig, ...config };
|
|
38
|
+
if (obj == null || !isObject(obj)) {
|
|
39
|
+
return '';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return Object.entries(obj)
|
|
43
|
+
.filter(([, value]) => value !== null) // filter out null values
|
|
44
|
+
.map(([key, value]) => {
|
|
45
|
+
if (Array.isArray(value))
|
|
46
|
+
return encode(key, value.join(','), { eq, fn, prefix });
|
|
47
|
+
|
|
48
|
+
if (isObject(value))
|
|
49
|
+
return stringify(value, { eq, sep, fn, prefix: getKey(key, prefix) });
|
|
50
|
+
|
|
51
|
+
return encode(key, value, { eq, fn, prefix });
|
|
52
|
+
})
|
|
53
|
+
.join(sep);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// encode the key and add prefix if necessary
|
|
57
|
+
const getKey = (key: string, prefix = ''): string => {
|
|
58
|
+
const encodedKey = encodeURIComponent(key);
|
|
59
|
+
if (prefix) return `${prefix}[${encodedKey}]`;
|
|
60
|
+
return encodedKey;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// encode the key and value of a query object
|
|
64
|
+
const encode = (
|
|
65
|
+
key: string,
|
|
66
|
+
value: any,
|
|
67
|
+
{ eq = defaultEq, fn = defaultStringifyFunction, prefix = '' }
|
|
68
|
+
): string => {
|
|
69
|
+
const newValue = encodeURIComponent(fn(key, value));
|
|
70
|
+
const newKey = getKey(key, prefix);
|
|
71
|
+
|
|
72
|
+
return [newKey, newValue].join(eq);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// check if the given value is an object
|
|
76
|
+
function isObject(obj: QueryObject) {
|
|
77
|
+
const type = typeof obj;
|
|
78
|
+
return (obj && (type === 'object' || type === 'function')) || false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export default stringify;
|