Allow useAxios without an url
See original GitHub issueClear 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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that request the same feature to avoid creating a duplicate.
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@aalsamoht Maybe I can try sending a pr
@aalsamoht Functions have been merged