Skip to content

Commit c8496f7

Browse files
committedJun 11, 2024··
fix: return proper results when handling framework navigation functions
1 parent 55779d1 commit c8496f7

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed
 

‎packages/next-safe-action/src/hooks.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ const useActionCallbacks = <
102102
await Promise.resolve(onExecute?.({ input }));
103103
break;
104104
case "hasSucceeded":
105-
await Promise.resolve(onSuccess?.({ data: result.data!, input }));
105+
await Promise.resolve(onSuccess?.({ data: result?.data, input }));
106106
await Promise.resolve(onSettled?.({ result, input }));
107107
break;
108108
case "hasErrored":
@@ -155,6 +155,7 @@ export const useAction = <
155155
.then((res) => setResult(res ?? EMPTY_HOOK_RESULT))
156156
.catch((e) => {
157157
if (isRedirectError(e) || isNotFoundError(e)) {
158+
setResult(EMPTY_HOOK_RESULT);
158159
throw e;
159160
}
160161

@@ -183,6 +184,7 @@ export const useAction = <
183184
})
184185
.catch((e) => {
185186
if (isRedirectError(e) || isNotFoundError(e)) {
187+
setResult(EMPTY_HOOK_RESULT);
186188
throw e;
187189
}
188190

@@ -205,7 +207,7 @@ export const useAction = <
205207
};
206208

207209
useActionCallbacks({
208-
result,
210+
result: result ?? EMPTY_HOOK_RESULT,
209211
input: clientInput as S extends Schema ? InferIn<S> : undefined,
210212
status,
211213
cb: utils,
@@ -268,6 +270,7 @@ export const useOptimisticAction = <
268270
.then((res) => setResult(res ?? EMPTY_HOOK_RESULT))
269271
.catch((e) => {
270272
if (isRedirectError(e) || isNotFoundError(e)) {
273+
setResult(EMPTY_HOOK_RESULT);
271274
throw e;
272275
}
273276

@@ -297,6 +300,7 @@ export const useOptimisticAction = <
297300
})
298301
.catch((e) => {
299302
if (isRedirectError(e) || isNotFoundError(e)) {
303+
setResult(EMPTY_HOOK_RESULT);
300304
throw e;
301305
}
302306

@@ -319,7 +323,7 @@ export const useOptimisticAction = <
319323
};
320324

321325
useActionCallbacks({
322-
result,
326+
result: result ?? EMPTY_HOOK_RESULT,
323327
input: clientInput as S extends Schema ? InferIn<S> : undefined,
324328
status,
325329
cb: {
@@ -384,7 +388,7 @@ export const useStateAction = <
384388
);
385389

386390
useActionCallbacks({
387-
result,
391+
result: result ?? EMPTY_HOOK_RESULT,
388392
input: clientInput as S extends Schema ? InferIn<S> : undefined,
389393
status,
390394
cb: {

‎packages/next-safe-action/src/hooks.types.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { InferIn, Schema } from "@typeschema/main";
22
import type { SafeActionResult } from "./index.types";
3-
import type { MaybePromise } from "./utils";
3+
import type { MaybePromise, Prettify } from "./utils";
44

55
/**
66
* Type of `result` object returned by `useAction`, `useOptimisticAction` and `useStateAction` hooks.
@@ -29,13 +29,13 @@ export type HookCallbacks<
2929
Data,
3030
> = {
3131
onExecute?: (args: { input: S extends Schema ? InferIn<S> : undefined }) => MaybePromise<void>;
32-
onSuccess?: (args: { data: Data; input: S extends Schema ? InferIn<S> : undefined }) => MaybePromise<void>;
32+
onSuccess?: (args: { data?: Data; input: S extends Schema ? InferIn<S> : undefined }) => MaybePromise<void>;
3333
onError?: (args: {
34-
error: Omit<HookResult<ServerError, S, BAS, CVE, CBAVE, Data>, "data">;
34+
error: Prettify<Omit<HookResult<ServerError, S, BAS, CVE, CBAVE, Data>, "data">>;
3535
input: S extends Schema ? InferIn<S> : undefined;
3636
}) => MaybePromise<void>;
3737
onSettled?: (args: {
38-
result: HookResult<ServerError, S, BAS, CVE, CBAVE, Data>;
38+
result: Prettify<HookResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
3939
input: S extends Schema ? InferIn<S> : undefined;
4040
}) => MaybePromise<void>;
4141
};

‎packages/next-safe-action/src/index.types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,19 @@ export type SafeActionCallbacks<
136136
Data,
137137
> = {
138138
onSuccess?: (args: {
139-
data: Data;
139+
data?: Data;
140140
clientInput: S extends Schema ? InferIn<S> : undefined;
141141
bindArgsClientInputs: InferInArray<BAS>;
142142
parsedInput: S extends Schema ? Infer<S> : undefined;
143143
bindArgsParsedInputs: InferArray<BAS>;
144144
}) => MaybePromise<void>;
145145
onError?: (args: {
146-
error: Omit<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>, "data">;
146+
error: Prettify<Omit<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>, "data">>;
147147
clientInput: S extends Schema ? InferIn<S> : undefined;
148148
bindArgsClientInputs: InferInArray<BAS>;
149149
}) => MaybePromise<void>;
150150
onSettled?: (args: {
151-
result: SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>;
151+
result: Prettify<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
152152
clientInput: S extends Schema ? InferIn<S> : undefined;
153153
bindArgsClientInputs: InferInArray<BAS>;
154154
}) => MaybePromise<void>;

‎website/docs/types.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export type SafeActionCallbacks<
183183
Data,
184184
> = {
185185
onSuccess?: (args: {
186-
data: Data;
186+
data?: Data;
187187
clientInput: S extends Schema ? InferIn<S> : undefined;
188188
bindArgsClientInputs: InferInArray<BAS>;
189189
parsedInput: S extends Schema ? Infer<S> : undefined;

0 commit comments

Comments
 (0)
Please sign in to comment.