Created
March 19, 2020 11:21
-
-
Save smokeyfro/5d035a83469072e2612b4ed720284e7e to your computer and use it in GitHub Desktop.
Create a collection of books based on a list of isbns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 isbn of allBooks) { | |
| const res = await axios.get(`http://openlibrary.org/api/books?bibkeys=isbn:${isbn}&format=json&jscmd=data`) | |
| if (res.data[`isbn:${isbn}`]) { | |
| books.push(res.data[ | |
| { | |
| `isbn:${isbn}`, | |
| `status:${status}`, | |
| `review:${review}` | |
| } | |
| ]) | |
| } | |
| } | |
| const contentType = store.addCollection({ | |
| typeName: 'BookEntry', | |
| 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