use-api-vue
use-api-vue
Typescript functions to work with AJAX requests and vue-query.
Install
npm i use-api-vue
1. Configure Provider
Add useApiProvider to the main component (example here). And configure the global variables:
- extraPostData: To add extra data in each
POSTrequest. - onError: To execute a function in each exception.
- getError: Convert exceptions to your custom exception format.
- requestConfig: AxiosRequestConfig
Using with Vue Query
This package can be used with vue-query.
import { usePost, ApiResponse } from 'use-api-vue';
const get = useGet({ onCancelCallback: onUnmounted });
const result = useQuery<ApiResponse<PostProps[]>, ErrorFormat>(
'posts',
() => get<PostProps[]>('/posts');
);
// ErrorFormat is defined in `getError` inside the `useApiProvider`
Use hooks like
The params data, error, loading are reactive. send function is not reactive.
import { useGetRef } from 'use-api-vue';
const get = useGet({ onCancelCallback: onUnmounted });
const { data, error, loading, send } = useGetRef<PostProps[], ErrorFormat>(
{ onCancelCallback: onUnmounted },
);
send<PostsParams>('/posts', { category: [1,2,3] }); // It will send a request to the server
return {
data,
error,
loading,
send,
};
Related Packages
vue-axios-cors
This is http client vue.js plugin for cross origin access without preflight. It...
14,521
5
vue-api-request
Control your API calls by using an amazing component which supports axios and vu...
4,076
125
Version History
| Version | Released | Vue | License |
|---|---|---|---|
| 0.0.15 | ^3.2.41 | MIT | |
| 0.0.14 | ^3.2.41 | MIT | |
| 0.0.13 | ^3.2.41 | MIT | |
| 0.0.12 | ^3.2.41 | MIT | |
| 0.0.11 | ^3.2.33 || | MIT | |
| 0.0.10 | ^3.2.33 || | MIT | |
| 0.0.9 | ^3.2.29 || | MIT | |
| 0.0.8 | ^3.2.29 || | MIT | |
| 0.0.7 | ^3.2.29 || | MIT | |
| 0.0.6 | ^3.2.29 | MIT |