-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
37 lines (33 loc) · 946 Bytes
/
index.js
File metadata and controls
37 lines (33 loc) · 946 Bytes
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
const axios = require('axios').default
class Book {
constructor (id, title, authors, description, publisher, publishedDate, thumbnailURL, totalItems) {
this.id = id
this.title = title
this.authors = authors
this.description = description
this.publisher = publisher
this.publishedDate = publishedDate
this.thumbnailURL = thumbnailURL
this.totalItems = totalItems
}
}
export async function getBook (name) {
const url = 'https://www.googleapis.com/books/v1/volumes'
const params = { q: name, maxResults: 1 }
const res = await axios.get(url, { params })
if (res.data.items === undefined) {
return 1
}
const data = res.data.items[0]
const volumeInfo = data.volumeInfo
return new Book(
data.id,
volumeInfo.title,
volumeInfo.authors,
volumeInfo.description,
volumeInfo.publisher,
volumeInfo.publishedDate,
volumeInfo.imageLinks.thumbnail,
data.totalItems
)
}