Skip to content

Instantly share code, notes, and snippets.

@smokeyfro
Created March 19, 2020 11:41
Show Gist options
  • Save smokeyfro/43765cbeaa1089723db9f9ec11d68524 to your computer and use it in GitHub Desktop.
Save smokeyfro/43765cbeaa1089723db9f9ec11d68524 to your computer and use it in GitHub Desktop.
Create a collection of books from an array of isbns
const axios = require('axios')
allBooks = [
{
isbn: "9781491997246",
status: "read",
review: "Will be applying this one to my Mzango build soon."
},
{
isbn: "9781484238042",
status: "read",
review: "This one helped me get my head around Vuex."
},
{
isbn: "9781617294624",
status: "unread",
review: ""
},
]
module.exports = function (api) {
api.loadSource(async store => {
let books = []
for (const book of allBooks) {
const res = await axios.get(`http://openlibrary.org/api/books?bibkeys=isbn:${book.isbn}&format=json&jscmd=data`)
if (res.data[`isbn:${book.isbn}`]) {
books.push(res.data[
`isbn:${book.isbn}`,
`status:${book.status}`,
`review:${book.review}`
])
}
}
const contentType = store.addCollection({
typeName: 'Book',
route: 'about/library/:title'
})
for (const item of books) {
contentType.addNode({
isbn: item.isbn,
title: item.title,
date: item.publish_date,
status: item.status,
review: item.review,
fields: {
...item
}
})
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment