@tanstack/query-core 4.12.0 → 4.13.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/query-core",
3
- "version": "4.12.0",
3
+ "version": "4.13.0",
4
4
  "description": "The framework agnostic core that powers TanStack Query",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
package/src/mutation.ts CHANGED
@@ -30,13 +30,16 @@ export interface MutationState<
30
30
  data: TData | undefined
31
31
  error: TError | null
32
32
  failureCount: number
33
+ failureReason: TError | null
33
34
  isPaused: boolean
34
35
  status: MutationStatus
35
36
  variables: TVariables | undefined
36
37
  }
37
38
 
38
- interface FailedAction {
39
+ interface FailedAction<TError> {
39
40
  type: 'failed'
41
+ failureCount: number
42
+ error: TError | null
40
43
  }
41
44
 
42
45
  interface LoadingAction<TVariables, TContext> {
@@ -71,7 +74,7 @@ interface SetStateAction<TData, TError, TVariables, TContext> {
71
74
  export type Action<TData, TError, TVariables, TContext> =
72
75
  | ContinueAction
73
76
  | ErrorAction<TError>
74
- | FailedAction
77
+ | FailedAction<TError>
75
78
  | LoadingAction<TVariables, TContext>
76
79
  | PauseAction
77
80
  | SetStateAction<TData, TError, TVariables, TContext>
@@ -171,8 +174,8 @@ export class Mutation<
171
174
  }
172
175
  return this.options.mutationFn(this.state.variables!)
173
176
  },
174
- onFail: () => {
175
- this.dispatch({ type: 'failed' })
177
+ onFail: (failureCount, error) => {
178
+ this.dispatch({ type: 'failed', failureCount, error })
176
179
  },
177
180
  onPause: () => {
178
181
  this.dispatch({ type: 'pause' })
@@ -272,7 +275,8 @@ export class Mutation<
272
275
  case 'failed':
273
276
  return {
274
277
  ...state,
275
- failureCount: state.failureCount + 1,
278
+ failureCount: action.failureCount,
279
+ failureReason: action.error,
276
280
  }
277
281
  case 'pause':
278
282
  return {
@@ -289,6 +293,8 @@ export class Mutation<
289
293
  ...state,
290
294
  context: action.context,
291
295
  data: undefined,
296
+ failureCount: 0,
297
+ failureReason: null,
292
298
  error: null,
293
299
  isPaused: !canFetch(this.options.networkMode),
294
300
  status: 'loading',
@@ -298,6 +304,8 @@ export class Mutation<
298
304
  return {
299
305
  ...state,
300
306
  data: action.data,
307
+ failureCount: 0,
308
+ failureReason: null,
301
309
  error: null,
302
310
  status: 'success',
303
311
  isPaused: false,
@@ -308,6 +316,7 @@ export class Mutation<
308
316
  data: undefined,
309
317
  error: action.error,
310
318
  failureCount: state.failureCount + 1,
319
+ failureReason: action.error,
311
320
  isPaused: false,
312
321
  status: 'error',
313
322
  }
@@ -344,6 +353,7 @@ export function getDefaultState<
344
353
  data: undefined,
345
354
  error: null,
346
355
  failureCount: 0,
356
+ failureReason: null,
347
357
  isPaused: false,
348
358
  status: 'idle',
349
359
  variables: undefined,
package/src/query.ts CHANGED
@@ -45,6 +45,7 @@ export interface QueryState<TData = unknown, TError = unknown> {
45
45
  errorUpdateCount: number
46
46
  errorUpdatedAt: number
47
47
  fetchFailureCount: number
48
+ fetchFailureReason: TError | null
48
49
  fetchMeta: any
49
50
  isInvalidated: boolean
50
51
  status: QueryStatus
@@ -82,8 +83,10 @@ export interface FetchOptions {
82
83
  meta?: any
83
84
  }
84
85
 
85
- interface FailedAction {
86
+ interface FailedAction<TError> {
86
87
  type: 'failed'
88
+ failureCount: number
89
+ error: TError
87
90
  }
88
91
 
89
92
  interface FetchAction {
@@ -124,7 +127,7 @@ interface SetStateAction<TData, TError> {
124
127
  export type Action<TData, TError> =
125
128
  | ContinueAction
126
129
  | ErrorAction<TError>
127
- | FailedAction
130
+ | FailedAction<TError>
128
131
  | FetchAction
129
132
  | InvalidateAction
130
133
  | PauseAction
@@ -473,8 +476,8 @@ export class Query<
473
476
  this.isFetchingOptimistic = false
474
477
  },
475
478
  onError,
476
- onFail: () => {
477
- this.dispatch({ type: 'failed' })
479
+ onFail: (failureCount, error) => {
480
+ this.dispatch({ type: 'failed', failureCount, error })
478
481
  },
479
482
  onPause: () => {
480
483
  this.dispatch({ type: 'pause' })
@@ -500,7 +503,8 @@ export class Query<
500
503
  case 'failed':
501
504
  return {
502
505
  ...state,
503
- fetchFailureCount: state.fetchFailureCount + 1,
506
+ fetchFailureCount: action.failureCount,
507
+ fetchFailureReason: action.error,
504
508
  }
505
509
  case 'pause':
506
510
  return {
@@ -516,6 +520,7 @@ export class Query<
516
520
  return {
517
521
  ...state,
518
522
  fetchFailureCount: 0,
523
+ fetchFailureReason: null,
519
524
  fetchMeta: action.meta ?? null,
520
525
  fetchStatus: canFetch(this.options.networkMode)
521
526
  ? 'fetching'
@@ -537,6 +542,7 @@ export class Query<
537
542
  ...(!action.manual && {
538
543
  fetchStatus: 'idle',
539
544
  fetchFailureCount: 0,
545
+ fetchFailureReason: null,
540
546
  }),
541
547
  }
542
548
  case 'error':
@@ -552,6 +558,7 @@ export class Query<
552
558
  errorUpdateCount: state.errorUpdateCount + 1,
553
559
  errorUpdatedAt: Date.now(),
554
560
  fetchFailureCount: state.fetchFailureCount + 1,
561
+ fetchFailureReason: error as TError,
555
562
  fetchStatus: 'idle',
556
563
  status: 'error',
557
564
  }
@@ -611,6 +618,7 @@ function getDefaultState<
611
618
  errorUpdateCount: 0,
612
619
  errorUpdatedAt: 0,
613
620
  fetchFailureCount: 0,
621
+ fetchFailureReason: null,
614
622
  fetchMeta: null,
615
623
  isInvalidated: false,
616
624
  status: hasData ? 'success' : 'loading',
@@ -562,6 +562,7 @@ export class QueryObserver<
562
562
  error,
563
563
  errorUpdatedAt,
564
564
  failureCount: state.fetchFailureCount,
565
+ failureReason: state.fetchFailureReason,
565
566
  errorUpdateCount: state.errorUpdateCount,
566
567
  isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,
567
568
  isFetchedAfterMount:
@@ -71,6 +71,7 @@ describe('mutations', () => {
71
71
  data: undefined,
72
72
  error: null,
73
73
  failureCount: 0,
74
+ failureReason: null,
74
75
  isError: false,
75
76
  isIdle: true,
76
77
  isLoading: false,
@@ -97,6 +98,7 @@ describe('mutations', () => {
97
98
  data: undefined,
98
99
  error: null,
99
100
  failureCount: 0,
101
+ failureReason: null,
100
102
  isError: false,
101
103
  isIdle: false,
102
104
  isLoading: true,
@@ -115,6 +117,7 @@ describe('mutations', () => {
115
117
  data: undefined,
116
118
  error: null,
117
119
  failureCount: 0,
120
+ failureReason: null,
118
121
  isError: false,
119
122
  isIdle: false,
120
123
  isLoading: true,
@@ -133,6 +136,7 @@ describe('mutations', () => {
133
136
  data: 'todo',
134
137
  error: null,
135
138
  failureCount: 0,
139
+ failureReason: null,
136
140
  isError: false,
137
141
  isIdle: false,
138
142
  isLoading: false,
@@ -172,6 +176,7 @@ describe('mutations', () => {
172
176
  data: undefined,
173
177
  error: null,
174
178
  failureCount: 0,
179
+ failureReason: null,
175
180
  isError: false,
176
181
  isIdle: false,
177
182
  isLoading: true,
@@ -190,6 +195,7 @@ describe('mutations', () => {
190
195
  data: undefined,
191
196
  error: null,
192
197
  failureCount: 0,
198
+ failureReason: null,
193
199
  isError: false,
194
200
  isIdle: false,
195
201
  isLoading: true,
@@ -208,6 +214,7 @@ describe('mutations', () => {
208
214
  data: undefined,
209
215
  error: null,
210
216
  failureCount: 1,
217
+ failureReason: 'err',
211
218
  isError: false,
212
219
  isIdle: false,
213
220
  isLoading: true,
@@ -226,6 +233,7 @@ describe('mutations', () => {
226
233
  data: undefined,
227
234
  error: 'err',
228
235
  failureCount: 2,
236
+ failureReason: 'err',
229
237
  isError: true,
230
238
  isIdle: false,
231
239
  isLoading: false,
@@ -264,6 +272,7 @@ describe('mutations', () => {
264
272
  data: undefined,
265
273
  error: null,
266
274
  failureCount: 1,
275
+ failureReason: 'err',
267
276
  isPaused: true,
268
277
  status: 'loading',
269
278
  variables: 'todo',
@@ -275,6 +284,7 @@ describe('mutations', () => {
275
284
  data: undefined,
276
285
  error: null,
277
286
  failureCount: 1,
287
+ failureReason: 'err',
278
288
  isPaused: true,
279
289
  status: 'loading',
280
290
  variables: 'todo',
@@ -286,7 +296,8 @@ describe('mutations', () => {
286
296
  context: 'todo',
287
297
  data: 'todo',
288
298
  error: null,
289
- failureCount: 1,
299
+ failureCount: 0,
300
+ failureReason: null,
290
301
  isPaused: false,
291
302
  status: 'success',
292
303
  variables: 'todo',
@@ -316,6 +327,7 @@ describe('mutations', () => {
316
327
  data: 'new',
317
328
  error: undefined,
318
329
  failureCount: 0,
330
+ failureReason: null,
319
331
  isPaused: false,
320
332
  status: 'success',
321
333
  })
package/src/types.ts CHANGED
@@ -370,6 +370,7 @@ export interface QueryObserverBaseResult<TData = unknown, TError = unknown> {
370
370
  error: TError | null
371
371
  errorUpdatedAt: number
372
372
  failureCount: number
373
+ failureReason: TError | null
373
374
  errorUpdateCount: number
374
375
  isError: boolean
375
376
  isFetched: boolean