next-safe-action@3.0.1.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. diff --git a/dist/hook.d.ts b/dist/hook.d.ts
  2. index 42220517df6bad298dd77f2e1961d6798ecfef0d..7b32f3634610ae20c0b108034a7da8048bc96205 100644
  3. --- a/dist/hook.d.ts
  4. +++ b/dist/hook.d.ts
  5. @@ -1,5 +1,9 @@
  6. -import { z } from 'zod';
  7. -import { C as ClientCaller, H as HookCallbacks, a as HookRes } from './types-31a698ec.js';
  8. +import { z } from "zod";
  9. +import {
  10. + C as ClientCaller,
  11. + H as HookCallbacks,
  12. + a as HookRes,
  13. +} from "./types-31a698ec.js";
  14. /**
  15. * Use the action from a Client Component via hook.
  16. @@ -8,14 +12,17 @@ import { C as ClientCaller, H as HookCallbacks, a as HookRes } from './types-31a
  17. *
  18. * {@link https://github.com/TheEdoRan/next-safe-action/tree/main/packages/next-safe-action#2-the-hook-way See an example}
  19. */
  20. -declare const useAction: <const IV extends z.ZodTypeAny, const Data>(clientCaller: ClientCaller<IV, Data>, cb?: HookCallbacks<IV, Data> | undefined) => {
  21. - execute: (input: z.input<IV>) => void;
  22. - isExecuting: boolean;
  23. - res: HookRes<IV, Data>;
  24. - reset: () => void;
  25. - hasExecuted: boolean;
  26. - hasSucceded: boolean;
  27. - hasErrored: boolean;
  28. +declare const useAction: <const IV extends z.ZodTypeAny, const Data>(
  29. + clientCaller: ClientCaller<IV, Data>,
  30. + cb?: HookCallbacks<IV, Data> | undefined
  31. +) => {
  32. + execute: (input: z.input<IV>) => void;
  33. + isExecuting: boolean;
  34. + res: HookRes<IV, Data>;
  35. + reset: () => void;
  36. + hasExecuted: boolean;
  37. + hasSucceded: boolean;
  38. + hasErrored: boolean;
  39. };
  40. /**
  41. * Use the action from a Client Component via hook, with optimistic data update.
  42. @@ -27,15 +34,22 @@ declare const useAction: <const IV extends z.ZodTypeAny, const Data>(clientCalle
  43. *
  44. * {@link https://github.com/TheEdoRan/next-safe-action/tree/main/packages/next-safe-action#optimistic-update--experimental See an example}
  45. */
  46. -declare const useOptimisticAction: <const IV extends z.ZodTypeAny, const Data>(clientCaller: ClientCaller<IV, Data>, initialOptData: Data, cb?: HookCallbacks<IV, Data> | undefined) => {
  47. - execute: (input: z.input<IV>, newOptimisticData: Partial<Data>) => Promise<void>;
  48. - isExecuting: boolean;
  49. - res: HookRes<IV, Data>;
  50. - optimisticData: Data;
  51. - reset: () => void;
  52. - hasExecuted: boolean;
  53. - hasSucceded: boolean;
  54. - hasErrored: boolean;
  55. +declare const useOptimisticAction: <const IV extends z.ZodTypeAny, const Data>(
  56. + clientCaller: ClientCaller<IV, Data>,
  57. + initialOptData: Data,
  58. + cb?: HookCallbacks<IV, Data> | undefined
  59. +) => {
  60. + execute: (
  61. + input: z.input<IV>,
  62. + newOptimisticData: Partial<Data>
  63. + ) => Promise<void>;
  64. + isExecuting: boolean;
  65. + res: HookRes<IV, Data>;
  66. + optimisticData: Data;
  67. + reset: () => void;
  68. + hasExecuted: boolean;
  69. + hasSucceded: boolean;
  70. + hasErrored: boolean;
  71. };
  72. export { HookCallbacks, HookRes, useAction, useOptimisticAction };
  73. diff --git a/dist/hook.mjs b/dist/hook.mjs
  74. index 89d77b1da720c87508e525f09e2c0005a1986819..5b0d50450441734813511cce9c6df203104c6f66 100644
  75. --- a/dist/hook.mjs
  76. +++ b/dist/hook.mjs
  77. @@ -1,3 +1,5 @@
  78. +"use client";
  79. +
  80. // src/hook.ts
  81. import {
  82. experimental_useOptimistic,
  83. @@ -5,31 +7,34 @@ import {
  84. useEffect,
  85. useRef,
  86. useState,
  87. - useTransition
  88. + useTransition,
  89. } from "react";
  90. // src/utils.ts
  91. -var isNextRedirectError = (e) => e instanceof Error && e.message === "NEXT_REDIRECT";
  92. -var isNextNotFoundError = (e) => e instanceof Error && e.message === "NEXT_NOT_FOUND";
  93. +var isError = (e) => e instanceof Error;
  94. +var isNextRedirectError = (e) => isError(e) && e.message === "NEXT_REDIRECT";
  95. +var isNextNotFoundError = (e) => isError(e) && e.message === "NEXT_NOT_FOUND";
  96. // src/hook.ts
  97. -"use client";
  98. var getActionStatus = (res) => {
  99. const hasSucceded = typeof res.data !== "undefined";
  100. - const hasErrored = typeof res.validationError !== "undefined" || typeof res.serverError !== "undefined" || typeof res.fetchError !== "undefined";
  101. + const hasErrored =
  102. + typeof res.validationError !== "undefined" ||
  103. + typeof res.serverError !== "undefined" ||
  104. + typeof res.fetchError !== "undefined";
  105. const hasExecuted = hasSucceded || hasErrored;
  106. return { hasExecuted, hasSucceded, hasErrored };
  107. };
  108. -var useActionCallbacks = (res, hasSucceded, hasErrored, reset, cb) => {
  109. +var useActionCallbacks = (input, res, hasSucceded, hasErrored, reset, cb) => {
  110. const onSuccessRef = useRef(cb?.onSuccess);
  111. const onErrorRef = useRef(cb?.onError);
  112. useEffect(() => {
  113. const onSuccess = onSuccessRef.current;
  114. const onError = onErrorRef.current;
  115. if (onSuccess && hasSucceded) {
  116. - onSuccess(res.data, reset);
  117. + onSuccess(res.data, reset, input);
  118. } else if (onError && hasErrored) {
  119. - onError(res, reset);
  120. + onError(res, reset, input);
  121. }
  122. }, [hasErrored, hasSucceded, res, reset]);
  123. };
  124. @@ -37,21 +42,31 @@ var useAction = (clientCaller, cb) => {
  125. const [isExecuting, startTransition] = useTransition();
  126. const executor = useRef(clientCaller);
  127. const [res, setRes] = useState({});
  128. + const [input, setInput] = useState();
  129. + const onExecuteRef = useRef(cb?.onExecute);
  130. const { hasExecuted, hasSucceded, hasErrored } = getActionStatus(res);
  131. - const execute = useCallback((input) => {
  132. + const execute = useCallback((input2) => {
  133. + setInput(input2);
  134. + const onExecute = onExecuteRef.current;
  135. + if (onExecute) {
  136. + onExecute(input2);
  137. + }
  138. return startTransition(() => {
  139. - return executor.current(input).then((res2) => setRes(res2)).catch((e) => {
  140. - if (isNextRedirectError(e) || isNextNotFoundError(e)) {
  141. - throw e;
  142. - }
  143. - setRes({ fetchError: e });
  144. - });
  145. + return executor
  146. + .current(input2)
  147. + .then((res2) => setRes(res2))
  148. + .catch((e) => {
  149. + if (isNextRedirectError(e) || isNextNotFoundError(e)) {
  150. + throw e;
  151. + }
  152. + setRes({ fetchError: e });
  153. + });
  154. });
  155. }, []);
  156. const reset = useCallback(() => {
  157. setRes({});
  158. }, []);
  159. - useActionCallbacks(res, hasSucceded, hasErrored, reset, cb);
  160. + useActionCallbacks(input, res, hasSucceded, hasErrored, reset, cb);
  161. return {
  162. execute,
  163. isExecuting,
  164. @@ -59,34 +74,47 @@ var useAction = (clientCaller, cb) => {
  165. reset,
  166. hasExecuted,
  167. hasSucceded,
  168. - hasErrored
  169. + hasErrored,
  170. };
  171. };
  172. var useOptimisticAction = (clientCaller, initialOptData, cb) => {
  173. const [res, setRes] = useState({});
  174. - const [optState, syncState] = experimental_useOptimistic({ ...initialOptData, ...res.data, __isExecuting__: false }, (state, newState) => ({
  175. - ...state,
  176. - ...newState,
  177. - __isExecuting__: true
  178. - }));
  179. + const [input, setInput] = useState();
  180. + const [optState, syncState] = experimental_useOptimistic(
  181. + { ...initialOptData, ...res.data, __isExecuting__: false },
  182. + (state, newState) => ({
  183. + ...state,
  184. + ...newState,
  185. + __isExecuting__: true,
  186. + })
  187. + );
  188. const executor = useRef(clientCaller);
  189. + const onExecuteRef = useRef(cb?.onExecute);
  190. const { hasExecuted, hasSucceded, hasErrored } = getActionStatus(res);
  191. const execute = useCallback(
  192. - (input, newOptimisticData) => {
  193. + (input2, newOptimisticData) => {
  194. syncState(newOptimisticData);
  195. - return executor.current(input).then((res2) => setRes(res2)).catch((e) => {
  196. - if (isNextRedirectError(e) || isNextNotFoundError(e)) {
  197. - throw e;
  198. - }
  199. - setRes({ fetchError: e });
  200. - });
  201. + setInput(input2);
  202. + const onExecute = onExecuteRef.current;
  203. + if (onExecute) {
  204. + onExecute(input2);
  205. + }
  206. + return executor
  207. + .current(input2)
  208. + .then((res2) => setRes(res2))
  209. + .catch((e) => {
  210. + if (isNextRedirectError(e) || isNextNotFoundError(e)) {
  211. + throw e;
  212. + }
  213. + setRes({ fetchError: e });
  214. + });
  215. },
  216. [syncState]
  217. );
  218. const reset = useCallback(() => {
  219. setRes({});
  220. }, []);
  221. - useActionCallbacks(res, hasSucceded, hasErrored, reset, cb);
  222. + useActionCallbacks(input, res, hasSucceded, hasErrored, reset, cb);
  223. const { __isExecuting__, ...optimisticData } = optState;
  224. return {
  225. execute,
  226. @@ -97,11 +125,8 @@ var useOptimisticAction = (clientCaller, initialOptData, cb) => {
  227. reset,
  228. hasExecuted,
  229. hasSucceded,
  230. - hasErrored
  231. + hasErrored,
  232. };
  233. };
  234. -export {
  235. - useAction,
  236. - useOptimisticAction
  237. -};
  238. +export { useAction, useOptimisticAction };
  239. //# sourceMappingURL=hook.mjs.map
  240. diff --git a/dist/types-31a698ec.d.ts b/dist/types-31a698ec.d.ts
  241. index c9339a0a530dd1825af0c6e1b4a6e9a58adc3c97..1eb8b8303d1534deee9f0084b6c62c9eabb147b2 100644
  242. --- a/dist/types-31a698ec.d.ts
  243. +++ b/dist/types-31a698ec.d.ts
  244. @@ -1,30 +1,53 @@
  245. -import { z } from 'zod';
  246. +import { z } from "zod";
  247. /**
  248. * Type of the function called from Client Components with typesafe input data for the Server Action.
  249. */
  250. -type ClientCaller<IV extends z.ZodTypeAny, Data> = (input: z.input<IV>) => Promise<{
  251. - data?: Data;
  252. - serverError?: true;
  253. - validationError?: Partial<Record<keyof z.input<IV>, string[]>>;
  254. +type ClientCaller<IV extends z.ZodTypeAny, Data> = (
  255. + input: z.input<IV>
  256. +) => Promise<{
  257. + data?: Data;
  258. + serverError?: string;
  259. + validationError?: Partial<Record<keyof z.input<IV>, string[]>>;
  260. }>;
  261. /**
  262. * Type of the function that executes server code when defining a new safe action.
  263. */
  264. -type ActionServerFn<IV extends z.ZodTypeAny, Data, Context extends object> = (parsedInput: z.input<IV>, ctx: Context) => Promise<Data>;
  265. +type ActionServerFn<IV extends z.ZodTypeAny, Data, Context extends object> = (
  266. + parsedInput: z.input<IV>,
  267. + ctx: Context
  268. +) => Promise<Data>;
  269. /**
  270. * Type of `res` object returned by `useAction` and `useOptimisticAction` hooks.
  271. */
  272. -type HookRes<IV extends z.ZodTypeAny, Data> = Awaited<ReturnType<ClientCaller<IV, Data>>> & {
  273. - fetchError?: unknown;
  274. +type HookRes<IV extends z.ZodTypeAny, Data> = Awaited<
  275. + ReturnType<ClientCaller<IV, Data>>
  276. +> & {
  277. + fetchError?: unknown;
  278. };
  279. /**
  280. * Type of hooks callbacks (`onSuccess` and `onError`).
  281. * These are executed when the action succeeds or fails.
  282. */
  283. type HookCallbacks<IV extends z.ZodTypeAny, Data> = {
  284. - onSuccess?: (data: NonNullable<Pick<HookRes<IV, Data>, "data">["data"]>, reset: () => void) => void;
  285. - onError?: (error: Omit<HookRes<IV, Data>, "data">, reset: () => void) => void;
  286. + onSuccess?: (
  287. + data: NonNullable<Pick<HookRes<IV, Data>, "data">["data"]>,
  288. + reset: () => void,
  289. + input: z.input<IV>
  290. + ) => void;
  291. + onError?: (
  292. + error: Omit<HookRes<IV, Data>, "data">,
  293. + reset: () => void,
  294. + input: z.input<IV>
  295. + ) => void;
  296. + onExecute?: (input: z.input<IV>) => unknown;
  297. };
  298. +type MaybePromise<T> = T | Promise<T>;
  299. -export { ActionServerFn as A, ClientCaller as C, HookCallbacks as H, HookRes as a };
  300. +export {
  301. + ActionServerFn as A,
  302. + ClientCaller as C,
  303. + HookCallbacks as H,
  304. + MaybePromise as M,
  305. + HookRes as a,
  306. +};