<h1>GitHub File API</h1>

<p>Check the JavaScript file to have a look at the <code>code</code>.</p>
let fileSHA, fileBlob, fileContent, file

const getFileSHA = async () => {
  try {
    const response = await fetch(
      "https://api.github.com/repos/hackernoon/where-startups-trend/contents/2021/"
    );
    const data = await response.json();
    // console.log(data);
    
    fileSHA = data[1].sha
    console.log(fileSHA);

  } catch (error) {
    console.log(error);
  }
  
  getFileBlob()
}

const getFileBlob = async (fileSHA)=> {
  try {
  const response = await fetch(
    `https://api.github.com/repos/hackernoon/where-startups-trend/git/blobs/a51a49dfc2bd7be262bd59bb85e85271ea0c18cd`
  );
  const data = await response.json();
    
  fileBlob = data.content
  convertBlob(fileBlob)
  } catch (error) {
    console.log(error);
  }
}

const convertBlob = async blob => {
  // console.log(blob)
  try {
    
    // const fileContents = Buffer.from(blob, "base64").toString()
    // file = JSON.parse(fileContents)
    // file = JSON.parse(fileContents)
  
    fileContents = base64EncodeUnicode(blob)
    file = JSON.parse(fileContents)
    console.log(file)
    
  } catch(error) {
    console.log(error)
  }
}

function base64EncodeUnicode(str) {
  utf8Bytes = decodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
    return String.fromCharCode('0x' + p1);
  });

  return atob(utf8Bytes);
}


getFileSHA()

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.