From ecb5f80584ea0a9fad8031ff370ec95a44d8ebf3 Mon Sep 17 00:00:00 2001 From: kodemon Date: Fri, 18 Apr 2025 22:44:46 +0000 Subject: [PATCH] feat: error when action handler is missing --- deno.json | 2 +- libraries/api.ts | 5 ++++- libraries/relay.ts | 7 +++++++ tests/mocks/relay.ts | 1 + tests/mocks/server.ts | 5 +++++ tests/route.test.ts | 4 ++++ 6 files changed, 22 insertions(+), 2 deletions(-) diff --git a/deno.json b/deno.json index 5466346..41ef5ae 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@valkyr/relay", - "version": "0.1.0", + "version": "0.1.1", "exports": { ".": "./mod.ts", "./http": "./adapters/http.ts" diff --git a/libraries/api.ts b/libraries/api.ts index fcc8c41..41b8b1a 100644 --- a/libraries/api.ts +++ b/libraries/api.ts @@ -126,7 +126,10 @@ export class Api { if (result.success === false) { 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) { context[key] = output[key]; } diff --git a/libraries/relay.ts b/libraries/relay.ts index 67134ca..a4dd56f 100644 --- a/libraries/relay.ts +++ b/libraries/relay.ts @@ -24,6 +24,13 @@ export class Relay { } } + /** + * 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 * for serving incoming third party requests. diff --git a/tests/mocks/relay.ts b/tests/mocks/relay.ts index 581e83e..31f8b56 100644 --- a/tests/mocks/relay.ts +++ b/tests/mocks/relay.ts @@ -19,4 +19,5 @@ export const relay = new Relay({ url: "http://localhost:36573", adapter }, [ .params({ userId: z.string().check(z.uuid()) }) .body(UserSchema.omit({ id: true, createdAt: true })), 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()), ]); diff --git a/tests/mocks/server.ts b/tests/mocks/server.ts index b1307f6..3d6a4d4 100644 --- a/tests/mocks/server.ts +++ b/tests/mocks/server.ts @@ -1,5 +1,6 @@ import { Api } from "../../libraries/api.ts"; import { NotFoundError } from "../../mod.ts"; +import { addTwoNumbers } from "./actions.ts"; import { relay } from "./relay.ts"; import { User } from "./user.ts"; @@ -30,4 +31,8 @@ export const api = new Api([ relay.route("DELETE", "/users/:userId").handle(async ({ userId }) => { users = users.filter((user) => user.id !== userId); }), + relay + .route("GET", "/add-two") + .actions([addTwoNumbers]) + .handle(async ({ added }) => added), ]); diff --git a/tests/route.test.ts b/tests/route.test.ts index cf84d7f..13530f4 100644 --- a/tests/route.test.ts +++ b/tests/route.test.ts @@ -48,4 +48,8 @@ describe("Relay", () => { assertEquals(users.length, 0); }); + + it("should successfully run .actions", async () => { + assertEquals(await relay.get("/add-two", { a: 1, b: 1 }), 2); + }); });