-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHadithApi.js
62 lines (49 loc) · 1.52 KB
/
HadithApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const { RESTDataSource } = require("apollo-datasource-rest");
class HadithApi extends RESTDataSource {
constructor() {
super();
this.baseURL = "https://api.sunnah.com/v1/";
}
willSendRequest(request) {
request.headers.set("x-api-key", process.env.X_API_KEY);
}
async getCollections() {
return this.get("collections");
}
async getCollection(name) {
return this.get(`collections/${name}`);
}
async getChapters(collectionName, bookNumber) {
return this.get(
`collections/${collectionName}/books/${bookNumber}/chapters`
);
}
async getHadithsByCollectionAndBookNumber(collectionName, bookNumber) {
return this.get(
`collections/${collectionName}/books/${bookNumber}/hadiths`
);
}
async getBook(collectionName, bookNumber) {
return this.get(`collections/${collectionName}/books/${bookNumber}`);
}
async getBooks(collectionName) {
return this.get(`collections/${collectionName}/books`);
}
async getHadithByCollectionAndHadithNumber(collectionName, hadithNumber) {
return this.get(`collections/${collectionName}/hadiths/${hadithNumber}`);
}
async getRandomHadith() {
return this.get("hadiths/random");
}
async getHadiths(queryParams) {
const searchParams = new URLSearchParams();
for (const key in queryParams) {
searchParams.append(key, queryParams[key]);
}
return this.get("hadiths?" + searchParams.toString());
}
async getHadithByUrn(urn) {
return this.get(`hadiths/${urn}`);
}
}
module.exports = HadithApi;