feat: add error response support to actions
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@valkyr/relay",
|
"name": "@valkyr/relay",
|
||||||
"version": "0.1.1",
|
"version": "0.1.2",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./mod.ts",
|
".": "./mod.ts",
|
||||||
"./http": "./adapters/http.ts"
|
"./http": "./adapters/http.ts"
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import z, { ZodObject, ZodRawShape } from "zod";
|
import z, { ZodObject, ZodRawShape } from "zod";
|
||||||
|
|
||||||
|
import { RelayError } from "./errors.ts";
|
||||||
|
|
||||||
export class Action<TActionState extends ActionState = ActionState> {
|
export class Action<TActionState extends ActionState = ActionState> {
|
||||||
constructor(readonly state: TActionState) {}
|
constructor(readonly state: TActionState) {}
|
||||||
|
|
||||||
@@ -61,5 +63,5 @@ type ActionState = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
type ActionHandlerFn<TInput = any, TOutput = any> = TInput extends ZodObject
|
type ActionHandlerFn<TInput = any, TOutput = any> = TInput extends ZodObject
|
||||||
? (input: z.infer<TInput>) => 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>> : Promise<void>;
|
: () => TOutput extends ZodObject ? Promise<z.infer<TOutput> | RelayError> : Promise<void | RelayError>;
|
||||||
|
|||||||
@@ -130,6 +130,9 @@ export class Api<TRoutes extends Route[]> {
|
|||||||
return toResponse(new InternalServerError(`Action '${action.state.name}' is missing handler.`));
|
return toResponse(new InternalServerError(`Action '${action.state.name}' is missing handler.`));
|
||||||
}
|
}
|
||||||
const output = await action.state.handle(result.data);
|
const output = await action.state.handle(result.data);
|
||||||
|
if (output instanceof RelayError) {
|
||||||
|
return toResponse(output);
|
||||||
|
}
|
||||||
for (const key in output) {
|
for (const key in output) {
|
||||||
context[key] = output[key];
|
context[key] = output[key];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
import z from "zod";
|
import z from "zod";
|
||||||
|
|
||||||
import { action } from "../../libraries/action.ts";
|
import { action } from "../../libraries/action.ts";
|
||||||
|
import { BadRequestError } from "../../mod.ts";
|
||||||
|
|
||||||
export const addTwoNumbers = action
|
export const addTwoNumbers = action
|
||||||
.make("addTwoNumbers")
|
.make("addTwoNumbers")
|
||||||
.input({ a: z.number(), b: z.number() })
|
.input({ a: z.number(), b: z.number() })
|
||||||
.output({ added: z.number() })
|
.output({ added: z.number() })
|
||||||
.handle(async ({ a, b }) => {
|
.handle(async ({ a, b }) => {
|
||||||
|
if (a < 0 || b < 0) {
|
||||||
|
return new BadRequestError("Invalid input numbers added");
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
added: a + b,
|
added: a + b,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -52,4 +52,8 @@ describe("Relay", () => {
|
|||||||
it("should successfully run .actions", async () => {
|
it("should successfully run .actions", async () => {
|
||||||
assertEquals(await relay.get("/add-two", { a: 1, b: 1 }), 2);
|
assertEquals(await relay.get("/add-two", { a: 1, b: 1 }), 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should reject .actions with error", async () => {
|
||||||
|
assertEquals(await relay.get("/add-two", { a: -1, b: 1 }), 2);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user