@ventlio/tanstack-query 0.2.72 → 0.2.73
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 +73 -39
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
### This is
|
|
1
|
+
### This is a complementary library that should be used with @tanstack/react-query for API REQUESTS and more
|
|
2
2
|
|
|
3
3
|
## WHY THIS PACKAGE?
|
|
4
4
|
|
|
@@ -12,12 +12,25 @@ But we were not discouraged. So, we set out to find a solution which led to the
|
|
|
12
12
|
|
|
13
13
|
> Please note that this package is still being developed and may not function as expected. We are working to refine its implementation structure to meet a functional standard. The documentation may not align with the current implementation, so if you encounter any difficulties while setting up the package, please raise an issue in the GitHub repository. We appreciate your patience and understanding as we work to improve this package.
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
### Tasks
|
|
16
|
+
|
|
17
|
+
- [✅] Global settings for requests
|
|
18
|
+
- [✅] Requests context implementations
|
|
19
|
+
- [✅] Post, Get, Patch Requests
|
|
20
|
+
- [✅] Query key tracker to track dynamic query and help fetch query cache from any page
|
|
21
|
+
- [Not Completed] Persistent queries implementation
|
|
22
|
+
- [❌] Put request
|
|
23
|
+
- [❌] Generic return type (this is currently an issue if the API does not return object with the necessary properties required by the library)
|
|
24
|
+
- [❌] Server sent events
|
|
25
|
+
- [❌] Socket implementations
|
|
26
|
+
- [❌] Tests
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
16
29
|
|
|
17
30
|
> You must install @tanstack/react-query and axios first to use this package
|
|
18
31
|
|
|
19
32
|
```
|
|
20
|
-
|
|
33
|
+
yarn add @tanstack/react-query axios
|
|
21
34
|
```
|
|
22
35
|
|
|
23
36
|
After that install this package
|
|
@@ -26,16 +39,41 @@ After that install this package
|
|
|
26
39
|
$ npm install @ventlio/tanstack-query
|
|
27
40
|
```
|
|
28
41
|
|
|
42
|
+
OR
|
|
43
|
+
|
|
29
44
|
```
|
|
30
45
|
$ yarn add @ventlio/tanstack-query
|
|
31
46
|
```
|
|
32
47
|
|
|
48
|
+
## CURRENT RETURN TYPE
|
|
49
|
+
|
|
50
|
+
Currently the library return type expects data structure of the below schema, so depending on the API design,
|
|
51
|
+
you can reach out to the developer to implement the return type that follows the below schema.
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
export interface IRequestError {
|
|
55
|
+
statusCode: number;
|
|
56
|
+
message: string;
|
|
57
|
+
timeStamp: Date;
|
|
58
|
+
status: boolean;
|
|
59
|
+
data?: any;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface IRequestSuccess<T> {
|
|
63
|
+
statusCode: number;
|
|
64
|
+
message: string;
|
|
65
|
+
timeStamp: Date;
|
|
66
|
+
status: boolean;
|
|
67
|
+
data: T;
|
|
68
|
+
}
|
|
69
|
+
```
|
|
33
70
|
|
|
34
71
|
## Getting Started
|
|
35
72
|
|
|
36
73
|
Follow the below instructions to have the package running on your project
|
|
37
74
|
|
|
38
75
|
### Set the environment variables
|
|
76
|
+
|
|
39
77
|
```env
|
|
40
78
|
# For ReactJS
|
|
41
79
|
REACT_APP_API_URL='https://api.example.com'
|
|
@@ -46,6 +84,7 @@ NEXT_PUBLIC_API_URL='https://api.example.com'
|
|
|
46
84
|
NEXT_PUBLIC_API_TIMEOUT=300000
|
|
47
85
|
|
|
48
86
|
```
|
|
87
|
+
|
|
49
88
|
```js
|
|
50
89
|
import { QueryClient } from '@tanstack/react-query';
|
|
51
90
|
import { TanstackQueryConfig, bootstrapQueryRequest } from '@ventlio/tanstack-query';
|
|
@@ -56,6 +95,17 @@ const queryClient = new QueryClient();
|
|
|
56
95
|
// do this before adding the queryClient to QueryClientProvider
|
|
57
96
|
bootstrapQueryRequest(queryClient);
|
|
58
97
|
|
|
98
|
+
// recommended setup for mobile apps as the .env setup won't work
|
|
99
|
+
bootstrapQueryRequest(queryClient, {
|
|
100
|
+
context: 'app', // this is required to make the library switch to app context where necessary
|
|
101
|
+
environments: {
|
|
102
|
+
appBaseUrl: baseUrl,
|
|
103
|
+
appTimeout: 30000,
|
|
104
|
+
},
|
|
105
|
+
modelConfig: {
|
|
106
|
+
idColumn: 'id', // used for useQueryModel to uniquely identify query data in a collection/array instance
|
|
107
|
+
},
|
|
108
|
+
});
|
|
59
109
|
```
|
|
60
110
|
|
|
61
111
|
You can now use it in a QueryClientProvider
|
|
@@ -77,19 +127,17 @@ function App() {
|
|
|
77
127
|
Updating the configurations inside a component
|
|
78
128
|
|
|
79
129
|
```jsx
|
|
80
|
-
import {
|
|
81
|
-
useQueryBaseURL,
|
|
82
|
-
useQueryHeaders,
|
|
83
|
-
useQueryTimeout,
|
|
84
|
-
} from '@ventlio/tanstack-query';
|
|
130
|
+
import { useQueryBaseURL, useQueryHeaders, useQueryTimeout } from '@ventlio/tanstack-query';
|
|
85
131
|
|
|
86
132
|
function LoginPage() {
|
|
87
133
|
const { headers, setQueryHeaders } = useQueryHeaders();
|
|
88
|
-
|
|
134
|
+
const [authToken, setAuthToken] = useState();
|
|
89
135
|
useEffect(() => {
|
|
90
136
|
// after user has logged in successfully set the authorization header token
|
|
91
|
-
|
|
92
|
-
|
|
137
|
+
// this should also be done mostly in the layout that contains the authenticated views of the app
|
|
138
|
+
// for instance in AuthLayout, so that after login in the authToken can still be used to authenticate future request
|
|
139
|
+
// when user refreshes the page
|
|
140
|
+
setQueryHeaders({ Authorization: `Bearer ${authToken}` });
|
|
93
141
|
}, []);
|
|
94
142
|
|
|
95
143
|
return <>{/** codes */}</>;
|
|
@@ -133,27 +181,16 @@ The `useGetRequest` hook returns an object with the following properties:
|
|
|
133
181
|
import { useGetRequest } from '@ventlio/tanstack-query';
|
|
134
182
|
|
|
135
183
|
const MyComponent = () => {
|
|
136
|
-
const {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
page,
|
|
147
|
-
queryKey,
|
|
148
|
-
} = useGetRequest({
|
|
149
|
-
path: '/api/mydata',
|
|
150
|
-
load: true,
|
|
151
|
-
queryOptions: {
|
|
152
|
-
staleTime: 10000,
|
|
153
|
-
refetchOnWindowFocus: false,
|
|
154
|
-
},
|
|
155
|
-
keyTracker: 'mydata',
|
|
156
|
-
});
|
|
184
|
+
const { data, isLoading, isError, error, updatePath, nextPage, prevPage, get, gotoPage, page, queryKey } =
|
|
185
|
+
useGetRequest({
|
|
186
|
+
path: '/api/mydata',
|
|
187
|
+
load: true,
|
|
188
|
+
queryOptions: {
|
|
189
|
+
staleTime: 10000,
|
|
190
|
+
refetchOnWindowFocus: false,
|
|
191
|
+
},
|
|
192
|
+
keyTracker: 'mydata',
|
|
193
|
+
});
|
|
157
194
|
|
|
158
195
|
return (
|
|
159
196
|
<div>
|
|
@@ -262,11 +299,10 @@ import { usePatchRequest } from '@ventlio/tanstack-query';
|
|
|
262
299
|
|
|
263
300
|
function App() {
|
|
264
301
|
const { patch, isLoading, isError, isSuccess, data } =
|
|
265
|
-
usePatchRequest
|
|
266
|
-
User >
|
|
302
|
+
usePatchRequest<User>(
|
|
267
303
|
{
|
|
268
304
|
path: '/users/1',
|
|
269
|
-
};
|
|
305
|
+
});
|
|
270
306
|
|
|
271
307
|
const updateUser = async (user: User) => {
|
|
272
308
|
await patch(user);
|
|
@@ -419,8 +455,7 @@ import { useKeyTrackerModel } from '@ventlio/tanstack-query';
|
|
|
419
455
|
2. Call `useKeyTrackerModel` with a `keyTracker` parameter which is a string that uniquely identifies the query key:
|
|
420
456
|
|
|
421
457
|
```javascript
|
|
422
|
-
const { refetchQuery, getQueryKey, queryKey, data } =
|
|
423
|
-
useKeyTrackerModel < MyDataType > 'myKeyTracker';
|
|
458
|
+
const { refetchQuery, getQueryKey, queryKey, data } = useKeyTrackerModel < MyDataType > 'myKeyTracker';
|
|
424
459
|
```
|
|
425
460
|
|
|
426
461
|
3. Invoke `getQueryKey` function to retrieve the query key:
|
|
@@ -467,8 +502,7 @@ import { useKeyTrackerModel } from '@ventlio/tanstack-query';
|
|
|
467
502
|
const MyComponent = () => {
|
|
468
503
|
const queryClient = useQueryClient();
|
|
469
504
|
|
|
470
|
-
const { refetchQuery, getQueryKey, queryKey, data } =
|
|
471
|
-
useKeyTrackerModel < MyDataType > 'myKeyTracker';
|
|
505
|
+
const { refetchQuery, getQueryKey, queryKey, data } = useKeyTrackerModel < MyDataType > 'myKeyTracker';
|
|
472
506
|
|
|
473
507
|
const handleClick = async () => {
|
|
474
508
|
// Retrieve the query key
|