Skip to content

Commit dfd8ad3

Browse files
committedJun 20, 2024
feat: support extending previous schemas inside schema method
1 parent 88f8c43 commit dfd8ad3

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed
 

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ export class SafeActionClient<
130130
* {@link https://next-safe-action.dev/docs/safe-action-client/instance-methods#schema See docs for more information}
131131
*/
132132
schema<
133-
OS extends Schema | (() => Promise<Schema>),
134-
AS extends Schema = OS extends () => Promise<Schema> ? Awaited<ReturnType<OS>> : OS, // actual schema
133+
OS extends Schema | ((prevSchema: S) => Promise<Schema>),
134+
AS extends Schema = OS extends (prevSchema: S) => Promise<Schema> ? Awaited<ReturnType<OS>> : OS, // actual schema
135135
OCVE = ODVES extends "flattened" ? FlattenedValidationErrors<ValidationErrors<AS>> : ValidationErrors<AS>,
136136
>(
137137
schema: OS,
@@ -147,7 +147,13 @@ export class SafeActionClient<
147147
metadataSchema: this.#metadataSchema,
148148
metadata: this.#metadata,
149149
// @ts-expect-error
150-
schemaFn: (schema[Symbol.toStringTag] === "AsyncFunction" ? schema : async () => schema) as SF,
150+
schemaFn: (schema[Symbol.toStringTag] === "AsyncFunction"
151+
? async () => {
152+
const prevSchema = await this.#schemaFn?.();
153+
// @ts-expect-error
154+
return schema(prevSchema as S) as AS;
155+
}
156+
: async () => schema) as SF,
151157
bindArgsSchemas: this.#bindArgsSchemas,
152158
handleValidationErrorsShape: (utils?.handleValidationErrorsShape ??
153159
this.#handleValidationErrorsShape) as HandleValidationErrorsShapeFn<AS, OCVE>,

0 commit comments

Comments
 (0)
Please sign in to comment.