Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
TEACHING-MATERIALS/exercises/09_api_basics/bookshop/async/modules/books.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
35 lines (28 sloc)
799 Bytes
This file contains 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
'use strict' | |
const fetch = require('node-fetch') | |
const errors = require('./errors') | |
module.exports = async function(q) { | |
const url = `https://www.googleapis.com/books/v1/volumes?q=${q}` | |
let result = null | |
try { | |
result = await fetch(url) | |
} catch (err) { | |
console.error(err) | |
throw new errors.ConnectionError() | |
} | |
if (result.status !== 200) { | |
console.error(result) | |
throw new errors.GatewayError() | |
} | |
const json = await result.json() | |
const items = json.items | |
const details = item => { | |
return { | |
id: item.id, | |
title: item.volumeInfo.title, | |
authors: item.volumeInfo.authors, | |
description: item.volumeInfo.description | |
} | |
} | |
return items.map(details) | |
} |