Template
1
0

feat: add error response support to actions

This commit is contained in:
2025-04-18 22:56:29 +00:00
parent ecb5f80584
commit b0a0494a28
5 changed files with 16 additions and 3 deletions

View File

@@ -1,5 +1,7 @@
import z, { ZodObject, ZodRawShape } from "zod";
import { RelayError } from "./errors.ts";
export class Action<TActionState extends ActionState = ActionState> {
constructor(readonly state: TActionState) {}
@@ -61,5 +63,5 @@ type ActionState = {
};
type ActionHandlerFn<TInput = any, TOutput = any> = TInput extends ZodObject
? (input: z.infer<TInput>) => TOutput extends ZodObject ? Promise<z.infer<TOutput>> : Promise<void>
: () => TOutput extends ZodObject ? Promise<z.infer<TOutput>> : Promise<void>;
? (input: z.infer<TInput>) => TOutput extends ZodObject ? Promise<z.infer<TOutput> | RelayError> : Promise<void | RelayError>
: () => TOutput extends ZodObject ? Promise<z.infer<TOutput> | RelayError> : Promise<void | RelayError>;

View File

@@ -130,6 +130,9 @@ export class Api<TRoutes extends Route[]> {
return toResponse(new InternalServerError(`Action '${action.state.name}' is missing handler.`));
}
const output = await action.state.handle(result.data);
if (output instanceof RelayError) {
return toResponse(output);
}
for (const key in output) {
context[key] = output[key];
}