@tanstack/react-query 4.0.5

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 (129) hide show
  1. package/build/cjs/query-core/build/esm/index.js +3110 -0
  2. package/build/cjs/query-core/build/esm/index.js.map +1 -0
  3. package/build/cjs/react-query/src/Hydrate.js +66 -0
  4. package/build/cjs/react-query/src/Hydrate.js.map +1 -0
  5. package/build/cjs/react-query/src/QueryClientProvider.js +96 -0
  6. package/build/cjs/react-query/src/QueryClientProvider.js.map +1 -0
  7. package/build/cjs/react-query/src/QueryErrorResetBoundary.js +67 -0
  8. package/build/cjs/react-query/src/QueryErrorResetBoundary.js.map +1 -0
  9. package/build/cjs/react-query/src/index.js +64 -0
  10. package/build/cjs/react-query/src/index.js.map +1 -0
  11. package/build/cjs/react-query/src/isRestoring.js +43 -0
  12. package/build/cjs/react-query/src/isRestoring.js.map +1 -0
  13. package/build/cjs/react-query/src/useBaseQuery.js +117 -0
  14. package/build/cjs/react-query/src/useBaseQuery.js.map +1 -0
  15. package/build/cjs/react-query/src/useInfiniteQuery.js +24 -0
  16. package/build/cjs/react-query/src/useInfiniteQuery.js.map +1 -0
  17. package/build/cjs/react-query/src/useIsFetching.js +50 -0
  18. package/build/cjs/react-query/src/useIsFetching.js.map +1 -0
  19. package/build/cjs/react-query/src/useIsMutating.js +50 -0
  20. package/build/cjs/react-query/src/useIsMutating.js.map +1 -0
  21. package/build/cjs/react-query/src/useMutation.js +68 -0
  22. package/build/cjs/react-query/src/useMutation.js.map +1 -0
  23. package/build/cjs/react-query/src/useQueries.js +71 -0
  24. package/build/cjs/react-query/src/useQueries.js.map +1 -0
  25. package/build/cjs/react-query/src/useQuery.js +24 -0
  26. package/build/cjs/react-query/src/useQuery.js.map +1 -0
  27. package/build/cjs/react-query/src/utils.js +25 -0
  28. package/build/cjs/react-query/src/utils.js.map +1 -0
  29. package/build/esm/index.js +3368 -0
  30. package/build/esm/index.js.map +1 -0
  31. package/build/stats-html.html +2689 -0
  32. package/build/stats.json +666 -0
  33. package/build/types/packages/query-core/src/focusManager.d.ts +16 -0
  34. package/build/types/packages/query-core/src/hydration.d.ts +34 -0
  35. package/build/types/packages/query-core/src/index.d.ts +20 -0
  36. package/build/types/packages/query-core/src/infiniteQueryBehavior.d.ts +15 -0
  37. package/build/types/packages/query-core/src/infiniteQueryObserver.d.ts +18 -0
  38. package/build/types/packages/query-core/src/logger.d.ts +8 -0
  39. package/build/types/packages/query-core/src/mutation.d.ts +70 -0
  40. package/build/types/packages/query-core/src/mutationCache.d.ts +52 -0
  41. package/build/types/packages/query-core/src/mutationObserver.d.ts +23 -0
  42. package/build/types/packages/query-core/src/notifyManager.d.ts +18 -0
  43. package/build/types/packages/query-core/src/onlineManager.d.ts +16 -0
  44. package/build/types/packages/query-core/src/queriesObserver.d.ts +23 -0
  45. package/build/types/packages/query-core/src/query.d.ts +119 -0
  46. package/build/types/packages/query-core/src/queryCache.d.ts +59 -0
  47. package/build/types/packages/query-core/src/queryClient.d.ts +65 -0
  48. package/build/types/packages/query-core/src/queryObserver.d.ts +61 -0
  49. package/build/types/packages/query-core/src/removable.d.ts +9 -0
  50. package/build/types/packages/query-core/src/retryer.d.ts +33 -0
  51. package/build/types/packages/query-core/src/subscribable.d.ts +10 -0
  52. package/build/types/packages/query-core/src/types.d.ts +417 -0
  53. package/build/types/packages/query-core/src/utils.d.ts +99 -0
  54. package/build/types/packages/react-query/src/Hydrate.d.ts +10 -0
  55. package/build/types/packages/react-query/src/QueryClientProvider.d.ts +24 -0
  56. package/build/types/packages/react-query/src/QueryErrorResetBoundary.d.ts +12 -0
  57. package/build/types/packages/react-query/src/__tests__/Hydrate.test.d.ts +1 -0
  58. package/build/types/packages/react-query/src/__tests__/QueryClientProvider.test.d.ts +1 -0
  59. package/build/types/packages/react-query/src/__tests__/QueryResetErrorBoundary.test.d.ts +6 -0
  60. package/build/types/packages/react-query/src/__tests__/ssr-hydration.test.d.ts +1 -0
  61. package/build/types/packages/react-query/src/__tests__/ssr.test.d.ts +4 -0
  62. package/build/types/packages/react-query/src/__tests__/suspense.test.d.ts +1 -0
  63. package/build/types/packages/react-query/src/__tests__/useInfiniteQuery.test.d.ts +1 -0
  64. package/build/types/packages/react-query/src/__tests__/useIsFetching.test.d.ts +1 -0
  65. package/build/types/packages/react-query/src/__tests__/useIsMutating.test.d.ts +1 -0
  66. package/build/types/packages/react-query/src/__tests__/useMutation.test.d.ts +1 -0
  67. package/build/types/packages/react-query/src/__tests__/useQueries.test.d.ts +1 -0
  68. package/build/types/packages/react-query/src/__tests__/useQuery.test.d.ts +1 -0
  69. package/build/types/packages/react-query/src/__tests__/useQuery.types.test.d.ts +2 -0
  70. package/build/types/packages/react-query/src/__tests__/utils.d.ts +8 -0
  71. package/build/types/packages/react-query/src/index.d.ts +17 -0
  72. package/build/types/packages/react-query/src/isRestoring.d.ts +3 -0
  73. package/build/types/packages/react-query/src/reactBatchedUpdates.d.ts +2 -0
  74. package/build/types/packages/react-query/src/reactBatchedUpdates.native.d.ts +2 -0
  75. package/build/types/packages/react-query/src/setBatchUpdatesFn.d.ts +1 -0
  76. package/build/types/packages/react-query/src/types.d.ts +35 -0
  77. package/build/types/packages/react-query/src/useBaseQuery.d.ts +3 -0
  78. package/build/types/packages/react-query/src/useInfiniteQuery.d.ts +5 -0
  79. package/build/types/packages/react-query/src/useIsFetching.d.ts +7 -0
  80. package/build/types/packages/react-query/src/useIsMutating.d.ts +7 -0
  81. package/build/types/packages/react-query/src/useMutation.d.ts +6 -0
  82. package/build/types/packages/react-query/src/useQueries.d.ts +49 -0
  83. package/build/types/packages/react-query/src/useQuery.d.ts +20 -0
  84. package/build/types/packages/react-query/src/utils.d.ts +1 -0
  85. package/build/types/tests/utils.d.ts +24 -0
  86. package/build/umd/index.development.js +3429 -0
  87. package/build/umd/index.development.js.map +1 -0
  88. package/build/umd/index.production.js +22 -0
  89. package/build/umd/index.production.js.map +1 -0
  90. package/codemods/v4/key-transformation.js +138 -0
  91. package/codemods/v4/replace-import-specifier.js +25 -0
  92. package/codemods/v4/utils/index.js +166 -0
  93. package/codemods/v4/utils/replacers/key-replacer.js +160 -0
  94. package/codemods/v4/utils/transformers/query-cache-transformer.js +115 -0
  95. package/codemods/v4/utils/transformers/query-client-transformer.js +49 -0
  96. package/codemods/v4/utils/transformers/use-query-like-transformer.js +32 -0
  97. package/codemods/v4/utils/unprocessable-key-error.js +8 -0
  98. package/package.json +63 -0
  99. package/src/Hydrate.tsx +36 -0
  100. package/src/QueryClientProvider.tsx +90 -0
  101. package/src/QueryErrorResetBoundary.tsx +52 -0
  102. package/src/__tests__/Hydrate.test.tsx +247 -0
  103. package/src/__tests__/QueryClientProvider.test.tsx +275 -0
  104. package/src/__tests__/QueryResetErrorBoundary.test.tsx +630 -0
  105. package/src/__tests__/ssr-hydration.test.tsx +274 -0
  106. package/src/__tests__/ssr.test.tsx +151 -0
  107. package/src/__tests__/suspense.test.tsx +1015 -0
  108. package/src/__tests__/useInfiniteQuery.test.tsx +1773 -0
  109. package/src/__tests__/useIsFetching.test.tsx +274 -0
  110. package/src/__tests__/useIsMutating.test.tsx +260 -0
  111. package/src/__tests__/useMutation.test.tsx +1099 -0
  112. package/src/__tests__/useQueries.test.tsx +1107 -0
  113. package/src/__tests__/useQuery.test.tsx +5746 -0
  114. package/src/__tests__/useQuery.types.test.tsx +157 -0
  115. package/src/__tests__/utils.tsx +45 -0
  116. package/src/index.ts +29 -0
  117. package/src/isRestoring.tsx +6 -0
  118. package/src/reactBatchedUpdates.native.ts +4 -0
  119. package/src/reactBatchedUpdates.ts +2 -0
  120. package/src/setBatchUpdatesFn.ts +4 -0
  121. package/src/types.ts +122 -0
  122. package/src/useBaseQuery.ts +140 -0
  123. package/src/useInfiniteQuery.ts +101 -0
  124. package/src/useIsFetching.ts +39 -0
  125. package/src/useIsMutating.ts +43 -0
  126. package/src/useMutation.ts +126 -0
  127. package/src/useQueries.ts +192 -0
  128. package/src/useQuery.ts +104 -0
  129. package/src/utils.ts +11 -0
@@ -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,166 @@
1
+ module.exports = ({ root, jscodeshift }) => {
2
+ const findImportIdentifierOf = (importSpecifiers, identifier) => {
3
+ const specifier = importSpecifiers
4
+ .filter(node => node.value.imported.name === identifier)
5
+ .paths()
6
+
7
+ if (specifier.length > 0) {
8
+ return specifier[0].value.local
9
+ }
10
+
11
+ return jscodeshift.identifier(identifier)
12
+ }
13
+
14
+ const findImportSpecifiers = () =>
15
+ root
16
+ .find(jscodeshift.ImportDeclaration, {
17
+ source: {
18
+ value: 'react-query',
19
+ },
20
+ })
21
+ .find(jscodeshift.ImportSpecifier, {})
22
+
23
+ const locateImports = identifiers => {
24
+ const findNamespaceImportIdentifier = () => {
25
+ const specifier = root
26
+ .find(jscodeshift.ImportDeclaration, {
27
+ source: {
28
+ value: 'react-query',
29
+ },
30
+ })
31
+ .find(jscodeshift.ImportNamespaceSpecifier)
32
+ .paths()
33
+
34
+ return specifier.length > 0 ? specifier[0].value.local : null
35
+ }
36
+
37
+ /**
38
+ * First, we search for the namespace import identifier because if we have any, we assume the consumer uses
39
+ * namespace imports. In this case, we won't search for named imports at all.
40
+ */
41
+ const namespaceImportIdentifier = findNamespaceImportIdentifier()
42
+
43
+ if (namespaceImportIdentifier) {
44
+ const identifierMap = {}
45
+
46
+ for (const identifier of identifiers) {
47
+ identifierMap[identifier] = jscodeshift.identifier(identifier)
48
+ }
49
+
50
+ return {
51
+ namespace: namespaceImportIdentifier,
52
+ ...identifierMap,
53
+ }
54
+ }
55
+
56
+ const importSpecifiers = findImportSpecifiers()
57
+ const identifierMap = {}
58
+
59
+ for (const identifier of identifiers) {
60
+ identifierMap[identifier] = findImportIdentifierOf(
61
+ importSpecifiers,
62
+ identifier
63
+ )
64
+ }
65
+
66
+ return {
67
+ namespace: null,
68
+ ...identifierMap,
69
+ }
70
+ }
71
+
72
+ const findAllMethodCalls = () =>
73
+ root
74
+ // First, we need to find all method calls.
75
+ .find(jscodeshift.CallExpression, {
76
+ callee: {
77
+ type: jscodeshift.MemberExpression.name,
78
+ property: {
79
+ type: jscodeshift.Identifier.name,
80
+ },
81
+ },
82
+ })
83
+
84
+ const findQueryClientIdentifiers = importIdentifiers =>
85
+ root
86
+ .find(jscodeshift.VariableDeclarator, {})
87
+ .filter(node => {
88
+ if (node.value.init) {
89
+ const initializer = node.value.init
90
+
91
+ return (
92
+ isClassInstantiationOf(
93
+ initializer,
94
+ getSelectorByImports(importIdentifiers, 'QueryClient')
95
+ ) ||
96
+ isFunctionCallOf(
97
+ initializer,
98
+ getSelectorByImports(importIdentifiers, 'useQueryClient')
99
+ )
100
+ )
101
+ }
102
+
103
+ return false
104
+ })
105
+ .paths()
106
+ .map(node => node.value.id.name)
107
+
108
+ const isCallExpression = node =>
109
+ jscodeshift.match(node, { type: jscodeshift.CallExpression.name })
110
+
111
+ const isIdentifier = node =>
112
+ jscodeshift.match(node, { type: jscodeshift.Identifier.name })
113
+
114
+ const isMemberExpression = node =>
115
+ jscodeshift.match(node, { type: jscodeshift.MemberExpression.name })
116
+
117
+ const isNewExpression = node =>
118
+ jscodeshift.match(node, { type: jscodeshift.NewExpression.name })
119
+
120
+ const isClassInstantiationOf = (node, selector) => {
121
+ if (!isNewExpression(node)) {
122
+ return false
123
+ }
124
+
125
+ const parts = selector.split('.')
126
+
127
+ return parts.length === 1
128
+ ? isIdentifier(node.callee) && node.callee.name === parts[0]
129
+ : isMemberExpression(node.callee) &&
130
+ node.callee.object.name === parts[0] &&
131
+ node.callee.property.name === parts[1]
132
+ }
133
+
134
+ const isFunctionCallOf = (node, selector) => {
135
+ if (!isCallExpression(node)) {
136
+ return false
137
+ }
138
+
139
+ const parts = selector.split('.')
140
+
141
+ return parts.length === 1
142
+ ? isIdentifier(node.callee) && node.callee.name === parts[0]
143
+ : isMemberExpression(node.callee) &&
144
+ node.callee.object.name === parts[0] &&
145
+ node.callee.property.name === parts[1]
146
+ }
147
+
148
+ const getSelectorByImports = (imports, path) =>
149
+ imports.namespace
150
+ ? `${imports.namespace.name}.${imports[path].name}`
151
+ : imports[path].name
152
+
153
+ return {
154
+ findAllMethodCalls,
155
+ getSelectorByImports,
156
+ isCallExpression,
157
+ isClassInstantiationOf,
158
+ isFunctionCallOf,
159
+ isIdentifier,
160
+ isMemberExpression,
161
+ locateImports,
162
+ queryClient: {
163
+ findQueryClientIdentifiers,
164
+ },
165
+ }
166
+ }
@@ -0,0 +1,160 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
2
+ const UnprocessableKeyError = require('../unprocessable-key-error')
3
+
4
+ module.exports = ({ jscodeshift, root, filePath, keyName = 'queryKey' }) => {
5
+ const isArrayExpression = node =>
6
+ jscodeshift.match(node, { type: jscodeshift.ArrayExpression.name })
7
+
8
+ const isStringLiteral = node =>
9
+ jscodeshift.match(node, { type: jscodeshift.StringLiteral.name }) ||
10
+ jscodeshift.match(node, { type: jscodeshift.Literal.name })
11
+
12
+ const isTemplateLiteral = node =>
13
+ jscodeshift.match(node, { type: jscodeshift.TemplateLiteral.name })
14
+
15
+ const findVariableDeclaration = node => {
16
+ const declarations = root
17
+ .find(jscodeshift.VariableDeclarator, {
18
+ id: {
19
+ type: jscodeshift.Identifier.name,
20
+ name: node.name,
21
+ },
22
+ })
23
+ .paths()
24
+
25
+ return declarations.length > 0 ? declarations[0] : null
26
+ }
27
+
28
+ const createKeyValue = node => {
29
+ // When the node is a string literal we convert it into an array of strings.
30
+ if (isStringLiteral(node)) {
31
+ return jscodeshift.arrayExpression([
32
+ jscodeshift.stringLiteral(node.value),
33
+ ])
34
+ }
35
+
36
+ // When the node is a template literal we convert it into an array of template literals.
37
+ if (isTemplateLiteral(node)) {
38
+ return jscodeshift.arrayExpression([
39
+ jscodeshift.templateLiteral(node.quasis, node.expressions),
40
+ ])
41
+ }
42
+
43
+ if (jscodeshift.match(node, { type: jscodeshift.Identifier.name })) {
44
+ // When the node is an identifier at first, we try to find its declaration, because we will try
45
+ // to guess its type.
46
+ const variableDeclaration = findVariableDeclaration(node)
47
+
48
+ if (!variableDeclaration) {
49
+ throw new UnprocessableKeyError(
50
+ `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.`
51
+ )
52
+ }
53
+
54
+ const initializer = variableDeclaration.value.init
55
+
56
+ // When it's a string, we just wrap it into an array expression.
57
+ if (isStringLiteral(initializer) || isTemplateLiteral(initializer)) {
58
+ return jscodeshift.arrayExpression([node])
59
+ }
60
+ }
61
+
62
+ throw new UnprocessableKeyError(
63
+ `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.`
64
+ )
65
+ }
66
+
67
+ const createKeyProperty = node =>
68
+ jscodeshift.property(
69
+ 'init',
70
+ jscodeshift.identifier(keyName),
71
+ createKeyValue(node)
72
+ )
73
+
74
+ const getPropertyFromObjectExpression = (objectExpression, propertyName) =>
75
+ objectExpression.properties.find(
76
+ property => property.key.name === propertyName
77
+ ) ?? null
78
+
79
+ const buildWithTypeArguments = (node, builder) => {
80
+ const newNode = builder(node)
81
+
82
+ if (node.typeParameters) {
83
+ newNode.typeArguments = node.typeParameters
84
+ }
85
+
86
+ return newNode
87
+ }
88
+
89
+ return ({ node }) => {
90
+ // When the node doesn't have the 'original' property, that means the codemod has been already applied,
91
+ // so we don't need to do any changes.
92
+ if (!node.original) {
93
+ return node
94
+ }
95
+
96
+ const methodArguments = node.arguments
97
+
98
+ // The method call doesn't have any arguments, we have nothing to do in this case.
99
+ if (methodArguments.length === 0) {
100
+ return node
101
+ }
102
+
103
+ try {
104
+ const [firstArgument, ...restOfTheArguments] = methodArguments
105
+
106
+ if (
107
+ jscodeshift.match(firstArgument, {
108
+ type: jscodeshift.ObjectExpression.name,
109
+ })
110
+ ) {
111
+ const originalKey = getPropertyFromObjectExpression(
112
+ firstArgument,
113
+ keyName
114
+ )
115
+
116
+ if (!originalKey) {
117
+ throw new UnprocessableKeyError(
118
+ `In file ${filePath} at line ${node.loc.start.line} the \`${keyName}\` couldn't be found. Did you forget to add it?`
119
+ )
120
+ }
121
+
122
+ const restOfTheProperties = firstArgument.properties.filter(
123
+ item => item.key.name !== keyName
124
+ )
125
+
126
+ return buildWithTypeArguments(node, originalNode =>
127
+ jscodeshift.callExpression(originalNode.original.callee, [
128
+ jscodeshift.objectExpression([
129
+ createKeyProperty(originalKey.value),
130
+ ...restOfTheProperties,
131
+ ]),
132
+ ...restOfTheArguments,
133
+ ])
134
+ )
135
+ }
136
+
137
+ // When the node is an array expression we just simply return it because we want query keys to be arrays.
138
+ if (isArrayExpression(firstArgument)) {
139
+ return node
140
+ }
141
+
142
+ return buildWithTypeArguments(node, originalNode =>
143
+ jscodeshift.callExpression(originalNode.original.callee, [
144
+ createKeyValue(firstArgument),
145
+ ...restOfTheArguments,
146
+ ])
147
+ )
148
+ } catch (error) {
149
+ if (error.name === 'UnprocessableKeyError') {
150
+ if (process.env.NODE_ENV !== 'test') {
151
+ console.warn(error.message)
152
+ }
153
+
154
+ return node
155
+ }
156
+
157
+ throw error
158
+ }
159
+ }
160
+ }
@@ -0,0 +1,115 @@
1
+ module.exports = ({ jscodeshift, utils, root }) => {
2
+ const isGetQueryCacheMethodCall = (
3
+ initializer,
4
+ importIdentifiers,
5
+ knownQueryClientIds
6
+ ) => {
7
+ const isKnownQueryClient = node =>
8
+ utils.isIdentifier(node) && knownQueryClientIds.includes(node.name)
9
+
10
+ const isGetQueryCacheIdentifier = node =>
11
+ utils.isIdentifier(node) && node.name === 'getQueryCache'
12
+
13
+ const isValidInitializer = node =>
14
+ utils.isCallExpression(node) && utils.isMemberExpression(node.callee)
15
+
16
+ if (isValidInitializer(initializer)) {
17
+ const instance = initializer.callee.object
18
+
19
+ return (
20
+ isGetQueryCacheIdentifier(initializer.callee.property) &&
21
+ (isKnownQueryClient(instance) ||
22
+ utils.isFunctionCallOf(
23
+ instance,
24
+ utils.getSelectorByImports(importIdentifiers, 'useQueryClient')
25
+ ))
26
+ )
27
+ }
28
+
29
+ return false
30
+ }
31
+
32
+ const findQueryCacheInstantiations = (
33
+ importIdentifiers,
34
+ knownQueryClientIds
35
+ ) =>
36
+ root.find(jscodeshift.VariableDeclarator, {}).filter(node => {
37
+ if (node.value.init) {
38
+ const initializer = node.value.init
39
+
40
+ return (
41
+ utils.isClassInstantiationOf(
42
+ initializer,
43
+ utils.getSelectorByImports(importIdentifiers, 'QueryCache')
44
+ ) ||
45
+ isGetQueryCacheMethodCall(
46
+ initializer,
47
+ importIdentifiers,
48
+ knownQueryClientIds
49
+ )
50
+ )
51
+ }
52
+
53
+ return false
54
+ })
55
+
56
+ const filterQueryCacheMethodCalls = node =>
57
+ utils.isIdentifier(node) && ['find', 'findAll'].includes(node.name)
58
+
59
+ const findQueryCacheMethodCalls = importIdentifiers => {
60
+ /**
61
+ * Here we collect all query client instantiations. We have to make aware of them because the query cache can be
62
+ * accessed by the query client as well.
63
+ */
64
+ const queryClientIdentifiers = utils.queryClient.findQueryClientIdentifiers(
65
+ importIdentifiers
66
+ )
67
+ /**
68
+ * Here we collect all query cache instantiations. The reason is simple: the methods can be called on query cache
69
+ * instances, to locate the possible usages we need to be aware of the identifier names.
70
+ */
71
+ const queryCacheIdentifiers = findQueryCacheInstantiations(
72
+ importIdentifiers,
73
+ queryClientIdentifiers
74
+ )
75
+ .paths()
76
+ .map(node => node.value.id.name)
77
+
78
+ return (
79
+ utils
80
+ // First, we need to find all method calls.
81
+ .findAllMethodCalls()
82
+ // Then we narrow the collection to all `fetch` and `fetchAll` methods.
83
+ .filter(node => filterQueryCacheMethodCalls(node.value.callee.property))
84
+ .filter(node => {
85
+ const object = node.value.callee.object
86
+
87
+ // If the method is called on a `QueryCache` instance, we keep it in the collection.
88
+ if (utils.isIdentifier(object)) {
89
+ return queryCacheIdentifiers.includes(object.name)
90
+ }
91
+
92
+ // If the method is called on a `QueryClient` instance, we keep it in the collection.
93
+ if (utils.isCallExpression(object)) {
94
+ return isGetQueryCacheMethodCall(
95
+ object,
96
+ importIdentifiers,
97
+ queryClientIdentifiers
98
+ )
99
+ }
100
+
101
+ return false
102
+ })
103
+ )
104
+ }
105
+
106
+ const execute = replacer => {
107
+ findQueryCacheMethodCalls(
108
+ utils.locateImports(['QueryCache', 'QueryClient', 'useQueryClient'])
109
+ ).replaceWith(replacer)
110
+ }
111
+
112
+ return {
113
+ execute,
114
+ }
115
+ }
@@ -0,0 +1,49 @@
1
+ module.exports = ({ jscodeshift, utils, root }) => {
2
+ const filterQueryClientMethodCalls = (node, methods) =>
3
+ utils.isIdentifier(node) && methods.includes(node.name)
4
+
5
+ const findQueryClientMethodCalls = (importIdentifiers, methods) => {
6
+ /**
7
+ * Here we collect all query client instantiations. We have to make aware of them because some method calls might
8
+ * be invoked on these instances.
9
+ */
10
+ const queryClientIdentifiers = utils.queryClient.findQueryClientIdentifiers(
11
+ importIdentifiers
12
+ )
13
+
14
+ return (
15
+ utils
16
+ // First, we need to find all method calls.
17
+ .findAllMethodCalls()
18
+ // Then we narrow the collection to `QueryClient` methods.
19
+ .filter(node =>
20
+ filterQueryClientMethodCalls(node.value.callee.property, methods)
21
+ )
22
+ .filter(node => {
23
+ const object = node.value.callee.object
24
+
25
+ // If the method is called on a `QueryClient` instance, we keep it in the collection.
26
+ if (utils.isIdentifier(object)) {
27
+ return queryClientIdentifiers.includes(object.name)
28
+ }
29
+
30
+ // If the method is called on the return value of `useQueryClient` hook, we keep it in the collection.
31
+ return utils.isFunctionCallOf(
32
+ object,
33
+ utils.getSelectorByImports(importIdentifiers, 'useQueryClient')
34
+ )
35
+ })
36
+ )
37
+ }
38
+
39
+ const execute = (methods, replacer) => {
40
+ findQueryClientMethodCalls(
41
+ utils.locateImports(['QueryClient', 'useQueryClient']),
42
+ methods
43
+ ).replaceWith(replacer)
44
+ }
45
+
46
+ return {
47
+ execute,
48
+ }
49
+ }
@@ -0,0 +1,32 @@
1
+ module.exports = ({ jscodeshift, utils, root }) => {
2
+ const filterUseQueryLikeHookCalls = (node, importIdentifiers, hooks) => {
3
+ for (const hook of hooks) {
4
+ const selector = utils.getSelectorByImports(importIdentifiers, hook)
5
+
6
+ if (utils.isFunctionCallOf(node, selector)) {
7
+ return true
8
+ }
9
+ }
10
+
11
+ return false
12
+ }
13
+
14
+ const findUseQueryLikeHookCalls = (importIdentifiers, hooks) =>
15
+ root
16
+ // First, we need to find all call expressions.
17
+ .find(jscodeshift.CallExpression, {})
18
+ // Then we narrow the collection to the `useQuery` like hook calls.
19
+ .filter(node =>
20
+ filterUseQueryLikeHookCalls(node.value, importIdentifiers, hooks)
21
+ )
22
+
23
+ const execute = (hooks, replacer) => {
24
+ findUseQueryLikeHookCalls(utils.locateImports(hooks), hooks).replaceWith(
25
+ replacer
26
+ )
27
+ }
28
+
29
+ return {
30
+ execute,
31
+ }
32
+ }
@@ -0,0 +1,8 @@
1
+ class UnprocessableKeyError extends Error {
2
+ constructor(message) {
3
+ super(message)
4
+ this.name = 'UnprocessableKeyError'
5
+ }
6
+ }
7
+
8
+ module.exports = UnprocessableKeyError