question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow useAxios without an url

See original GitHub issue

Clear and concise description of the problem

I would like to be able to call useAxios without an url, it would be great if the following method signatures could be supported:

useAxios() // implicit immediate=false or pass the option { immediate: false }
useAxios(AxiosInstance) // also immediate=false
useAxios(AxiosRequestConfig)
useAxios(AxiosRequestConfig, AxiosInstance)

With useAxios(AxiosInstance) it would be easier to extend useAxios with a shared config:

export const axiosWithToken = axios.create({
  baseURL: config.baseURL,
  timeout: config.timeout,
});

axiosWithToken.interceptors.request.use(async (axiosConfig) => {
  // ....

  axiosConfig.headers.Authorization = `Bearer ${response.accessToken}`;

  return axiosConfig;
});

// Import and use this version of useAxios for protected APIs
export function useTokenAxios(config?: AxiosRequestConfig) {
  return useAxios(config, axiosWithToken);
}

I would like to keep all my endpoint configuration in the same place:

export const bookEndpoints = {
  getAll(): AxiosRequestConfig {
    return { url: 'books' };
  },
  getPaged(pageSize: number, pagenumber: number): AxiosRequestConfig {
    return { url: 'books', params: { pageSize, pagenumber } };
  },
  getSingle(id: string): AxiosRequestConfig {
    return { url: `books/${encodeURIComponent(id)}`, transformResponse: transformData };
  },
  create(objData: unknown): AxiosRequestConfig {
    return { url: 'books', method: 'POST', data: objData };
  },
  update(id: string, objData: unknown): AxiosRequestConfig {
    return { url: `books/${encodeURIComponent(id)}`, method: 'PUT', data: objData };
  },
  delete(id: string): AxiosRequestConfig {
    return { url: `books/${encodeURIComponent(id)}`, method: 'DELETE' };
  },
};

API usage:

// create, update, delete or something else that is not going to be executed in setup
const { execute } = useAxios() // implicit immediate=false or pass the option { immediate: false }

// execute the request some time in the future when the user has updated the data
execute(bookEndpoints.update('aBzw', myUpdatedBook))

// others executed immediately
useAxios(bookEndpoints.getAll())
// or
useAxios(bookEndpoints.getAll(), axiosWithToken)
// or 
useTokenAxios(bookEndpoints.getAll())

Suggested solution

useAxios could allow the new method signatures or a new method could be exposed without the url. E.g.

useAxios(url : string) {
  return useAxiosWithoutUrl( { url: url } );
}

Alternative

No response

Additional context

Since useAxios uses axios(url, config) it is not possible to override the url using the config.

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
hengshanMWCcommented, Mar 9, 2022

@aalsamoht Maybe I can try sending a pr

0reactions
hengshanMWCcommented, Mar 11, 2022

@aalsamoht Functions have been merged

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to use axios get request with proxy without adding to the ...
You're missing a '/' in the url. Try with axios.get(/api/user/${user.id} instead of axios.get(api/user/${user.id}.
Read more >
simoneb/axios-hooks: React hooks for axios - GitHub
useAxios (url|config, options). The main React hook to execute HTTP requests. url|config - The request URL or config object, the same argument accepted...
Read more >
useAxios : A simple custom hook for calling APIs using axios
Now, let's dive in and create our custom hook, step by step! 1. Simple API call from the component. To create this example,...
Read more >
useAxios | VueUse
import { useAxios } from '@vueuse/integrations/useAxios' const { data, ... The execute function url here is optional, and url2 will replace the url1...
Read more >
useAxios: A React Hook for using Axios | by Harsha Vardhan
Make an API call in the component. Generally, all the APIs of an application have the same base URL. Here let's set up...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found