Created
July 8, 2018 14:45
-
-
Save jakubbarczyk/35ef08c1ad67a92344506db916c00619 to your computer and use it in GitHub Desktop.
Fetch all Dominaria cards from Magic: the Gathering API
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
'use-strict' | |
const api = 'https://api.magicthegathering.io/v1' | |
const dominariaPage1 = fetch(new Request(Resource(Page(1)))) | |
const dominariaPage2 = fetch(new Request(Resource(Page(2)))) | |
const dominariaPage3 = fetch(new Request(Resource(Page(3)))) | |
Promise | |
.all([dominariaPage1, dominariaPage2, dominariaPage3]) | |
.then((responses) => Promise.all(responses.map(toJSON))) | |
.then((pages) => pages.map(toCards)) | |
.then((cards) => cards.reduce(flatten)) | |
.then((cards) => cards.map(toTinyCard)) | |
.then(console.info) | |
.catch(console.error) | |
function Page(number = 1) { | |
return { page: number } | |
} | |
function Resource({ set = 'dom', page = 1 }) { | |
return `${api}/cards?set=${set}&page=${page}` | |
} | |
function toJSON(response) { | |
return response.json() | |
} | |
function toCards({ cards = [] }) { | |
return cards | |
} | |
function flatten(prev, next) { | |
return prev.concat(next) | |
} | |
function toTinyCard({ number, name, rarity }) { | |
return { number: Number(number), name, rarity } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment