Skip to content

API request improvements #5

@yashsehgal

Description

@yashsehgal

As of the now, the number of API fetches happening inside the hook for all the methods i.e. userInfo, getRepositories and their respective sub-methods - are really high.

I have some ideas-as-solution to this issue, which will help reduce the API calls and improve overall dependency for all methods.

  1. One function to rule them all: Let's create a single function for all our API calls.
const githubRequest = async (endpoint, options = {}) => {
  // Code for all the API calls
};
  1. We're calling the API a lot. Let's cache some responses - Caching can be a good option for solve this issue.
const cachedData = {};

const getCachedOrFetch = async (key, fetchFn) => {
  if (cachedData[key]) return cachedData[key];
  const data = await fetchFn();
  cachedData[key] = data;
  return data;
};
  1. Sometimes GitHub might be slow - We can have timeouts to avoid delayed responses and UI renders.
const timeoutPromise = (ms) => new Promise((_, reject) => {
  setTimeout(() => reject(new Error('Request timed out')), ms);
});

const githubRequest = async (endpoint, options = {}) => {
  return Promise.race([
    actualRequest(endpoint, options),
    timeoutPromise(5000)  // 5 second timeout
  ]);
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions