@tanstack/react-query 4.0.10 → 4.1.3

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.
@@ -27,12 +27,17 @@ interface NotifyEventMutationObserverRemoved {
27
27
  mutation: Mutation<any, any, any, any>;
28
28
  observer: MutationObserver<any, any, any>;
29
29
  }
30
+ interface NotifyEventMutationObserverOptionsUpdated {
31
+ type: 'observerOptionsUpdated';
32
+ mutation?: Mutation<any, any, any, any>;
33
+ observer: MutationObserver<any, any, any, any>;
34
+ }
30
35
  interface NotifyEventMutationUpdated {
31
36
  type: 'updated';
32
37
  mutation: Mutation<any, any, any, any>;
33
38
  action: Action<any, any, any, any>;
34
39
  }
35
- declare type MutationCacheNotifyEvent = NotifyEventMutationAdded | NotifyEventMutationRemoved | NotifyEventMutationObserverAdded | NotifyEventMutationObserverRemoved | NotifyEventMutationUpdated;
40
+ declare type MutationCacheNotifyEvent = NotifyEventMutationAdded | NotifyEventMutationRemoved | NotifyEventMutationObserverAdded | NotifyEventMutationObserverRemoved | NotifyEventMutationObserverOptionsUpdated | NotifyEventMutationUpdated;
36
41
  declare type MutationCacheListener = (event: MutationCacheNotifyEvent) => void;
37
42
  export declare class MutationCache extends Subscribable<MutationCacheListener> {
38
43
  config: MutationCacheConfig;
@@ -35,7 +35,12 @@ interface NotifyEventQueryObserverResultsUpdated {
35
35
  type: 'observerResultsUpdated';
36
36
  query: Query<any, any, any, any>;
37
37
  }
38
- declare type QueryCacheNotifyEvent = NotifyEventQueryAdded | NotifyEventQueryRemoved | NotifyEventQueryUpdated | NotifyEventQueryObserverAdded | NotifyEventQueryObserverRemoved | NotifyEventQueryObserverResultsUpdated;
38
+ interface NotifyEventQueryObserverOptionsUpdated {
39
+ type: 'observerOptionsUpdated';
40
+ query: Query<any, any, any, any>;
41
+ observer: QueryObserver<any, any, any, any, any>;
42
+ }
43
+ declare type QueryCacheNotifyEvent = NotifyEventQueryAdded | NotifyEventQueryRemoved | NotifyEventQueryUpdated | NotifyEventQueryObserverAdded | NotifyEventQueryObserverRemoved | NotifyEventQueryObserverResultsUpdated | NotifyEventQueryObserverOptionsUpdated;
39
44
  declare type QueryCacheListener = (event: QueryCacheNotifyEvent) => void;
40
45
  export declare class QueryCache extends Subscribable<QueryCacheListener> {
41
46
  config: QueryCacheConfig;
@@ -274,7 +274,8 @@ export interface QueryObserverSuccessResult<TData = unknown, TError = unknown> e
274
274
  isSuccess: true;
275
275
  status: 'success';
276
276
  }
277
- export declare type QueryObserverResult<TData = unknown, TError = unknown> = QueryObserverLoadingErrorResult<TData, TError> | QueryObserverLoadingResult<TData, TError> | QueryObserverRefetchErrorResult<TData, TError> | QueryObserverSuccessResult<TData, TError>;
277
+ export declare type DefinedQueryObserverResult<TData = unknown, TError = unknown> = QueryObserverRefetchErrorResult<TData, TError> | QueryObserverSuccessResult<TData, TError>;
278
+ export declare type QueryObserverResult<TData = unknown, TError = unknown> = DefinedQueryObserverResult<TData, TError> | QueryObserverLoadingErrorResult<TData, TError> | QueryObserverLoadingResult<TData, TError>;
278
279
  export interface InfiniteQueryObserverBaseResult<TData = unknown, TError = unknown> extends QueryObserverBaseResult<InfiniteData<TData>, TError> {
279
280
  fetchNextPage: (options?: FetchNextPageOptions) => Promise<InfiniteQueryObserverResult<TData, TError>>;
280
281
  fetchPreviousPage: (options?: FetchPreviousPageOptions) => Promise<InfiniteQueryObserverResult<TData, TError>>;
@@ -333,9 +334,9 @@ export interface MutationOptions<TData = unknown, TError = unknown, TVariables =
333
334
  mutationKey?: MutationKey;
334
335
  variables?: TVariables;
335
336
  onMutate?: (variables: TVariables) => Promise<TContext | undefined> | TContext | undefined;
336
- onSuccess?: (data: TData, variables: TVariables, context: TContext | undefined) => Promise<unknown> | void;
337
- onError?: (error: TError, variables: TVariables, context: TContext | undefined) => Promise<unknown> | void;
338
- onSettled?: (data: TData | undefined, error: TError | null, variables: TVariables, context: TContext | undefined) => Promise<unknown> | void;
337
+ onSuccess?: (data: TData, variables: TVariables, context: TContext | undefined) => Promise<unknown> | unknown;
338
+ onError?: (error: TError, variables: TVariables, context: TContext | undefined) => Promise<unknown> | unknown;
339
+ onSettled?: (data: TData | undefined, error: TError | null, variables: TVariables, context: TContext | undefined) => Promise<unknown> | unknown;
339
340
  retry?: RetryValue<TError>;
340
341
  retryDelay?: RetryDelayValue<TError>;
341
342
  networkMode?: NetworkMode;
@@ -347,9 +348,9 @@ export interface MutationObserverOptions<TData = unknown, TError = unknown, TVar
347
348
  useErrorBoundary?: boolean | ((error: TError) => boolean);
348
349
  }
349
350
  export interface MutateOptions<TData = unknown, TError = unknown, TVariables = void, TContext = unknown> {
350
- onSuccess?: (data: TData, variables: TVariables, context: TContext) => Promise<unknown> | void;
351
- onError?: (error: TError, variables: TVariables, context: TContext | undefined) => Promise<unknown> | void;
352
- onSettled?: (data: TData | undefined, error: TError | null, variables: TVariables, context: TContext | undefined) => Promise<unknown> | void;
351
+ onSuccess?: (data: TData, variables: TVariables, context: TContext) => Promise<unknown> | unknown;
352
+ onError?: (error: TError, variables: TVariables, context: TContext | undefined) => Promise<unknown> | unknown;
353
+ onSettled?: (data: TData | undefined, error: TError | null, variables: TVariables, context: TContext | undefined) => Promise<unknown> | unknown;
353
354
  }
354
355
  export declare type MutateFunction<TData = unknown, TError = unknown, TVariables = void, TContext = unknown> = (variables: TVariables, options?: MutateOptions<TData, TError, TVariables, TContext>) => Promise<TData>;
355
356
  export interface MutationObserverBaseResult<TData = unknown, TError = unknown, TVariables = void, TContext = unknown> extends MutationState<TData, TError, TVariables, TContext> {
@@ -1,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import { InfiniteQueryObserverOptions, InfiniteQueryObserverResult, MutationObserverResult, QueryObserverOptions, QueryObserverResult, QueryKey, MutationObserverOptions, MutateFunction } from '@tanstack/query-core';
2
+ import { InfiniteQueryObserverOptions, InfiniteQueryObserverResult, MutationObserverResult, QueryObserverOptions, QueryObserverResult, QueryKey, MutationObserverOptions, MutateFunction, DefinedQueryObserverResult } from '@tanstack/query-core';
3
3
  import type { QueryClient } from '@tanstack/query-core';
4
4
  export interface ContextOptions {
5
5
  /**
@@ -15,9 +15,8 @@ export interface UseInfiniteQueryOptions<TQueryFnData = unknown, TError = unknow
15
15
  }
16
16
  export declare type UseBaseQueryResult<TData = unknown, TError = unknown> = QueryObserverResult<TData, TError>;
17
17
  export declare type UseQueryResult<TData = unknown, TError = unknown> = UseBaseQueryResult<TData, TError>;
18
- export declare type DefinedUseQueryResult<TData = unknown, TError = unknown> = Omit<UseQueryResult<TData, TError>, 'data'> & {
19
- data: TData;
20
- };
18
+ export declare type DefinedUseBaseQueryResult<TData = unknown, TError = unknown> = DefinedQueryObserverResult<TData, TError>;
19
+ export declare type DefinedUseQueryResult<TData = unknown, TError = unknown> = DefinedUseBaseQueryResult<TData, TError>;
21
20
  export declare type UseInfiniteQueryResult<TData = unknown, TError = unknown> = InfiniteQueryObserverResult<TData, TError>;
22
21
  export interface UseMutationOptions<TData = unknown, TError = unknown, TVariables = void, TContext = unknown> extends ContextOptions, Omit<MutationObserverOptions<TData, TError, TVariables, TContext>, '_defaulted' | 'variables'> {
23
22
  }
@@ -2093,9 +2093,7 @@
2093
2093
  const matchingDefaults = this.queryDefaults.filter(x => partialMatchKey(queryKey, x.queryKey)); // It is ok not having defaults, but it is error prone to have more than 1 default for a given key
2094
2094
 
2095
2095
  if (matchingDefaults.length > 1) {
2096
- {
2097
- this.logger.error("[QueryClient] Several query defaults match with key '" + JSON.stringify(queryKey) + "'. The first matching query defaults are used. Please check how query defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetquerydefaults.");
2098
- }
2096
+ this.logger.error("[QueryClient] Several query defaults match with key '" + JSON.stringify(queryKey) + "'. The first matching query defaults are used. Please check how query defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetquerydefaults.");
2099
2097
  }
2100
2098
  }
2101
2099
 
@@ -2128,9 +2126,7 @@
2128
2126
  const matchingDefaults = this.mutationDefaults.filter(x => partialMatchKey(mutationKey, x.mutationKey)); // It is ok not having defaults, but it is error prone to have more than 1 default for a given key
2129
2127
 
2130
2128
  if (matchingDefaults.length > 1) {
2131
- {
2132
- this.logger.error("[QueryClient] Several mutation defaults match with key '" + JSON.stringify(mutationKey) + "'. The first matching mutation defaults are used. Please check how mutation defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetmutationdefaults.");
2133
- }
2129
+ this.logger.error("[QueryClient] Several mutation defaults match with key '" + JSON.stringify(mutationKey) + "'. The first matching mutation defaults are used. Please check how mutation defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetmutationdefaults.");
2134
2130
  }
2135
2131
  }
2136
2132
 
@@ -2237,6 +2233,14 @@
2237
2233
  const prevQuery = this.currentQuery;
2238
2234
  this.options = this.client.defaultQueryOptions(options);
2239
2235
 
2236
+ if (!shallowEqualObjects(prevOptions, this.options)) {
2237
+ this.client.getQueryCache().notify({
2238
+ type: 'observerOptionsUpdated',
2239
+ query: this.currentQuery,
2240
+ observer: this
2241
+ });
2242
+ }
2243
+
2240
2244
  if (typeof this.options.enabled !== 'undefined' && typeof this.options.enabled !== 'boolean') {
2241
2245
  throw new Error('Expected enabled to be a boolean');
2242
2246
  } // Keep previous query key if the user does not supply one
@@ -2920,7 +2924,16 @@
2920
2924
  }
2921
2925
 
2922
2926
  setOptions(options) {
2927
+ const prevOptions = this.options;
2923
2928
  this.options = this.client.defaultMutationOptions(options);
2929
+
2930
+ if (!shallowEqualObjects(prevOptions, this.options)) {
2931
+ this.client.getMutationCache().notify({
2932
+ type: 'observerOptionsUpdated',
2933
+ mutation: this.currentMutation,
2934
+ observer: this
2935
+ });
2936
+ }
2924
2937
  }
2925
2938
 
2926
2939
  onUnsubscribe() {