Skip to content

Commit

Permalink
refactor: type return of createSafeActionClient
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Aug 20, 2024
1 parent 214e648 commit 52bee97
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
30 changes: 22 additions & 8 deletions packages/next-safe-action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
formatBindArgsValidationErrors,
formatValidationErrors,
} from "./validation-errors";
import type { HandleBindArgsValidationErrorsShapeFn, HandleValidationErrorsShapeFn } from "./validation-errors.types";

export { createMiddleware } from "./middleware";
export { ActionMetadataError, DEFAULT_SERVER_ERROR_MESSAGE } from "./utils";
Expand All @@ -32,12 +33,25 @@ export type * from "./validation-errors.types";
* {@link https://next-safe-action.dev/docs/safe-action-client/initialization-options See docs for more information}
*/
export const createSafeActionClient = <
ODVES extends DVES | undefined = undefined,
ServerError = string,
MetadataSchema extends Schema | undefined = undefined,
ODVES extends DVES | undefined = undefined,
CVE = undefined,
CBAVE = undefined,
>(
createOpts?: SafeActionClientOpts<ServerError, MetadataSchema, ODVES>
) => {
): SafeActionClient<
ServerError,
ODVES,
MetadataSchema,
MetadataSchema extends Schema ? Infer<MetadataSchema> : undefined,
{},
undefined,
undefined,
readonly [],
CVE,
CBAVE
> => {
// If server log function is not provided, default to `console.error` for logging
// server error messages.
const handleServerErrorLog =
Expand Down Expand Up @@ -67,11 +81,11 @@ 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:
createOpts?.defaultValidationErrorsShape === "flattened"
? flattenBindArgsValidationErrors
: formatBindArgsValidationErrors,
handleValidationErrorsShape: (createOpts?.defaultValidationErrorsShape === "flattened"
? flattenValidationErrors
: formatValidationErrors) as unknown as HandleValidationErrorsShapeFn<undefined, CVE>,
handleBindArgsValidationErrorsShape: (createOpts?.defaultValidationErrorsShape === "flattened"
? flattenBindArgsValidationErrors
: formatBindArgsValidationErrors) as HandleBindArgsValidationErrorsShapeFn<readonly [], CBAVE>,
});
};
4 changes: 2 additions & 2 deletions packages/next-safe-action/src/safe-action-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class SafeActionClient<
ServerError,
ODVES extends DVES | undefined, // override default validation errors shape
MetadataSchema extends Schema | undefined = undefined,
MD = MetadataSchema extends Schema ? Infer<Schema> : undefined,
MD = MetadataSchema extends Schema ? Infer<MetadataSchema> : undefined,
Ctx extends object = {},
SF extends (() => Promise<Schema>) | undefined = undefined, // schema function
S extends Schema | undefined = SF extends Function ? Awaited<ReturnType<SF>> : undefined,
Expand Down Expand Up @@ -233,7 +233,7 @@ export class SafeActionClient<
}

/**
* Define the stateful action (without input validation schema, bind arguments validation schemas or metadata).
* Define the stateful action.
* To be used with the [`useStateAction`](https://next-safe-action.dev/docs/execution/hooks/usestateaction) hook.
* @param serverCodeFn Code that will be executed on the **server side**
* @param [cb] Optional callbacks that will be called after action execution, on the server.
Expand Down

0 comments on commit 52bee97

Please sign in to comment.