[miniProjects] 28_Github Profiles

보리·2023λ…„ 6μ›” 16일
0

miniProjects

λͺ©λ‘ 보기
28/47

28_Github Profiles

πŸ’» 주제 : κΉƒν—ˆλΈŒ API둜 μ‚¬μš©μžλ₯Ό λΆˆλŸ¬μ™€ μ‚¬μš©μžλ₯Ό 검색해 ν”„λ‘œν•„ 데이터λ₯Ό μ–»μ–΄ μΉ΄λ“œμ— ν‘œμ‹œν•¨. λ˜ν•œ, μ‚¬μš©μžμ˜ μ΅œμ‹  리포지토리 5개λ₯Ό λ³΄μ—¬μ€Œ.

const APIURL = 'https://api.github.com/users/'

const main = document.getElementById('main')
const form = document.getElementById('form')
const search = document.getElementById('search')

async function getUser(username) {
    try {
        const { data } = await axios(APIURL + username)

        createUserCard(data)
        getRepos(username)
    } catch(err) {
        if(err.response.status == 404) {
            createErrorCard('이 이름을 가진 μ‚¬μš©μžκ°€ μ—†μŠ΅λ‹ˆλ‹€.')
        }
    }
}

async function getRepos(username) {
    try {
        const { data } = await axios(APIURL + username + '/repos?sort=created')

        addReposToCard(data)
    } catch(err) {
        createErrorCard('Problem fetching repos')
    }
}

function createUserCard(user) {
    const userID = user.name || user.login
    const userBio = user.bio ? `<p>${user.bio}</p>` : ''
    const cardHTML = `
    <div class="card">
    <div>
      <img src="${user.avatar_url}" alt="${user.name}" class="avatar">
    </div>
    <div class="user-info">
      <h2>${userID}</h2>
      ${userBio}
      <ul>
        <li>${user.followers} <strong>Followers</strong></li>
        <li>${user.following} <strong>Following</strong></li>
        <li>${user.public_repos} <strong>Repos</strong></li>
      </ul>

      <div id="repos"></div>
    </div>
  </div>
    `
    main.innerHTML = cardHTML
    
}

function createErrorCard(msg) {
    const cardHTML = `
        <div class="card">
            <h1>${msg}</h1>
        </div>
    `

    main.innerHTML = cardHTML
}

function addReposToCard(repos) {
    const reposEl = document.getElementById('repos')

    repos
        .slice(0, 5)
        .forEach(repo => {
            const repoEl = document.createElement('a')
            repoEl.classList.add('repo')
            repoEl.href = repo.html_url
            repoEl.target = '_blank'
            repoEl.innerText = repo.name

            reposEl.appendChild(repoEl)
        })
}

form.addEventListener('submit', (e) => {
    e.preventDefault()

    const user = search.value

    if(user) {
        getUser(user)

        search.value = ''
    }
})
  • APIURL λ³€μˆ˜λŠ” GitHub API의 κΈ°λ³Έ URL을 μ €μž₯ν•œλ‹€.
  • getUser ν•¨μˆ˜λŠ” μ‚¬μš©μž 이름을 인수둜 λ°›μ•„ ν•΄λ‹Ή μ‚¬μš©μžμ˜ ν”„λ‘œν•„ 정보λ₯Ό κ°€μ Έμ˜€λŠ” 역할을 ν•œλ‹€. axiosλ₯Ό μ‚¬μš©ν•˜μ—¬ API μš”μ²­μ„ 보내고, κ°€μ Έμ˜¨ 데이터λ₯Ό createUserCard ν•¨μˆ˜μ— μ „λ‹¬ν•˜μ—¬ μ‚¬μš©μž μΉ΄λ“œλ₯Ό μƒμ„±ν•œλ‹€. λ˜ν•œ, getRepos ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜μ—¬ μ‚¬μš©μžμ˜ λ ˆν¬μ§€ν† λ¦¬ 정보λ₯Ό κ°€μ Έμ˜¨λ‹€. μ—λŸ¬ 처리λ₯Ό μœ„ν•΄ try-catch 문을 μ‚¬μš©ν•˜κ³ , 404 μƒνƒœ μ½”λ“œμΌ κ²½μš°μ—λŠ” "이 이름을 가진 μ‚¬μš©μžκ°€ μ—†μŠ΅λ‹ˆλ‹€." 였λ₯˜ λ©”μ‹œμ§€λ₯Ό μƒμ„±ν•œλ‹€.
  • getRepos ν•¨μˆ˜λŠ” μ‚¬μš©μž 이름을 인수둜 λ°›μ•„ ν•΄λ‹Ή μ‚¬μš©μžμ˜ λ ˆν¬μ§€ν† λ¦¬ 정보λ₯Ό κ°€μ Έμ˜€λŠ” 역할을 ν•œλ‹€. axiosλ₯Ό μ‚¬μš©ν•˜μ—¬ API μš”μ²­μ„ 보내고, κ°€μ Έμ˜¨ 데이터λ₯Ό addReposToCard ν•¨μˆ˜μ— μ „λ‹¬ν•˜μ—¬ λ ˆν¬μ§€ν† λ¦¬ μΉ΄λ“œλ₯Ό μƒμ„±ν•œλ‹€. μ—λŸ¬ 처리λ₯Ό μœ„ν•΄ try-catch 문을 μ‚¬μš©ν•˜κ³ , μ—λŸ¬ λ°œμƒ μ‹œ "Problem fetching repos" 였λ₯˜ λ©”μ‹œμ§€λ₯Ό μƒμ„±ν•œλ‹€.
  • createUserCard ν•¨μˆ˜λŠ” 주어진 μ‚¬μš©μž 정보λ₯Ό 기반으둜 μ‚¬μš©μž μΉ΄λ“œλ₯Ό HTML ν˜•μ‹μœΌλ‘œ μƒμ„±ν•˜μ—¬ main μš”μ†Œμ— μ‚½μž…ν•œλ‹€.
  • addReposToCard ν•¨μˆ˜λŠ” 주어진 λ ˆν¬μ§€ν† λ¦¬ 배열을 μ‚¬μš©ν•˜μ—¬ λ ˆν¬μ§€ν† λ¦¬ 링크λ₯Ό μ‚¬μš©μž μΉ΄λ“œμ— μΆ”κ°€ν•œλ‹€.
  • createErrorCard ν•¨μˆ˜λŠ” 주어진 λ©”μ‹œμ§€λ₯Ό μ‚¬μš©ν•˜μ—¬ μ—λŸ¬ μΉ΄λ“œλ₯Ό μƒμ„±ν•˜μ—¬ main μš”μ†Œμ— μ‚½μž…ν•œλ‹€.
  • form μš”μ†Œμ˜ submit μ΄λ²€νŠΈμ— λŒ€ν•œ 이벀트 λ¦¬μŠ€λ„ˆκ°€ λ“±λ‘λ˜μ–΄ 있으며, μ‚¬μš©μžκ°€ 검색 폼을 μ œμΆœν•˜λ©΄ μž…λ ₯된 μ‚¬μš©μž μ΄λ¦„μœΌλ‘œ getUser ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜μ—¬ μ‚¬μš©μž 정보λ₯Ό κ²€μƒ‰ν•œλ‹€. 검색 폼이 제좜된 ν›„μ—λŠ” 검색 ν•„λ“œκ°€ μ΄ˆκΈ°ν™”λœλ‹€.
πŸ‘πŸ» κΉƒν—ˆλΈŒ μ‚¬μš©μž API

https://api.github.com/users/ 뒀에 검색 창에 μž…λ ₯된 μ‚¬μš©μž IDκ°€ 듀어감
μ•„λž˜ μ΄λ―Έμ§€λŠ” λ‚˜μ˜ κΉƒν—ˆλΈŒ idλ₯Ό μž…λ ₯ν–ˆμ„ λ•Œ λ‚˜μ˜€λŠ” μ •λ³΄μž„.

profile
μ •μ‹ μ°¨λ € 이 κ°λ°•ν•œ μ„Έμƒμ†μ—μ„œ

0개의 λŒ“κΈ€