@ventlio/tanstack-query 0.2.10 → 0.2.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
### This is not a replacement for @tanstack/react-query
|
|
2
2
|
|
|
3
3
|
## WHY THIS PACKAGE?
|
|
4
4
|
|
|
@@ -6,7 +6,7 @@ We have been working on a project using ReactJS and React-Native and we were hap
|
|
|
6
6
|
|
|
7
7
|
However, we still faced some challenges. For each project, we have to configure how Tanstack-Query would connect to the Backend API and manage GET, POST, PUT, and PATCH requests efficiently. One of the major challenges was handling dynamic queryKeys in Tanstack-Query. For example, when building a screen with pagination, useQuery will have re-call the queryFunction every time the key changed, which will load the current page data. This meant that the queryKey changed dynamically, and sometimes we needed to use the queryKey of the current page to perform certain tasks like updating a specific row in the view. This was always tedious.
|
|
8
8
|
|
|
9
|
-
But we were not discouraged.
|
|
9
|
+
But we were not discouraged. So, we set out to find a solution which led to the development of this package. This package would handle dynamic queryKeys in Tanstack-Query and solve other related problems. It made configuring Tanstack-Query to connect to the Backend API and managing GET, POST, PUT, and PATCH requests a breeze. It also solved the problem of dynamic queryKeys, making it much easier to update specific rows in the view.
|
|
10
10
|
|
|
11
11
|
---
|
|
12
12
|
|
|
@@ -31,3 +31,130 @@ $ yarn add @ventlio/tanstack-query
|
|
|
31
31
|
## Getting Started
|
|
32
32
|
|
|
33
33
|
Follow the below instructions to have the package running on your project
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import { QueryClient } from '@tanstack/react-query';
|
|
37
|
+
import { TanstackQueryConfig } from '@ventlio/tanstack-query';
|
|
38
|
+
|
|
39
|
+
// Global queryClient
|
|
40
|
+
const queryClient = new QueryClient();
|
|
41
|
+
|
|
42
|
+
// do this before adding the queryClient to QueryClientProvider
|
|
43
|
+
queryClient.setQueryData <
|
|
44
|
+
TanstackQueryConfig >
|
|
45
|
+
(['config'],
|
|
46
|
+
{
|
|
47
|
+
baseURL: 'https://pokeapi.co/api/v2',
|
|
48
|
+
timeout: 10000,
|
|
49
|
+
headers: {
|
|
50
|
+
Authorization: `Bearer Hello`,
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
You can now use it in a QueryClientProvider
|
|
56
|
+
|
|
57
|
+
```jsx
|
|
58
|
+
import { QueryClientProvider } from '@tanstack/react-query';
|
|
59
|
+
import { queryClient } from './queryClient';
|
|
60
|
+
|
|
61
|
+
function App() {
|
|
62
|
+
return (
|
|
63
|
+
<QueryClientProvider client={queryClient}>
|
|
64
|
+
<HomePage />
|
|
65
|
+
<About />
|
|
66
|
+
</QueryClientProvider>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Updating the configurations inside a component
|
|
72
|
+
|
|
73
|
+
```jsx
|
|
74
|
+
import {
|
|
75
|
+
useQueryBaseURL,
|
|
76
|
+
useQueryHeaders,
|
|
77
|
+
useQueryTimeout,
|
|
78
|
+
} from '@ventlio/tanstack-query';
|
|
79
|
+
|
|
80
|
+
function LoginPage() {
|
|
81
|
+
const { baseURL, setQueryBaseUrl } = useQueryBaseURL();
|
|
82
|
+
const { headers, setQueryHeaders } = useQueryHeaders();
|
|
83
|
+
const { timeout, setQueryTimeout } = useQueryTimeout();
|
|
84
|
+
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
// after user has logged in successfully set the authorization header token
|
|
87
|
+
headers.Authorization = 'Bearer token'; // this will be used for subsequent queries
|
|
88
|
+
setQueryHeaders(headers);
|
|
89
|
+
}, []);
|
|
90
|
+
|
|
91
|
+
return <>{/** codes */}</>;
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Hooks
|
|
96
|
+
|
|
97
|
+
- `useQueryBaseURL()`
|
|
98
|
+
return
|
|
99
|
+
|
|
100
|
+
- `baseURL: string`; the global baseURL
|
|
101
|
+
- `setBaseURL(url: string)`; update the global baseURL
|
|
102
|
+
|
|
103
|
+
- `useQueryHeaders()`
|
|
104
|
+
return
|
|
105
|
+
|
|
106
|
+
- `headers: RawAxiosRequestHeaders`; the global request headers
|
|
107
|
+
- `setHeaders(headers: RawAxiosRequestHeaders)`; update the global request headers
|
|
108
|
+
|
|
109
|
+
- `useGetRequest<T>(config: { path, load, queryOptions })`
|
|
110
|
+
|
|
111
|
+
- params
|
|
112
|
+
- config
|
|
113
|
+
- `path: string`: request path
|
|
114
|
+
- `load: boolean`: if true the request starts immediately otherwise it will wait until it is triggered to true, defaults to false
|
|
115
|
+
- `queryOptions: TanstackQueryOption<TResponse>`: query options
|
|
116
|
+
- `keyTracker`: query key tracker for dynamic queries
|
|
117
|
+
|
|
118
|
+
- `usePostRequest<T>(config: { path, isFormData })`
|
|
119
|
+
|
|
120
|
+
- params
|
|
121
|
+
- config
|
|
122
|
+
- `path: string`: request path
|
|
123
|
+
- `isFormData: boolean`: set it to true if you want to upload file with the post request
|
|
124
|
+
|
|
125
|
+
- `useDeleteRequest<T>()`
|
|
126
|
+
|
|
127
|
+
- params
|
|
128
|
+
- no params
|
|
129
|
+
|
|
130
|
+
- `usePatchRequest<T>({ path })`
|
|
131
|
+
- params
|
|
132
|
+
- `path : string`: path to the request to be deleted
|
|
133
|
+
|
|
134
|
+
## Examples
|
|
135
|
+
|
|
136
|
+
Make fetch Request
|
|
137
|
+
|
|
138
|
+
```jsx
|
|
139
|
+
import { useGetRequest } from '@ventlio/tanstack-query';
|
|
140
|
+
|
|
141
|
+
function PostPage() {
|
|
142
|
+
const { isLoading, data, isInitialLoading, get } = useGetRequest({
|
|
143
|
+
path: '/posts/1',
|
|
144
|
+
load: true,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
if (isLoading || isInitialLoading || !data) {
|
|
148
|
+
return <LoadingSpinner />;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return (
|
|
152
|
+
<div>
|
|
153
|
+
<h1> {data.data.title} </h1>
|
|
154
|
+
<article> {data.data.description} </article>
|
|
155
|
+
</div>
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## We welcome any contribution that will help the project
|
package/dist/index.mjs
CHANGED
|
@@ -321,7 +321,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, }) => {
|
|
|
321
321
|
};
|
|
322
322
|
};
|
|
323
323
|
|
|
324
|
-
const usePatchRequest = ({ path
|
|
324
|
+
const usePatchRequest = ({ path }) => {
|
|
325
325
|
const { headers, baseURL, timeout } = useQueryConfig();
|
|
326
326
|
// register post mutation
|
|
327
327
|
const mutation = useMutation((postData) => new Promise((res, rej) => {
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
2
|
import type { IRequestError, IRequestSuccess } from '../request/request.interface';
|
|
3
|
-
export declare const usePatchRequest: <TResponse>({ path
|
|
3
|
+
export declare const usePatchRequest: <TResponse>({ path }: {
|
|
4
4
|
path: string;
|
|
5
|
-
isFormData?: boolean | undefined;
|
|
6
5
|
}) => {
|
|
7
6
|
data: undefined;
|
|
8
7
|
error: null;
|
|
@@ -7,7 +7,7 @@ var makeRequest = require('../request/make-request.js');
|
|
|
7
7
|
var request_enum = require('../request/request.enum.js');
|
|
8
8
|
var useQueryConfig = require('../config/useQueryConfig.js');
|
|
9
9
|
|
|
10
|
-
const usePatchRequest = ({ path
|
|
10
|
+
const usePatchRequest = ({ path }) => {
|
|
11
11
|
const { headers, baseURL, timeout } = useQueryConfig.useQueryConfig();
|
|
12
12
|
// register post mutation
|
|
13
13
|
const mutation = reactQuery.useMutation((postData) => new Promise((res, rej) => {
|
package/package.json
CHANGED
|
@@ -9,12 +9,7 @@ import type {
|
|
|
9
9
|
IRequestSuccess,
|
|
10
10
|
} from '../request/request.interface';
|
|
11
11
|
|
|
12
|
-
export const usePatchRequest = <TResponse>({
|
|
13
|
-
path,
|
|
14
|
-
}: {
|
|
15
|
-
path: string;
|
|
16
|
-
isFormData?: boolean;
|
|
17
|
-
}) => {
|
|
12
|
+
export const usePatchRequest = <TResponse>({ path }: { path: string }) => {
|
|
18
13
|
const { headers, baseURL, timeout } = useQueryConfig();
|
|
19
14
|
|
|
20
15
|
// register post mutation
|