@tanstack/react-query 5.0.0-alpha.21 → 5.0.0-alpha.24

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.
Files changed (131) hide show
  1. package/build/codemods/utils/index.js +205 -0
  2. package/build/codemods/utils/transformers/query-cache-transformer.js +116 -0
  3. package/build/codemods/utils/transformers/query-client-transformer.js +48 -0
  4. package/build/codemods/utils/transformers/use-query-like-transformer.js +32 -0
  5. package/build/codemods/v4/__testfixtures__/default-import.input.tsx +94 -0
  6. package/build/codemods/v4/__testfixtures__/default-import.output.tsx +96 -0
  7. package/build/codemods/v4/__testfixtures__/named-import.input.tsx +96 -0
  8. package/build/codemods/v4/__testfixtures__/named-import.output.tsx +98 -0
  9. package/build/codemods/v4/__testfixtures__/namespaced-import.input.tsx +86 -0
  10. package/build/codemods/v4/__testfixtures__/namespaced-import.output.tsx +88 -0
  11. package/build/codemods/v4/__testfixtures__/parameter-is-identifier.input.tsx +49 -0
  12. package/build/codemods/v4/__testfixtures__/parameter-is-identifier.output.tsx +49 -0
  13. package/build/codemods/v4/__testfixtures__/parameter-is-object-expression.input.tsx +128 -0
  14. package/build/codemods/v4/__testfixtures__/parameter-is-object-expression.output.tsx +175 -0
  15. package/build/codemods/v4/__testfixtures__/replace-import-specifier.input.tsx +10 -0
  16. package/build/codemods/v4/__testfixtures__/replace-import-specifier.output.tsx +10 -0
  17. package/build/codemods/v4/__testfixtures__/type-arguments.input.tsx +25 -0
  18. package/build/codemods/v4/__testfixtures__/type-arguments.output.tsx +31 -0
  19. package/build/codemods/v4/__tests__/key-transformation.test.js +32 -0
  20. package/build/codemods/v4/__tests__/replace-import-specifier.test.js +12 -0
  21. package/build/codemods/v4/key-transformation.js +138 -0
  22. package/build/codemods/v4/replace-import-specifier.js +25 -0
  23. package/build/codemods/v4/utils/replacers/key-replacer.js +164 -0
  24. package/build/codemods/v5/remove-overloads/__testfixtures__/default-import.input.tsx +199 -0
  25. package/build/codemods/v5/remove-overloads/__testfixtures__/default-import.output.tsx +484 -0
  26. package/build/codemods/v5/remove-overloads/__tests__/remove-overloads.test.js +6 -0
  27. package/build/codemods/v5/remove-overloads/remove-overloads.js +59 -0
  28. package/build/codemods/v5/remove-overloads/transformers/filter-aware-usage-transformer.js +153 -0
  29. package/build/codemods/v5/remove-overloads/transformers/query-fn-aware-usage-transformer.js +188 -0
  30. package/build/codemods/v5/remove-overloads/utils/index.js +124 -0
  31. package/build/codemods/v5/remove-overloads/utils/unknown-usage-error.js +26 -0
  32. package/build/lib/HydrationBoundary.d.ts +1 -0
  33. package/build/lib/HydrationBoundary.d.ts.map +1 -0
  34. package/build/lib/QueryClientProvider.d.ts +1 -0
  35. package/build/lib/QueryClientProvider.d.ts.map +1 -0
  36. package/build/lib/QueryErrorResetBoundary.d.ts +1 -0
  37. package/build/lib/QueryErrorResetBoundary.d.ts.map +1 -0
  38. package/build/lib/__tests__/HydrationBoundary.test.d.ts +1 -0
  39. package/build/lib/__tests__/HydrationBoundary.test.d.ts.map +1 -0
  40. package/build/lib/__tests__/QueryClientProvider.test.d.ts +1 -0
  41. package/build/lib/__tests__/QueryClientProvider.test.d.ts.map +1 -0
  42. package/build/lib/__tests__/QueryResetErrorBoundary.test.d.ts +1 -0
  43. package/build/lib/__tests__/QueryResetErrorBoundary.test.d.ts.map +1 -0
  44. package/build/lib/__tests__/ssr-hydration.test.d.ts +1 -0
  45. package/build/lib/__tests__/ssr-hydration.test.d.ts.map +1 -0
  46. package/build/lib/__tests__/ssr.test.d.ts +1 -0
  47. package/build/lib/__tests__/ssr.test.d.ts.map +1 -0
  48. package/build/lib/__tests__/suspense.test.d.ts +1 -0
  49. package/build/lib/__tests__/suspense.test.d.ts.map +1 -0
  50. package/build/lib/__tests__/useInfiniteQuery.test.d.ts +1 -0
  51. package/build/lib/__tests__/useInfiniteQuery.test.d.ts.map +1 -0
  52. package/build/lib/__tests__/useInfiniteQuery.type.test.d.ts +1 -0
  53. package/build/lib/__tests__/useInfiniteQuery.type.test.d.ts.map +1 -0
  54. package/build/lib/__tests__/useIsFetching.test.d.ts +1 -0
  55. package/build/lib/__tests__/useIsFetching.test.d.ts.map +1 -0
  56. package/build/lib/__tests__/useMutation.test.d.ts +1 -0
  57. package/build/lib/__tests__/useMutation.test.d.ts.map +1 -0
  58. package/build/lib/__tests__/useMutationState.test.d.ts +1 -0
  59. package/build/lib/__tests__/useMutationState.test.d.ts.map +1 -0
  60. package/build/lib/__tests__/useQueries.test.d.ts +1 -0
  61. package/build/lib/__tests__/useQueries.test.d.ts.map +1 -0
  62. package/build/lib/__tests__/useQuery.test.d.ts +1 -0
  63. package/build/lib/__tests__/useQuery.test.d.ts.map +1 -0
  64. package/build/lib/__tests__/useQuery.types.test.d.ts +1 -0
  65. package/build/lib/__tests__/useQuery.types.test.d.ts.map +1 -0
  66. package/build/lib/__tests__/utils.d.ts +1 -0
  67. package/build/lib/__tests__/utils.d.ts.map +1 -0
  68. package/build/lib/errorBoundaryUtils.d.ts +4 -3
  69. package/build/lib/errorBoundaryUtils.d.ts.map +1 -0
  70. package/build/lib/errorBoundaryUtils.esm.js +3 -3
  71. package/build/lib/errorBoundaryUtils.esm.js.map +1 -1
  72. package/build/lib/errorBoundaryUtils.js +3 -3
  73. package/build/lib/errorBoundaryUtils.js.map +1 -1
  74. package/build/lib/errorBoundaryUtils.mjs +3 -3
  75. package/build/lib/errorBoundaryUtils.mjs.map +1 -1
  76. package/build/lib/index.d.ts +1 -0
  77. package/build/lib/index.d.ts.map +1 -0
  78. package/build/lib/isRestoring.d.ts +1 -0
  79. package/build/lib/isRestoring.d.ts.map +1 -0
  80. package/build/lib/suspense.d.ts +1 -0
  81. package/build/lib/suspense.d.ts.map +1 -0
  82. package/build/lib/types.d.ts +1 -0
  83. package/build/lib/types.d.ts.map +1 -0
  84. package/build/lib/useBaseQuery.d.ts +1 -0
  85. package/build/lib/useBaseQuery.d.ts.map +1 -0
  86. package/build/lib/useBaseQuery.esm.js +1 -1
  87. package/build/lib/useBaseQuery.esm.js.map +1 -1
  88. package/build/lib/useBaseQuery.js +1 -1
  89. package/build/lib/useBaseQuery.js.map +1 -1
  90. package/build/lib/useBaseQuery.mjs +1 -1
  91. package/build/lib/useBaseQuery.mjs.map +1 -1
  92. package/build/lib/useInfiniteQuery.d.ts +1 -0
  93. package/build/lib/useInfiniteQuery.d.ts.map +1 -0
  94. package/build/lib/useIsFetching.d.ts +1 -0
  95. package/build/lib/useIsFetching.d.ts.map +1 -0
  96. package/build/lib/useMutation.d.ts +1 -0
  97. package/build/lib/useMutation.d.ts.map +1 -0
  98. package/build/lib/useMutation.esm.js +1 -1
  99. package/build/lib/useMutation.esm.js.map +1 -1
  100. package/build/lib/useMutation.js +1 -1
  101. package/build/lib/useMutation.js.map +1 -1
  102. package/build/lib/useMutation.mjs +1 -1
  103. package/build/lib/useMutation.mjs.map +1 -1
  104. package/build/lib/useMutationState.d.ts +1 -0
  105. package/build/lib/useMutationState.d.ts.map +1 -0
  106. package/build/lib/useQueries.d.ts +1 -0
  107. package/build/lib/useQueries.d.ts.map +1 -0
  108. package/build/lib/useQueries.esm.js +1 -1
  109. package/build/lib/useQueries.esm.js.map +1 -1
  110. package/build/lib/useQueries.js +1 -1
  111. package/build/lib/useQueries.js.map +1 -1
  112. package/build/lib/useQueries.mjs +1 -1
  113. package/build/lib/useQueries.mjs.map +1 -1
  114. package/build/lib/useQuery.d.ts +1 -0
  115. package/build/lib/useQuery.d.ts.map +1 -0
  116. package/build/lib/utils.d.ts +1 -0
  117. package/build/lib/utils.d.ts.map +1 -0
  118. package/build/umd/index.development.js +11 -11
  119. package/build/umd/index.development.js.map +1 -1
  120. package/build/umd/index.production.js +1 -1
  121. package/build/umd/index.production.js.map +1 -1
  122. package/package.json +8 -3
  123. package/src/__tests__/QueryResetErrorBoundary.test.tsx +11 -11
  124. package/src/__tests__/suspense.test.tsx +6 -6
  125. package/src/__tests__/useMutation.test.tsx +4 -4
  126. package/src/__tests__/useQueries.test.tsx +9 -9
  127. package/src/__tests__/useQuery.test.tsx +6 -6
  128. package/src/errorBoundaryUtils.ts +5 -5
  129. package/src/useBaseQuery.ts +1 -1
  130. package/src/useMutation.ts +1 -1
  131. package/src/useQueries.ts +1 -1
@@ -0,0 +1,10 @@
1
+ // React Query
2
+ import { useQuery, useQueryClient } from '@tanstack/react-query'
3
+ import { useQuery as RenamedUseQuery, useQueryClient as RenamedUseQueryClient } from '@tanstack/react-query'
4
+ import DefaultReactQuery from '@tanstack/react-query'
5
+ import * as NamespacedReactQuery from '@tanstack/react-query'
6
+ // Devtools
7
+ import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
8
+ import { ReactQueryDevtools as RenamedReactQueryDevtools } from '@tanstack/react-query-devtools'
9
+ import DefaultReactQueryDevtools from '@tanstack/react-query-devtools'
10
+ import * as NamespacedReactQueryDevtools from '@tanstack/react-query-devtools'
@@ -0,0 +1,25 @@
1
+ import * as React from 'react'
2
+ import { useQueries, useQuery, useQueryClient } from 'react-query'
3
+
4
+ type Todos = {
5
+ items: readonly {
6
+ id: string
7
+ text: string
8
+ }[]
9
+ ts: number
10
+ }
11
+
12
+ export const Examples = () => {
13
+ useQuery<Todos>('todos')
14
+ useQueries<Array<Todos>>([query1, query2])
15
+ // QueryClient methods
16
+ // --- Instantiated hook call.
17
+ const queryClient = useQueryClient()
18
+ queryClient.getQueriesData<Todos>('todos')
19
+ queryClient.getQueriesData<Todos>({ queryKey: 'todos' })
20
+ // --- Direct hook call.
21
+ useQueryClient().getQueriesData<Todos>('todos')
22
+ useQueryClient().getQueriesData<Todos>({ queryKey: 'todos' })
23
+
24
+ return <div>Example Component</div>
25
+ }
@@ -0,0 +1,31 @@
1
+ import * as React from 'react'
2
+ import { useQueries, useQuery, useQueryClient } from 'react-query'
3
+
4
+ type Todos = {
5
+ items: readonly {
6
+ id: string
7
+ text: string
8
+ }[]
9
+ ts: number
10
+ }
11
+
12
+ export const Examples = () => {
13
+ useQuery<Todos>(['todos'])
14
+ useQueries<Array<Todos>>({
15
+ queries: [query1, query2]
16
+ })
17
+ // QueryClient methods
18
+ // --- Instantiated hook call.
19
+ const queryClient = useQueryClient()
20
+ queryClient.getQueriesData<Todos>(['todos'])
21
+ queryClient.getQueriesData<Todos>({
22
+ queryKey: ['todos']
23
+ })
24
+ // --- Direct hook call.
25
+ useQueryClient().getQueriesData<Todos>(['todos'])
26
+ useQueryClient().getQueriesData<Todos>({
27
+ queryKey: ['todos']
28
+ })
29
+
30
+ return <div>Example Component</div>
31
+ }
@@ -0,0 +1,32 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
2
+ const defineTest = require('jscodeshift/dist/testUtils').defineTest
3
+
4
+ defineTest(__dirname, 'key-transformation', null, 'default-import', {
5
+ parser: 'tsx',
6
+ })
7
+
8
+ defineTest(__dirname, 'key-transformation', null, 'named-import', {
9
+ parser: 'tsx',
10
+ })
11
+
12
+ defineTest(__dirname, 'key-transformation', null, 'namespaced-import', {
13
+ parser: 'tsx',
14
+ })
15
+
16
+ defineTest(__dirname, 'key-transformation', null, 'parameter-is-identifier', {
17
+ parser: 'tsx',
18
+ })
19
+
20
+ defineTest(
21
+ __dirname,
22
+ 'key-transformation',
23
+ null,
24
+ 'parameter-is-object-expression',
25
+ {
26
+ parser: 'tsx',
27
+ },
28
+ )
29
+
30
+ defineTest(__dirname, 'key-transformation', null, 'type-arguments', {
31
+ parser: 'tsx',
32
+ })
@@ -0,0 +1,12 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
2
+ const defineTest = require('jscodeshift/dist/testUtils').defineTest
3
+
4
+ defineTest(
5
+ __dirname,
6
+ 'replace-import-specifier',
7
+ null,
8
+ 'replace-import-specifier',
9
+ {
10
+ parser: 'tsx',
11
+ },
12
+ )
@@ -0,0 +1,138 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
2
+ const createUtilsObject = require('../utils')
3
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
4
+ const createKeyReplacer = require('./utils/replacers/key-replacer')
5
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
6
+ const createUseQueryLikeTransformer = require('../utils/transformers/use-query-like-transformer')
7
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
8
+ const createQueryClientTransformer = require('../utils/transformers/query-client-transformer')
9
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
10
+ const createQueryCacheTransformer = require('../utils/transformers/query-cache-transformer')
11
+
12
+ const transformQueryClientUsages = ({ jscodeshift, utils, root, filePath }) => {
13
+ const transformer = createQueryClientTransformer({ jscodeshift, utils, root })
14
+ const replacer = createKeyReplacer({ jscodeshift, root, filePath })
15
+
16
+ transformer.execute(
17
+ [
18
+ // Not object syntax-aware methods.
19
+ 'getMutationDefaults',
20
+ 'getQueriesData',
21
+ 'getQueryData',
22
+ 'getQueryDefaults',
23
+ 'getQueryState',
24
+ 'isFetching',
25
+ 'setMutationDefaults',
26
+ 'setQueriesData',
27
+ 'setQueryData',
28
+ 'setQueryDefaults',
29
+ // Object syntax-aware methods.
30
+ 'cancelQueries',
31
+ 'fetchInfiniteQuery',
32
+ 'fetchQuery',
33
+ 'invalidateQueries',
34
+ 'prefetchInfiniteQuery',
35
+ 'prefetchQuery',
36
+ 'refetchQueries',
37
+ 'removeQueries',
38
+ 'resetQueries',
39
+ ],
40
+ replacer,
41
+ )
42
+ }
43
+
44
+ const transformUseQueriesUsages = ({ jscodeshift, utils, root }) => {
45
+ const transformer = createUseQueryLikeTransformer({
46
+ jscodeshift,
47
+ utils,
48
+ root,
49
+ })
50
+ const replacer = ({ node }) => {
51
+ /**
52
+ * When the node doesn't have the 'original' property, that means the codemod has been already applied,
53
+ * so we don't need to do any changes.
54
+ */
55
+ if (!node.original) {
56
+ return node
57
+ }
58
+
59
+ const newCallExpression = jscodeshift.callExpression(node.original.callee, [
60
+ jscodeshift.objectExpression([
61
+ jscodeshift.property(
62
+ 'init',
63
+ jscodeshift.identifier('queries'),
64
+ node.original.arguments[0],
65
+ ),
66
+ ]),
67
+ ])
68
+
69
+ // TODO: This should be part of one function!
70
+ if (node.typeParameters) {
71
+ newCallExpression.typeArguments = node.typeParameters
72
+ }
73
+
74
+ return newCallExpression
75
+ }
76
+
77
+ transformer.execute(['useQueries'], replacer)
78
+ }
79
+
80
+ const transformUseQueryLikeUsages = ({
81
+ jscodeshift,
82
+ utils,
83
+ root,
84
+ filePath,
85
+ }) => {
86
+ const transformer = createUseQueryLikeTransformer({
87
+ jscodeshift,
88
+ utils,
89
+ root,
90
+ })
91
+
92
+ transformer.execute(
93
+ ['useQuery', 'useInfiniteQuery', 'useIsFetching', 'useIsMutating'],
94
+ createKeyReplacer({
95
+ jscodeshift,
96
+ root,
97
+ filePath,
98
+ keyName: 'queryKey',
99
+ }),
100
+ )
101
+ transformer.execute(
102
+ ['useMutation'],
103
+ createKeyReplacer({
104
+ jscodeshift,
105
+ root,
106
+ filePath,
107
+ keyName: 'mutationKey',
108
+ }),
109
+ )
110
+ }
111
+
112
+ const transformQueryCacheUsages = ({ jscodeshift, utils, root, filePath }) => {
113
+ const transformer = createQueryCacheTransformer({ jscodeshift, utils, root })
114
+ const replacer = createKeyReplacer({ jscodeshift, root, filePath })
115
+
116
+ transformer.execute(replacer)
117
+ }
118
+
119
+ module.exports = (file, api) => {
120
+ const jscodeshift = api.jscodeshift
121
+ const root = jscodeshift(file.source)
122
+
123
+ // TODO: Execute the transformers only when it contains a `react-query` import!
124
+
125
+ const utils = createUtilsObject({ root, jscodeshift })
126
+ const filePath = file.path
127
+
128
+ // This function transforms usages like `useQuery` and `useMutation`.
129
+ transformUseQueryLikeUsages({ jscodeshift, utils, root, filePath })
130
+ // This function transforms usages of `useQueries`.
131
+ transformUseQueriesUsages({ jscodeshift, utils, root })
132
+ // This function transforms usages of `QueryClient`.
133
+ transformQueryClientUsages({ jscodeshift, utils, root, filePath })
134
+ // This function transforms usages of `QueryCache`.
135
+ transformQueryCacheUsages({ jscodeshift, utils, root, filePath })
136
+
137
+ return root.toSource({ quote: 'single' })
138
+ }
@@ -0,0 +1,25 @@
1
+ module.exports = (file, api) => {
2
+ const jscodeshift = api.jscodeshift
3
+ const root = jscodeshift(file.source)
4
+
5
+ const replacements = [
6
+ { from: 'react-query', to: '@tanstack/react-query' },
7
+ { from: 'react-query/devtools', to: '@tanstack/react-query-devtools' },
8
+ ]
9
+
10
+ replacements.forEach(({ from, to }) => {
11
+ root
12
+ .find(jscodeshift.ImportDeclaration, {
13
+ source: {
14
+ value: from,
15
+ },
16
+ })
17
+ .replaceWith(({ node }) => {
18
+ node.source.value = to
19
+
20
+ return node
21
+ })
22
+ })
23
+
24
+ return root.toSource({ quote: 'single' })
25
+ }
@@ -0,0 +1,164 @@
1
+ class UnprocessableKeyError extends Error {
2
+ constructor(message) {
3
+ super(message)
4
+ this.name = 'UnprocessableKeyError'
5
+ }
6
+ }
7
+
8
+ module.exports = ({ jscodeshift, root, filePath, keyName = 'queryKey' }) => {
9
+ const isArrayExpression = (node) =>
10
+ jscodeshift.match(node, { type: jscodeshift.ArrayExpression.name })
11
+
12
+ const isStringLiteral = (node) =>
13
+ jscodeshift.match(node, { type: jscodeshift.StringLiteral.name }) ||
14
+ jscodeshift.match(node, { type: jscodeshift.Literal.name })
15
+
16
+ const isTemplateLiteral = (node) =>
17
+ jscodeshift.match(node, { type: jscodeshift.TemplateLiteral.name })
18
+
19
+ const findVariableDeclaration = (node) => {
20
+ const declarations = root
21
+ .find(jscodeshift.VariableDeclarator, {
22
+ id: {
23
+ type: jscodeshift.Identifier.name,
24
+ name: node.name,
25
+ },
26
+ })
27
+ .paths()
28
+
29
+ return declarations.length > 0 ? declarations[0] : null
30
+ }
31
+
32
+ const createKeyValue = (node) => {
33
+ // When the node is a string literal we convert it into an array of strings.
34
+ if (isStringLiteral(node)) {
35
+ return jscodeshift.arrayExpression([
36
+ jscodeshift.stringLiteral(node.value),
37
+ ])
38
+ }
39
+
40
+ // When the node is a template literal we convert it into an array of template literals.
41
+ if (isTemplateLiteral(node)) {
42
+ return jscodeshift.arrayExpression([
43
+ jscodeshift.templateLiteral(node.quasis, node.expressions),
44
+ ])
45
+ }
46
+
47
+ if (jscodeshift.match(node, { type: jscodeshift.Identifier.name })) {
48
+ // When the node is an identifier at first, we try to find its declaration, because we will try
49
+ // to guess its type.
50
+ const variableDeclaration = findVariableDeclaration(node)
51
+
52
+ if (!variableDeclaration) {
53
+ throw new UnprocessableKeyError(
54
+ `In file ${filePath} at line ${node.loc.start.line} the type of identifier \`${node.name}\` couldn't be recognized, so the codemod couldn't be applied. Please migrate manually.`,
55
+ )
56
+ }
57
+
58
+ const initializer = variableDeclaration.value.init
59
+
60
+ // When it's a string, we just wrap it into an array expression.
61
+ if (isStringLiteral(initializer) || isTemplateLiteral(initializer)) {
62
+ return jscodeshift.arrayExpression([node])
63
+ }
64
+ }
65
+
66
+ throw new UnprocessableKeyError(
67
+ `In file ${filePath} at line ${node.loc.start.line} the type of the \`${keyName}\` couldn't be recognized, so the codemod couldn't be applied. Please migrate manually.`,
68
+ )
69
+ }
70
+
71
+ const createKeyProperty = (node) =>
72
+ jscodeshift.property(
73
+ 'init',
74
+ jscodeshift.identifier(keyName),
75
+ createKeyValue(node),
76
+ )
77
+
78
+ const getPropertyFromObjectExpression = (objectExpression, propertyName) =>
79
+ objectExpression.properties.find(
80
+ (property) => property.key.name === propertyName,
81
+ ) ?? null
82
+
83
+ const buildWithTypeArguments = (node, builder) => {
84
+ const newNode = builder(node)
85
+
86
+ if (node.typeParameters) {
87
+ newNode.typeArguments = node.typeParameters
88
+ }
89
+
90
+ return newNode
91
+ }
92
+
93
+ return ({ node }) => {
94
+ // When the node doesn't have the 'original' property, that means the codemod has been already applied,
95
+ // so we don't need to do any changes.
96
+ if (!node.original) {
97
+ return node
98
+ }
99
+
100
+ const methodArguments = node.arguments
101
+
102
+ // The method call doesn't have any arguments, we have nothing to do in this case.
103
+ if (methodArguments.length === 0) {
104
+ return node
105
+ }
106
+
107
+ try {
108
+ const [firstArgument, ...restOfTheArguments] = methodArguments
109
+
110
+ if (
111
+ jscodeshift.match(firstArgument, {
112
+ type: jscodeshift.ObjectExpression.name,
113
+ })
114
+ ) {
115
+ const originalKey = getPropertyFromObjectExpression(
116
+ firstArgument,
117
+ keyName,
118
+ )
119
+
120
+ if (!originalKey) {
121
+ throw new UnprocessableKeyError(
122
+ `In file ${filePath} at line ${node.loc.start.line} the \`${keyName}\` couldn't be found. Did you forget to add it?`,
123
+ )
124
+ }
125
+
126
+ const restOfTheProperties = firstArgument.properties.filter(
127
+ (item) => item.key.name !== keyName,
128
+ )
129
+
130
+ return buildWithTypeArguments(node, (originalNode) =>
131
+ jscodeshift.callExpression(originalNode.original.callee, [
132
+ jscodeshift.objectExpression([
133
+ createKeyProperty(originalKey.value),
134
+ ...restOfTheProperties,
135
+ ]),
136
+ ...restOfTheArguments,
137
+ ]),
138
+ )
139
+ }
140
+
141
+ // When the node is an array expression we just simply return it because we want query keys to be arrays.
142
+ if (isArrayExpression(firstArgument)) {
143
+ return node
144
+ }
145
+
146
+ return buildWithTypeArguments(node, (originalNode) =>
147
+ jscodeshift.callExpression(originalNode.original.callee, [
148
+ createKeyValue(firstArgument),
149
+ ...restOfTheArguments,
150
+ ]),
151
+ )
152
+ } catch (error) {
153
+ if (error.name === 'UnprocessableKeyError') {
154
+ if (process.env.NODE_ENV !== 'test') {
155
+ console.warn(error.message)
156
+ }
157
+
158
+ return node
159
+ }
160
+
161
+ throw error
162
+ }
163
+ }
164
+ }
@@ -0,0 +1,199 @@
1
+ import * as React from 'react'
2
+ import {
3
+ useIsFetching,
4
+ useIsMutating,
5
+ useQueryClient,
6
+ } from '@tanstack/react-query'
7
+ import { queryKeysFromAnotherModule } from '../another/module'
8
+
9
+ export const WithKnownParameters = () => {
10
+ useIsFetching(['foo', 'bar'])
11
+ useIsFetching(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
12
+ useIsFetching(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { context: undefined })
13
+ useIsFetching(['foo', 'bar'], { type: 'all', exact: true })
14
+ useIsFetching(['foo', 'bar'], { type: 'all', exact: true }, { context: undefined })
15
+ useIsFetching({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
16
+ useIsFetching({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { context: undefined })
17
+
18
+ useIsMutating(['foo', 'bar'])
19
+ useIsMutating(['foo', 'bar'], { exact: true })
20
+ useIsMutating(['foo', 'bar'], { exact: true }, { context: undefined })
21
+ useIsMutating({ mutationKey: ['foo', 'bar'], exact: true })
22
+ useIsMutating({ mutationKey: ['foo', 'bar'], exact: true }, { context: undefined })
23
+
24
+ // QueryClient methods
25
+ // --- Instantiated hook call.
26
+ const queryClient = useQueryClient()
27
+ queryClient.cancelQueries(['foo', 'bar'])
28
+ queryClient.cancelQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
29
+ queryClient.cancelQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { silent: true })
30
+ queryClient.cancelQueries(['foo', 'bar'], { type: 'all', exact: true })
31
+ queryClient.cancelQueries(['foo', 'bar'], { type: 'all', exact: true }, { silent: true })
32
+ queryClient.cancelQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
33
+ queryClient.cancelQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { silent: true })
34
+
35
+ queryClient.getQueriesData(['foo', 'bar'])
36
+ queryClient.getQueriesData({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
37
+
38
+ queryClient.invalidateQueries(['foo', 'bar'])
39
+ queryClient.invalidateQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
40
+ queryClient.invalidateQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
41
+ queryClient.invalidateQueries(['foo', 'bar'], { type: 'all', exact: true })
42
+ queryClient.invalidateQueries(['foo', 'bar'], { type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
43
+ queryClient.invalidateQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
44
+ queryClient.invalidateQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
45
+
46
+ queryClient.isFetching(['foo', 'bar'])
47
+ queryClient.isFetching(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
48
+ queryClient.isFetching(['foo', 'bar'], { type: 'all', exact: true })
49
+ queryClient.isFetching({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
50
+
51
+ queryClient.refetchQueries(['foo', 'bar'])
52
+ queryClient.refetchQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
53
+ queryClient.refetchQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
54
+ queryClient.refetchQueries(['foo', 'bar'], { type: 'all', exact: true })
55
+ queryClient.refetchQueries(['foo', 'bar'], { type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
56
+ queryClient.refetchQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
57
+ queryClient.refetchQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
58
+
59
+ queryClient.removeQueries(['foo', 'bar'])
60
+ queryClient.removeQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
61
+ queryClient.removeQueries({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
62
+
63
+ queryClient.resetQueries(['foo', 'bar'])
64
+ queryClient.resetQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
65
+ queryClient.resetQueries(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
66
+ queryClient.resetQueries(['foo', 'bar'], { type: 'all', exact: true })
67
+ queryClient.resetQueries(['foo', 'bar'], { type: 'all', exact: true }, { cancelRefetch: false, throwOnError: true })
68
+ queryClient.resetQueries({ queryKey: ['foo', 'bar'], exact: true })
69
+ queryClient.resetQueries({ queryKey: ['foo', 'bar'], exact: true }, { cancelRefetch: false, throwOnError: true })
70
+
71
+ queryClient.setQueriesData(['foo', 'bar'], null)
72
+ queryClient.setQueriesData(['foo', 'bar'], null, { updatedAt: 1000 })
73
+ queryClient.setQueriesData({ queryKey: ['foo', 'bar'] }, null)
74
+ queryClient.setQueriesData({ queryKey: ['foo', 'bar'] }, null, { updatedAt: 1000 })
75
+
76
+ queryClient.fetchQuery(['foo', 'bar'])
77
+ queryClient.fetchQuery(['foo', 'bar'], { queryKey: ['todos'], staleTime: 1000 })
78
+ queryClient.fetchQuery(['foo', 'bar'], { queryKey: ['todos'], queryFn: () => 'data', staleTime: 1000 })
79
+ queryClient.fetchQuery(['foo', 'bar'], () => 'data', { queryKey: ['todos'], staleTime: 1000 })
80
+ queryClient.fetchQuery(['foo', 'bar'], function myFn() { return 'data' }, { queryKey: ['todos'], staleTime: 1000 })
81
+ queryClient.fetchQuery({ queryKey: ['foo', 'bar'], queryFn: () => 'data', retry: true })
82
+
83
+ const queryCache = queryClient.getQueryCache()
84
+
85
+ queryCache.find(['foo', 'bar'])
86
+ queryCache.find(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
87
+ queryCache.find(['foo', 'bar'], { type: 'all', exact: true })
88
+
89
+ queryCache.findAll(['foo', 'bar'])
90
+ queryCache.findAll(['foo', 'bar'], { type: 'all', exact: true })
91
+ queryCache.findAll(['foo', 'bar'], { queryKey: ['todos'], type: 'all', exact: true })
92
+ queryCache.findAll({ queryKey: ['foo', 'bar'], type: 'all', exact: true })
93
+
94
+ return <div>Example Component</div>
95
+ }
96
+
97
+ const globalQueryKey = ['module', 'level']
98
+
99
+ export const WithIdentifiers = () => {
100
+ const queryKey = ['foo', 'bar']
101
+ const mutationKey = ['posts', 'articles']
102
+ const filters = { type: 'all', exact: true } as const
103
+ const options = { context: undefined } as const
104
+ const mutationOptions = { exact: true, fetching: false } as const
105
+ const cancelOptions = { silent: true } as const
106
+ const invalidateOptions = { cancelRefetch: true, throwOnError: true } as const
107
+ const refetchOptions = { cancelRefetch: false, throwOnError: true } as const
108
+ const resetOptions = { cancelRefetch: false, throwOnError: true } as const
109
+ const fetchOptions = { queryFn: () => 'data', retry: true } as const
110
+ const queryFn = () => 'data'
111
+
112
+ useIsFetching(queryKey)
113
+ useIsFetching(queryKey, filters)
114
+ useIsFetching(queryKey, filters, options)
115
+ useIsFetching(queryKey, { type: 'all', exact: true })
116
+ useIsFetching(queryKey, { type: 'all', exact: true }, { context: undefined })
117
+ useIsFetching(queryKey, { queryKey: ['todos'], ...filters }, options)
118
+ useIsFetching({ queryKey: queryKey, ...filters })
119
+ useIsFetching({ queryKey: queryKey, ...filters }, { context: undefined })
120
+
121
+ useIsMutating(mutationKey)
122
+ useIsMutating(mutationKey, { exact: true, status: 'idle' })
123
+ useIsMutating(mutationKey, { ...mutationOptions, exact: false })
124
+ useIsMutating({ mutationKey, ...mutationOptions })
125
+ useIsMutating({ mutationKey: ['foo', 'bar'], exact: true, status: 'idle' })
126
+
127
+ // QueryClient methods
128
+ // --- Instantiated hook call.
129
+ const queryClient = useQueryClient()
130
+ queryClient.cancelQueries(queryKey)
131
+ queryClient.cancelQueries(queryKey, filters)
132
+ queryClient.cancelQueries(queryKey, filters, cancelOptions)
133
+ queryClient.cancelQueries(queryKey, { type: 'all', exact: true })
134
+ queryClient.cancelQueries(queryKey, { type: 'all', exact: true }, { revert: true })
135
+ queryClient.cancelQueries(queryKey, { queryKey: ['todos'], ...filters }, cancelOptions)
136
+ queryClient.cancelQueries({ queryKey: queryKey, type: 'all', exact: true })
137
+ queryClient.cancelQueries({ queryKey: ['foo', 'bar'], ...filters }, cancelOptions)
138
+
139
+ queryClient.getQueriesData(globalQueryKey)
140
+ queryClient.getQueriesData({ queryKey: globalQueryKey, ...filters })
141
+ queryClient.getQueriesData({ queryKey: ['foo', 'bar'], type: 'all' })
142
+
143
+ queryClient.invalidateQueries(queryKey)
144
+ queryClient.invalidateQueries(queryKey, filters)
145
+ queryClient.invalidateQueries(queryKey, filters, invalidateOptions)
146
+ queryClient.invalidateQueries(queryKey, { queryKey: ['todos'], stale: true, ...filters })
147
+ queryClient.invalidateQueries(queryKey, { queryKey: ['todos'], stale: true, ...filters }, invalidateOptions)
148
+ queryClient.invalidateQueries({ queryKey: globalQueryKey, ...filters, stale: true })
149
+ queryClient.invalidateQueries({ queryKey: globalQueryKey, ...filters, stale: true }, invalidateOptions)
150
+
151
+ queryClient.isFetching(globalQueryKey)
152
+ queryClient.isFetching(globalQueryKey, filters)
153
+ queryClient.isFetching(globalQueryKey, { queryKey: ['todos'], type: 'all', exact: true })
154
+ queryClient.isFetching(globalQueryKey, { queryKey: ['todos'], ...filters })
155
+ queryClient.isFetching({ queryKey: globalQueryKey, ...filters, stale: true })
156
+ // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
157
+ queryClient.isFetching(queryKeysFromAnotherModule)
158
+
159
+ queryClient.refetchQueries(queryKey)
160
+ queryClient.refetchQueries(queryKey, filters)
161
+ queryClient.refetchQueries(queryKey, filters, refetchOptions)
162
+ queryClient.refetchQueries(queryKey, { queryKey: ['todos'], ...filters }, { ...refetchOptions, cancelRefetch: true })
163
+ queryClient.refetchQueries({ queryKey: queryKey, ...filters })
164
+ queryClient.refetchQueries({ queryKey: queryKey, ...filters }, { ...refetchOptions, cancelRefetch: true })
165
+ // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
166
+ queryClient.refetchQueries(queryKeysFromAnotherModule)
167
+ queryClient.refetchQueries(queryKeysFromAnotherModule, filters)
168
+ queryClient.refetchQueries(queryKeysFromAnotherModule, filters, refetchOptions)
169
+
170
+ queryClient.removeQueries(queryKey)
171
+ queryClient.removeQueries(queryKey, filters)
172
+ queryClient.removeQueries(queryKey, { queryKey: ['todos'], ...filters, stale: true })
173
+ queryClient.removeQueries({ queryKey, ...filters, stale: true })
174
+ // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
175
+ queryClient.removeQueries(queryKeysFromAnotherModule)
176
+ queryClient.removeQueries(queryKeysFromAnotherModule, filters)
177
+
178
+ queryClient.resetQueries(queryKey)
179
+ queryClient.resetQueries(queryKey, filters)
180
+ queryClient.resetQueries(queryKey, filters, resetOptions)
181
+ queryClient.resetQueries(queryKey, { queryKey: ['todos'], ...filters, stale: true })
182
+ queryClient.resetQueries(queryKey, { queryKey: ['todos'], ...filters, stale: true }, resetOptions)
183
+ queryClient.resetQueries({ queryKey, ...filters, stale: true })
184
+ queryClient.resetQueries({ queryKey, ...filters, stale: true }, resetOptions)
185
+ // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
186
+ queryClient.resetQueries(queryKeysFromAnotherModule)
187
+ queryClient.resetQueries(queryKeysFromAnotherModule, filters)
188
+ queryClient.resetQueries(queryKeysFromAnotherModule, filters, resetOptions)
189
+
190
+ queryClient.fetchQuery(queryKey)
191
+ queryClient.fetchQuery(queryKey, fetchOptions)
192
+ queryClient.fetchQuery(queryKey, { networkMode: 'always', ...fetchOptions })
193
+ queryClient.fetchQuery(queryKey, queryFn, fetchOptions)
194
+ queryClient.fetchQuery(queryKey, () => 'data', { networkMode: 'always', ...fetchOptions })
195
+ // Stays as it is because the code couldn't infer the type of the "queryKeysFromAnotherModule" identifier.
196
+ queryClient.fetchQuery(queryKeysFromAnotherModule)
197
+ queryClient.fetchQuery(queryKeysFromAnotherModule, fetchOptions)
198
+ queryClient.fetchQuery(queryKeysFromAnotherModule, queryFn, fetchOptions)
199
+ }