Template
1
0

feat: error when action handler is missing

This commit is contained in:
2025-04-18 22:44:46 +00:00
parent d7398f6bff
commit ecb5f80584
6 changed files with 22 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@valkyr/relay", "name": "@valkyr/relay",
"version": "0.1.0", "version": "0.1.1",
"exports": { "exports": {
".": "./mod.ts", ".": "./mod.ts",
"./http": "./adapters/http.ts" "./http": "./adapters/http.ts"

View File

@@ -126,7 +126,10 @@ export class Api<TRoutes extends Route[]> {
if (result.success === false) { if (result.success === false) {
return toResponse(new BadRequestError("Invalid action input", z.prettifyError(result.error))); return toResponse(new BadRequestError("Invalid action input", z.prettifyError(result.error)));
} }
const output = (await action.state.handle?.(result.data)) ?? {}; if (action.state.handle === undefined) {
return toResponse(new InternalServerError(`Action '${action.state.name}' is missing handler.`));
}
const output = await action.state.handle(result.data);
for (const key in output) { for (const key in output) {
context[key] = output[key]; context[key] = output[key];
} }

View File

@@ -24,6 +24,13 @@ export class Relay<TRoutes extends Route[]> {
} }
} }
/**
* Override relay url configuration.
*/
set url(value: string) {
this.config.url = value;
}
/** /**
* Retrieve a route for the given method/path combination which can be further extended * Retrieve a route for the given method/path combination which can be further extended
* for serving incoming third party requests. * for serving incoming third party requests.

View File

@@ -19,4 +19,5 @@ export const relay = new Relay({ url: "http://localhost:36573", adapter }, [
.params({ userId: z.string().check(z.uuid()) }) .params({ userId: z.string().check(z.uuid()) })
.body(UserSchema.omit({ id: true, createdAt: true })), .body(UserSchema.omit({ id: true, createdAt: true })),
route.delete("/users/:userId").params({ userId: z.string().check(z.uuid()) }), route.delete("/users/:userId").params({ userId: z.string().check(z.uuid()) }),
route.get("/add-two").search({ a: z.coerce.number(), b: z.coerce.number() }).response(z.number()),
]); ]);

View File

@@ -1,5 +1,6 @@
import { Api } from "../../libraries/api.ts"; import { Api } from "../../libraries/api.ts";
import { NotFoundError } from "../../mod.ts"; import { NotFoundError } from "../../mod.ts";
import { addTwoNumbers } from "./actions.ts";
import { relay } from "./relay.ts"; import { relay } from "./relay.ts";
import { User } from "./user.ts"; import { User } from "./user.ts";
@@ -30,4 +31,8 @@ export const api = new Api([
relay.route("DELETE", "/users/:userId").handle(async ({ userId }) => { relay.route("DELETE", "/users/:userId").handle(async ({ userId }) => {
users = users.filter((user) => user.id !== userId); users = users.filter((user) => user.id !== userId);
}), }),
relay
.route("GET", "/add-two")
.actions([addTwoNumbers])
.handle(async ({ added }) => added),
]); ]);

View File

@@ -48,4 +48,8 @@ describe("Relay", () => {
assertEquals(users.length, 0); assertEquals(users.length, 0);
}); });
it("should successfully run .actions", async () => {
assertEquals(await relay.get("/add-two", { a: 1, b: 1 }), 2);
});
}); });