floppy-disk 2.5.0-beta.1 → 2.5.0-beta.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.
@@ -1,7 +1,8 @@
1
1
  /// <reference types="react" />
2
2
  import { FunctionComponent } from 'preact';
3
+ import { Maybe } from '../utils';
3
4
  import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
4
- export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
5
+ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = {
5
6
  /**
6
7
  * Query store key, an object that will be hashed into a string as a query store identifier.
7
8
  */
@@ -19,17 +20,17 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
19
20
  *
20
21
  * @returns Promise that will always get resolved.
21
22
  */
22
- forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError>>;
23
+ forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
23
24
  /**
24
25
  * Fetch next page if has next page.
25
26
  *
26
27
  * If the data is empty, it will just fetch the first page.
27
28
  *
28
- * You can ignore this if your query is not paginated.
29
+ * You can ignore this if your query is not an infinite query.
29
30
  *
30
31
  * @returns Promise that will always get resolved.
31
32
  */
32
- fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError>>;
33
+ fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
33
34
  /**
34
35
  * Set query state (data, error, etc) to initial state.
35
36
  */
@@ -41,7 +42,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
41
42
  *
42
43
  * IMPORTANT NOTE: This won't work well on infinite query.
43
44
  */
44
- optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError>) => TResponse)) => {
45
+ optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
45
46
  revert: () => void;
46
47
  invalidate: () => void;
47
48
  };
@@ -55,14 +56,19 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
55
56
  isWaitingNextPage: boolean;
56
57
  isRefetching: boolean;
57
58
  isRefetchError: boolean;
59
+ /**
60
+ * Will be `true` if the response/data comes from the previous query key.
61
+ *
62
+ * @see `keepPreviousData` option
63
+ */
58
64
  isPreviousData: boolean;
59
65
  isOptimisticData: boolean;
60
- error: TError | null;
61
- errorUpdatedAt: number | null;
66
+ error: TError | undefined;
67
+ errorUpdatedAt: number | undefined;
62
68
  retryCount: number;
63
69
  isGoingToRetry: boolean;
64
- pageParam: any;
65
- pageParams: any[];
70
+ pageParam: Maybe<TPageParam>;
71
+ pageParams: Maybe<TPageParam>[];
66
72
  hasNextPage: boolean;
67
73
  retryNextPageCount: number;
68
74
  isGoingToRetryNextPage: boolean;
@@ -78,6 +84,8 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
78
84
  *
79
85
  * It has no relation with network fetching state.
80
86
  * If you're looking for network fetching state, use `isWaiting` instead.
87
+ *
88
+ * @see https://floppy-disk.vercel.app/docs/query/introduction#query-state--network-fetching-state
81
89
  */
82
90
  status: 'loading';
83
91
  /**
@@ -85,6 +93,8 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
85
93
  *
86
94
  * It has no relation with network fetching state.
87
95
  * If you're looking for network fetching state, use `isWaiting` instead.
96
+ *
97
+ * @see https://floppy-disk.vercel.app/docs/query/introduction#query-state--network-fetching-state
88
98
  */
89
99
  isLoading: true;
90
100
  /**
@@ -99,9 +109,9 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
99
109
  * If data fetched successfully but then an error occured, `isError` will be `false` but `isRefetchError` will be `true`.
100
110
  */
101
111
  isError: false;
102
- data: null;
103
- response: null;
104
- responseUpdatedAt: null;
112
+ data: undefined;
113
+ response: undefined;
114
+ responseUpdatedAt: undefined;
105
115
  } | {
106
116
  status: 'success';
107
117
  isLoading: false;
@@ -109,18 +119,18 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
109
119
  isError: false;
110
120
  data: TData;
111
121
  response: TResponse;
112
- responseUpdatedAt: number | null;
122
+ responseUpdatedAt: number | undefined;
113
123
  } | {
114
124
  status: 'error';
115
125
  isLoading: false;
116
126
  isSuccess: false;
117
127
  isError: true;
118
- data: null;
119
- response: null;
120
- responseUpdatedAt: null;
128
+ data: undefined;
129
+ response: undefined;
130
+ responseUpdatedAt: undefined;
121
131
  });
122
- export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = CreateStoresOptions<TKey, QueryState<TKey, TResponse, TData, TError>> & {
123
- select?: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError>, 'data' | 'key'>) => TData;
132
+ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = CreateStoresOptions<TKey, QueryState<TKey, TResponse, TData, TError, TPageParam>> & {
133
+ select?: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError, TPageParam>, 'data' | 'key'>) => TData;
124
134
  /**
125
135
  * Stale time in miliseconds.
126
136
  *
@@ -161,29 +171,31 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
161
171
  *
162
172
  * Defaults to `1`.
163
173
  */
164
- retry?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError>) => number);
174
+ retry?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number);
165
175
  /**
166
176
  * Error retry delay in miliseconds.
167
177
  *
168
178
  * Defaults to `2000` (2 seconds).
169
179
  */
170
- retryDelay?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError>) => number);
180
+ retryDelay?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number);
171
181
  /**
172
- * If set to `true`, previous `data` will be kept when fetching new data because the query key changed.
182
+ * Used for lagged query.
173
183
  *
174
- * This will only happened if there is no `data` in the next query.
184
+ * If set to `true`, then:
185
+ * when the query key changed and there is no `data` in the next query key cache,
186
+ * the previous query key cache `data` will be used while fetching new data.
175
187
  */
176
188
  keepPreviousData?: boolean;
177
189
  /**
178
190
  * Only set this if you have an infinite query.
179
191
  *
180
- * This function should return a variable that will be used when fetching next page (`pageParam`).
192
+ * This function should return a variable that will be stored as `pageParam` that can be used when fetching next page.
181
193
  */
182
- getNextPageParam?: (lastPage: TResponse, index: number) => any;
183
- onBeforeFetch?: (cancel: () => void, state: QueryState<TKey, TResponse, TData, TError>) => void;
184
- onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
185
- onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
186
- onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
194
+ getNextPageParam?: (lastPage: TResponse, index: number) => Maybe<TPageParam>;
195
+ onBeforeFetch?: (cancel: () => void, state: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
196
+ onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
197
+ onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
198
+ onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
187
199
  /**
188
200
  * Cache time in miliseconds.
189
201
  *
@@ -204,9 +216,9 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
204
216
  *
205
217
  * @see https://floppy-disk.vercel.app/docs/query/polling
206
218
  */
207
- refetchInterval?: number | false | ((state: QueryState<TKey, TResponse, TData, TError>) => number | false);
219
+ refetchInterval?: number | false | ((state: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number | false);
208
220
  };
209
- export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = UseStores<TKey, QueryState<TKey, TResponse, TData, TError>> & {
221
+ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = UseStores<TKey, QueryState<TKey, TResponse, TData, TError, TPageParam>> & {
210
222
  /**
211
223
  * Set query's initial response.
212
224
  *
@@ -215,7 +227,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
215
227
  * IMPORTANT NOTE: Put this on the root component or parent component, before any component subscribed!
216
228
  */
217
229
  setInitialResponse: (options: {
218
- key?: TKey | null;
230
+ key?: Maybe<TKey>;
219
231
  response: TResponse;
220
232
  skipRevalidation?: boolean;
221
233
  }) => void;
@@ -226,7 +238,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
226
238
  /**
227
239
  * Set query state (data, error, etc) to initial state.
228
240
  */
229
- resetSpecificKey: (key?: TKey | null) => void;
241
+ resetSpecificKey: (key?: Maybe<TKey>) => void;
230
242
  /**
231
243
  * Invalidate query means marking a query as stale, and will refetch only if the query is active (has subscriber)
232
244
  */
@@ -234,7 +246,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
234
246
  /**
235
247
  * Invalidate query means marking a query as stale, and will refetch only if the query is active (has subscriber)
236
248
  */
237
- invalidateSpecificKey: (key?: TKey | null) => void;
249
+ invalidateSpecificKey: (key?: Maybe<TKey>) => void;
238
250
  /**
239
251
  * Optimistic update.
240
252
  *
@@ -243,8 +255,8 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
243
255
  * IMPORTANT NOTE: This won't work well on infinite query.
244
256
  */
245
257
  optimisticUpdate: (options: {
246
- key?: TKey | null;
247
- response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError>) => TResponse);
258
+ key?: Maybe<TKey>;
259
+ response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse);
248
260
  }) => {
249
261
  revert: () => void;
250
262
  invalidate: () => void;
@@ -252,11 +264,11 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
252
264
  /**
253
265
  * Use query with suspense mode.
254
266
  */
255
- suspend: (key?: TKey | null) => Extract<QueryState<TKey, TResponse, TData, TError>, {
267
+ suspend: (key?: Maybe<TKey>) => Extract<QueryState<TKey, TResponse, TData, TError, TPageParam>, {
256
268
  status: 'success';
257
269
  }>;
258
270
  Render: (props: {
259
- queryKey?: TKey | null;
271
+ queryKey?: Maybe<TKey>;
260
272
  loading?: FunctionComponent<TKey>;
261
273
  success?: FunctionComponent<TKey>;
262
274
  error?: FunctionComponent<TKey>;
@@ -265,4 +277,4 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
265
277
  /**
266
278
  * @see https://floppy-disk.vercel.app/docs/api#createquery
267
279
  */
268
- export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError>) => UseQuery<TKey, TResponse, TData, TError>;
280
+ export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError, TPageParam>) => UseQuery<TKey, TResponse, TData, TError, TPageParam>;
@@ -16,11 +16,11 @@ const INITIAL_QUERY_STATE = {
16
16
  isRefetchError: false,
17
17
  isPreviousData: false,
18
18
  isOptimisticData: false,
19
- data: null,
20
- response: null,
21
- responseUpdatedAt: null,
22
- error: null,
23
- errorUpdatedAt: null,
19
+ data: undefined,
20
+ response: undefined,
21
+ responseUpdatedAt: undefined,
22
+ error: undefined,
23
+ errorUpdatedAt: undefined,
24
24
  retryCount: 0,
25
25
  isGoingToRetry: false,
26
26
  pageParam: undefined,
@@ -57,11 +57,11 @@ const createQuery = (queryFn, options = {}) => {
57
57
  return { shouldRetry: retryCount < maxRetryCount, delay };
58
58
  };
59
59
  const forceFetch = () => new Promise((resolve) => {
60
+ const state = get();
60
61
  const responseAllPages = [];
61
- const newPageParams = [undefined];
62
- let pageParam = undefined;
62
+ const newPageParams = [state.pageParams[0]];
63
+ let pageParam = state.pageParams[0];
63
64
  clearTimeout(refetchIntervalTimeoutId.get(keyHash));
64
- const state = get();
65
65
  const { isWaiting, isLoading, pageParams } = state;
66
66
  if (isWaiting || !(0, utils_1.getValueOrComputedValue)(enabled, key))
67
67
  return resolve(state);
@@ -112,11 +112,11 @@ const createQuery = (queryFn, options = {}) => {
112
112
  isOptimisticData: false,
113
113
  data: responseAllPages.reduce((prev, responseCurrentPage) => {
114
114
  return select(responseCurrentPage, { key, data: prev });
115
- }, null),
115
+ }, undefined),
116
116
  response,
117
117
  responseUpdatedAt: Date.now(),
118
- error: null,
119
- errorUpdatedAt: null,
118
+ error: undefined,
119
+ errorUpdatedAt: undefined,
120
120
  retryCount: 0,
121
121
  pageParam: newPageParam,
122
122
  pageParams: newPageParams,
@@ -131,7 +131,6 @@ const createQuery = (queryFn, options = {}) => {
131
131
  }
132
132
  set(nextState);
133
133
  onSuccess(response, stateBeforeCallQuery);
134
- resolve(get());
135
134
  })
136
135
  .catch((error) => {
137
136
  const prevState = get();
@@ -145,7 +144,7 @@ const createQuery = (queryFn, options = {}) => {
145
144
  data: responseAllPages.length
146
145
  ? responseAllPages.reduce((prev, response) => {
147
146
  return select(response, { key, data: prev });
148
- }, null)
147
+ }, undefined)
149
148
  : prevState.data,
150
149
  error,
151
150
  errorUpdatedAt,
@@ -158,7 +157,7 @@ const createQuery = (queryFn, options = {}) => {
158
157
  status: 'error',
159
158
  isLoading: false,
160
159
  isError: true,
161
- data: null,
160
+ data: undefined,
162
161
  error,
163
162
  errorUpdatedAt,
164
163
  isGoingToRetry: shouldRetry,
@@ -172,17 +171,17 @@ const createQuery = (queryFn, options = {}) => {
172
171
  }, delay));
173
172
  }
174
173
  onError(error, stateBeforeCallQuery);
175
- resolve(get());
176
174
  })
177
175
  .finally(() => {
178
176
  onSettled(stateBeforeCallQuery);
177
+ resolve(get());
179
178
  });
180
179
  };
181
180
  callQuery();
182
181
  });
183
182
  const fetch = () => {
184
183
  const { responseUpdatedAt } = get();
185
- const isStale = Date.now() > Number(responseUpdatedAt) + staleTime;
184
+ const isStale = Date.now() > (responseUpdatedAt || 0) + staleTime;
186
185
  if (!isStale)
187
186
  return;
188
187
  forceFetch();
@@ -221,7 +220,6 @@ const createQuery = (queryFn, options = {}) => {
221
220
  hasNextPage: (0, utils_1.hasValue)(newPageParam),
222
221
  });
223
222
  onSuccess(response, stateBeforeCallQuery);
224
- resolve(get());
225
223
  })
226
224
  .catch((error) => {
227
225
  const prevState = get();
@@ -240,10 +238,10 @@ const createQuery = (queryFn, options = {}) => {
240
238
  }, delay));
241
239
  }
242
240
  onError(error, stateBeforeCallQuery);
243
- resolve(get());
244
241
  })
245
242
  .finally(() => {
246
243
  onSettled(stateBeforeCallQuery);
244
+ resolve(get());
247
245
  });
248
246
  });
249
247
  return {
@@ -341,8 +339,8 @@ const createQuery = (queryFn, options = {}) => {
341
339
  isSuccess: true,
342
340
  isError: false,
343
341
  response,
344
- responseUpdatedAt: skipRevalidation ? Date.now() : null,
345
- data: select(response, { key: key, data: null }),
342
+ responseUpdatedAt: skipRevalidation ? Date.now() : undefined,
343
+ data: select(response, { key: key, data: undefined }),
346
344
  pageParam: newPageParam,
347
345
  pageParams: [undefined, newPageParam],
348
346
  hasNextPage: (0, utils_1.hasValue)(newPageParam),
@@ -361,14 +359,14 @@ const createQuery = (queryFn, options = {}) => {
361
359
  useQuery.invalidate = () => {
362
360
  useQuery.getStores().forEach((store) => {
363
361
  const { get, set, getSubscribers } = store;
364
- set({ responseUpdatedAt: null });
362
+ set({ responseUpdatedAt: undefined });
365
363
  if (getSubscribers().size > 0)
366
364
  get().forceFetch();
367
365
  });
368
366
  };
369
367
  useQuery.invalidateSpecificKey = (key) => {
370
368
  const { get, set, getSubscribers } = useQuery.getStore(key);
371
- set({ responseUpdatedAt: null });
369
+ set({ responseUpdatedAt: undefined });
372
370
  if (getSubscribers().size > 0)
373
371
  get().forceFetch();
374
372
  };
@@ -380,7 +378,7 @@ const createQuery = (queryFn, options = {}) => {
380
378
  useQuery.set(key, {
381
379
  isOptimisticData: true,
382
380
  response: optimisticResponse,
383
- data: select(optimisticResponse, { key: key, data: null }),
381
+ data: select(optimisticResponse, { key: key, data: undefined }),
384
382
  });
385
383
  preventReplaceResponse.set(prevState.keyHash, true);
386
384
  const revert = () => {
@@ -1,6 +1,6 @@
1
+ import { Maybe } from '../utils';
1
2
  import { InitStoreOptions, InitStoreReturn, SelectDeps, SetStoreData, StoreData, Subscribers } from '../vanilla';
2
3
  import { WatchProps } from './create-store';
3
- type Maybe<T> = T | null | undefined;
4
4
  export type StoreKey = Record<string, any> | undefined;
5
5
  export type StoresInitializer<TKey extends StoreKey = StoreKey, T extends StoreData = StoreData> = (api: {
6
6
  get: () => T;
@@ -50,4 +50,3 @@ export type CreateStoresOptions<TKey extends StoreKey = StoreKey, T extends Stor
50
50
  * @see https://floppy-disk.vercel.app/docs/api#createstores
51
51
  */
52
52
  export declare const createStores: <TKey extends StoreKey = StoreKey, T extends StoreData = StoreData>(initializer: StoresInitializer<TKey, T>, options?: CreateStoresOptions<TKey, T>) => UseStores<TKey, T>;
53
- export {};
@@ -7,10 +7,10 @@ export type MutationState<TVar, TResponse = any, TError = unknown> = {
7
7
  isWaiting: boolean;
8
8
  isSuccess: boolean;
9
9
  isError: boolean;
10
- response: TResponse | null;
11
- responseUpdatedAt: number | null;
12
- error: TError | null;
13
- errorUpdatedAt: number | null;
10
+ response: TResponse | undefined;
11
+ responseUpdatedAt: number | undefined;
12
+ error: TError | undefined;
13
+ errorUpdatedAt: number | undefined;
14
14
  /**
15
15
  * Mutate function.
16
16
  *
@@ -12,10 +12,10 @@ const createMutation = (mutationFn, options = {}) => {
12
12
  isWaiting: false,
13
13
  isSuccess: false,
14
14
  isError: false,
15
- response: null,
16
- responseUpdatedAt: null,
17
- error: null,
18
- errorUpdatedAt: null,
15
+ response: undefined,
16
+ responseUpdatedAt: undefined,
17
+ error: undefined,
18
+ errorUpdatedAt: undefined,
19
19
  mutate: ((variables) => {
20
20
  set({ isWaiting: true });
21
21
  const stateBeforeMutate = get();
@@ -29,8 +29,8 @@ const createMutation = (mutationFn, options = {}) => {
29
29
  isError: false,
30
30
  response,
31
31
  responseUpdatedAt: Date.now(),
32
- error: null,
33
- errorUpdatedAt: null,
32
+ error: undefined,
33
+ errorUpdatedAt: undefined,
34
34
  });
35
35
  onSuccess(response, variables, stateBeforeMutate);
36
36
  resolve({ response, variables });
@@ -1,6 +1,7 @@
1
1
  import { FunctionComponent } from 'react';
2
+ import { Maybe } from '../utils';
2
3
  import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
3
- export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
4
+ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = {
4
5
  /**
5
6
  * Query store key, an object that will be hashed into a string as a query store identifier.
6
7
  */
@@ -18,17 +19,17 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
18
19
  *
19
20
  * @returns Promise that will always get resolved.
20
21
  */
21
- forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError>>;
22
+ forceFetch: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
22
23
  /**
23
24
  * Fetch next page if has next page.
24
25
  *
25
26
  * If the data is empty, it will just fetch the first page.
26
27
  *
27
- * You can ignore this if your query is not paginated.
28
+ * You can ignore this if your query is not an infinite query.
28
29
  *
29
30
  * @returns Promise that will always get resolved.
30
31
  */
31
- fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError>>;
32
+ fetchNextPage: () => Promise<QueryState<TKey, TResponse, TData, TError, TPageParam>>;
32
33
  /**
33
34
  * Set query state (data, error, etc) to initial state.
34
35
  */
@@ -40,7 +41,7 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
40
41
  *
41
42
  * IMPORTANT NOTE: This won't work well on infinite query.
42
43
  */
43
- optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError>) => TResponse)) => {
44
+ optimisticUpdate: (response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse)) => {
44
45
  revert: () => void;
45
46
  invalidate: () => void;
46
47
  };
@@ -54,14 +55,19 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
54
55
  isWaitingNextPage: boolean;
55
56
  isRefetching: boolean;
56
57
  isRefetchError: boolean;
58
+ /**
59
+ * Will be `true` if the response/data comes from the previous query key.
60
+ *
61
+ * @see `keepPreviousData` option
62
+ */
57
63
  isPreviousData: boolean;
58
64
  isOptimisticData: boolean;
59
- error: TError | null;
60
- errorUpdatedAt: number | null;
65
+ error: TError | undefined;
66
+ errorUpdatedAt: number | undefined;
61
67
  retryCount: number;
62
68
  isGoingToRetry: boolean;
63
- pageParam: any;
64
- pageParams: any[];
69
+ pageParam: Maybe<TPageParam>;
70
+ pageParams: Maybe<TPageParam>[];
65
71
  hasNextPage: boolean;
66
72
  retryNextPageCount: number;
67
73
  isGoingToRetryNextPage: boolean;
@@ -77,6 +83,8 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
77
83
  *
78
84
  * It has no relation with network fetching state.
79
85
  * If you're looking for network fetching state, use `isWaiting` instead.
86
+ *
87
+ * @see https://floppy-disk.vercel.app/docs/query/introduction#query-state--network-fetching-state
80
88
  */
81
89
  status: 'loading';
82
90
  /**
@@ -84,6 +92,8 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
84
92
  *
85
93
  * It has no relation with network fetching state.
86
94
  * If you're looking for network fetching state, use `isWaiting` instead.
95
+ *
96
+ * @see https://floppy-disk.vercel.app/docs/query/introduction#query-state--network-fetching-state
87
97
  */
88
98
  isLoading: true;
89
99
  /**
@@ -98,9 +108,9 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
98
108
  * If data fetched successfully but then an error occured, `isError` will be `false` but `isRefetchError` will be `true`.
99
109
  */
100
110
  isError: false;
101
- data: null;
102
- response: null;
103
- responseUpdatedAt: null;
111
+ data: undefined;
112
+ response: undefined;
113
+ responseUpdatedAt: undefined;
104
114
  } | {
105
115
  status: 'success';
106
116
  isLoading: false;
@@ -108,18 +118,18 @@ export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData
108
118
  isError: false;
109
119
  data: TData;
110
120
  response: TResponse;
111
- responseUpdatedAt: number | null;
121
+ responseUpdatedAt: number | undefined;
112
122
  } | {
113
123
  status: 'error';
114
124
  isLoading: false;
115
125
  isSuccess: false;
116
126
  isError: true;
117
- data: null;
118
- response: null;
119
- responseUpdatedAt: null;
127
+ data: undefined;
128
+ response: undefined;
129
+ responseUpdatedAt: undefined;
120
130
  });
121
- export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = CreateStoresOptions<TKey, QueryState<TKey, TResponse, TData, TError>> & {
122
- select?: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError>, 'data' | 'key'>) => TData;
131
+ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = CreateStoresOptions<TKey, QueryState<TKey, TResponse, TData, TError, TPageParam>> & {
132
+ select?: (response: TResponse, state: Pick<QueryState<TKey, TResponse, TData, TError, TPageParam>, 'data' | 'key'>) => TData;
123
133
  /**
124
134
  * Stale time in miliseconds.
125
135
  *
@@ -160,29 +170,31 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
160
170
  *
161
171
  * Defaults to `1`.
162
172
  */
163
- retry?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError>) => number);
173
+ retry?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number);
164
174
  /**
165
175
  * Error retry delay in miliseconds.
166
176
  *
167
177
  * Defaults to `2000` (2 seconds).
168
178
  */
169
- retryDelay?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError>) => number);
179
+ retryDelay?: number | ((error: TError, prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number);
170
180
  /**
171
- * If set to `true`, previous `data` will be kept when fetching new data because the query key changed.
181
+ * Used for lagged query.
172
182
  *
173
- * This will only happened if there is no `data` in the next query.
183
+ * If set to `true`, then:
184
+ * when the query key changed and there is no `data` in the next query key cache,
185
+ * the previous query key cache `data` will be used while fetching new data.
174
186
  */
175
187
  keepPreviousData?: boolean;
176
188
  /**
177
189
  * Only set this if you have an infinite query.
178
190
  *
179
- * This function should return a variable that will be used when fetching next page (`pageParam`).
191
+ * This function should return a variable that will be stored as `pageParam` that can be used when fetching next page.
180
192
  */
181
- getNextPageParam?: (lastPage: TResponse, index: number) => any;
182
- onBeforeFetch?: (cancel: () => void, state: QueryState<TKey, TResponse, TData, TError>) => void;
183
- onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
184
- onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
185
- onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError>) => void;
193
+ getNextPageParam?: (lastPage: TResponse, index: number) => Maybe<TPageParam>;
194
+ onBeforeFetch?: (cancel: () => void, state: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
195
+ onSuccess?: (response: TResponse, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
196
+ onError?: (error: TError, stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
197
+ onSettled?: (stateBeforeCallQuery: QueryState<TKey, TResponse, TData, TError, TPageParam>) => void;
186
198
  /**
187
199
  * Cache time in miliseconds.
188
200
  *
@@ -203,9 +215,9 @@ export type CreateQueryOptions<TKey extends StoreKey = StoreKey, TResponse = any
203
215
  *
204
216
  * @see https://floppy-disk.vercel.app/docs/query/polling
205
217
  */
206
- refetchInterval?: number | false | ((state: QueryState<TKey, TResponse, TData, TError>) => number | false);
218
+ refetchInterval?: number | false | ((state: QueryState<TKey, TResponse, TData, TError, TPageParam>) => number | false);
207
219
  };
208
- export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = UseStores<TKey, QueryState<TKey, TResponse, TData, TError>> & {
220
+ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any> = UseStores<TKey, QueryState<TKey, TResponse, TData, TError, TPageParam>> & {
209
221
  /**
210
222
  * Set query's initial response.
211
223
  *
@@ -214,7 +226,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
214
226
  * IMPORTANT NOTE: Put this on the root component or parent component, before any component subscribed!
215
227
  */
216
228
  setInitialResponse: (options: {
217
- key?: TKey | null;
229
+ key?: Maybe<TKey>;
218
230
  response: TResponse;
219
231
  skipRevalidation?: boolean;
220
232
  }) => void;
@@ -225,7 +237,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
225
237
  /**
226
238
  * Set query state (data, error, etc) to initial state.
227
239
  */
228
- resetSpecificKey: (key?: TKey | null) => void;
240
+ resetSpecificKey: (key?: Maybe<TKey>) => void;
229
241
  /**
230
242
  * Invalidate query means marking a query as stale, and will refetch only if the query is active (has subscriber)
231
243
  */
@@ -233,7 +245,7 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
233
245
  /**
234
246
  * Invalidate query means marking a query as stale, and will refetch only if the query is active (has subscriber)
235
247
  */
236
- invalidateSpecificKey: (key?: TKey | null) => void;
248
+ invalidateSpecificKey: (key?: Maybe<TKey>) => void;
237
249
  /**
238
250
  * Optimistic update.
239
251
  *
@@ -242,8 +254,8 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
242
254
  * IMPORTANT NOTE: This won't work well on infinite query.
243
255
  */
244
256
  optimisticUpdate: (options: {
245
- key?: TKey | null;
246
- response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError>) => TResponse);
257
+ key?: Maybe<TKey>;
258
+ response: TResponse | ((prevState: QueryState<TKey, TResponse, TData, TError, TPageParam>) => TResponse);
247
259
  }) => {
248
260
  revert: () => void;
249
261
  invalidate: () => void;
@@ -251,11 +263,11 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
251
263
  /**
252
264
  * Use query with suspense mode.
253
265
  */
254
- suspend: (key?: TKey | null) => Extract<QueryState<TKey, TResponse, TData, TError>, {
266
+ suspend: (key?: Maybe<TKey>) => Extract<QueryState<TKey, TResponse, TData, TError, TPageParam>, {
255
267
  status: 'success';
256
268
  }>;
257
269
  Render: (props: {
258
- queryKey?: TKey | null;
270
+ queryKey?: Maybe<TKey>;
259
271
  loading?: FunctionComponent<TKey>;
260
272
  success?: FunctionComponent<TKey>;
261
273
  error?: FunctionComponent<TKey>;
@@ -264,4 +276,4 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
264
276
  /**
265
277
  * @see https://floppy-disk.vercel.app/docs/api#createquery
266
278
  */
267
- export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError>) => UseQuery<TKey, TResponse, TData, TError>;
279
+ export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown, TPageParam = any>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError, TPageParam>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError, TPageParam>) => UseQuery<TKey, TResponse, TData, TError, TPageParam>;