@tramvai/react-query 2.70.0 → 2.72.0
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/lib/baseQuery/types.es.js +6 -0
- package/lib/baseQuery/types.js +11 -0
- package/lib/index.es.js +6 -278
- package/lib/index.js +15 -287
- package/lib/infiniteQuery/create.es.js +85 -0
- package/lib/infiniteQuery/create.js +93 -0
- package/lib/infiniteQuery/use.es.js +17 -0
- package/lib/infiniteQuery/use.js +21 -0
- package/lib/mutation/create.es.js +52 -0
- package/lib/mutation/create.js +60 -0
- package/lib/mutation/types.es.js +6 -0
- package/lib/mutation/types.js +11 -0
- package/lib/mutation/use.es.js +17 -0
- package/lib/mutation/use.js +21 -0
- package/lib/query/create.es.js +83 -0
- package/lib/query/create.js +91 -0
- package/lib/query/use.es.js +31 -0
- package/lib/query/use.js +36 -0
- package/lib/shared/normalizeKey.es.js +10 -0
- package/lib/shared/normalizeKey.js +18 -0
- package/lib/shared/resolveDI.es.js +12 -0
- package/lib/shared/resolveDI.js +16 -0
- package/package.json +9 -10
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var identity = require('@tinkoff/utils/function/identity');
|
|
6
|
+
var core = require('@tramvai/core');
|
|
7
|
+
var moduleReactQuery = require('@tramvai/module-react-query');
|
|
8
|
+
var tokensCommon = require('@tramvai/tokens-common');
|
|
9
|
+
var dippy = require('@tinkoff/dippy');
|
|
10
|
+
var types = require('../baseQuery/types.js');
|
|
11
|
+
var normalizeKey = require('../shared/normalizeKey.js');
|
|
12
|
+
var resolveDI = require('../shared/resolveDI.js');
|
|
13
|
+
|
|
14
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
15
|
+
|
|
16
|
+
var identity__default = /*#__PURE__*/_interopDefaultLegacy(identity);
|
|
17
|
+
|
|
18
|
+
const convertToRawQuery = (query, di, options) => {
|
|
19
|
+
const { key = identity__default["default"], fn, getNextPageParam, getPreviousPageParam, deps = {}, conditions, infiniteQueryOptions, } = query[types.QUERY_PARAMETERS];
|
|
20
|
+
const resolvedDeps = di.getOfDeps(deps);
|
|
21
|
+
const ctx = { deps: resolvedDeps };
|
|
22
|
+
const rawQueryKey = typeof key === 'function' ? key.call(ctx, options) : key;
|
|
23
|
+
const queryKey = normalizeKey.normalizeKey(rawQueryKey);
|
|
24
|
+
const actionWrapper = core.declareAction({
|
|
25
|
+
name: 'infiniteQueryExecution',
|
|
26
|
+
async fn(pageParam) {
|
|
27
|
+
return fn.call(ctx, options, pageParam, ctx.deps);
|
|
28
|
+
},
|
|
29
|
+
conditionsFailResult: 'reject',
|
|
30
|
+
deps,
|
|
31
|
+
conditions,
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
...infiniteQueryOptions,
|
|
35
|
+
getNextPageParam,
|
|
36
|
+
getPreviousPageParam,
|
|
37
|
+
queryKey,
|
|
38
|
+
tramvaiOptions: {
|
|
39
|
+
conditions,
|
|
40
|
+
},
|
|
41
|
+
queryFn: ({ pageParam }) => {
|
|
42
|
+
const context = di.get(tokensCommon.CONTEXT_TOKEN);
|
|
43
|
+
return context.executeAction(actionWrapper, pageParam);
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
const createInfiniteQuery = (queryParameters) => {
|
|
48
|
+
const { infiniteQueryOptions, conditions } = queryParameters;
|
|
49
|
+
const query = {
|
|
50
|
+
[types.QUERY_PARAMETERS]: queryParameters,
|
|
51
|
+
fork: (options) => {
|
|
52
|
+
return createInfiniteQuery({
|
|
53
|
+
...queryParameters,
|
|
54
|
+
infiniteQueryOptions: {
|
|
55
|
+
...infiniteQueryOptions,
|
|
56
|
+
...options,
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
raw: (diOrContext, options) => {
|
|
61
|
+
return convertToRawQuery(query, resolveDI.resolveDI(diOrContext), options);
|
|
62
|
+
},
|
|
63
|
+
prefetchAction: (options) => {
|
|
64
|
+
return core.declareAction({
|
|
65
|
+
name: 'infiniteQueryPrefetch',
|
|
66
|
+
fn() {
|
|
67
|
+
return this.deps.queryClient.prefetchInfiniteQuery(convertToRawQuery(query, this.deps.di, options));
|
|
68
|
+
},
|
|
69
|
+
deps: {
|
|
70
|
+
di: dippy.DI_TOKEN,
|
|
71
|
+
queryClient: moduleReactQuery.QUERY_CLIENT_TOKEN,
|
|
72
|
+
},
|
|
73
|
+
conditions,
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
fetchAction: (options) => {
|
|
77
|
+
return core.declareAction({
|
|
78
|
+
name: 'infiniteQueryFetch',
|
|
79
|
+
fn() {
|
|
80
|
+
return this.deps.queryClient.fetchInfiniteQuery(convertToRawQuery(query, this.deps.di, options));
|
|
81
|
+
},
|
|
82
|
+
deps: {
|
|
83
|
+
di: dippy.DI_TOKEN,
|
|
84
|
+
queryClient: moduleReactQuery.QUERY_CLIENT_TOKEN,
|
|
85
|
+
},
|
|
86
|
+
conditions,
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
return query;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
exports.createInfiniteQuery = createInfiniteQuery;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useInfiniteQuery as useInfiniteQuery$1 } from '@tanstack/react-query';
|
|
3
|
+
import { useDiContainer } from '@tramvai/react';
|
|
4
|
+
import { isQuery } from '../baseQuery/types.es.js';
|
|
5
|
+
|
|
6
|
+
function useInfiniteQuery(query, options) {
|
|
7
|
+
const di = useDiContainer();
|
|
8
|
+
const resultQuery = useMemo(() => {
|
|
9
|
+
if (isQuery(query)) {
|
|
10
|
+
return query.raw(di, options);
|
|
11
|
+
}
|
|
12
|
+
return query;
|
|
13
|
+
}, [query, di, options]);
|
|
14
|
+
return useInfiniteQuery$1(resultQuery);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { useInfiniteQuery };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react$1 = require('react');
|
|
6
|
+
var reactQuery = require('@tanstack/react-query');
|
|
7
|
+
var react = require('@tramvai/react');
|
|
8
|
+
var types = require('../baseQuery/types.js');
|
|
9
|
+
|
|
10
|
+
function useInfiniteQuery(query, options) {
|
|
11
|
+
const di = react.useDiContainer();
|
|
12
|
+
const resultQuery = react$1.useMemo(() => {
|
|
13
|
+
if (types.isQuery(query)) {
|
|
14
|
+
return query.raw(di, options);
|
|
15
|
+
}
|
|
16
|
+
return query;
|
|
17
|
+
}, [query, di, options]);
|
|
18
|
+
return reactQuery.useInfiniteQuery(resultQuery);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
exports.useInfiniteQuery = useInfiniteQuery;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import identity from '@tinkoff/utils/function/identity';
|
|
2
|
+
import { declareAction } from '@tramvai/core';
|
|
3
|
+
import { CONTEXT_TOKEN } from '@tramvai/tokens-common';
|
|
4
|
+
import { MUTATION_PARAMETERS } from './types.es.js';
|
|
5
|
+
import { normalizeKey } from '../shared/normalizeKey.es.js';
|
|
6
|
+
import { resolveDI } from '../shared/resolveDI.es.js';
|
|
7
|
+
|
|
8
|
+
const convertToRawMutation = (mutation, di, options) => {
|
|
9
|
+
const { key = identity, fn, deps = {}, conditions, mutationOptions, } = mutation[MUTATION_PARAMETERS];
|
|
10
|
+
const resolvedDeps = di.getOfDeps(deps);
|
|
11
|
+
const ctx = { deps: resolvedDeps };
|
|
12
|
+
const rawMutationKey = typeof key === 'function' ? key.call(ctx, options) : key;
|
|
13
|
+
const mutationKey = normalizeKey(rawMutationKey);
|
|
14
|
+
const actionWrapper = declareAction({
|
|
15
|
+
name: 'mutationExecution',
|
|
16
|
+
async fn(variables) {
|
|
17
|
+
return fn.call(ctx, options, variables, ctx.deps);
|
|
18
|
+
},
|
|
19
|
+
deps,
|
|
20
|
+
conditions,
|
|
21
|
+
conditionsFailResult: 'reject',
|
|
22
|
+
});
|
|
23
|
+
return {
|
|
24
|
+
...mutationOptions,
|
|
25
|
+
mutationKey,
|
|
26
|
+
mutationFn: (variables) => {
|
|
27
|
+
const context = di.get(CONTEXT_TOKEN);
|
|
28
|
+
return context.executeAction(actionWrapper, variables);
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
const createMutation = (mutationParameters) => {
|
|
33
|
+
const { mutationOptions } = mutationParameters;
|
|
34
|
+
const mutation = {
|
|
35
|
+
[MUTATION_PARAMETERS]: mutationParameters,
|
|
36
|
+
fork: (options) => {
|
|
37
|
+
return createMutation({
|
|
38
|
+
...mutationParameters,
|
|
39
|
+
mutationOptions: {
|
|
40
|
+
...mutationOptions,
|
|
41
|
+
...options,
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
raw: (diOrContext, options) => {
|
|
46
|
+
return convertToRawMutation(mutation, resolveDI(diOrContext), options);
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
return mutation;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export { createMutation };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var identity = require('@tinkoff/utils/function/identity');
|
|
6
|
+
var core = require('@tramvai/core');
|
|
7
|
+
var tokensCommon = require('@tramvai/tokens-common');
|
|
8
|
+
var types = require('./types.js');
|
|
9
|
+
var normalizeKey = require('../shared/normalizeKey.js');
|
|
10
|
+
var resolveDI = require('../shared/resolveDI.js');
|
|
11
|
+
|
|
12
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
13
|
+
|
|
14
|
+
var identity__default = /*#__PURE__*/_interopDefaultLegacy(identity);
|
|
15
|
+
|
|
16
|
+
const convertToRawMutation = (mutation, di, options) => {
|
|
17
|
+
const { key = identity__default["default"], fn, deps = {}, conditions, mutationOptions, } = mutation[types.MUTATION_PARAMETERS];
|
|
18
|
+
const resolvedDeps = di.getOfDeps(deps);
|
|
19
|
+
const ctx = { deps: resolvedDeps };
|
|
20
|
+
const rawMutationKey = typeof key === 'function' ? key.call(ctx, options) : key;
|
|
21
|
+
const mutationKey = normalizeKey.normalizeKey(rawMutationKey);
|
|
22
|
+
const actionWrapper = core.declareAction({
|
|
23
|
+
name: 'mutationExecution',
|
|
24
|
+
async fn(variables) {
|
|
25
|
+
return fn.call(ctx, options, variables, ctx.deps);
|
|
26
|
+
},
|
|
27
|
+
deps,
|
|
28
|
+
conditions,
|
|
29
|
+
conditionsFailResult: 'reject',
|
|
30
|
+
});
|
|
31
|
+
return {
|
|
32
|
+
...mutationOptions,
|
|
33
|
+
mutationKey,
|
|
34
|
+
mutationFn: (variables) => {
|
|
35
|
+
const context = di.get(tokensCommon.CONTEXT_TOKEN);
|
|
36
|
+
return context.executeAction(actionWrapper, variables);
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
const createMutation = (mutationParameters) => {
|
|
41
|
+
const { mutationOptions } = mutationParameters;
|
|
42
|
+
const mutation = {
|
|
43
|
+
[types.MUTATION_PARAMETERS]: mutationParameters,
|
|
44
|
+
fork: (options) => {
|
|
45
|
+
return createMutation({
|
|
46
|
+
...mutationParameters,
|
|
47
|
+
mutationOptions: {
|
|
48
|
+
...mutationOptions,
|
|
49
|
+
...options,
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
raw: (diOrContext, options) => {
|
|
54
|
+
return convertToRawMutation(mutation, resolveDI.resolveDI(diOrContext), options);
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
return mutation;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
exports.createMutation = createMutation;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const MUTATION_PARAMETERS = '__mutations_parameters__';
|
|
6
|
+
const isMutation = (arg) => {
|
|
7
|
+
return MUTATION_PARAMETERS in arg;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
exports.MUTATION_PARAMETERS = MUTATION_PARAMETERS;
|
|
11
|
+
exports.isMutation = isMutation;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useMutation as useMutation$1 } from '@tanstack/react-query';
|
|
3
|
+
import { useDiContainer } from '@tramvai/react';
|
|
4
|
+
import { isMutation } from './types.es.js';
|
|
5
|
+
|
|
6
|
+
const useMutation = (mutation, options) => {
|
|
7
|
+
const di = useDiContainer();
|
|
8
|
+
const resultMutation = useMemo(() => {
|
|
9
|
+
if (isMutation(mutation)) {
|
|
10
|
+
return mutation.raw(di, options);
|
|
11
|
+
}
|
|
12
|
+
return mutation;
|
|
13
|
+
}, [mutation, di, options]);
|
|
14
|
+
return useMutation$1(resultMutation);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { useMutation };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react$1 = require('react');
|
|
6
|
+
var reactQuery = require('@tanstack/react-query');
|
|
7
|
+
var react = require('@tramvai/react');
|
|
8
|
+
var types = require('./types.js');
|
|
9
|
+
|
|
10
|
+
const useMutation = (mutation, options) => {
|
|
11
|
+
const di = react.useDiContainer();
|
|
12
|
+
const resultMutation = react$1.useMemo(() => {
|
|
13
|
+
if (types.isMutation(mutation)) {
|
|
14
|
+
return mutation.raw(di, options);
|
|
15
|
+
}
|
|
16
|
+
return mutation;
|
|
17
|
+
}, [mutation, di, options]);
|
|
18
|
+
return reactQuery.useMutation(resultMutation);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
exports.useMutation = useMutation;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import identity from '@tinkoff/utils/function/identity';
|
|
2
|
+
import { DI_TOKEN } from '@tinkoff/dippy';
|
|
3
|
+
import { declareAction } from '@tramvai/core';
|
|
4
|
+
import { QUERY_CLIENT_TOKEN } from '@tramvai/module-react-query';
|
|
5
|
+
import { CONTEXT_TOKEN } from '@tramvai/tokens-common';
|
|
6
|
+
import { QUERY_PARAMETERS } from '../baseQuery/types.es.js';
|
|
7
|
+
import { normalizeKey } from '../shared/normalizeKey.es.js';
|
|
8
|
+
import { resolveDI } from '../shared/resolveDI.es.js';
|
|
9
|
+
|
|
10
|
+
const convertToRawQuery = (query, di, options) => {
|
|
11
|
+
const { key = identity, fn, deps = {}, conditions, queryOptions } = query[QUERY_PARAMETERS];
|
|
12
|
+
const resolvedDeps = di.getOfDeps(deps);
|
|
13
|
+
const ctx = { deps: resolvedDeps };
|
|
14
|
+
const rawQueryKey = typeof key === 'function' ? key.call(ctx, options) : key;
|
|
15
|
+
const queryKey = normalizeKey(rawQueryKey);
|
|
16
|
+
const actionWrapper = declareAction({
|
|
17
|
+
name: 'queryExecution',
|
|
18
|
+
async fn() {
|
|
19
|
+
return fn.call(ctx, options, ctx.deps);
|
|
20
|
+
},
|
|
21
|
+
deps,
|
|
22
|
+
conditions,
|
|
23
|
+
conditionsFailResult: 'reject',
|
|
24
|
+
});
|
|
25
|
+
return {
|
|
26
|
+
...queryOptions,
|
|
27
|
+
queryKey,
|
|
28
|
+
tramvaiOptions: {
|
|
29
|
+
conditions,
|
|
30
|
+
},
|
|
31
|
+
queryFn: () => {
|
|
32
|
+
const context = di.get(CONTEXT_TOKEN);
|
|
33
|
+
return context.executeAction(actionWrapper);
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
const createQuery = (queryParameters) => {
|
|
38
|
+
const { queryOptions, conditions } = queryParameters;
|
|
39
|
+
const query = {
|
|
40
|
+
[QUERY_PARAMETERS]: queryParameters,
|
|
41
|
+
fork: (options) => {
|
|
42
|
+
return createQuery({
|
|
43
|
+
...queryParameters,
|
|
44
|
+
queryOptions: {
|
|
45
|
+
...queryOptions,
|
|
46
|
+
...options,
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
raw: (diOrContext, options) => {
|
|
51
|
+
return convertToRawQuery(query, resolveDI(diOrContext), options);
|
|
52
|
+
},
|
|
53
|
+
prefetchAction: (options) => {
|
|
54
|
+
return declareAction({
|
|
55
|
+
name: 'queryPrefetch',
|
|
56
|
+
fn() {
|
|
57
|
+
return this.deps.queryClient.prefetchQuery(convertToRawQuery(query, this.deps.di, options));
|
|
58
|
+
},
|
|
59
|
+
deps: {
|
|
60
|
+
di: DI_TOKEN,
|
|
61
|
+
queryClient: QUERY_CLIENT_TOKEN,
|
|
62
|
+
},
|
|
63
|
+
conditions,
|
|
64
|
+
});
|
|
65
|
+
},
|
|
66
|
+
fetchAction: (options) => {
|
|
67
|
+
return declareAction({
|
|
68
|
+
name: 'queryFetch',
|
|
69
|
+
fn() {
|
|
70
|
+
return this.deps.queryClient.fetchQuery(convertToRawQuery(query, this.deps.di, options));
|
|
71
|
+
},
|
|
72
|
+
deps: {
|
|
73
|
+
di: DI_TOKEN,
|
|
74
|
+
queryClient: QUERY_CLIENT_TOKEN,
|
|
75
|
+
},
|
|
76
|
+
conditions,
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
return query;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export { createQuery };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var identity = require('@tinkoff/utils/function/identity');
|
|
6
|
+
var dippy = require('@tinkoff/dippy');
|
|
7
|
+
var core = require('@tramvai/core');
|
|
8
|
+
var moduleReactQuery = require('@tramvai/module-react-query');
|
|
9
|
+
var tokensCommon = require('@tramvai/tokens-common');
|
|
10
|
+
var types = require('../baseQuery/types.js');
|
|
11
|
+
var normalizeKey = require('../shared/normalizeKey.js');
|
|
12
|
+
var resolveDI = require('../shared/resolveDI.js');
|
|
13
|
+
|
|
14
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
15
|
+
|
|
16
|
+
var identity__default = /*#__PURE__*/_interopDefaultLegacy(identity);
|
|
17
|
+
|
|
18
|
+
const convertToRawQuery = (query, di, options) => {
|
|
19
|
+
const { key = identity__default["default"], fn, deps = {}, conditions, queryOptions } = query[types.QUERY_PARAMETERS];
|
|
20
|
+
const resolvedDeps = di.getOfDeps(deps);
|
|
21
|
+
const ctx = { deps: resolvedDeps };
|
|
22
|
+
const rawQueryKey = typeof key === 'function' ? key.call(ctx, options) : key;
|
|
23
|
+
const queryKey = normalizeKey.normalizeKey(rawQueryKey);
|
|
24
|
+
const actionWrapper = core.declareAction({
|
|
25
|
+
name: 'queryExecution',
|
|
26
|
+
async fn() {
|
|
27
|
+
return fn.call(ctx, options, ctx.deps);
|
|
28
|
+
},
|
|
29
|
+
deps,
|
|
30
|
+
conditions,
|
|
31
|
+
conditionsFailResult: 'reject',
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
...queryOptions,
|
|
35
|
+
queryKey,
|
|
36
|
+
tramvaiOptions: {
|
|
37
|
+
conditions,
|
|
38
|
+
},
|
|
39
|
+
queryFn: () => {
|
|
40
|
+
const context = di.get(tokensCommon.CONTEXT_TOKEN);
|
|
41
|
+
return context.executeAction(actionWrapper);
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
const createQuery = (queryParameters) => {
|
|
46
|
+
const { queryOptions, conditions } = queryParameters;
|
|
47
|
+
const query = {
|
|
48
|
+
[types.QUERY_PARAMETERS]: queryParameters,
|
|
49
|
+
fork: (options) => {
|
|
50
|
+
return createQuery({
|
|
51
|
+
...queryParameters,
|
|
52
|
+
queryOptions: {
|
|
53
|
+
...queryOptions,
|
|
54
|
+
...options,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
raw: (diOrContext, options) => {
|
|
59
|
+
return convertToRawQuery(query, resolveDI.resolveDI(diOrContext), options);
|
|
60
|
+
},
|
|
61
|
+
prefetchAction: (options) => {
|
|
62
|
+
return core.declareAction({
|
|
63
|
+
name: 'queryPrefetch',
|
|
64
|
+
fn() {
|
|
65
|
+
return this.deps.queryClient.prefetchQuery(convertToRawQuery(query, this.deps.di, options));
|
|
66
|
+
},
|
|
67
|
+
deps: {
|
|
68
|
+
di: dippy.DI_TOKEN,
|
|
69
|
+
queryClient: moduleReactQuery.QUERY_CLIENT_TOKEN,
|
|
70
|
+
},
|
|
71
|
+
conditions,
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
fetchAction: (options) => {
|
|
75
|
+
return core.declareAction({
|
|
76
|
+
name: 'queryFetch',
|
|
77
|
+
fn() {
|
|
78
|
+
return this.deps.queryClient.fetchQuery(convertToRawQuery(query, this.deps.di, options));
|
|
79
|
+
},
|
|
80
|
+
deps: {
|
|
81
|
+
di: dippy.DI_TOKEN,
|
|
82
|
+
queryClient: moduleReactQuery.QUERY_CLIENT_TOKEN,
|
|
83
|
+
},
|
|
84
|
+
conditions,
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
return query;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
exports.createQuery = createQuery;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { useQuery as useQuery$1, useQueries as useQueries$1 } from '@tanstack/react-query';
|
|
3
|
+
import { useShallowEqual } from '@tinkoff/react-hooks';
|
|
4
|
+
import { useDiContainer } from '@tramvai/react';
|
|
5
|
+
import { isQuery } from '../baseQuery/types.es.js';
|
|
6
|
+
|
|
7
|
+
function useQuery(query, options) {
|
|
8
|
+
const di = useDiContainer();
|
|
9
|
+
const resultQuery = useMemo(() => {
|
|
10
|
+
if (isQuery(query)) {
|
|
11
|
+
return query.raw(di, options);
|
|
12
|
+
}
|
|
13
|
+
return query;
|
|
14
|
+
}, [query, di, options]);
|
|
15
|
+
return useQuery$1(resultQuery);
|
|
16
|
+
}
|
|
17
|
+
function useQueries(queries) {
|
|
18
|
+
const di = useDiContainer();
|
|
19
|
+
const memoQueries = useShallowEqual(queries);
|
|
20
|
+
const resultQueries = useMemo(() => {
|
|
21
|
+
return memoQueries.map((query) => {
|
|
22
|
+
if (isQuery(query)) {
|
|
23
|
+
return query.raw(di);
|
|
24
|
+
}
|
|
25
|
+
return query;
|
|
26
|
+
});
|
|
27
|
+
}, [memoQueries, di]);
|
|
28
|
+
return useQueries$1({ queries: resultQueries });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { useQueries, useQuery };
|
package/lib/query/use.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var react$1 = require('react');
|
|
6
|
+
var reactQuery = require('@tanstack/react-query');
|
|
7
|
+
var reactHooks = require('@tinkoff/react-hooks');
|
|
8
|
+
var react = require('@tramvai/react');
|
|
9
|
+
var types = require('../baseQuery/types.js');
|
|
10
|
+
|
|
11
|
+
function useQuery(query, options) {
|
|
12
|
+
const di = react.useDiContainer();
|
|
13
|
+
const resultQuery = react$1.useMemo(() => {
|
|
14
|
+
if (types.isQuery(query)) {
|
|
15
|
+
return query.raw(di, options);
|
|
16
|
+
}
|
|
17
|
+
return query;
|
|
18
|
+
}, [query, di, options]);
|
|
19
|
+
return reactQuery.useQuery(resultQuery);
|
|
20
|
+
}
|
|
21
|
+
function useQueries(queries) {
|
|
22
|
+
const di = react.useDiContainer();
|
|
23
|
+
const memoQueries = reactHooks.useShallowEqual(queries);
|
|
24
|
+
const resultQueries = react$1.useMemo(() => {
|
|
25
|
+
return memoQueries.map((query) => {
|
|
26
|
+
if (types.isQuery(query)) {
|
|
27
|
+
return query.raw(di);
|
|
28
|
+
}
|
|
29
|
+
return query;
|
|
30
|
+
});
|
|
31
|
+
}, [memoQueries, di]);
|
|
32
|
+
return reactQuery.useQueries({ queries: resultQueries });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
exports.useQueries = useQueries;
|
|
36
|
+
exports.useQuery = useQuery;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var isArray = require('@tinkoff/utils/is/array');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);
|
|
10
|
+
|
|
11
|
+
const normalizeKey = (key) => {
|
|
12
|
+
if (isArray__default["default"](key)) {
|
|
13
|
+
return key;
|
|
14
|
+
}
|
|
15
|
+
return [key];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
exports.normalizeKey = normalizeKey;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const isActionContext = (diOrContext) => {
|
|
2
|
+
return 'di' in diOrContext;
|
|
3
|
+
};
|
|
4
|
+
// @TODO: v3: leave only di container in next major
|
|
5
|
+
const resolveDI = (diOrContext) => {
|
|
6
|
+
if (isActionContext(diOrContext)) {
|
|
7
|
+
return diOrContext.di;
|
|
8
|
+
}
|
|
9
|
+
return diOrContext;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export { resolveDI };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const isActionContext = (diOrContext) => {
|
|
6
|
+
return 'di' in diOrContext;
|
|
7
|
+
};
|
|
8
|
+
// @TODO: v3: leave only di container in next major
|
|
9
|
+
const resolveDI = (diOrContext) => {
|
|
10
|
+
if (isActionContext(diOrContext)) {
|
|
11
|
+
return diOrContext.di;
|
|
12
|
+
}
|
|
13
|
+
return diOrContext;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
exports.resolveDI = resolveDI;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/react-query",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.72.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
@@ -13,19 +13,18 @@
|
|
|
13
13
|
"url": "git@github.com:Tinkoff/tramvai.git"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
|
-
"build": "tramvai-build --
|
|
17
|
-
"watch": "tsc -w"
|
|
18
|
-
"build-for-publish": "true"
|
|
16
|
+
"build": "tramvai-build --forPublish --preserveModules",
|
|
17
|
+
"watch": "tsc -w"
|
|
19
18
|
},
|
|
20
19
|
"dependencies": {
|
|
21
|
-
"@tinkoff/react-hooks": "0.1.
|
|
22
|
-
"@tramvai/core": "2.
|
|
23
|
-
"@tramvai/module-react-query": "2.
|
|
24
|
-
"@tramvai/react": "2.
|
|
25
|
-
"@tramvai/tokens-common": "2.
|
|
20
|
+
"@tinkoff/react-hooks": "0.1.5",
|
|
21
|
+
"@tramvai/core": "2.72.0",
|
|
22
|
+
"@tramvai/module-react-query": "2.72.0",
|
|
23
|
+
"@tramvai/react": "2.72.0",
|
|
24
|
+
"@tramvai/tokens-common": "2.72.0"
|
|
26
25
|
},
|
|
27
26
|
"peerDependencies": {
|
|
28
|
-
"@tinkoff/dippy": "0.8.
|
|
27
|
+
"@tinkoff/dippy": "0.8.13",
|
|
29
28
|
"@tinkoff/utils": "^2.1.2",
|
|
30
29
|
"react": ">=16.14.0",
|
|
31
30
|
"@tanstack/react-query": "^4.7.1",
|