@hyper-fetch/react 9.6.2 → 9.7.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": "@hyper-fetch/react",
3
- "version": "9.6.2",
3
+ "version": "9.7.0",
4
4
  "description": "React hooks and utils for the hyper-fetch",
5
5
  "author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
6
6
  "homepage": "https://hyperfetch.bettertyped.com/",
@@ -2,7 +2,9 @@ import type { RequestInstance, RequiredKeys } from "@hyper-fetch/core";
2
2
 
3
3
  import type { UseSubmitOptionsType } from "./use-submit.types";
4
4
 
5
- type DefaultOptionsType = RequiredKeys<Omit<UseSubmitOptionsType<RequestInstance>, "initialResponse" | "select">> & {
5
+ type DefaultOptionsType = RequiredKeys<
6
+ Omit<UseSubmitOptionsType<RequestInstance>, "initialResponse" | "select" | "optimistic">
7
+ > & {
6
8
  initialResponse: null;
7
9
  };
8
10
 
@@ -151,7 +151,10 @@ export const useSubmit = <RequestType extends RequestInstance, TSelected = Extra
151
151
  return Promise.resolve(getNotSentResponse("Cannot submit request. Option 'disabled' is enabled"));
152
152
  }
153
153
 
154
- const requestClone = request.clone(submitOptions as any) as unknown as RequestType;
154
+ const { optimistic: submitOptimistic, ...cloneOptions } = submitOptions || {};
155
+ const requestClone = request.clone(cloneOptions as any) as unknown as RequestType;
156
+ // Read directly from options - functions are not part of the memoized options key
157
+ const optimistic = submitOptimistic ?? options?.optimistic;
155
158
 
156
159
  const triggerRequest = () => {
157
160
  addCacheDataListener(requestClone);
@@ -159,6 +162,7 @@ export const useSubmit = <RequestType extends RequestInstance, TSelected = Extra
159
162
  const configuration: RequestSendOptionsType<RequestType> = {
160
163
  dispatcherType: "submit",
161
164
  ...(submitOptions as RequestSendOptionsType<RequestType>),
165
+ optimistic,
162
166
  onBeforeSent: (data) => {
163
167
  // Deduplicated submit joins the request which is already in flight and shares its id.
164
168
  // We keep a single set of listeners for it, so the callbacks fire once per response.
@@ -2,6 +2,7 @@ import type {
2
2
  ExtractAdapterResolvedType,
3
3
  ExtractResponseType,
4
4
  NullableType,
5
+ OptimisticSendOptionType,
5
6
  RequestInstance,
6
7
  RequestSendType,
7
8
  } from "@hyper-fetch/core";
@@ -47,6 +48,11 @@ export type UseSubmitOptionsType<T extends RequestInstance, TSelected = ExtractR
47
48
  * Keep references of the unchanged parts of the selected data between updates (default true).
48
49
  */
49
50
  structuralSharing?: boolean;
51
+ /**
52
+ * Optimistic update of every submit from this hook - `optimistic()` operations or a callback like the one of
53
+ * `setOptimistic`. Replaces the request's own optimistic callback; `submit({ optimistic })` replaces this one.
54
+ */
55
+ optimistic?: OptimisticSendOptionType<T>;
50
56
  } & (
51
57
  | {
52
58
  /**