Skip to content

Commit e6f0bcc

Browse files
fix: do not mutate input (#5240)
* fix: do not mutate input params passed to function from consumers should be treated as readonly and thus shouldn't be modified * test: remove unexpected `exact` props from find methods --------- Co-authored-by: Damian Osipiuk <[email protected]>
1 parent 5940415 commit e6f0bcc

File tree

5 files changed

+10
-16
lines changed

5 files changed

+10
-16
lines changed

packages/query-core/src/mutationCache.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
139139
>(
140140
filters: MutationFilters,
141141
): Mutation<TData, TError, TVariables, TContext> | undefined {
142-
if (typeof filters.exact === 'undefined') {
143-
filters.exact = true
144-
}
142+
const defaultedFilters = { exact: true, ...filters }
145143

146-
return this.#mutations.find((mutation) => matchMutation(filters, mutation))
144+
return this.#mutations.find((mutation) =>
145+
matchMutation(defaultedFilters, mutation),
146+
)
147147
}
148148

149149
findAll(filters: MutationFilters = {}): Mutation[] {

packages/query-core/src/queryCache.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,11 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
176176
find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(
177177
filters: WithRequired<QueryFilters, 'queryKey'>,
178178
): Query<TQueryFnData, TError, TData> | undefined {
179-
if (typeof filters.exact === 'undefined') {
180-
filters.exact = true
181-
}
179+
const defaultedFilters = { exact: true, ...filters }
182180

183-
return this.getAll().find((query) => matchQuery(filters, query)) as
184-
| Query<TQueryFnData, TError, TData>
185-
| undefined
181+
return this.getAll().find((query) =>
182+
matchQuery(defaultedFilters, query),
183+
) as Query<TQueryFnData, TError, TData> | undefined
186184
}
187185

188186
findAll(filters: QueryFilters = {}): Query[] {

packages/query-core/src/queryClient.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,12 @@ export class QueryClient {
212212
filters: QueryFilters = {},
213213
cancelOptions: CancelOptions = {},
214214
): Promise<void> {
215-
if (typeof cancelOptions.revert === 'undefined') {
216-
cancelOptions.revert = true
217-
}
215+
const defaultedCancelOptions = { revert: true, ...cancelOptions }
218216

219217
const promises = notifyManager.batch(() =>
220218
this.#queryCache
221219
.findAll(filters)
222-
.map((query) => query.cancel(cancelOptions)),
220+
.map((query) => query.cancel(defaultedCancelOptions)),
223221
)
224222

225223
return Promise.all(promises).then(noop).catch(noop)

packages/vue-query/src/__tests__/mutationCache.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ describe('MutationCache', () => {
1919
})
2020

2121
expect(MutationCacheOrigin.prototype.find).toBeCalledWith({
22-
exact: true,
2322
mutationKey: ['baz'],
2423
})
2524
})

packages/vue-query/src/__tests__/queryCache.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ describe('QueryCache', () => {
2020

2121
expect(QueryCacheOrigin.prototype.find).toBeCalledWith({
2222
queryKey: ['foo', 'bar'],
23-
exact: true, //Exact is true, as `find` in QueryCacheOrigin sets exact to true in the passed filters if exact is undefined
2423
})
2524
})
2625
})

0 commit comments

Comments
 (0)