forked from vercel/swr
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes.ts
More file actions
386 lines (360 loc) · 11.2 KB
/
types.ts
File metadata and controls
386 lines (360 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import type * as revalidateEvents from './constants'
import type { defaultConfig } from './utils/config'
export type GlobalState = [
Record<string, RevalidateCallback[]>, // EVENT_REVALIDATORS
Record<string, [number, number]>, // MUTATION: [ts, end_ts]
Record<string, [any, number]>, // FETCH: [data, ts]
Record<string, FetcherResponse<any>>, // PRELOAD
ScopedMutator, // Mutator
(key: string, value: any, prev: any) => void, // Setter
(key: string, callback: (current: any, prev: any) => void) => () => void // Subscriber
]
export type FetcherResponse<Data = unknown> = Data | Promise<Data>
export type BareFetcher<Data = unknown> = (
...args: any[]
) => FetcherResponse<Data>
export type Fetcher<
Data = unknown,
SWRKey extends Key = Key
> = SWRKey extends () => infer Arg | null | undefined | false
? (args: Arg) => FetcherResponse<Data>
: SWRKey extends null | undefined | false
? never
: SWRKey extends infer Arg
? (args: Arg) => FetcherResponse<Data>
: never
// Configuration types that are only used internally, not exposed to the user.
export interface InternalConfiguration {
cache: Cache
mutate: ScopedMutator
}
/**
* @link https://swr.vercel.app/docs/options
*/
export interface PublicConfiguration<
Data = any,
Error = any,
Fn extends Fetcher = BareFetcher
> {
/**
* error retry interval in milliseconds
* @defaultValue 5000
*/
errorRetryInterval: number
/** max error retry count */
errorRetryCount?: number
/**
* timeout to trigger the onLoadingSlow event in milliseconds
* @defaultValue 3000
*/
loadingTimeout: number
/**
* only revalidate once during a time span in milliseconds
* @defaultValue 5000
*/
focusThrottleInterval: number
/**
* dedupe requests with the same key in this time span in milliseconds
* @defaultValue 2000
*/
dedupingInterval: number
/**
* @link https://swr.vercel.app/docs/revalidation
* * Disabled by default: `refreshInterval = 0`
* * If set to a number, polling interval in milliseconds
* * If set to a function, the function will receive the latest data and should return the interval in milliseconds
*/
refreshInterval?: number | ((latestData: Data | undefined) => number)
/**
* polling when the window is invisible (if `refreshInterval` is enabled)
* @defaultValue false
*
*/
refreshWhenHidden?: boolean
/**
* polling when the browser is offline (determined by `navigator.onLine`)
*/
refreshWhenOffline?: boolean
/**
* automatically revalidate when window gets focused
* @defaultValue true
* @link https://swr.vercel.app/docs/revalidation
*/
revalidateOnFocus: boolean
/**
* automatically revalidate when the browser regains a network connection (via `navigator.onLine`)
* @defaultValue true
* @link https://swr.vercel.app/docs/revalidation
*/
revalidateOnReconnect: boolean
/**
* enable or disable automatic revalidation when component is mounted
*/
revalidateOnMount?: boolean
/**
* automatically revalidate even if there is stale data
* @defaultValue true
* @link https://swr.vercel.app/docs/revalidation#disable-automatic-revalidations
*/
revalidateIfStale: boolean
/**
* retry when fetcher has an error
* @defaultValue true
*/
shouldRetryOnError: boolean | ((err: Error) => boolean)
/**
* keep the previous result when key is changed but data is not ready
* @defaultValue false
*/
keepPreviousData?: boolean
/**
* @experimental enable React Suspense mode
* @defaultValue false
* @link https://swr.vercel.app/docs/suspense
*/
suspense?: boolean
/**
* initial data to be returned (note: ***This is per-hook***)
*/
fallbackData?: Data
/**
* the fetcher function
*/
fetcher?: Fn
/**
* array of middleware functions
* @link https://swr.vercel.app/docs/middleware
*/
use?: Middleware[]
/**
* a key-value object of multiple fallback data
* @link https://swr.vercel.app/docs/with-nextjs#pre-rendering-with-default-data
*/
fallback: { [key: string]: any }
/**
* function to detect whether pause revalidations, will ignore fetched data and errors when it returns true. Returns false by default.
*/
isPaused: () => boolean
/**
* callback function when a request takes too long to load (see `loadingTimeout`)
*/
onLoadingSlow: (
key: string,
config: Readonly<PublicConfiguration<Data, Error, Fn>>
) => void
/**
* callback function when a request finishes successfully
*/
onSuccess: (
data: Data,
key: string,
config: Readonly<PublicConfiguration<Data, Error, Fn>>
) => void
/**
* callback function when a request returns an error
*/
onError: (
err: Error,
key: string,
config: Readonly<PublicConfiguration<Data, Error, Fn>>
) => void
/**
* handler for error retry
*/
onErrorRetry: (
err: Error,
key: string,
config: Readonly<PublicConfiguration<Data, Error, Fn>>,
revalidate: Revalidator,
revalidateOpts: Required<RevalidatorOptions>
) => void
/**
* callback function when a request is ignored
*/
onDiscarded: (key: string) => void
/**
* comparison function used to detect when returned data has changed, to avoid spurious rerenders. By default, [stable-hash](https://github.com/shuding/stable-hash) is used.
*/
compare: (a: Data | undefined, b: Data | undefined) => boolean
/**
* isOnline and isVisible are functions that return a boolean, to determine if the application is "active". By default, SWR will bail out a revalidation if these conditions are not met.
* @link https://swr.vercel.app/docs/advanced/react-native#customize-focus-and-reconnect-events
*/
isOnline: () => boolean
/**
* isOnline and isVisible are functions that return a boolean, to determine if the application is "active". By default, SWR will bail out a revalidation if these conditions are not met.
* @link https://swr.vercel.app/docs/advanced/react-native#customize-focus-and-reconnect-events
*/
isVisible: () => boolean
}
export type FullConfiguration = InternalConfiguration & PublicConfiguration
export type ProviderConfiguration = {
initFocus: (callback: () => void) => (() => void) | void
initReconnect: (callback: () => void) => (() => void) | void
}
/**
* @example
* ```ts
* const { data, error } = useSWR(key, fetcher)
* ```
*/
export interface SWRHook {
<Data = any, Error = any, SWRKey extends Key = null>(
key: SWRKey
): SWRResponse<Data, Error>
<Data = any, Error = any, SWRKey extends Key = null>(
key: SWRKey,
fetcher: Fetcher<Data, SWRKey> | null
): SWRResponse<Data, Error>
<Data = any, Error = any, SWRKey extends Key = null>(
key: SWRKey,
config: SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined
): SWRResponse<Data, Error>
<Data = any, Error = any, SWRKey extends Key = null>(
key: SWRKey,
fetcher: Fetcher<Data, SWRKey> | null,
config: SWRConfiguration<Data, Error, Fetcher<Data, SWRKey>> | undefined
): SWRResponse<Data, Error>
<Data = any, Error = any>(key: Key): SWRResponse<Data, Error>
<Data = any, Error = any>(
key: Key,
fetcher: BareFetcher<Data> | null
): SWRResponse<Data, Error>
<Data = any, Error = any>(
key: Key,
config: SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined
): SWRResponse<Data, Error>
<Data = any, Error = any>(
key: Key,
fetcher: BareFetcher<Data> | null,
config: SWRConfiguration<Data, Error, BareFetcher<Data>> | undefined
): SWRResponse<Data, Error>
}
// Middleware guarantees that a SWRHook receives a key, fetcher, and config as the argument
export type Middleware = (
useSWRNext: SWRHook
) => <Data = any, Error = any>(
key: Key,
fetcher: BareFetcher<Data> | null,
config: typeof defaultConfig &
SWRConfiguration<Data, Error, BareFetcher<Data>>
) => SWRResponse<Data, Error>
type ArgumentsTuple = [any, ...unknown[]] | readonly [any, ...unknown[]]
export type Arguments =
| string
| ArgumentsTuple
| Record<any, any>
| null
| undefined
| false
export type Key = Arguments | (() => Arguments)
export type MutatorCallback<Data = any> = (
currentData?: Data
) => Promise<undefined | Data> | undefined | Data
export type MutatorOptions<Data = any> = {
revalidate?: boolean
populateCache?:
| boolean
| ((result: any, currentData: Data | undefined) => Data)
optimisticData?: Data | ((currentData?: Data) => Data)
rollbackOnError?: boolean
}
export type MutatorConfig = {
revalidate?: boolean
populateCache?: boolean
}
export type Broadcaster<Data = any, Error = any> = (
cache: Cache<Data>,
key: string,
data: Data,
error?: Error,
isValidating?: boolean,
revalidate?: boolean,
populateCache?: boolean
) => Promise<Data>
export type State<Data = any, Error = any> = {
data?: Data
error?: Error
isValidating?: boolean
isLoading?: boolean
}
export type MutatorFn<Data = any> = (
cache: Cache,
key: Key,
data?: Data | Promise<Data> | MutatorCallback<Data>,
opts?: boolean | MutatorOptions<Data>
) => Promise<Data | undefined>
export type MutatorWrapper<Fn> = Fn extends (
...args: [...infer Parameters]
) => infer Result
? Parameters[3] extends boolean
? Result
: Parameters[3] extends Required<Pick<MutatorOptions, 'populateCache'>>
? Parameters[3]['populateCache'] extends false
? never
: Result
: Result
: never
export type Mutator<Data = any> = MutatorWrapper<MutatorFn<Data>>
export interface ScopedMutator<Data = any> {
<T = Data>(
matcher: (key?: Arguments) => boolean,
data?: T | Promise<T> | MutatorCallback<T>,
opts?: boolean | MutatorOptions<Data>
): Promise<Array<T | undefined>>
<T = Data>(
key: Arguments,
data?: T | Promise<T> | MutatorCallback<T>,
opts?: boolean | MutatorOptions<Data>
): Promise<T | undefined>
}
export type KeyedMutator<Data> = (
data?: Data | Promise<Data> | MutatorCallback<Data>,
opts?: boolean | MutatorOptions<Data>
) => Promise<Data | undefined>
export type SWRConfiguration<
Data = any,
Error = any,
Fn extends BareFetcher<any> = BareFetcher<any>
> = Partial<PublicConfiguration<Data, Error, Fn>>
export interface SWRResponse<Data = any, Error = any> {
data: Data | undefined
error: Error | undefined
mutate: KeyedMutator<Data>
isValidating: boolean
isLoading: boolean
}
export type KeyLoader<Args extends Arguments = Arguments> =
| ((index: number, previousPageData: any | null) => Args)
| null
export interface RevalidatorOptions {
retryCount?: number
dedupe?: boolean
}
export type Revalidator = (
revalidateOpts?: RevalidatorOptions
) => Promise<boolean> | void
export type RevalidateEvent =
| typeof revalidateEvents.FOCUS_EVENT
| typeof revalidateEvents.RECONNECT_EVENT
| typeof revalidateEvents.MUTATE_EVENT
type RevalidateCallbackReturnType = {
[revalidateEvents.FOCUS_EVENT]: void
[revalidateEvents.RECONNECT_EVENT]: void
[revalidateEvents.MUTATE_EVENT]: Promise<boolean>
}
export type RevalidateCallback = <K extends RevalidateEvent>(
type: K
) => RevalidateCallbackReturnType[K]
export interface Cache<Data = any> {
keys(): IterableIterator<string>
get(key: Key): State<Data> | undefined
set(key: Key, value: State<Data>): void
delete(key: Key): void
}
export interface StateDependencies {
data?: boolean
error?: boolean
isValidating?: boolean
isLoading?: boolean
}