0.0.15 • Published 1 year ago

use-api-vue v0.0.15

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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 POST request.
  • 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,
  };
0.0.12

1 year ago

0.0.13

1 year ago

0.0.14

1 year ago

0.0.15

1 year ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago