@tramvai/react-query 1.46.10 → 1.48.2
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 +102 -20
- package/lib/baseQuery/types.d.ts +5 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.es.js +59 -5
- package/lib/index.js +61 -3
- package/lib/infiniteQuery/create.d.ts +0 -3
- package/lib/mutation/create.d.ts +0 -3
- package/lib/mutation/types.d.ts +3 -2
- package/lib/query/create.d.ts +0 -3
- package/lib/query/use.d.ts +2 -1
- package/package.json +6 -5
- package/README.en.md +0 -194
package/README.md
CHANGED
|
@@ -1,16 +1,67 @@
|
|
|
1
1
|
# React Query
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A library for handling requests in React components. Based on [react-query](https://react-query.tanstack.com/).
|
|
4
4
|
|
|
5
5
|
## Explanation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
For the library to work, the module [@tramvai/module-react-query](references/modules/react-query.md) must be added to the tramvai application
|
|
8
8
|
|
|
9
9
|
## Api
|
|
10
10
|
|
|
11
|
+
### Query
|
|
12
|
+
|
|
13
|
+
A wrapper around react-query options with tramvai integration.
|
|
14
|
+
|
|
15
|
+
#### fork
|
|
16
|
+
|
|
17
|
+
Create new `Query` from an existing query with option to override settings.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { createQuery } from '@tramvai/react-query';
|
|
21
|
+
|
|
22
|
+
const query = createQuery();
|
|
23
|
+
const newQuery = query.fork({
|
|
24
|
+
refetchInterval: 2000,
|
|
25
|
+
refetchIntervalInBackground: false,
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
#### prefetchAction
|
|
30
|
+
|
|
31
|
+
Return a tramvai action that can be used to prefetch current query
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
export default function PageComponent() {
|
|
35
|
+
const { data, isLoading } = useQuery(query);
|
|
36
|
+
|
|
37
|
+
return <div>{isLoading ? 'loading...' : data}</div>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
Component.actions = [query.prefetchAction()];
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### fetchAction
|
|
44
|
+
|
|
45
|
+
Return a tramvai action that can be used to fetch current query
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
const action = createAction({
|
|
49
|
+
name: 'action',
|
|
50
|
+
fn: async (context) => {
|
|
51
|
+
const result = await context.executeAction(query.fetchAction());
|
|
52
|
+
|
|
53
|
+
console.log('__action__', result);
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
#### raw
|
|
59
|
+
|
|
60
|
+
Might be used when the raw query options is needed. The result can be passed to the underlying methods of `react-query` lib in cases when `@tramvai/react-query` doesn't provide appropriate wrapper. This method is used internally in the lib to redirect calls to `react-query`.
|
|
61
|
+
|
|
11
62
|
### createQuery
|
|
12
63
|
|
|
13
|
-
|
|
64
|
+
Allows you to create a `Query` object that can later be used in components using `useQuery`. Used to execute single data retrieval requests.
|
|
14
65
|
|
|
15
66
|
```ts
|
|
16
67
|
import { createQuery } from '@tramvai/react-query';
|
|
@@ -28,17 +79,16 @@ const query = createQuery({
|
|
|
28
79
|
});
|
|
29
80
|
```
|
|
30
81
|
|
|
31
|
-
####
|
|
82
|
+
#### Unique query parameters
|
|
32
83
|
|
|
33
|
-
|
|
34
|
-
подробнее об этом можно почитать в разделе официальной документации [Query Keys](https://react-query.tanstack.com/guides/query-keys)
|
|
84
|
+
To create a generic `query` that takes parameters for a query, you must return a unique `key`, you can read more about this in the official documentation section [Query Keys](https://react-query.tanstack.com/guides/query-keys)
|
|
35
85
|
|
|
36
|
-
|
|
86
|
+
As a parameter `key` you can use:
|
|
37
87
|
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
41
|
-
-
|
|
88
|
+
- a string, such as `key: 'query-name'`
|
|
89
|
+
- an array where any serializable data can be used as elements, for example `key: ['query-name', false, { bar: 'baz }]`
|
|
90
|
+
- a function that takes the parameters with which `query` is called and returns a string - `key: (options) => 'query-name'`
|
|
91
|
+
- a function that accepts parameters, with which `query` is called, and returns an array, where any serializable data can be used as elements - `key: (options) => ['query-name', options, { bar: 'baz' }]`
|
|
42
92
|
|
|
43
93
|
```ts
|
|
44
94
|
import { createQuery, useQuery } from '@tramvai/react-query';
|
|
@@ -64,9 +114,9 @@ export function Component({ id }) {
|
|
|
64
114
|
|
|
65
115
|
### useQuery
|
|
66
116
|
|
|
67
|
-
React
|
|
117
|
+
React hook for working with `Query` object
|
|
68
118
|
|
|
69
|
-
[
|
|
119
|
+
[react-query docs](https://react-query.tanstack.com/reference/useQuery)
|
|
70
120
|
|
|
71
121
|
```ts
|
|
72
122
|
import { useQuery } from '@tramvai/react-query';
|
|
@@ -78,9 +128,33 @@ export function Component() {
|
|
|
78
128
|
}
|
|
79
129
|
```
|
|
80
130
|
|
|
131
|
+
### useQueries
|
|
132
|
+
|
|
133
|
+
React Hook for working with the list of `Query` objects
|
|
134
|
+
|
|
135
|
+
[react-query docs](https://react-query.tanstack.com/reference/useQueries)
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { useQueries } from '@tramvai/react-query';
|
|
139
|
+
|
|
140
|
+
export function Component() {
|
|
141
|
+
const [
|
|
142
|
+
{ data: data1, isLoading: isLoading1 },
|
|
143
|
+
{ data: data2, isLoading: isLoading2 },
|
|
144
|
+
] = useQueries([query1, query2]);
|
|
145
|
+
|
|
146
|
+
return (
|
|
147
|
+
<div>
|
|
148
|
+
<div>{isLoading1 ? 'loading1...' : data1}</div>
|
|
149
|
+
<div>{isLoading2 ? 'loading2...' : data2}</div>
|
|
150
|
+
</div>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
81
155
|
### createInfiniteQuery
|
|
82
156
|
|
|
83
|
-
|
|
157
|
+
Creates an `InfiniteQuery` object that can later be used in components using `useInfiniteQuery`. It is used to execute queries to obtain a sequence of data that can be loaded as the component runs.
|
|
84
158
|
|
|
85
159
|
```ts
|
|
86
160
|
import { createInfiniteQuery } from '@tramvai/react-query';
|
|
@@ -108,9 +182,9 @@ const query = createInfiniteQuery({
|
|
|
108
182
|
|
|
109
183
|
### useInfiniteQuery
|
|
110
184
|
|
|
111
|
-
React
|
|
185
|
+
React hook for working with the `InfiniteQuery` object
|
|
112
186
|
|
|
113
|
-
[
|
|
187
|
+
[react-query docs](https://react-query.tanstack.com/reference/useInfiniteQuery)
|
|
114
188
|
|
|
115
189
|
```ts
|
|
116
190
|
import { useInfiniteQuery } from '@tramvai/react-query';
|
|
@@ -143,7 +217,7 @@ export function Component() {
|
|
|
143
217
|
|
|
144
218
|
### createMutation
|
|
145
219
|
|
|
146
|
-
|
|
220
|
+
Creates a `Mutation` object that can later be used in components using `useMutation`. Used to send and modify data in the api.
|
|
147
221
|
|
|
148
222
|
```ts
|
|
149
223
|
import { createMutation } from '@tramvai/react-query';
|
|
@@ -167,9 +241,9 @@ const mutation = createMutation({
|
|
|
167
241
|
|
|
168
242
|
### useMutation
|
|
169
243
|
|
|
170
|
-
React
|
|
244
|
+
React hook for working with the `Mutation` object
|
|
171
245
|
|
|
172
|
-
[
|
|
246
|
+
[react-query docs](https://react-query.tanstack.com/reference/useMutation)
|
|
173
247
|
|
|
174
248
|
```ts
|
|
175
249
|
import { useMutation } from '@tramvai/react-query';
|
|
@@ -191,4 +265,12 @@ export function Component() {
|
|
|
191
265
|
|
|
192
266
|
## How-to
|
|
193
267
|
|
|
194
|
-
[
|
|
268
|
+
[Examples of using @tramvai/react-query](how-to/react-query-usage.md)
|
|
269
|
+
|
|
270
|
+
### Use `react-query` directly
|
|
271
|
+
|
|
272
|
+
:::warning Prefer to use methods from the `@tramvai/react-query` as it is can work both with the `Query` wrapper and the query options to `react-query` itself :::
|
|
273
|
+
|
|
274
|
+
You can get [`QueryClient`](https://react-query.tanstack.com/reference/QueryClient) from di by token `QUERY_CLIENT_TOKEN` or using method `useQueryClient` in React-components.
|
|
275
|
+
|
|
276
|
+
To convert wrapped `Query` object to object acceptable by `react-query` use method [raw](#raw) of the `Query` instance.
|
package/lib/baseQuery/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Action, ActionConditionsParameters } from '@tramvai/core';
|
|
1
|
+
import type { Action, ActionConditionsParameters, ActionContext } from '@tramvai/core';
|
|
2
2
|
import type { QUERY_CLIENT_TOKEN } from '@tramvai/module-react-query';
|
|
3
3
|
import type { QueryKey as ReactQueryKey, QueryOptions } from 'react-query';
|
|
4
4
|
export declare const QUERY_PARAMETERS = "__query_parameters__";
|
|
@@ -12,8 +12,12 @@ export interface BaseCreateQueryOptions<Options, Deps> {
|
|
|
12
12
|
export interface BaseQuery<Options, TCreateQuery, TQuery, TUseQuery> {
|
|
13
13
|
[QUERY_PARAMETERS]: TCreateQuery;
|
|
14
14
|
fork(options: TUseQuery): TQuery;
|
|
15
|
+
raw(context: ActionContext, options?: Options): TUseQuery;
|
|
15
16
|
prefetchAction(options?: Options): Action<void, void, {
|
|
16
17
|
queryClient: typeof QUERY_CLIENT_TOKEN;
|
|
17
18
|
}>;
|
|
19
|
+
fetchAction(options?: Options): Action<void, void, {
|
|
20
|
+
queryClient: typeof QUERY_CLIENT_TOKEN;
|
|
21
|
+
}>;
|
|
18
22
|
}
|
|
19
23
|
export declare const isQuery: <Options, Result, TCreateQuery, TQuery, TUseQuery>(arg: BaseQuery<Options, TCreateQuery, TQuery, TUseQuery> | QueryOptions<Result, any, any, any>) => arg is BaseQuery<Options, TCreateQuery, TQuery, TUseQuery>;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { createQuery } from './query/create';
|
|
2
|
-
export { useQuery } from './query/use';
|
|
2
|
+
export { useQuery, useQueries } from './query/use';
|
|
3
3
|
export { createInfiniteQuery } from './infiniteQuery/create';
|
|
4
4
|
export { useInfiniteQuery } from './infiniteQuery/use';
|
|
5
5
|
export { createMutation } from './mutation/create';
|
|
6
6
|
export { useMutation } from './mutation/use';
|
|
7
|
+
export { useQueryClient } from 'react-query';
|
package/lib/index.es.js
CHANGED
|
@@ -4,8 +4,10 @@ import { QUERY_CLIENT_TOKEN } from '@tramvai/module-react-query';
|
|
|
4
4
|
import isString from '@tinkoff/utils/is/string';
|
|
5
5
|
import isArray from '@tinkoff/utils/is/array';
|
|
6
6
|
import { useMemo } from 'react';
|
|
7
|
-
import { useQuery as useQuery$1, useInfiniteQuery as useInfiniteQuery$1, useMutation as useMutation$1 } from 'react-query';
|
|
7
|
+
import { useQuery as useQuery$1, useQueries as useQueries$1, useInfiniteQuery as useInfiniteQuery$1, useMutation as useMutation$1 } from 'react-query';
|
|
8
|
+
export { useQueryClient } from 'react-query';
|
|
8
9
|
import { useConsumerContext } from '@tramvai/state';
|
|
10
|
+
import { useShallowEqual } from '@tinkoff/react-hooks';
|
|
9
11
|
|
|
10
12
|
const QUERY_PARAMETERS = '__query_parameters__';
|
|
11
13
|
const isQuery = (arg) => {
|
|
@@ -51,6 +53,9 @@ const createQuery = (queryParameters) => {
|
|
|
51
53
|
},
|
|
52
54
|
});
|
|
53
55
|
},
|
|
56
|
+
raw: (context, options) => {
|
|
57
|
+
return convertToRawQuery$1(query, context, options);
|
|
58
|
+
},
|
|
54
59
|
prefetchAction: (options) => {
|
|
55
60
|
return createAction({
|
|
56
61
|
name: 'queryPrefetch',
|
|
@@ -66,6 +71,21 @@ const createQuery = (queryParameters) => {
|
|
|
66
71
|
},
|
|
67
72
|
});
|
|
68
73
|
},
|
|
74
|
+
fetchAction: (options) => {
|
|
75
|
+
return createAction({
|
|
76
|
+
name: 'queryFetch',
|
|
77
|
+
fn: (context, __, { queryClient }) => {
|
|
78
|
+
return queryClient.fetchQuery(convertToRawQuery$1(query, context, options));
|
|
79
|
+
},
|
|
80
|
+
deps: {
|
|
81
|
+
queryClient: QUERY_CLIENT_TOKEN,
|
|
82
|
+
},
|
|
83
|
+
conditions: {
|
|
84
|
+
...conditions,
|
|
85
|
+
onlyServer: true,
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
},
|
|
69
89
|
};
|
|
70
90
|
return query;
|
|
71
91
|
};
|
|
@@ -74,12 +94,25 @@ function useQuery(query, options) {
|
|
|
74
94
|
const context = useConsumerContext();
|
|
75
95
|
const resultQuery = useMemo(() => {
|
|
76
96
|
if (isQuery(query)) {
|
|
77
|
-
return
|
|
97
|
+
return query.raw(context, options);
|
|
78
98
|
}
|
|
79
99
|
return query;
|
|
80
100
|
}, [query, context, options]);
|
|
81
101
|
return useQuery$1(resultQuery);
|
|
82
102
|
}
|
|
103
|
+
function useQueries(queries) {
|
|
104
|
+
const context = useConsumerContext();
|
|
105
|
+
const memoQueries = useShallowEqual(queries);
|
|
106
|
+
const resultQueries = useMemo(() => {
|
|
107
|
+
return memoQueries.map((query) => {
|
|
108
|
+
if (isQuery(query)) {
|
|
109
|
+
return query.raw(context);
|
|
110
|
+
}
|
|
111
|
+
return query;
|
|
112
|
+
});
|
|
113
|
+
}, [memoQueries, context]);
|
|
114
|
+
return useQueries$1(resultQueries);
|
|
115
|
+
}
|
|
83
116
|
|
|
84
117
|
const convertToRawQuery = (query, context, options) => {
|
|
85
118
|
const { key = defaultKey, fn, getNextPageParam, getPreviousPageParam, deps, conditions, infiniteQueryOptions, } = query[QUERY_PARAMETERS];
|
|
@@ -115,6 +148,9 @@ const createInfiniteQuery = (queryParameters) => {
|
|
|
115
148
|
},
|
|
116
149
|
});
|
|
117
150
|
},
|
|
151
|
+
raw: (context, options) => {
|
|
152
|
+
return convertToRawQuery(query, context, options);
|
|
153
|
+
},
|
|
118
154
|
prefetchAction: (options) => {
|
|
119
155
|
return createAction({
|
|
120
156
|
name: 'infiniteQueryPrefetch',
|
|
@@ -130,6 +166,21 @@ const createInfiniteQuery = (queryParameters) => {
|
|
|
130
166
|
},
|
|
131
167
|
});
|
|
132
168
|
},
|
|
169
|
+
fetchAction: (options) => {
|
|
170
|
+
return createAction({
|
|
171
|
+
name: 'infiniteQueryFetch',
|
|
172
|
+
fn: (context, __, { queryClient }) => {
|
|
173
|
+
return queryClient.fetchInfiniteQuery(convertToRawQuery(query, context, options));
|
|
174
|
+
},
|
|
175
|
+
deps: {
|
|
176
|
+
queryClient: QUERY_CLIENT_TOKEN,
|
|
177
|
+
},
|
|
178
|
+
conditions: {
|
|
179
|
+
...conditions,
|
|
180
|
+
onlyServer: true,
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
},
|
|
133
184
|
};
|
|
134
185
|
return query;
|
|
135
186
|
};
|
|
@@ -138,7 +189,7 @@ function useInfiniteQuery(query, options) {
|
|
|
138
189
|
const context = useConsumerContext();
|
|
139
190
|
const resultQuery = useMemo(() => {
|
|
140
191
|
if (isQuery(query)) {
|
|
141
|
-
return
|
|
192
|
+
return query.raw(context, options);
|
|
142
193
|
}
|
|
143
194
|
return query;
|
|
144
195
|
}, [query, context, options]);
|
|
@@ -182,6 +233,9 @@ const createMutation = (mutationParameters) => {
|
|
|
182
233
|
},
|
|
183
234
|
});
|
|
184
235
|
},
|
|
236
|
+
raw: (context, options) => {
|
|
237
|
+
return convertToRawMutation(mutation, context, options);
|
|
238
|
+
},
|
|
185
239
|
};
|
|
186
240
|
return mutation;
|
|
187
241
|
};
|
|
@@ -190,11 +244,11 @@ const useMutation = (mutation, options) => {
|
|
|
190
244
|
const context = useConsumerContext();
|
|
191
245
|
const resultMutation = useMemo(() => {
|
|
192
246
|
if (isMutation(mutation)) {
|
|
193
|
-
return
|
|
247
|
+
return mutation.raw(context, options);
|
|
194
248
|
}
|
|
195
249
|
return mutation;
|
|
196
250
|
}, [mutation, context, options]);
|
|
197
251
|
return useMutation$1(resultMutation);
|
|
198
252
|
};
|
|
199
253
|
|
|
200
|
-
export { createInfiniteQuery, createMutation, createQuery, useInfiniteQuery, useMutation, useQuery };
|
|
254
|
+
export { createInfiniteQuery, createMutation, createQuery, useInfiniteQuery, useMutation, useQueries, useQuery };
|
package/lib/index.js
CHANGED
|
@@ -10,6 +10,7 @@ var isArray = require('@tinkoff/utils/is/array');
|
|
|
10
10
|
var react = require('react');
|
|
11
11
|
var reactQuery = require('react-query');
|
|
12
12
|
var state = require('@tramvai/state');
|
|
13
|
+
var reactHooks = require('@tinkoff/react-hooks');
|
|
13
14
|
|
|
14
15
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
15
16
|
|
|
@@ -61,6 +62,9 @@ const createQuery = (queryParameters) => {
|
|
|
61
62
|
},
|
|
62
63
|
});
|
|
63
64
|
},
|
|
65
|
+
raw: (context, options) => {
|
|
66
|
+
return convertToRawQuery$1(query, context, options);
|
|
67
|
+
},
|
|
64
68
|
prefetchAction: (options) => {
|
|
65
69
|
return core.createAction({
|
|
66
70
|
name: 'queryPrefetch',
|
|
@@ -76,6 +80,21 @@ const createQuery = (queryParameters) => {
|
|
|
76
80
|
},
|
|
77
81
|
});
|
|
78
82
|
},
|
|
83
|
+
fetchAction: (options) => {
|
|
84
|
+
return core.createAction({
|
|
85
|
+
name: 'queryFetch',
|
|
86
|
+
fn: (context, __, { queryClient }) => {
|
|
87
|
+
return queryClient.fetchQuery(convertToRawQuery$1(query, context, options));
|
|
88
|
+
},
|
|
89
|
+
deps: {
|
|
90
|
+
queryClient: moduleReactQuery.QUERY_CLIENT_TOKEN,
|
|
91
|
+
},
|
|
92
|
+
conditions: {
|
|
93
|
+
...conditions,
|
|
94
|
+
onlyServer: true,
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
},
|
|
79
98
|
};
|
|
80
99
|
return query;
|
|
81
100
|
};
|
|
@@ -84,12 +103,25 @@ function useQuery(query, options) {
|
|
|
84
103
|
const context = state.useConsumerContext();
|
|
85
104
|
const resultQuery = react.useMemo(() => {
|
|
86
105
|
if (isQuery(query)) {
|
|
87
|
-
return
|
|
106
|
+
return query.raw(context, options);
|
|
88
107
|
}
|
|
89
108
|
return query;
|
|
90
109
|
}, [query, context, options]);
|
|
91
110
|
return reactQuery.useQuery(resultQuery);
|
|
92
111
|
}
|
|
112
|
+
function useQueries(queries) {
|
|
113
|
+
const context = state.useConsumerContext();
|
|
114
|
+
const memoQueries = reactHooks.useShallowEqual(queries);
|
|
115
|
+
const resultQueries = react.useMemo(() => {
|
|
116
|
+
return memoQueries.map((query) => {
|
|
117
|
+
if (isQuery(query)) {
|
|
118
|
+
return query.raw(context);
|
|
119
|
+
}
|
|
120
|
+
return query;
|
|
121
|
+
});
|
|
122
|
+
}, [memoQueries, context]);
|
|
123
|
+
return reactQuery.useQueries(resultQueries);
|
|
124
|
+
}
|
|
93
125
|
|
|
94
126
|
const convertToRawQuery = (query, context, options) => {
|
|
95
127
|
const { key = defaultKey, fn, getNextPageParam, getPreviousPageParam, deps, conditions, infiniteQueryOptions, } = query[QUERY_PARAMETERS];
|
|
@@ -125,6 +157,9 @@ const createInfiniteQuery = (queryParameters) => {
|
|
|
125
157
|
},
|
|
126
158
|
});
|
|
127
159
|
},
|
|
160
|
+
raw: (context, options) => {
|
|
161
|
+
return convertToRawQuery(query, context, options);
|
|
162
|
+
},
|
|
128
163
|
prefetchAction: (options) => {
|
|
129
164
|
return core.createAction({
|
|
130
165
|
name: 'infiniteQueryPrefetch',
|
|
@@ -140,6 +175,21 @@ const createInfiniteQuery = (queryParameters) => {
|
|
|
140
175
|
},
|
|
141
176
|
});
|
|
142
177
|
},
|
|
178
|
+
fetchAction: (options) => {
|
|
179
|
+
return core.createAction({
|
|
180
|
+
name: 'infiniteQueryFetch',
|
|
181
|
+
fn: (context, __, { queryClient }) => {
|
|
182
|
+
return queryClient.fetchInfiniteQuery(convertToRawQuery(query, context, options));
|
|
183
|
+
},
|
|
184
|
+
deps: {
|
|
185
|
+
queryClient: moduleReactQuery.QUERY_CLIENT_TOKEN,
|
|
186
|
+
},
|
|
187
|
+
conditions: {
|
|
188
|
+
...conditions,
|
|
189
|
+
onlyServer: true,
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
},
|
|
143
193
|
};
|
|
144
194
|
return query;
|
|
145
195
|
};
|
|
@@ -148,7 +198,7 @@ function useInfiniteQuery(query, options) {
|
|
|
148
198
|
const context = state.useConsumerContext();
|
|
149
199
|
const resultQuery = react.useMemo(() => {
|
|
150
200
|
if (isQuery(query)) {
|
|
151
|
-
return
|
|
201
|
+
return query.raw(context, options);
|
|
152
202
|
}
|
|
153
203
|
return query;
|
|
154
204
|
}, [query, context, options]);
|
|
@@ -192,6 +242,9 @@ const createMutation = (mutationParameters) => {
|
|
|
192
242
|
},
|
|
193
243
|
});
|
|
194
244
|
},
|
|
245
|
+
raw: (context, options) => {
|
|
246
|
+
return convertToRawMutation(mutation, context, options);
|
|
247
|
+
},
|
|
195
248
|
};
|
|
196
249
|
return mutation;
|
|
197
250
|
};
|
|
@@ -200,16 +253,21 @@ const useMutation = (mutation, options) => {
|
|
|
200
253
|
const context = state.useConsumerContext();
|
|
201
254
|
const resultMutation = react.useMemo(() => {
|
|
202
255
|
if (isMutation(mutation)) {
|
|
203
|
-
return
|
|
256
|
+
return mutation.raw(context, options);
|
|
204
257
|
}
|
|
205
258
|
return mutation;
|
|
206
259
|
}, [mutation, context, options]);
|
|
207
260
|
return reactQuery.useMutation(resultMutation);
|
|
208
261
|
};
|
|
209
262
|
|
|
263
|
+
Object.defineProperty(exports, 'useQueryClient', {
|
|
264
|
+
enumerable: true,
|
|
265
|
+
get: function () { return reactQuery.useQueryClient; }
|
|
266
|
+
});
|
|
210
267
|
exports.createInfiniteQuery = createInfiniteQuery;
|
|
211
268
|
exports.createMutation = createMutation;
|
|
212
269
|
exports.createQuery = createQuery;
|
|
213
270
|
exports.useInfiniteQuery = useInfiniteQuery;
|
|
214
271
|
exports.useMutation = useMutation;
|
|
272
|
+
exports.useQueries = useQueries;
|
|
215
273
|
exports.useQuery = useQuery;
|
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
import type { UseInfiniteQueryOptions, QueryKey as ReactQueryKey } from 'react-query';
|
|
2
|
-
import type { ActionContext } from '@tramvai/core';
|
|
3
1
|
import type { CreateInfiniteQueryOptions, InfiniteQuery } from './types';
|
|
4
|
-
export declare const convertToRawQuery: <Options, PageParam, Result, Deps>(query: InfiniteQuery<Options, PageParam, Result, Deps>, context: ActionContext, options: Options) => UseInfiniteQueryOptions<Result, Error, Result, Result, ReactQueryKey>;
|
|
5
2
|
export declare const createInfiniteQuery: <Options = unknown, PageParam = unknown, Result = unknown, Deps = unknown>(queryParameters: CreateInfiniteQueryOptions<Options, PageParam, Result, Deps>) => InfiniteQuery<Options, PageParam, Result, Deps>;
|
package/lib/mutation/create.d.ts
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
import type { UseMutationOptions } from 'react-query';
|
|
2
|
-
import type { ActionContext } from '@tramvai/core';
|
|
3
1
|
import type { CreateMutationOptions, Mutation, MutationKey } from './types';
|
|
4
|
-
export declare const convertToRawMutation: <Options, Variables, Result, Deps, Key extends MutationKey<Options>>(mutation: Mutation<Options, Variables, Result, Deps, Key>, context: ActionContext, options?: Options | undefined) => UseMutationOptions<Result, any, Variables, unknown>;
|
|
5
2
|
export declare const createMutation: <Options, Variables, Result, Deps, Key extends MutationKey<Options>>(mutationParameters: CreateMutationOptions<Options, Variables, Result, Deps, Key>) => Mutation<Options, Variables, Result, Deps, Key>;
|
package/lib/mutation/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ActionConditionsParameters } from '@tramvai/core';
|
|
1
|
+
import type { ActionConditionsParameters, ActionContext } from '@tramvai/core';
|
|
2
2
|
import type { ProvideDepsIterator } from '@tinkoff/dippy';
|
|
3
3
|
import type { MutationKey as ReactMutationKey, MutationOptions, UseMutationOptions } from 'react-query';
|
|
4
4
|
export declare const MUTATION_PARAMETERS = "__mutations_parameters__";
|
|
@@ -12,6 +12,7 @@ export interface CreateMutationOptions<Options, Variables, Result, Deps, Key ext
|
|
|
12
12
|
}
|
|
13
13
|
export interface Mutation<Options, Variables, Result, Deps, Key extends MutationKey<Options>> {
|
|
14
14
|
[MUTATION_PARAMETERS]: CreateMutationOptions<Options, Variables, Result, Deps, Key>;
|
|
15
|
-
fork(options: MutationOptions<Result,
|
|
15
|
+
fork(options: MutationOptions<Result, Error, Variables, any>): Mutation<Options, Variables, Result, Deps, Key>;
|
|
16
|
+
raw(context: ActionContext, options?: Options): UseMutationOptions<Result, Error, Variables>;
|
|
16
17
|
}
|
|
17
18
|
export declare const isMutation: <Options, Variables, Result, Deps, Key extends MutationKey<Options>>(arg: Mutation<Options, Variables, Result, Deps, Key> | MutationOptions<Result, any, Variables, any>) => arg is Mutation<Options, Variables, Result, Deps, Key>;
|
package/lib/query/create.d.ts
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
import type { UseQueryOptions, QueryKey as ReactQueryKey } from 'react-query';
|
|
2
|
-
import type { ActionContext } from '@tramvai/core';
|
|
3
1
|
import type { CreateQueryOptions, Query } from './types';
|
|
4
|
-
export declare const convertToRawQuery: <Options, Result, Deps>(query: Query<Options, Result, Deps>, context: ActionContext, options: Options) => UseQueryOptions<Result, Error, Result, ReactQueryKey>;
|
|
5
2
|
export declare const createQuery: <Options = unknown, Result = unknown, Deps = unknown>(queryParameters: CreateQueryOptions<Options, Result, Deps>) => Query<Options, Result, Deps>;
|
package/lib/query/use.d.ts
CHANGED
|
@@ -2,4 +2,5 @@ import type { UseQueryOptions, QueryObserverResult } from 'react-query';
|
|
|
2
2
|
import type { Query } from './types';
|
|
3
3
|
declare function useQuery<Options extends void, Result, Deps>(query: UseQueryOptions<Result, Error> | Query<Options, Result, Deps>): QueryObserverResult<Result, Error>;
|
|
4
4
|
declare function useQuery<Options, Result, Deps>(query: UseQueryOptions<Result, Error> | Query<Options, Result, Deps>, options: Options): QueryObserverResult<Result, Error>;
|
|
5
|
-
|
|
5
|
+
declare function useQueries<Result, Deps>(queries: Array<UseQueryOptions<Result, Error> | Query<any, Result, Deps>>): import("react-query").UseQueryResult<unknown extends Result ? Result : Result, Error>[];
|
|
6
|
+
export { useQuery, useQueries };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/react-query",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.48.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"sideEffects": false,
|
|
11
11
|
"repository": {
|
|
12
12
|
"type": "git",
|
|
13
|
-
"url": "git@github.com:
|
|
13
|
+
"url": "git@github.com:Tinkoff/tramvai.git"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
16
|
"build": "tramvai-build --for-publish",
|
|
@@ -18,9 +18,10 @@
|
|
|
18
18
|
"build-for-publish": "true"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@
|
|
22
|
-
"@tramvai/
|
|
23
|
-
"@tramvai/
|
|
21
|
+
"@tinkoff/react-hooks": "0.0.23",
|
|
22
|
+
"@tramvai/core": "1.48.2",
|
|
23
|
+
"@tramvai/module-react-query": "1.48.2",
|
|
24
|
+
"@tramvai/state": "1.48.2"
|
|
24
25
|
},
|
|
25
26
|
"peerDependencies": {
|
|
26
27
|
"@tinkoff/dippy": "0.7.36",
|
package/README.en.md
DELETED
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
# React Query
|
|
2
|
-
|
|
3
|
-
A library for handling requests in React components. Based on [react-query](https://react-query.tanstack.com/).
|
|
4
|
-
|
|
5
|
-
## Explanation
|
|
6
|
-
|
|
7
|
-
For the library to work, the module [@tramvai/module-react-query](references/modules/react-query.md) must be added to the tramvai application
|
|
8
|
-
|
|
9
|
-
## Api
|
|
10
|
-
|
|
11
|
-
### createQuery
|
|
12
|
-
|
|
13
|
-
Allows you to create a `Query` object that can later be used in components using `useQuery`. Used to execute single data retrieval requests.
|
|
14
|
-
|
|
15
|
-
```ts
|
|
16
|
-
import { createQuery } from '@tramvai/react-query';
|
|
17
|
-
|
|
18
|
-
const query = createQuery({
|
|
19
|
-
key: 'base',
|
|
20
|
-
fn: async (_, { apiClient }) => {
|
|
21
|
-
const { payload } = await apiClient.get('api/base');
|
|
22
|
-
|
|
23
|
-
return payload;
|
|
24
|
-
},
|
|
25
|
-
deps: {
|
|
26
|
-
apiClient: TINKOFF_API_SERVICE,
|
|
27
|
-
},
|
|
28
|
-
});
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
#### Unique query parameters
|
|
32
|
-
|
|
33
|
-
To create a generic `query` that takes parameters for a query, you must return a unique `key`,
|
|
34
|
-
you can read more about this in the official documentation section [Query Keys](https://react-query.tanstack.com/guides/query-keys)
|
|
35
|
-
|
|
36
|
-
As a parameter `key` you can use:
|
|
37
|
-
|
|
38
|
-
- a string, such as `key: 'query-name'`
|
|
39
|
-
- an array where any serializable data can be used as elements, for example `key: ['query-name', false, { bar: 'baz }]`
|
|
40
|
-
- a function that takes the parameters with which `query` is called and returns a string - `key: (options) => 'query-name'`
|
|
41
|
-
- a function that accepts parameters, with which `query` is called, and returns an array, where any serializable data can be used as elements - `key: (options) => ['query-name', options, { bar: 'baz' }]`
|
|
42
|
-
|
|
43
|
-
```ts
|
|
44
|
-
import { createQuery, useQuery } from '@tramvai/react-query';
|
|
45
|
-
|
|
46
|
-
const query = createQuery({
|
|
47
|
-
key: (id: number) => ['user', id],
|
|
48
|
-
fn: async (id, { apiClient }) => {
|
|
49
|
-
const { payload } = await apiClient.get(`api/user/${id}`);
|
|
50
|
-
|
|
51
|
-
return payload;
|
|
52
|
-
},
|
|
53
|
-
deps: {
|
|
54
|
-
apiClient: TINKOFF_API_SERVICE,
|
|
55
|
-
},
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
export function Component({ id }) {
|
|
59
|
-
const { data, isLoading } = useQuery(query, id);
|
|
60
|
-
|
|
61
|
-
return <div>{isLoading ? 'loading...' : data}</div>;
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### useQuery
|
|
66
|
-
|
|
67
|
-
React hook for working with `Query` object
|
|
68
|
-
|
|
69
|
-
[Документация из react-query](https://react-query.tanstack.com/reference/useQuery)
|
|
70
|
-
|
|
71
|
-
```ts
|
|
72
|
-
import { useQuery } from '@tramvai/react-query';
|
|
73
|
-
|
|
74
|
-
export function Component() {
|
|
75
|
-
const { data, isLoading } = useQuery(query);
|
|
76
|
-
|
|
77
|
-
return <div>{isLoading ? 'loading...' : data}</div>;
|
|
78
|
-
}
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
### createInfiniteQuery
|
|
82
|
-
|
|
83
|
-
Creates an `InfiniteQuery` object that can later be used in components using `useInfiniteQuery`. It is used to execute queries to obtain a sequence of data that can be loaded as the component runs.
|
|
84
|
-
|
|
85
|
-
```ts
|
|
86
|
-
import { createInfiniteQuery } from '@tramvai/react-query';
|
|
87
|
-
|
|
88
|
-
const query = createInfiniteQuery({
|
|
89
|
-
key: 'list',
|
|
90
|
-
fn: async (_, start = 0, { apiClient }) => {
|
|
91
|
-
const { payload } = await apiClient.get<Response>('api/list', {
|
|
92
|
-
query: {
|
|
93
|
-
count: 30,
|
|
94
|
-
start,
|
|
95
|
-
},
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
return payload;
|
|
99
|
-
},
|
|
100
|
-
getNextPageParam: (page: Response) => {
|
|
101
|
-
return page.nextPage;
|
|
102
|
-
},
|
|
103
|
-
deps: {
|
|
104
|
-
apiClient: TINKOFF_API_SERVICE,
|
|
105
|
-
},
|
|
106
|
-
});
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
### useInfiniteQuery
|
|
110
|
-
|
|
111
|
-
React hook for working with the `InfiniteQuery` object
|
|
112
|
-
|
|
113
|
-
[Документация из react-query](https://react-query.tanstack.com/reference/useInfiniteQuery)
|
|
114
|
-
|
|
115
|
-
```ts
|
|
116
|
-
import { useInfiniteQuery } from '@tramvai/react-query';
|
|
117
|
-
|
|
118
|
-
export function Component() {
|
|
119
|
-
const { data, isLoading, fetchNextPage, hasNextPage } = useInfiniteQuery(query);
|
|
120
|
-
|
|
121
|
-
if (isLoading) {
|
|
122
|
-
return 'loading...';
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return (
|
|
126
|
-
<div>
|
|
127
|
-
<div>
|
|
128
|
-
{data.pages.map((page) => {
|
|
129
|
-
return page.list.map((entry) => {
|
|
130
|
-
return <div key={entry}>{entry}</div>;
|
|
131
|
-
});
|
|
132
|
-
})}
|
|
133
|
-
</div>
|
|
134
|
-
{hasNextPage && (
|
|
135
|
-
<button type="button" onClick={() => fetchNextPage()}>
|
|
136
|
-
Load more
|
|
137
|
-
</button>
|
|
138
|
-
)}
|
|
139
|
-
</div>
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
### createMutation
|
|
145
|
-
|
|
146
|
-
Creates a `Mutation` object that can later be used in components using `useMutation`. Used to send and modify data in the api.
|
|
147
|
-
|
|
148
|
-
```ts
|
|
149
|
-
import { createMutation } from '@tramvai/react-query';
|
|
150
|
-
|
|
151
|
-
const mutation = createMutation({
|
|
152
|
-
key: 'post',
|
|
153
|
-
fn: async (_, data: string, { apiClient }) => {
|
|
154
|
-
const { payload } = await apiClient.post('api/post', {
|
|
155
|
-
body: {
|
|
156
|
-
data,
|
|
157
|
-
},
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
return payload;
|
|
161
|
-
},
|
|
162
|
-
deps: {
|
|
163
|
-
apiClient: TINKOFF_API_SERVICE,
|
|
164
|
-
},
|
|
165
|
-
});
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
### useMutation
|
|
169
|
-
|
|
170
|
-
React hook for working with the `Mutation` object
|
|
171
|
-
|
|
172
|
-
[Документация из react-query](https://react-query.tanstack.com/reference/useMutation)
|
|
173
|
-
|
|
174
|
-
```ts
|
|
175
|
-
import { useMutation } from '@tramvai/react-query';
|
|
176
|
-
|
|
177
|
-
export function Component() {
|
|
178
|
-
const { isLoading, mutate } = useMutation(mutation);
|
|
179
|
-
|
|
180
|
-
if (isLoading) {
|
|
181
|
-
return 'loading...';
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
return (
|
|
185
|
-
<button type="button" onClick={() => mutate('test')}>
|
|
186
|
-
Send data
|
|
187
|
-
</button>
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
## How-to
|
|
193
|
-
|
|
194
|
-
[Examples of using @tramvai/react-query](how-to/react-query-usage.md)
|