0

I am using this endpoint https://api.github.com/user/${githubIdOrLogin}. Right now, it has 60 API calls/hour. I want to increase that. How to do that. This is my code for the API call:

export const getGitHubUserData = async (githubIdOrLogin) => {
  return fetch(`https://api.github.com/user/${githubIdOrLogin}`, {
    headers: { Accept: "application/json" },
  }).then((res) => {
    if (!res.ok) {
      const err = new Error()
      err.response = res
      if (res.status === 403 && res.headers.get("X-RateLimit-Remaining") === "0") {
        const resetsAtMS = Number(`${res.headers.get("X-RateLimit-Reset")}000`)
        err.message = `Rate limit exceeded, try again in ${Math.ceil(
          (resetsAtMS - Date.now()) / 60000
        )}m`
        err.code = "github/rate-limit-exceeded"
        err.resetsAt = resetsAtMS
      } else if (res.status === 404) {
        err.message = `Could not find user data for github:${githubIdOrLogin}`
        err.code = "github/not-found"
      } else {
        // add other cases if you want to handle them
        err.message = `Unexpected status code: ${res.status}`
        err.code = "github/unknown"
      }
      return Promise.reject(err)
    }
    return res.json()
  })
}

Anyone please help me with this.

r121
  • 2,478
  • 8
  • 25
  • 44
  • I need to add an authentication token to increase an API limit somewhere in the code but I don't know how to do it. – r121 Nov 01 '21 at 05:53
  • Ok, did you generate a token? and what did you try to far to add it? – Evert Nov 01 '21 at 05:59
  • I generated a token from here: https://github.com/settings/tokens/new – r121 Nov 01 '21 at 06:16
  • and write the above code but I don't have any idea how to use that token. – r121 Nov 01 '21 at 06:20
  • Have you read this doc? https://docs.github.com/en/rest/overview/other-authentication-methods – Evert Nov 01 '21 at 06:32
  • One thing I want to know is if I pass Authorization: token TOKEN to the headers then will my token be visible on the client-side even after accessing the token from the .env file? – r121 Nov 01 '21 at 06:34
  • Yes, you should never let that token appear in a browser app, used by people you don't trust. – Evert Nov 01 '21 at 06:36
  • so, what should I do to hide this token? One way I know is to create a node server and then call the github API endpoint from my server. – r121 Nov 01 '21 at 06:38
  • You are correct, that's the normal way to handle this. – Evert Nov 01 '21 at 06:40
  • So, there is no way to do it from the frontend I have to write a server and then host it somewhere and then use that URL to call github API endpoint. – r121 Nov 01 '21 at 06:42
  • You might be able to do it with a more complex authentication flow, such as OAuth2, but I bet you will need a server at some point in the process. – Evert Nov 01 '21 at 06:56
  • so, I'll create a server. – r121 Nov 01 '21 at 07:19

0 Answers0