Skip to content

Commit

Permalink
feat: support extending previous schemas inside schema method
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Jun 20, 2024
1 parent 88f8c43 commit dfd8ad3
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/next-safe-action/src/safe-action-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ export class SafeActionClient<
* {@link https://next-safe-action.dev/docs/safe-action-client/instance-methods#schema See docs for more information}
*/
schema<
OS extends Schema | (() => Promise<Schema>),
AS extends Schema = OS extends () => Promise<Schema> ? Awaited<ReturnType<OS>> : OS, // actual schema
OS extends Schema | ((prevSchema: S) => Promise<Schema>),
AS extends Schema = OS extends (prevSchema: S) => Promise<Schema> ? Awaited<ReturnType<OS>> : OS, // actual schema
OCVE = ODVES extends "flattened" ? FlattenedValidationErrors<ValidationErrors<AS>> : ValidationErrors<AS>,
>(
schema: OS,
Expand All @@ -147,7 +147,13 @@ export class SafeActionClient<
metadataSchema: this.#metadataSchema,
metadata: this.#metadata,
// @ts-expect-error
schemaFn: (schema[Symbol.toStringTag] === "AsyncFunction" ? schema : async () => schema) as SF,
schemaFn: (schema[Symbol.toStringTag] === "AsyncFunction"
? async () => {
const prevSchema = await this.#schemaFn?.();
// @ts-expect-error
return schema(prevSchema as S) as AS;
}
: async () => schema) as SF,
bindArgsSchemas: this.#bindArgsSchemas,
handleValidationErrorsShape: (utils?.handleValidationErrorsShape ??
this.#handleValidationErrorsShape) as HandleValidationErrorsShapeFn<AS, OCVE>,
Expand Down

0 comments on commit dfd8ad3

Please sign in to comment.