Created
January 5, 2022 08:50
-
-
Save RomneyDa/94f9ce55e6942bd8d7afbcb203068d1f to your computer and use it in GitHub Desktop.
Random-data-api.com simple fetch function for getting a certain number of items from any of their apis. See https://random-data-api.com/documentation
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
// Generic data getter from random-data-api.com/api | |
const getRandomDataAPI = async (resource, numItems) => { | |
const api_url = `https://random-data-api.com/api/${resource}?size=${numItems}` | |
const response = await fetch(api_url) | |
const data = await response.json(); | |
return data; | |
} | |
// Example - Random beer | |
const getBeers = async (numBeers) => { | |
const beers = await getRandomDataAPI("beer/random_beer", numBeers) | |
// Map to a beer object with an ID, name, and description | |
return beers.map((beer) => { | |
return { | |
id: beer.id, | |
name: beer.name, | |
description: `${beer.brand} ${beer.style}, ${beer.alcohol} alcohol. Hop: ${beer.hop}. Yeast: ${beer.yeast}. Malts: ${beer.malts}` | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment