Skip to content

Commit

Permalink
refactor: require async functions in action declaration files
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Dec 8, 2024
1 parent 9021482 commit 7668e06
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
17 changes: 8 additions & 9 deletions packages/next-safe-action/src/action-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
isRedirectError,
winningBoolean,
} from "./utils";
import type { MaybePromise } from "./utils.types";
import {
ActionMetadataValidationError,
ActionOutputDataValidationError,
Expand Down Expand Up @@ -295,11 +294,11 @@ export function actionBuilder<
// Execute middleware chain + action function.
await executeMiddlewareStack();

const callbacksToExecute: MaybePromise<unknown>[] = [];
const callbackPromises: (Promise<unknown> | undefined)[] = [];

// If an internal framework error occurred, throw it, so it will be processed by Next.js.
if (frameworkError) {
callbacksToExecute.push(
callbackPromises.push(
utils?.onSuccess?.({
data: undefined,
metadata: args.metadata,
Expand All @@ -314,7 +313,7 @@ export function actionBuilder<
})
);

callbacksToExecute.push(
callbackPromises.push(
utils?.onSettled?.({
metadata: args.metadata,
ctx: currentCtx as Ctx,
Expand All @@ -327,7 +326,7 @@ export function actionBuilder<
})
);

await Promise.all(callbacksToExecute);
await Promise.all(callbackPromises);

throw frameworkError;
}
Expand Down Expand Up @@ -361,7 +360,7 @@ export function actionBuilder<
actionResult.data = middlewareResult.data as Data;
}

callbacksToExecute.push(
callbackPromises.push(
utils?.onSuccess?.({
metadata: args.metadata,
ctx: currentCtx as Ctx,
Expand All @@ -376,7 +375,7 @@ export function actionBuilder<
})
);
} else {
callbacksToExecute.push(
callbackPromises.push(
utils?.onError?.({
metadata: args.metadata,
ctx: currentCtx as Ctx,
Expand All @@ -388,7 +387,7 @@ export function actionBuilder<
}

// onSettled, if provided, is always executed.
callbacksToExecute.push(
callbackPromises.push(
utils?.onSettled?.({
metadata: args.metadata,
ctx: currentCtx as Ctx,
Expand All @@ -401,7 +400,7 @@ export function actionBuilder<
})
);

await Promise.all(callbacksToExecute);
await Promise.all(callbackPromises);

return actionResult;
};
Expand Down
12 changes: 7 additions & 5 deletions packages/next-safe-action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ export const createSafeActionClient = <
metadata: undefined as MetadataSchema extends Schema ? Infer<MetadataSchema> : undefined,
defaultValidationErrorsShape: (createOpts?.defaultValidationErrorsShape ?? "formatted") as ODVES,
throwValidationErrors: Boolean(createOpts?.throwValidationErrors),
handleValidationErrorsShape:
createOpts?.defaultValidationErrorsShape === "flattened" ? flattenValidationErrors : formatValidationErrors,
handleBindArgsValidationErrorsShape:
handleValidationErrorsShape: async (ve) =>
createOpts?.defaultValidationErrorsShape === "flattened"
? flattenBindArgsValidationErrors
: formatBindArgsValidationErrors,
? flattenValidationErrors(ve)
: formatValidationErrors(ve),
handleBindArgsValidationErrorsShape: async (ve) =>
createOpts?.defaultValidationErrorsShape === "flattened"
? flattenBindArgsValidationErrors(ve)
: formatBindArgsValidationErrors(ve),
});
};
6 changes: 3 additions & 3 deletions packages/next-safe-action/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ export type SafeActionUtils<
hasRedirected: boolean;
hasNotFound: boolean;
hasForbidden: boolean;
}) => MaybePromise<unknown>;
}) => Promise<unknown>;
onError?: (args: {
error: Prettify<Omit<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>, "data">>;
metadata: MD;
ctx?: Prettify<Ctx>;
clientInput: S extends Schema ? InferIn<S> : undefined;
bindArgsClientInputs: InferInArray<BAS>;
}) => MaybePromise<unknown>;
}) => Promise<unknown>;
onSettled?: (args: {
result: Prettify<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
metadata: MD;
Expand All @@ -193,7 +193,7 @@ export type SafeActionUtils<
hasRedirected: boolean;
hasNotFound: boolean;
hasForbidden: boolean;
}) => MaybePromise<unknown>;
}) => Promise<unknown>;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/next-safe-action/src/validation-errors.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export type HandleValidationErrorsShapeFn<
metadata: MD;
ctx: Prettify<Ctx>;
}
) => CVE | Promise<CVE>;
) => Promise<CVE>;

/**
* Type of the function used to format bind arguments validation errors.
Expand All @@ -82,4 +82,4 @@ export type HandleBindArgsValidationErrorsShapeFn<
metadata: MD;
ctx: Prettify<Ctx>;
}
) => CBAVE | Promise<CBAVE>;
) => Promise<CBAVE>;

0 comments on commit 7668e06

Please sign in to comment.