|
1 | | -import { OptionsMaybeURL, NoUrlOptions, CachePolicies, Interceptors, OverwriteGlobalOptions, Options, RetryOn, RetryDelay, UseFetchArgsReturn, ResponseType } from './types' |
| 1 | +import { OptionsMaybeURL, NoUrlOptions, CachePolicies, Interceptors, OverwriteGlobalOptions, Options, RetryOn, RetryDelay, UseFetchArgsReturn, ResponseType, OnError } from './types' |
2 | 2 | import { isString, isObject, invariant, pullOutRequestInit, isFunction, isPositiveNumber } from './utils' |
3 | 3 | import { useContext, useMemo } from 'react' |
4 | 4 | import FetchContext from './FetchContext' |
@@ -62,25 +62,26 @@ export default function useFetchArgs( |
62 | 62 | }, [optionsNoURLsOrOverwriteGlobalOrDeps, deps]) |
63 | 63 |
|
64 | 64 | const data = useField('data', urlOrOptions, optionsNoURLs) |
65 | | - const path = useField<string>('path', urlOrOptions, optionsNoURLs) |
66 | | - const timeout = useField<number>('timeout', urlOrOptions, optionsNoURLs) |
67 | | - const persist = useField<boolean>('persist', urlOrOptions, optionsNoURLs) |
| 65 | + const cacheLife = useField<number>('cacheLife', urlOrOptions, optionsNoURLs) |
| 66 | + invariant(Number.isInteger(cacheLife) && cacheLife >= 0, '`cacheLife` must be a number >= 0') |
| 67 | + const cachePolicy = useField<CachePolicies>('cachePolicy', urlOrOptions, optionsNoURLs) |
68 | 68 | const onAbort = useField<() => void>('onAbort', urlOrOptions, optionsNoURLs) |
69 | | - const onTimeout = useField<() => void>('onTimeout', urlOrOptions, optionsNoURLs) |
| 69 | + const onError = useField<OnError>('onError', urlOrOptions, optionsNoURLs) |
70 | 70 | const onNewData = useField<() => void>('onNewData', urlOrOptions, optionsNoURLs) |
| 71 | + const onTimeout = useField<() => void>('onTimeout', urlOrOptions, optionsNoURLs) |
| 72 | + const path = useField<string>('path', urlOrOptions, optionsNoURLs) |
71 | 73 | const perPage = useField<number>('perPage', urlOrOptions, optionsNoURLs) |
72 | | - const cachePolicy = useField<CachePolicies>('cachePolicy', urlOrOptions, optionsNoURLs) |
73 | | - const cacheLife = useField<number>('cacheLife', urlOrOptions, optionsNoURLs) |
74 | | - invariant(Number.isInteger(cacheLife) && cacheLife >= 0, '`cacheLife` must be a number >= 0') |
75 | | - const suspense = useField<boolean>('suspense', urlOrOptions, optionsNoURLs) |
| 74 | + const persist = useField<boolean>('persist', urlOrOptions, optionsNoURLs) |
| 75 | + const responseType = useField<ResponseType>('responseType', urlOrOptions, optionsNoURLs) |
76 | 76 | const retries = useField<number>('retries', urlOrOptions, optionsNoURLs) |
77 | 77 | invariant(Number.isInteger(retries) && retries >= 0, '`retries` must be a number >= 0') |
| 78 | + const retryDelay = useField<RetryDelay>('retryDelay', urlOrOptions, optionsNoURLs) |
| 79 | + invariant(isFunction(retryDelay) || Number.isInteger(retryDelay as number) && retryDelay >= 0, '`retryDelay` must be a positive number or a function returning a positive number.') |
78 | 80 | const retryOn = useField<RetryOn>('retryOn', urlOrOptions, optionsNoURLs) |
79 | 81 | const isValidRetryOn = isFunction(retryOn) || (Array.isArray(retryOn) && retryOn.every(isPositiveNumber)) |
80 | 82 | invariant(isValidRetryOn, '`retryOn` must be an array of positive numbers or a function returning a boolean.') |
81 | | - const retryDelay = useField<RetryDelay>('retryDelay', urlOrOptions, optionsNoURLs) |
82 | | - invariant(isFunction(retryDelay) || Number.isInteger(retryDelay as number) && retryDelay >= 0, '`retryDelay` must be a positive number or a function returning a positive number.') |
83 | | - const responseType = useField<ResponseType>('responseType', urlOrOptions, optionsNoURLs) |
| 83 | + const suspense = useField<boolean>('suspense', urlOrOptions, optionsNoURLs) |
| 84 | + const timeout = useField<number>('timeout', urlOrOptions, optionsNoURLs) |
84 | 85 |
|
85 | 86 | const loading = useMemo((): boolean => { |
86 | 87 | if (isObject(urlOrOptions)) return !!urlOrOptions.loading || Array.isArray(dependencies) |
@@ -130,6 +131,7 @@ export default function useFetchArgs( |
130 | 131 | cachePolicy, |
131 | 132 | interceptors, |
132 | 133 | onAbort, |
| 134 | + onError, |
133 | 135 | onNewData, |
134 | 136 | onTimeout, |
135 | 137 | path, |
|
0 commit comments