Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axios wrapper #1

Open
nimatrengo opened this issue May 10, 2024 · 0 comments
Open

Axios wrapper #1

nimatrengo opened this issue May 10, 2024 · 0 comments

Comments

@nimatrengo
Copy link

nimatrengo commented May 10, 2024

It's always better to have a wrapper of axios, you can call it request method, which is calling axios inside your code.

A simple example of mentioned method:

export function request<T>(
  url: string,
  method: 'POST' | 'GET' | 'PUT' | 'PATCH' | 'DELETE',
  data: unknown = {},
  headers: unknown = {},
  params: unknown = {}
): AxiosPromise<T> {
  return axios({
    method,
    url,
    data,
    headers,
    params,
  });
}

In the future, you can replace your axios method with anything else such as GraphQL, fetch, and etc... easily

try {
const response = await axios.get(url);
const moviesData = response.data.data;
setMovies((prevMovies) => [...prevMovies, ...moviesData]);
setPage((prevPage) => prevPage + 1);
} catch (error) {
console.error("Error fetching movies:", error);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant