Skip to content

Commit

Permalink
refactor(validation): use Zod adapter by default
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Jul 19, 2024
1 parent 555390f commit 7940d10
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
19 changes: 10 additions & 9 deletions packages/next-safe-action/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Infer, Schema } from "./adapters/types";
import { zodAdapter } from "./adapters/zod";
import type { DVES, SafeActionClientOpts } from "./index.types";
import { SafeActionClient } from "./safe-action-client";
import { DEFAULT_SERVER_ERROR_MESSAGE } from "./utils";
Expand Down Expand Up @@ -34,12 +35,12 @@ export const createSafeActionClient = <
ServerError = string,
MetadataSchema extends Schema | undefined = undefined,
>(
createOpts: SafeActionClientOpts<ServerError, MetadataSchema, ODVES>
createOpts?: SafeActionClientOpts<ServerError, MetadataSchema, ODVES>
) => {
// If server log function is not provided, default to `console.error` for logging
// server error messages.
const handleServerErrorLog =
createOpts.handleServerErrorLog ||
createOpts?.handleServerErrorLog ||
(((originalError: Error) => {
console.error("Action error:", originalError.message);
}) as unknown as NonNullable<SafeActionClientOpts<ServerError, MetadataSchema, ODVES>["handleServerErrorLog"]>);
Expand All @@ -48,7 +49,7 @@ export const createSafeActionClient = <
// messages returned on the client.
// Otherwise mask the error and use a generic message.
const handleReturnedServerError =
createOpts.handleReturnedServerError ||
createOpts?.handleReturnedServerError ||
((() => DEFAULT_SERVER_ERROR_MESSAGE) as unknown as NonNullable<
SafeActionClientOpts<ServerError, MetadataSchema, ODVES>["handleReturnedServerError"]
>);
Expand All @@ -59,16 +60,16 @@ export const createSafeActionClient = <
handleReturnedServerError,
schemaFn: undefined,
bindArgsSchemas: [],
validationAdapter: createOpts.validationAdapter,
validationAdapter: createOpts?.validationAdapter ?? zodAdapter(), // use zod adapter by default
ctxType: undefined,
metadataSchema: (createOpts.defineMetadataSchema?.() ?? undefined) as MetadataSchema,
metadataSchema: (createOpts?.defineMetadataSchema?.() ?? undefined) as MetadataSchema,
metadata: undefined as MetadataSchema extends Schema ? Infer<MetadataSchema> : undefined,
defaultValidationErrorsShape: (createOpts.defaultValidationErrorsShape ?? "formatted") as ODVES,
throwValidationErrors: Boolean(createOpts.throwValidationErrors),
defaultValidationErrorsShape: (createOpts?.defaultValidationErrorsShape ?? "formatted") as ODVES,
throwValidationErrors: Boolean(createOpts?.throwValidationErrors),
handleValidationErrorsShape:
createOpts.defaultValidationErrorsShape === "flattened" ? flattenValidationErrors : formatValidationErrors,
createOpts?.defaultValidationErrorsShape === "flattened" ? flattenValidationErrors : formatValidationErrors,
handleBindArgsValidationErrorsShape:
createOpts.defaultValidationErrorsShape === "flattened"
createOpts?.defaultValidationErrorsShape === "flattened"
? flattenBindArgsValidationErrors
: formatBindArgsValidationErrors,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/next-safe-action/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type SafeActionClientOpts<
MetadataSchema extends Schema | undefined,
ODVES extends DVES | undefined,
> = {
validationAdapter: ValidationAdapter;
validationAdapter?: ValidationAdapter;
defineMetadataSchema?: () => MetadataSchema;
handleReturnedServerError?: (
error: Error,
Expand Down

0 comments on commit 7940d10

Please sign in to comment.