-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
78 lines (69 loc) · 1.96 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import SpeechKit from '@mastashake08/speech-kit'
export default class Gutendex {
/*
* Returns a new instance of Gutendex.
* @params {string} baseUrl - the base URL for gutendex API. Defaults to cloud instance
* @returns {Gutendex} - a new instance of Gutendex
*/
constructor (baseUrl = 'https://gutendex.com/books') {
this.baseUrl = baseUrl
}
/*
* Returns a new book by ID from Gutenberg database.
* @params {string} baseUrl - the base URL for gutendex API. Defaults to cloud instance
* @returns {object} - ebook metadata JSON object
*/
async getBook (id) {
try {
const res = await fetch(this.baseUrl + `/${id}`)
return await res.json()
} catch (e) {
alert(`${e.name} - ${e.message}`)
}
}
/*
* Returns a collection of books by ID from Gutenberg database.
* @params {string} baseUrl - the base URL for gutendex API. Defaults to cloud instance
* @returns {array | null} - ebook metadata JSON collection
*/
async getBooksByIds (ids) {
try {
const res = await fetch(this.baseUrl + `?ids=${ids}`)
return await res.json()
} catch (e) {
alert(`${e.name} - ${e.message}`)
}
}
async getBooksByTopic (topic) {
try {
const res = await fetch(this.baseUrl + `?topic=${topic}`)
return await res.json()
} catch (e) {
alert(`${e.name} - ${e.message}`)
}
}
async getBooksByTerm (term) {
try {
const res = await fetch(this.baseUrl + `?search=${topic}`)
return await res.json()
} catch (e) {
alert(`${e.name} - ${e.message}`)
}
}
async getBooksByMimeType (type) {
try {
const res = await fetch(this.baseUrl + `?mime_type=${type}`)
return await res.json()
} catch (e) {
alert(`${e.name} - ${e.message}`)
}
}
async readBook (id) {
try {
const res = await fetch(this.baseUrl + `/${id}?mime_type=text%2F`)
return await res.json()
} catch (e) {
alert(`${e.name} - ${e.message}`)
}
}
}