const BASE = `https://dummyapi.io/data/api/` function get_collection(collection) { let request = new XMLHttpRequest(); request.open('GET', `${BASE}${collection}`); request.setRequestHeader('app-id', '5f76987a523f015e7c5ae49b'); request.onload = function () { let prefix = collection.includes('/') ? '' : `

${collection}s

`; document.getElementById(collection).innerHTML = ( prefix + JSON.parse(request.response).data.map( function (member) { let c = collection.slice(-4); let id = member.id; if (c == 'post') id += '/comment'; let call = `"get_member('${c}', '${id}')"`; if (c == 'post') return `
`; let repr = `${member.firstName} ${member.lastName}`; return `
`; }).join("\n")); } request.send(); } function user_format(r) { let l = r.location; return `

${r.title} ${r.firstName} ${r.lastName}

gender: ${r.gender}

email: ${r.email}

date of birth: ${r.dateOfBirth}

register date: ${r.registerDate}

phone: ${r.phone}

picture: ${r.picture}

location: ${l.street}, ${l.city}, ${l.state}, ${l.country}

` } function post_format(r) { return r.data.map(comment => `

${comment.message}

`).join("\n"); } function get_member(collection, member) { let request = new XMLHttpRequest(); console.log(`${BASE}${collection}/${member}`); request.open('GET', `${BASE}${collection}/${member}`); request.setRequestHeader('app-id', '5f76987a523f015e7c5ae49b'); request.onload = function () { let format = collection == 'user' ? user_format : post_format; document.getElementById(member).innerHTML = format( JSON.parse(request.response)); } request.send(); }